146.1. nethack-5.0.0
Roguelike dungeon game in characters, with saves under the player's own home
| Version | 5.0.0, release 2 |
| Shelf | games-rpg — RPGs, roguelikes and interactive fiction |
| Phase | 41_system (System), 573 of 1047 in build order |
| Licence | NGPL AND MIT |
| Homepage | https://www.nethack.org/ |
| Build time | 0.5 SBU |
| Installed size | 5.9 MiB in 13 files |
| Recipe | ports/core/games-rpg/nethack/kpkgbuild |
About
NetHack is the roguelike: a descent through the Dungeons of Doom drawn in characters, played in a terminal. KDOS builds its tty and curses interfaces and installs it so that every player has a playground of their own, under ${XDG_DATA_HOME:-~/.local/share}/nethack, with no setgid binary and no score file shared between players.
NetHack has no configure. sys/unix/setup.sh splices a hints file into each generated Makefile, so the hints file that build.sh writes to sys/unix/hints/kdos is the port’s whole configuration. Assignments in its #-PRE segment land before the stock Makefile body and lose to anything the body assigns; util/Makefile’s body sets LIBS empty, which is why LIBS is set in the #-POST segment instead.
Why it is in KDOS
The comment above it in script/phases/41_system/packages.d/games-rpg.txt:
The dungeon of Doom, 3.6.7. No setgid playground and no shared score file: the wrapper points $NETHACKDIR at the player's own data directory, which is the only way a compile-time HACKDIR can become per-player.
Sources
nethack-5.0.0.tar.gz(10.3 MiB)
sha2562959b7886aac76185b90aea0c9f80d14343f604de0ae96b3dd2a760f7ab3bde9
upstream: https://www.nethack.org/download/5.0.0/nethack-500-src.tgz
archive copy: sources-2/games-rpg--nethack-5.0.0.tar.gzlua-5.4.8.tar.gz(365.6 KiB)
sha2564f18ddae154e793e46eeab727c59ef1c0c0c2b744e7b94219710d76f530629ae
upstream: https://www.lua.org/ftp/lua-5.4.8.tar.gz
archive copy: sources-2/games-rpg--lua-5.4.8.tar.gz
Dependencies
Required by: no port names it.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install nethack'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/games-rpg/nethack.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/games-rpg/nethack
echo '2959b7886aac76185b90aea0c9f80d14343f604de0ae96b3dd2a760f7ab3bde9 nethack-5.0.0.tar.gz' | sha256sum -c -
echo '4f18ddae154e793e46eeab727c59ef1c0c0c2b744e7b94219710d76f530629ae lua-5.4.8.tar.gz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/nethack
SRC=$SRC_ROOT/nethack-5.0.0
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf nethack-5.0.0.tar.gz -C "$SRC" --strip-components=1
tar --no-same-permissions --warning=no-unknown-keyword -xf lua-5.4.8.tar.gz -C "$SRC_ROOT"
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the recipe’s helpers _luaver and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
mkdir -p lib
cp -R "$SRC_ROOT/lua-$_luaver" lib/
cat > sys/unix/hints/kdos <<HINTS
#-PRE
PREFIX=/usr
HACKDIR=\$(PREFIX)/share/$name
SHELLDIR=
INSTDIR=$PKG\$(HACKDIR)
VARDIR=\$(INSTDIR)
CFLAGS=$CFLAGS -D_GNU_SOURCE -I../include
CFLAGS+=-DDLB
CFLAGS+=-DZLIB_COMP
CFLAGS+=-DHACKDIR=\"\$(HACKDIR)\"
CFLAGS+=-DSYSCF -DSYSCF_FILE=\"/etc/$name/sysconf\"
CFLAGS+=-DTIMED_DELAY
CFLAGS+=-DDUMPLOG
CFLAGS+=-DCURSES_GRAPHICS
CFLAGS+=-DCURSES_UNICODE -D_XOPEN_SOURCE_EXTENDED
CFLAGS+=-DCONFIG_ERROR_SECURE=FALSE
CFLAGS+=-DNOCRASHREPORT
CFLAGS+=-DSELF_RECOVER
SYSCFLAGS=-DLUA_USE_POSIX $CFLAGS
LINK=\$(CC)
LFLAGS=$LDFLAGS
WINSRC = \$(WINTTYSRC) \$(WINCURSESSRC)
WINOBJ = \$(WINTTYOBJ) \$(WINCURSESOBJ)
WINLIB = -lncursesw
CHOWN=true
CHGRP=true
GAMEPERM = 0755
VARDIRPERM = 0755
VARFILEPERM = 0644
#-POST
LIBS = -lz
HINTS
sh sys/unix/setup.sh sys/unix/hints/kdos
make -C src LUA_VERSION=$_luaver hacklib.a
make LUA_VERSION=$_luaver install
install -Dm755 "$PKG/usr/share/$name/$name" "$PKG/usr/libexec/$name/$name"
install -Dm755 "$PKG/usr/share/$name/recover" "$PKG/usr/libexec/$name/recover"
rm -f "$PKG/usr/share/$name/$name" "$PKG/usr/share/$name/recover"
rm -f "$PKG/usr/share/$name"/perm "$PKG/usr/share/$name"/record \
"$PKG/usr/share/$name"/logfile "$PKG/usr/share/$name"/xlogfile \
"$PKG/usr/share/$name"/livelog
rmdir "$PKG/usr/share/$name/save"
install -Dm644 dat/license "$PKG/usr/share/licenses/$name/license"
sed -n '/^\* Copyright (C)/,/^\*\*\*\*/p' "lib/lua-$_luaver/src/lua.h" > "$PKG/usr/share/licenses/$name/lua-COPYRIGHT"
install -Dm644 doc/$name.6 "$PKG/usr/share/man/man6/$name.6"
install -Dm644 doc/recover.6 "$PKG/usr/share/man/man6/$name-recover.6"
install -d "$PKG/etc/$name"
cat > "$PKG/etc/$name/sysconf" <<SYSCONF
# System-wide NetHack configuration. Per-player options go in ~/.nethackrc.
# Wizard and explore mode are open to everyone because there is nobody to
# protect them from: each player's playground is their own directory and the
# binary carries no privilege, so a debug game can only rewrite files its
# player already owns.
WIZARDS=*
EXPLORERS=*
# Named so that a dump lands in the player's own playground, which is the
# process's working directory. An absolute path here would be one directory
# every player writes to.
DUMPLOGFILE=dumplog.%n.%d.txt
SYSCONF
chmod 644 "$PKG/etc/$name/sysconf"
install -d "$PKG/usr/bin"
cat > "$PKG/usr/bin/$name" <<'WRAPPER'
#!/bin/sh
# Start NetHack with a playground under the player's own data directory.
set -e
NETHACKDIR="${XDG_DATA_HOME:-$HOME/.local/share}/nethack"
export NETHACKDIR
# save/ is not created on demand: set_savefile_name() builds "save/<uid><name>"
# and the open fails as a save error mid-game if the directory is missing.
mkdir -p "$NETHACKDIR/save"
# Every file the game takes an fcntl lock on has to exist first: lock_file()
# opens its target O_RDWR with no O_CREAT, so an absent one is not created but
# reported as "Cannot open file <name>. Is NetHack installed correctly?".
# perm is the instance lock and stays empty; the other two are appended to at
# the end of each game. The score file is not in this list because
# check_recordfile() does create that one.
for f in perm logfile xlogfile; do
[ -f "$NETHACKDIR/$f" ] || : > "$NETHACKDIR/$f"
done
for f in nhdat license symbols; do
ln -sf "/usr/share/nethack/$f" "$NETHACKDIR/$f"
done
exec /usr/libexec/nethack/nethack "$@"
WRAPPER
chmod 755 "$PKG/usr/bin/$name"
cat > "$PKG/usr/bin/$name-recover" <<'WRAPPER'
#!/bin/sh
# Rebuild a save file from the level files an interrupted game left behind.
set -e
cd "${XDG_DATA_HOME:-$HOME/.local/share}/nethack"
exec /usr/libexec/nethack/recover "$@"
WRAPPER
chmod 755 "$PKG/usr/bin/$name-recover"
install -d "$PKG/usr/share/applications"
cat > "$PKG/usr/share/applications/$name.desktop" <<'ENTRY'
[Desktop Entry]
Type=Application
Name=NetHack
GenericName=Roguelike
Comment=Descend the Dungeons of Doom and recover the Amulet of Yendor
Exec=nethack
Icon=input-gaming
Terminal=true
Categories=Game;AdventureGame;RolePlaying;
Keywords=roguelike;dungeon;rpg;adventure;nethack;hack;
ENTRY
chmod 644 "$PKG/usr/share/applications/$name.desktop"
The switches, and why each is set:
-D_GNU_SOURCE— Ask the C library for its GNU extensions in every file.config1.hdefines_GNU_SOURCEitself and needs it to come before any system header, butwin/curses/cursmain.cincludescurses.hon the line abovehack.h, so that file would configure the C library without it and then callmempcpy()through a fortifying<string.h>. Set on the command line, it is in force before the first include of every file.-DDLB— Pack most of the game’s data files into one archive,nhdat, read through NetHack’s data librarian, instead of installing them as separate files.-DZLIB_COMP— Compress save and bones files in-process with zlib rather than by running an externalcompressprogram. It is whyLIBS = -lzis in the hints.-DHACKDIR— The compiled-in directory of the read-only game data,/usr/share/nethack.-DSYSCF,-DSYSCF_FILE— Read a system-wide configuration file, and name it/etc/nethack/sysconf.config.hturnsSYSCFon in any case, and a build with it refuses to start when it cannot read the file, so the recipe installs one. The path is absolute because the game runs in the player’s playground, where a relative name would not find it.-DTIMED_DELAY— Pause for display effects with a timer,usleep(), rather than with extra screen output, whose delay varies with the terminal.-DDUMPLOG— Write an end-of-game dump of the character. The shippedsysconfnames the filedumplog.%n.%d.txt, a relative name, so the dump lands in the player’s own playground.-DCURSES_GRAPHICS— Build the curses interface beside the tty one.-DCURSES_UNICODE,-D_XOPEN_SOURCE_EXTENDED— Draw the curses interface through ncurses’s wide-character API. The curses sources usecchar_twhenever the header definesNCURSES_WIDECHAR, and the ncurses header defines it to 0 unless X/Open’s extended interface is asked for, which leaves the type undeclared and the build failing._XOPEN_SOURCE_EXTENDEDexposes it, andCURSES_UNICODEis upstream’s switch for drawing through it; both go with the-lncurseswinWINLIB.-DCONFIG_ERROR_SECURE=FALSE— Report every error in a configuration file, with line numbers and context. The default,TRUE, shows only the first error whenNETHACKOPTIONSpoints the game at a file.-DNOCRASHREPORT— Build no crash reporter. On Linuxconfig.hotherwise compiles one in that, on a panic, runs/usr/bin/xdg-opento open a browser on the bug-report pagesysconfnames inCRASHREPORTURL, and turns onNOSTATICFN, which dropsstaticfrom the game’s internal functions. The shippedsysconfnames no such page, so the reporter would never open one.-DSELF_RECOVER— Let the game itself offer to recover an interrupted game when it finds that character’s lock and level files. Without it the player is told only to destroy the old game or quit, andnethack-recoveris the one way back.-DLUA_USE_POSIX— Build the bundled Lua with its POSIX facilities, inSYSCFLAGS, which is what Lua’s own build reads; it never seesCFLAGS, so the tree’s flags are repeated there.LUA_VERSION— Passed to bothmakelines, so the directory the Makefiles look under,lib/lua-<version>, and the Lua release the recipe pins are the same version.WINLIB—-lncurseswalone. The ncurses port is built wide-character and without--with-termlib, so there is nolibtinfoto link, and itslibncurses.sois only a link tolibncursesw.so.CHOWN,CHGRP—true, so the install changes no owner or group: the game runs as the player, and every file it writes belongs to the player.
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/nethack, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf nethack-5.0.0-2.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/nethack, and runs the triggers its paths call for.
Contents
Installed programs: nethack, nethack-recover.
Installed directories: /etc/nethack, /usr/libexec/nethack, /usr/share/licenses/nethack, /usr/share/nethack.
Short descriptions
nethack |
Exploring The Mazes of Menace |
nethack-recover |
recover a NetHack game interrupted by disaster |
Notes
The game is linked against a static Lua built from lib/lua-<version>/src, a tree the release archive does not carry (upstream’s make fetch-lua downloads it). The recipe’s second source is that Lua release, and build.sh copies it into lib/ before the build.
hacklib.a is built first, on its own: src/Makefile builds it, and so does util/Makefile, by running a second make hacklib.a in src/. Under a parallel make the two runs of ar can meet and leave an archive with no index, which fails the link of makedefs. make install then builds Lua, the game, recover, the data files and the DLB archive; make all is not run because it also builds the Guidebook, which needs nroff, tbl and col.
The playground is per player. NetHack keeps saves, bones, level files, locks and the score file in its working directory, and the alternative, VAR_PLAYGROUND, is a compile-time string that no runtime value can reach. The one way to give each player their own is to give each player a working directory: unixmain.c reads $NETHACKDIR and changes into it before it opens a file. /usr/bin/nethack is a wrapper that sets NETHACKDIR, creates save/ (the game does not, and a missing one is a save error mid-game), creates the empty perm, logfile and xlogfile that the game locks but opens without O_CREAT, links nhdat, license and symbols from /usr/share/nethack, and runs the real binary from /usr/libexec/nethack. nethack-recover runs recover from the same directory.
For the same reason the binaries move out of HACKDIR into /usr/libexec/nethack, and the score file, logs, lock and empty save/ that make install seeds there are deleted: they are per-player state, and shipped in a read-only directory they would be files nobody may write.
The shipped sysconf sets WIZARDS=* and EXPLORERS=*, opening debug and explore mode to every player: with no shared playground and no privileged binary, such a game can only rewrite files its player already owns.