blob: d0204e63729593262ddead96e9101f61f1889794 [file] [log] [blame]
Skyler Grey35bf1382023-01-26 00:44:10 +00001{ lib, stdenv
2, makeWrapper
3, makeDesktopItem
4, copyDesktopItems
5, fetchFromGitHub
6, gradle_6
7, jdk
8, perl
9
10# for arc
11, SDL2
12, pkg-config
13, stb
14, ant
15, alsa-lib
16, alsa-plugins
17, glew
18
19# Make the build version easily overridable.
20# Server and client build versions must match, and an empty build version means
21# any build is allowed, so this parameter acts as a simple whitelist.
22# Takes the package version and returns the build version.
23, makeBuildVersion ? (v: v)
24, enableClient ? true
25, enableServer ? true
26}:
27
28let
29 pname = "mindustry-beta";
30 # Note: when raising the version, ensure that all SNAPSHOT versions in
31 # build.gradle are replaced by a fixed version
32 # (the current one at the time of release) (see postPatch).
33 version = "140.4";
34 buildVersion = makeBuildVersion version;
35
36 Mindustry = fetchFromGitHub {
37 owner = "Anuken";
38 repo = "Mindustry";
39 rev = "v${version}";
40 sha256 = "URmjmfzQAVVl6erbh3+FVFdN7vGTNwYKPtcrwtt9vkg=";
41 };
42 Arc = fetchFromGitHub {
43 owner = "Anuken";
44 repo = "Arc";
45 rev = "v${version}";
46 sha256 = "pUUak5P9t4RmSdT+/oH/8oo6l7rjIN08XDJ06TcUn8I=";
47 };
48 soloud = fetchFromGitHub {
49 owner = "Anuken";
50 repo = "soloud";
51 # this is never pinned in upstream, see https://github.com/Anuken/Arc/issues/39
52 rev = "b33dfc5178fcb2613ee68136f4a4869cadc0b06a";
53 sha256 = "1vf68i3pnsixch37285ib7afkwmlrc05v783395jsdjzj9i67lj3";
54 };
55
56 patches = [
57 ./0001-fix-include-path-for-SDL2-on-linux.patch
58 ];
59
60 unpackPhase = ''
61 cp -r ${Mindustry} Mindustry
62 cp -r ${Arc} Arc
63 chmod -R u+w -- Mindustry Arc
64 cp ${stb.src}/stb_image.h Arc/arc-core/csrc/
65 cp -r ${soloud} Arc/arc-core/csrc/soloud
66 chmod -R u+w -- Arc
67 '';
68
69 desktopItem = makeDesktopItem {
70 name = "Mindustry";
71 desktopName = "Mindustry";
72 exec = "mindustry";
73 icon = "mindustry";
74 };
75
76 cleanupMindustrySrc = ''
77 pushd Mindustry
78
79 # Remove unbuildable iOS stuff
80 sed -i '/^project(":ios"){/,/^}/d' build.gradle
81 sed -i '/robo(vm|VM)/d' build.gradle
82 rm ios/build.gradle
83
84 # Pin 'SNAPSHOT' versions
85 sed -i 's/com.github.anuken:packr:-SNAPSHOT/com.github.anuken:packr:034efe51781d2d8faa90370492133241bfb0283c/' build.gradle
86
87 popd
88 '';
89
90 gradle = (gradle_6.override (old: { java = jdk; }));
91
92 # fake build to pre-download deps into fixed-output derivation
93 deps = stdenv.mkDerivation {
94 pname = "${pname}-deps";
95 inherit version unpackPhase patches;
96 postPatch = cleanupMindustrySrc;
97
98 nativeBuildInputs = [ gradle perl ];
99 # Here we download dependencies for both the server and the client so
100 # we only have to specify one hash for 'deps'. Deps can be garbage
101 # collected after the build, so this is not really an issue.
102 buildPhase = ''
103 pushd Mindustry
104 export GRADLE_USER_HOME=$(mktemp -d)
105 gradle --no-daemon resolveDependencies
106 popd
107 '';
108 # perl code mavenizes pathes (com.squareup.okio/okio/1.13.0/a9283170b7305c8d92d25aff02a6ab7e45d06cbe/okio-1.13.0.jar -> com/squareup/okio/okio/1.13.0/okio-1.13.0.jar)
109 installPhase = ''
110 find $GRADLE_USER_HOME/caches/modules-2 -type f -regex '.*\.\(jar\|pom\)' \
111 | perl -pe 's#(.*/([^/]+)/([^/]+)/([^/]+)/[0-9a-f]{30,40}/([^/\s]+))$# ($x = $2) =~ tr|\.|/|; "install -Dm444 $1 \$out/$x/$3/$4/$5" #e' \
112 | sh
113 '';
114 outputHashAlgo = "sha256";
115 outputHashMode = "recursive";
116 outputHash = "+7vSwQT6LwHgKE9DubISznq4G4DgvlnD7WaF1KywBzU=";
117 };
118
119in
120assert lib.assertMsg (enableClient || enableServer)
121 "mindustry: at least one of 'enableClient' and 'enableServer' must be true";
122stdenv.mkDerivation rec {
123 inherit pname version unpackPhase patches;
124
125 postPatch = ''
126 # ensure the prebuilt shared objects don't accidentally get shipped
127 rm Arc/natives/natives-desktop/libs/libarc*.so
128 rm Arc/backends/backend-sdl/libs/linux64/libsdl-arc*.so
129 '' + cleanupMindustrySrc;
130
131 buildInputs = lib.optionals enableClient [
132 SDL2
133 glew
134 alsa-lib
135 ];
136 nativeBuildInputs = [
137 pkg-config
138 gradle
139 makeWrapper
140 jdk
141 ] ++ lib.optionals enableClient [
142 ant
143 copyDesktopItems
144 ];
145
146 desktopItems = lib.optional enableClient desktopItem;
147
148 buildPhase = with lib; ''
149 export GRADLE_USER_HOME=$(mktemp -d)
150
151 # point to offline repo
152 sed -ie "s#mavenLocal()#mavenLocal(); maven { url '${deps}' }#g" Mindustry/build.gradle
153 sed -ie "s#mavenCentral()#mavenCentral(); maven { url '${deps}' }#g" Arc/build.gradle
154
155 pushd Mindustry
156 '' + optionalString enableClient ''
157 gradle --offline --no-daemon jnigenBuild -Pbuildversion=${buildVersion}
158 gradle --offline --no-daemon sdlnatives -Pdynamic -Pbuildversion=${buildVersion}
159 glewlib=${lib.getLib glew}/lib/libGLEW.so
160 sdllib=${lib.getLib SDL2}/lib/libSDL2.so
161 patchelf ../Arc/backends/backend-sdl/libs/linux64/libsdl-arc*.so \
162 --add-needed $glewlib \
163 --add-needed $sdllib
164 gradle --offline --no-daemon desktop:dist -Pbuildversion=${buildVersion}
165 '' + optionalString enableServer ''
166 gradle --offline --no-daemon server:dist -Pbuildversion=${buildVersion}
167 '';
168
169 installPhase = with lib; ''
170 runHook preInstall
171 '' + optionalString enableClient ''
172 install -Dm644 desktop/build/libs/Mindustry.jar $out/share/mindustry.jar
173 mkdir -p $out/bin
174 makeWrapper ${jdk}/bin/java $out/bin/mindustry \
175 --add-flags "-jar $out/share/mindustry.jar" \
176 --set ALSA_PLUGIN_DIR ${alsa-plugins}/lib/alsa-lib/
177
178 # Retain runtime depends to prevent them from being cleaned up.
179 # Since a jar is a compressed archive, nix can't figure out that the dependency is actually in there,
180 # and will assume that it's not actually needed.
181 # This can cause issues.
182 # See https://github.com/NixOS/nixpkgs/issues/109798.
183 echo "# Retained runtime dependencies: " >> $out/bin/mindustry
184 for dep in ${SDL2.out} ${alsa-lib.out} ${glew.out}; do
185 echo "# $dep" >> $out/bin/mindustry
186 done
187
188 install -Dm644 core/assets/icons/icon_64.png $out/share/icons/hicolor/64x64/apps/mindustry.png
189 '' + optionalString enableServer ''
190 install -Dm644 server/build/libs/server-release.jar $out/share/mindustry-server.jar
191 mkdir -p $out/bin
192 makeWrapper ${jdk}/bin/java $out/bin/mindustry-server \
193 --add-flags "-jar $out/share/mindustry-server.jar"
194 '' + ''
195 runHook postInstall
196 '';
197
198 meta = with lib; {
199 homepage = "https://mindustrygame.github.io/";
200 downloadPage = "https://github.com/Anuken/Mindustry/releases";
201 description = "A sandbox tower defense game";
202 license = licenses.gpl3Plus;
203 maintainers = with maintainers; [ fgaz petabyteboy ];
204 platforms = platforms.x86_64;
205 # Hash mismatch on darwin:
206 # https://github.com/NixOS/nixpkgs/pull/105590#issuecomment-737120293
207 broken = stdenv.isDarwin;
208 };
209}