summaryrefslogtreecommitdiffstats
path: root/pkgs/games/xonotic/default.nix
blob: 3dced799e8dc9ab3165799dc563eaf0129e3bb47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
{
  lib,
  stdenv,
  fetchurl,
  fetchzip,
  makeWrapper,
  runCommand,
  makeDesktopItem,
  xonotic-data,
  copyDesktopItems,
  # required for both
  unzip,
  libjpeg,
  zlib,
  libvorbis,
  curl,
  freetype,
  libpng,
  libtheora,
  libx11,
  # glx
  libGLU,
  libGL,
  libxpm,
  libxext,
  libxxf86vm,
  alsa-lib,
  # sdl
  SDL2,
  # blind
  gmp,

  withSDL ? true,
  withGLX ? false,
  withDedicated ? true,
}:

let
  pname = "xonotic";
  version = "0.8.6";
  variant =
    if withSDL && withGLX then
      ""
    else if withSDL then
      "-sdl"
    else if withGLX then
      "-glx"
    else if withDedicated then
      "-dedicated"
    else
      "-what-even-am-i";

  meta = {
    description = "Free fast-paced first-person shooter";
    longDescription = ''
      Xonotic is a free, fast-paced first-person shooter that works on
      Windows, macOS and Linux. The project is geared towards providing
      addictive arena shooter gameplay which is all spawned and driven
      by the community itself. Xonotic is a direct successor of the
      Nexuiz project with years of development between them, and it
      aims to become the best possible open-source FPS of its kind.
    '';
    homepage = "https://www.xonotic.org/";
    license = lib.licenses.gpl2Plus;
    maintainers = with lib.maintainers; [
      zalakain
    ];
    platforms = lib.platforms.linux;
  };

  desktopItem = makeDesktopItem {
    name = "xonotic";
    exec = "xonotic";
    comment = meta.description;
    desktopName = "Xonotic";
    categories = [
      "Game"
      "Shooter"
    ];
    icon = "xonotic";
    startupNotify = false;
  };

  xonotic-unwrapped = stdenv.mkDerivation rec {
    pname = "xonotic${variant}-unwrapped";
    inherit version;

    src = fetchurl {
      url = "https://dl.xonotic.org/xonotic-${version}-source.zip";
      hash = "sha256-i5KseBz/SuicEhoj6s197AWiqr7azMI6GdGglYtAEqg=";
    };

    patches = [ ./fix-build-with-c23.patch ];

    nativeBuildInputs = [ unzip ];
    buildInputs = [
      libjpeg
      zlib
      libvorbis
      curl
      gmp
      libx11
    ]
    ++ lib.optionals withGLX [
      libGLU
      libGL
      libxpm
      libxext
      libxxf86vm
      alsa-lib
    ]
    ++ lib.optionals withSDL [ SDL2 ];

    sourceRoot = "Xonotic/source/darkplaces";

    # "debug", "release", "profile"
    target = "release";

    dontStrip = target != "release";

    postConfigure = ''
      pushd ../d0_blind_id
      ./configure $configureFlags
      popd
    '';

    buildPhase =
      (
        lib.optionalString withDedicated ''
          make -j $NIX_BUILD_CORES sv-${target}
        ''
        + lib.optionalString withGLX ''
          make -j $NIX_BUILD_CORES cl-${target}
        ''
        + lib.optionalString withSDL ''
          make -j $NIX_BUILD_CORES sdl-${target}
        ''
      )
      + ''
        pushd ../d0_blind_id
        make -j $NIX_BUILD_CORES
        popd
      '';

    enableParallelBuilding = true;

    installPhase =
      (
        ''
          install -Dm644 ../../misc/logos/xonotic_icon.svg \
            $out/share/icons/hicolor/scalable/apps/xonotic.svg
          pushd ../../misc/logos/icons_png
          for img in *.png; do
            size=''${img#xonotic_}
            size=''${size%.png}
            dimensions="''${size}x''${size}"
            install -Dm644 $img \
              $out/share/icons/hicolor/$dimensions/apps/xonotic.png
          done
          popd
        ''
        + lib.optionalString withDedicated ''
          install -Dm755 darkplaces-dedicated "$out/bin/xonotic-dedicated"
        ''
        + lib.optionalString withGLX ''
          install -Dm755 darkplaces-glx "$out/bin/xonotic-glx"
        ''
        + lib.optionalString withSDL ''
          install -Dm755 darkplaces-sdl "$out/bin/xonotic-sdl"
        ''
      )
      + ''
        pushd ../d0_blind_id
        make install
        popd
      '';

    # Xonotic needs to find libcurl.so at runtime for map downloads
    dontPatchELF = true;
    postFixup =
      lib.optionalString withDedicated ''
        patchelf --add-needed ${curl.out}/lib/libcurl.so $out/bin/xonotic-dedicated
      ''
      + lib.optionalString withGLX ''
        patchelf \
            --add-needed ${curl.out}/lib/libcurl.so \
            --add-needed ${libvorbis}/lib/libvorbisfile.so \
            --add-needed ${libvorbis}/lib/libvorbisenc.so \
            --add-needed ${libvorbis}/lib/libvorbis.so \
            --add-needed ${libGL.out}/lib/libGL.so \
            --add-needed ${freetype}/lib/libfreetype.so \
            --add-needed ${libpng}/lib/libpng.so \
            --add-needed ${libtheora}/lib/libtheora.so \
            $out/bin/xonotic-glx
      ''
      + lib.optionalString withSDL ''
        patchelf \
            --add-needed ${curl.out}/lib/libcurl.so \
            --add-needed ${libvorbis}/lib/libvorbisfile.so \
            --add-needed ${libvorbis}/lib/libvorbisenc.so \
            --add-needed ${libvorbis}/lib/libvorbis.so \
            --add-needed ${freetype}/lib/libfreetype.so \
            --add-needed ${libpng}/lib/libpng.so \
            --add-needed ${libtheora}/lib/libtheora.so \
            $out/bin/xonotic-sdl
      '';

    meta.mainProgram = "xonotic-${
      if withSDL then
        "sdl"
      else if withGLX then
        "glx"
      else
        "dedicated"
    }";
  };

