nix: only install final qcc output, and install it into bin
[xonotic/xonotic.git] / default.nix
1 # nix-shell -A shell
2 # --argstr cc clang
3 {
4     nixpkgs ? <nixpkgs>,
5     pkgs ? (import nixpkgs) {},
6     cc ? null
7 }:
8 with pkgs;
9 let
10     VERSION = "0.8.2";
11     cmake = pkgs.cmake_2_8;
12     targets = rec {
13         xonotic = mkDerivation { pki = true; dp = true; data = true; } rec {
14             name = "xonotic-${version}";
15             version = VERSION;
16
17             src = lib.sourceFilesBySuffices ./. [
18                 ".txt" ".cmake" ".in"
19                 ".c" ".cpp" ".h"
20                 ".inc" ".def"
21                 ".qc" ".qh"
22                 ".sh"
23             ];
24
25             env = {
26                 QCC = "${gmqcc}/bin/gmqcc";
27             };
28
29             nativeBuildInputs = [
30                 cmake   # for building
31                 git     # for versioning
32                 # unzip # for downloading maps
33             ];
34
35             cmakeFlags = [
36                 "-DDOWNLOAD_MAPS=0"
37             ];
38
39             buildInputs = [
40                 openssl # for d0_blind_id
41                 SDL2    # for darkplaces
42             ];
43
44             runtimeInputs = [
45                 zlib
46                 curl
47
48                 libjpeg
49                 libpng
50
51                 freetype
52
53                 libogg
54                 libtheora
55                 libvorbis
56             ];
57
58             installPhase = ''
59                 mkdir $out
60
61                 exe=darkplaces/darkplaces
62                 rpath=$(patchelf --print-rpath $exe)
63                 rpath_firstparty=$out/d0_blind_id
64                 rpath_thirdparty=${lib.makeLibraryPath runtimeInputs}
65                 rpath=''${rpath:+$rpath:}$rpath_firstparty:$rpath_thirdparty
66                 patchelf --set-rpath $rpath $exe
67
68                 cp -r . $out
69             '';
70
71             dontPatchELF = true;
72         };
73
74         gmqcc = mkDerivation { qcc = true; } rec {
75             name = "gmqcc-${version}";
76             version = "xonotic-${VERSION}";
77
78             src = ./gmqcc;
79
80             installPhase = ''
81                 mkdir -p $out/bin
82                 cp gmqcc $out/bin
83             '';
84         };
85
86         netradiant = mkDerivation { radiant = true; } rec {
87             name = "netradiant-${version}";
88             version = VERSION;
89
90             src = ./netradiant;
91
92             nativeBuildInputs = [
93                 cmake   # for building
94                 git     # for versioning
95             ];
96
97             cmakeFlags = [
98                 "-DDOWNLOAD_MAPS=0"
99             ];
100
101             buildInputs = [
102                 pkgconfig
103                 glib
104                 pcre
105                 libxml2
106                 ncurses
107                 libjpeg
108                 libpng
109                 minizip
110
111                 mesa
112
113                 xorg.libXt
114                 xorg.libXmu
115                 xorg.libSM
116                 xorg.libICE
117                 xorg.libpthreadstubs
118                 xorg.libXdmcp
119
120                 gnome3.gtk
121                 gnome2.gtk
122                 gnome2.gtkglext
123             ];
124         };
125     };
126     stdenv = if (cc != null) then overrideCC pkgs.stdenv pkgs."${cc}" else pkgs.stdenv;
127     mkEnableTargets = args: {
128         XON_NO_PKI = !args?pki;
129         XON_NO_DP = !args?dp;
130         XON_NO_DATA = !args?data;
131         XON_NO_QCC = !args?qcc;
132         XON_NO_RADIANT = !args?radiant;
133     };
134     mkDerivation = targets: {env ? {}, shellHook ? "", runtimeInputs ? [], ...}@args:
135         stdenv.mkDerivation (
136             (mkEnableTargets targets)
137             // { enableParallelBuilding = true; }
138             // (removeAttrs args ["env" "shellHook" "runtimeInputs"])  # passthru
139             // env
140             // {
141                 shellHook = ''
142                     ${shellHook}
143                     ${lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "export ${n}=${v}") env)}
144                     export LD_LIBRARY_PATH=''${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}${lib.makeLibraryPath runtimeInputs}
145                 '';
146             }
147         );
148     shell = let inputs = (lib.mapAttrsToList (n: v: v) targets); in stdenv.mkDerivation (rec {
149         name = "xon-shell";
150         XON_NO_DAEMON = true;
151         nativeBuildInputs = builtins.map (it: it.nativeBuildInputs) inputs;
152         buildInputs = builtins.map (it: it.buildInputs) inputs;
153         shellHook = builtins.map (it: it.shellHook) (builtins.filter (it: it?shellHook) inputs);
154     });
155 in { inherit shell; } // targets