nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 fetchurl,
5 pkg-config,
6 unzip,
7 zlib,
8 libpng,
9 bzip2,
10 SDL,
11 SDL_mixer,
12 buildEnv,
13 config,
14 runtimeShell,
15}:
16
17let
18 # Choose your "paksets" of objects, images, text, music, etc.
19 paksets = config.simutrans.paksets or "pak64 pak64.japan pak128 pak128.britain pak128.german";
20
21 result = withPaks (
22 if paksets == "*" then
23 lib.attrValues pakSpec # taking all
24 else
25 map (name: pakSpec.${name}) (lib.splitString " " paksets)
26 );
27
28 ver1 = "121";
29 ver2 = "0";
30 ver3 = "";
31 version = "${ver1}.${ver2}${lib.optionalString (ver3 != "") ".${ver3}"}";
32 ver_dash = "${ver1}-${ver2}${lib.optionalString (ver3 != "") "-${ver3}"}";
33
34 binary_src = fetchurl {
35 url = "mirror://sourceforge/simutrans/simutrans/${ver_dash}/simutrans-src-${ver_dash}.zip";
36 sha256 = "1f463r6kr5ig0zd3mncc74k93xbjywsq3d06j5r17831jyc9bzb9";
37 };
38
39 # As of 2021/07, many of these paksets have not been updated for years, so are on old versions.
40 pakSpec = lib.mapAttrs (pakName: attrs: mkPak (attrs // { inherit pakName; })) {
41 pak64 = {
42 srcPath = "${ver_dash}/simupak64-${ver_dash}";
43 sha256 = "1k335kh8dhm1hdn5iwn3sdgnrlpk0rqxmmgqgqcwsi09cmw45m5c";
44 };
45 "pak64.japan" = {
46 # No release for 121.0 yet!
47 srcPath = "120-0/simupak64.japan-120-0-1";
48 sha256 = "14swy3h4ij74bgaw7scyvmivfb5fmp21nixmhlpk3mav3wr3167i";
49 };
50
51 pak128 = {
52 srcPath = "pak128%20for%20ST%20120.4.1%20%282.8.1%2C%20priority%20signals%20%2B%20bugfix%29/pak128";
53 sha256 = "0z01y7r0rz7q79vr17bbnkgcbjjrimphy1dwb1pgbiv4klz7j5xw";
54 };
55 "pak128.britain" = {
56 srcPath = "pak128.Britain%20for%20120-1/pak128.Britain.1.18-120-3";
57 sha256 = "1kyb0s54kysvdr0zdln9106yx75d71j4lbw3v87k3i440cj3r1d3";
58 };
59 "pak128.cs" = {
60 # note: it needs pak128 to work
61 url = "mirror://sourceforge/simutrans/Pak128.CS/pak128.cz_v.0.2.1.zip";
62 sha256 = "008d8x1s0vxsq78rkczlnf57pv1n5hi1v5nbd1l5w3yls7lk11sc";
63 };
64 "pak128.german" = {
65 url =
66 "mirror://sourceforge/simutrans/PAK128.german/"
67 + "pak128.german_1.2_for_ST_121.0/PAK128.german_1.2_for_ST_121-0.zip";
68 sha256 = "1cv1rzl1a3i5dvk476zq094wawk9hhdh2f0y4xrdny5gn17mb2xi";
69 };
70
71 /*
72 This release contains accented filenames that prevent unzipping.
73 "pak192.comic" = {
74 srcPath = "pak192comic%20for%20${ver2_dash}/pak192comic-0.4-${ver2_dash}up";
75 sha256 = throw "";
76 };
77 */
78 };
79
80 mkPak =
81 {
82 sha256,
83 pakName,
84 srcPath ? null,
85 url ? "mirror://sourceforge/simutrans/${pakName}/${srcPath}.zip",
86 }:
87 stdenv.mkDerivation {
88 name = "simutrans-${pakName}";
89 dontUnpack = true;
90 preferLocalBuild = true;
91 installPhase =
92 let
93 src = fetchurl { inherit url sha256; };
94 in
95 ''
96 mkdir -p "$out/share/simutrans/${pakName}"
97 cd "$out/share/simutrans/${pakName}"
98 "${unzip}/bin/unzip" "${src}"
99 chmod -R +w . # some zipfiles need that
100
101 set +o pipefail # no idea why it's needed
102 toStrip=`find . -iname '*.pak' | head -n 1 | sed 's|\./\(.*\)/[^/]*$|\1|'`
103 echo "Detected path '$toStrip' to strip"
104 mv ./"$toStrip"/* .
105 rm -f "$toStrip/.directory" #pak128.german had this
106 rmdir -p "$toStrip"
107 '';
108 };
109
110 /*
111 The binaries need all data in one directory; the default is directory
112 of the executable, and another option is the current directory :-/
113 */
114 withPaks =
115 paks:
116 buildEnv {
117 inherit (binaries) pname version;
118 paths = [ binaries ] ++ paks;
119 postBuild = ''
120 rm "$out/bin" && mkdir "$out/bin"
121 cat > "$out/bin/simutrans" <<EOF
122 #!${runtimeShell}
123 cd "$out"/share/simutrans
124 exec "${binaries}/bin/simutrans" -use_workdir "\$@"
125 EOF
126 chmod +x "$out/bin/simutrans"
127 '';
128
129 passthru.meta = binaries.meta // {
130 hydraPlatforms = [ ];
131 };
132 passthru.binaries = binaries;
133 };
134
135 binaries = stdenv.mkDerivation {
136 pname = "simutrans";
137 inherit version;
138
139 src = binary_src;
140
141 sourceRoot = ".";
142
143 nativeBuildInputs = [
144 pkg-config
145 unzip
146 ];
147 buildInputs = [
148 zlib
149 libpng
150 bzip2
151 SDL
152 SDL_mixer
153 ];
154
155 configurePhase =
156 let
157 # Configuration as per the readme.txt and config.template
158 platform =
159 if stdenv.hostPlatform.isLinux then
160 "linux"
161 else if stdenv.hostPlatform.isDarwin then
162 "mac"
163 else
164 throw "add your platform";
165 config = ''
166 BACKEND = mixer_sdl
167 COLOUR_DEPTH = 16
168 OSTYPE = ${platform}
169 VERBOSE = 1
170 '';
171 in
172 #TODO: MULTI_THREAD = 1 is "highly recommended",
173 # but it's roughly doubling CPU usage for me
174 ''
175 echo "${config}" > config.default
176
177 # Use ~/.simutrans instead of ~/simutrans
178 substituteInPlace simsys.cc --replace '%s/simutrans' '%s/.simutrans'
179
180 # use -O2 optimization (defaults are -O or -O3)
181 sed -i -e '/CFLAGS += -O/d' Makefile
182 export CFLAGS+=-O2
183 '';
184
185 enableParallelBuilding = true;
186
187 installPhase = ''
188 mkdir -p $out/share/
189 mv simutrans $out/share/
190
191 mkdir -p $out/bin/
192 mv build/default/sim $out/bin/simutrans
193 '';
194
195 meta = {
196 description = "Simulation game in which the player strives to run a successful transport system";
197 mainProgram = "simutrans";
198 longDescription = ''
199 Simutrans is a cross-platform simulation game in which the
200 player strives to run a successful transport system by
201 transporting goods, passengers, and mail between
202 places. Simutrans is an open source remake of Transport Tycoon.
203 '';
204
205 homepage = "http://www.simutrans.com/";
206 license = with lib.licenses; [
207 artistic1
208 gpl1Plus
209 ];
210 maintainers = [ ];
211 platforms = lib.platforms.linux; # TODO: ++ darwin;
212 };
213 };
214
215in
216result