in
rec {
  xonotic-data = fetchzip {
    name = "xonotic-data";
    url = "https://dl.xonotic.org/xonotic-${version}.zip";
    hash = "sha256-Lhjpyk7idmfQAVn4YUb7diGyyKZQBfwNXxk2zMOqiZQ=";
    postFetch = ''
      cd $out
      rm -rf $(ls | grep -v "^data$" | grep -v "^key_0.d0pk$")
    '';
    meta.hydraPlatforms = [ ];
    inherit version pname;
  };

  xonotic =
    runCommand "xonotic${variant}-${version}"
      {
        inherit xonotic-unwrapped version;
        pname = "${pname}${variant}";
        nativeBuildInputs = [
          makeWrapper
          copyDesktopItems
        ];
        desktopItems = [ desktopItem ];
        meta = meta // {
          hydraPlatforms = [ ];
          mainProgram = if withSDL || withGLX then "xonotic" else "xonotic-dedicated";
        };
      }
      (
        ''
          mkdir -p $out/bin
        ''
        + lib.optionalString withDedicated ''
          ln -s ${xonotic-unwrapped}/bin/xonotic-dedicated $out/bin/
        ''
        + lib.optionalString withGLX ''
          ln -s ${xonotic-unwrapped}/bin/xonotic-glx $out/bin/xonotic-glx
          ln -s $out/bin/xonotic-glx $out/bin/xonotic
        ''
        + lib.optionalString withSDL ''
          ln -s ${xonotic-unwrapped}/bin/xonotic-sdl $out/bin/xonotic-sdl
          ln -sf $out/bin/xonotic-sdl $out/bin/xonotic
        ''
        + lib.optionalString (withSDL || withGLX) ''
          mkdir -p $out/share
          ln -s ${xonotic-unwrapped}/share/icons $out/share/icons
          copyDesktopItems
        ''
        + ''
          for binary in $out/bin/xonotic-*; do
            wrapProgram $binary --add-flags "-basedir ${xonotic-data}" --prefix LD_LIBRARY_PATH : "${xonotic-unwrapped}/lib"
          done
        ''
      );
}