nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 stdenv,
3 lib,
4 fetchurl,
5 coreutils,
6 ncurses,
7 gzip,
8 gccStdenv,
9 flex,
10 bison,
11 less,
12 buildPackages,
13 x11Mode ? false,
14 qtMode ? false,
15 libXaw,
16 libXext,
17 libXpm,
18 bdftopcf,
19 mkfontdir,
20 pkg-config,
21 qt5,
22 copyDesktopItems,
23 makeDesktopItem,
24}:
25
26let
27 stdenvUsed = if qtMode then gccStdenv else stdenv;
28
29 platform =
30 if stdenvUsed.hostPlatform.isUnix then
31 "unix"
32 else
33 throw "Unknown platform for NetHack: ${stdenvUsed.hostPlatform.system}";
34 unixHint =
35 if x11Mode then
36 "linux-x11"
37 else if qtMode then
38 "linux-qt4"
39 else if stdenvUsed.hostPlatform.isLinux then
40 "linux"
41 else if stdenvUsed.hostPlatform.isDarwin then
42 "macosx10.14"
43 # We probably want something different for Darwin
44 else
45 "unix";
46 userDir = "~/.config/nethack";
47 binPath = lib.makeBinPath [
48 coreutils
49 less
50 ];
51
52in
53stdenvUsed.mkDerivation (finalAttrs: {
54 version = "3.6.7";
55 pname =
56 if x11Mode then
57 "nethack-x11"
58 else if qtMode then
59 "nethack-qt"
60 else
61 "nethack";
62
63 src = fetchurl {
64 url = "https://nethack.org/download/${finalAttrs.version}/nethack-${
65 lib.replaceStrings [ "." ] [ "" ] finalAttrs.version
66 }-src.tgz";
67 hash = "sha256-mM9n323r+WaKYXRaqEwJvKs2Ll0z9blE7FFV1E0qrLI=";
68 };
69
70 patches = [
71 # Newer GCC rejects function declarations without explicit parameters.
72 ./function-parameters.patch
73 ];
74
75 buildInputs = [
76 ncurses
77 ]
78 ++ lib.optionals x11Mode [
79 libXaw
80 libXext
81 libXpm
82 ]
83 ++ lib.optionals qtMode [
84 gzip
85 qt5.qtbase.bin
86 qt5.qtmultimedia.bin
87 ];
88
89 nativeBuildInputs = [
90 flex
91 bison
92 copyDesktopItems
93 ]
94 ++ lib.optionals x11Mode [
95 mkfontdir
96 bdftopcf
97 ]
98 ++ lib.optionals qtMode [
99 pkg-config
100 mkfontdir
101 qt5.qtbase.dev
102 qt5.qtmultimedia.dev
103 qt5.wrapQtAppsHook
104 bdftopcf
105 ];
106
107 makeFlags = [ "PREFIX=$(out)" ];
108
109 postPatch = ''
110 sed -e '/^ *cd /d' -i sys/unix/nethack.sh
111 sed \
112 -e 's/^YACC *=.*/YACC = bison -y/' \
113 -e 's/^LEX *=.*/LEX = flex/' \
114 -i sys/unix/Makefile.utl
115 sed \
116 -e 's,^WINQT4LIB =.*,WINQT4LIB = `pkg-config Qt5Gui --libs` \\\
117 `pkg-config Qt5Widgets --libs` \\\
118 `pkg-config Qt5Multimedia --libs`,' \
119 -i sys/unix/Makefile.src
120 sed \
121 -e 's,^CFLAGS=-g,CFLAGS=,' \
122 -e 's,/bin/gzip,${gzip}/bin/gzip,g' \
123 -e 's,^WINTTYLIB=.*,WINTTYLIB=-lncurses,' \
124 -i sys/unix/hints/linux
125 sed \
126 -e 's,^#WANT_WIN_CURSES=1$,WANT_WIN_CURSES=1,' \
127 -e 's,^CC=.*$,CC=${stdenvUsed.cc.targetPrefix}cc,' \
128 -e 's,^HACKDIR=.*$,HACKDIR=\$(PREFIX)/games/lib/\$(GAME)dir,' \
129 -e 's,^SHELLDIR=.*$,SHELLDIR=\$(PREFIX)/games,' \
130 -e 's,^CFLAGS=-g,CFLAGS=,' \
131 -e 's,/usr/bin/true,${coreutils}/bin/true,g' \
132 -i sys/unix/hints/macosx10.14
133 sed -e '/define CHDIR/d' -i include/config.h
134 ${lib.optionalString qtMode ''
135 sed \
136 -e 's,^QTDIR *=.*,QTDIR=${qt5.qtbase.dev},' \
137 -e 's,CFLAGS.*QtGui.*,CFLAGS += `pkg-config Qt5Gui --cflags`,' \
138 -e 's,CFLAGS+=-DCOMPRESS.*,CFLAGS+=-DCOMPRESS=\\"${gzip}/bin/gzip\\" \\\
139 -DCOMPRESS_EXTENSION=\\".gz\\",' \
140 -e 's,moc-qt4,moc,' \
141 -i sys/unix/hints/linux-qt4
142 ''}
143 ${lib.optionalString (stdenvUsed.buildPlatform != stdenvUsed.hostPlatform)
144 # If we're cross-compiling, replace the paths to the data generation tools
145 # with the ones from the build platform's nethack package, since we can't
146 # run the ones we've built here.
147 ''
148 ${buildPackages.perl}/bin/perl -p \
149 -e 's,[a-z./]+/(makedefs|dgn_comp|lev_comp|dlb)(?!\.),${buildPackages.nethack}/libexec/nethack/\1,g' \
150 -i sys/unix/Makefile.*
151 ''
152 }
153 sed -i -e '/rm -f $(MAKEDEFS)/d' sys/unix/Makefile.src
154 # Fix building on darwin where otherwise __has_attribute fails with an empty parameter
155 sed -e 's/define __warn_unused_result__ .*/define __warn_unused_result__ __unused__/' -i include/tradstdc.h
156 sed -e 's/define warn_unused_result .*/define warn_unused_result __unused__/' -i include/tradstdc.h
157 '';
158
159 configurePhase = ''
160 pushd sys/${platform}
161 ${lib.optionalString (platform == "unix") ''
162 sh setup.sh hints/${unixHint}
163 ''}
164 popd
165 '';
166
167 # https://github.com/NixOS/nixpkgs/issues/294751
168 enableParallelBuilding = false;
169
170 preFixup = lib.optionalString qtMode ''
171 wrapQtApp "$out/games/nethack"
172 '';
173
174 postInstall = ''
175 mkdir -p $out/games/lib/nethackuserdir
176 for i in xlogfile logfile perm record save; do
177 mv $out/games/lib/nethackdir/$i $out/games/lib/nethackuserdir
178 done
179
180 mkdir -p $out/bin
181 cat <<EOF >$out/bin/nethack
182 #! ${stdenvUsed.shell} -e
183 PATH=${binPath}:\$PATH
184
185 if [ ! -d ${userDir} ]; then
186 mkdir -p ${userDir}
187 cp -r $out/games/lib/nethackuserdir/* ${userDir}
188 chmod -R +w ${userDir}
189 fi
190
191 RUNDIR=\$(mktemp -d)
192
193 cleanup() {
194 rm -rf \$RUNDIR
195 }
196 trap cleanup EXIT
197
198 cd \$RUNDIR
199 for i in ${userDir}/*; do
200 ln -s \$i \$(basename \$i)
201 done
202 for i in $out/games/lib/nethackdir/*; do
203 ln -s \$i \$(basename \$i)
204 done
205 set +e
206 $out/games/nethack "\$@"
207 if [[ \$? -gt 128 ]]; then
208 echo "nethack exited abnormally, attempting to recover save file..."
209 ./recover -d . ?lock.0
210 fi
211 EOF
212 chmod +x $out/bin/nethack
213 ${lib.optionalString x11Mode "mv $out/bin/nethack $out/bin/nethack-x11"}
214 ${lib.optionalString qtMode "mv $out/bin/nethack $out/bin/nethack-qt"}
215 install -Dm 555 util/{makedefs,dgn_comp,lev_comp} -t $out/libexec/nethack/
216 ${lib.optionalString (!(x11Mode || qtMode)) "install -Dm 555 util/dlb -t $out/libexec/nethack/"}
217 '';
218
219 desktopItems = [
220 (makeDesktopItem {
221 name = "NetHack";
222 exec =
223 if x11Mode then
224 "nethack-x11"
225 else if qtMode then
226 "nethack-qt"
227 else
228 "nethack";
229 icon = "nethack";
230 desktopName = "NetHack";
231 comment = "NetHack is a single player dungeon exploration game";
232 categories = [
233 "Game"
234 "ActionGame"
235 ];
236 })
237 ];
238
239 meta = {
240 description = "Rogue-like game";
241 homepage = "http://nethack.org/";
242 license = lib.licenses.ngpl;
243 platforms = if x11Mode then lib.platforms.linux else lib.platforms.unix;
244 maintainers = [ ];
245 mainProgram = "nethack";
246 broken = if qtMode then stdenv.hostPlatform.isDarwin else false;
247 };
248})