Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

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

Dependencies

Depends on: ncurses, zlib.

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:

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.