- Nix 90%
- Shell 10%
mawile #
This is my NixOS configuration for my home server, "Mawile". Some things I run here:
- Media library management and streaming (Jellyfin, Navidrome, Radarr, Sonarr, Calibre)
- Home automation (Home Assistant, Zigbee2MQTT, Mosquitto)
- File sync (Syncthing)
- Read-it-later archive (Readeck)
- RSS reader (Miniflux)
- WAN access (Caddy reverse proxy)
- Monitoring (Gatus dashboard at
http://mawile:8080, alerts via ntfy.sh, healthchecks.io as an off-site dead man's switch). Each alert's text says what it means and what to run first.
I use ZFS for the entire machine. There is an encrypted root that unlocks remotely over SSH. Everything is one flake for one host. It's licensed 0BSD, so take anything you want.
Everything below this was written by Claude to help describe some of the quirks of my setup so I'm not scratching my head in the future.
Applying changes #
sudo nixos-rebuild switch
Evaluation is pure — no --impure. Nothing here is read from outside the
flake while Nix is evaluating, and the section below is what makes that
possible for the one value that wants to be.
Secrets #
Credentials live in secrets/*.age, encrypted with
agenix and committed. They decrypt to
/run/agenix/<name> at activation, mode 0400, owned by the consuming
service. Each is encrypted to three recipients (secrets/secrets.nix): the
host key so the machine decrypts unattended, a workstation key for editing,
and an offline age identity kept in a password manager — encrypting to the
host key alone would mean a dead host takes the only decryption path with it.
cd secrets
agenix -e cloudflare.age -i ~/.ssh/<key> # edit
agenix --rekey # after changing recipients
Two things about agenix that cost me time. Renaming a secret needs no
re-encryption at all — the filename isn't bound into the ciphertext, so
git mv plus a secrets.nix edit is the whole job. And agenix -e silently
overrides $EDITOR to cp -- /dev/stdin whenever stdin isn't a TTY, so
writing one non-interactively means piping the plaintext in
(agenix -e foo.age < plaintext); setting EDITOR gets you an empty secret
that decrypts perfectly happily. Check the byte count.
Keeping the domain out of the Nix store #
The interesting problem. My base domain is in secrets/domain.age, but its
consumers would normally want it while Nix is evaluating — a Caddy
virtualHost is an attribute name, a ddclient zone is a build-time string, a
Readeck setting gets rendered into a TOML file — and everything Nix evaluates
lands in the world-readable store. agenix decrypts at activation, far too late
to help. This is what the config used to reach for an absolute-path import and
--impure to solve.
The fix is to let each service learn it at runtime instead:
- Caddy takes
domain.ageas itsenvironmentFile. The secret is a singleDOMAIN=<domain>line so systemd reads it verbatim, and the vhosts are namedjellyfin.{$DOMAIN},navidrome.{$DOMAIN},readeck.{$DOMAIN},miniflux.{$DOMAIN}. The Caddyfile adapter expands{$VAR}when it loads the config. The NixOS module's only build-time step over the Caddyfile iscaddy fmt, which leaves the placeholder alone — there's no build-timecaddy validateto trip over it. Unset the variable and the site address becomesjellyfin., which Caddy rejects outright; the failure is loud, not silent. - ddclient gets
zone = "@domain@"and a matchingdomainsentry, substituted by an extraExecStartPrethat sources the same secret andseds the config in/run. It has to belib.mkAfter, since the module's own prestart is what puts the file there, and!-prefixed so it runs as root and can read the0400secret. - Readeck wants the domain for
server.allowed_hosts, andservices.readeck.settingsbecomes a store-visiblereadeck.toml. So that one option is simply left out ofsettingsand supplied as aREADECK_ALLOWED_HOSTS=line insecrets/readeck.age, which the module is already reading as itsenvironmentFilefor the session key. Readeck maps every setting to aREADECK_*variable, so anything else that needs the domain can move the same way. - Miniflux needs it for
BASE_URL, andservices.miniflux.configbecomesEnvironment=lines in a store-visible unit file. SoBASE_URLrides insecrets/miniflux.agenext to the admin credentials, which the module already reads as anEnvironmentFileviaadminCredentialsFile.
domain.age has no owner for the same reason cloudflare.age doesn't:
systemd reads EnvironmentFile as PID 1 before dropping privileges, and the
ddclient hook is root. readeck.age and miniflux.age have none either, even
though a static readeck user does exist.
Boot #
The root filesystem is LUKS-encrypted, and the machine is on wireless, so unattended boot isn't possible. The initrd brings up wpa_supplicant, gets DHCP, and starts sshd on port 2222:
ssh -p 2222 root@mawile # drops straight into the passphrase prompt
After switch-root, zfs-import-datapool.service unlocks the pool using the
key at /home/stalker/datapool.key.
Gotchas #
- The ZFS pool key lives on the root disk (
/home/stalker/datapool.key). raidz2 survives two disk failures; it does nothing if that passphrase is lost. Keep a copy off this machine. wpa_supplicant.confis the one plaintext secret (gitignored, not agenix). The initrd consumes it before agenix has run, and it's needed to reach the network to unlock the disk at all.boot.initrd.secretspoints at it by absolute path and pure evaluation is fine with that — the module onlytoStrings the value intoappend-initrd-secrets, never reading or copying it, which is also what keeps the plaintext out of the store. OnlyimportorreadFileof an absolute path trips pure eval.- NFS port 2049 is not in
allowedTCPPorts. It's opened by an explicit source-scoped rule innetworking.firewall.extraCommands, because the exports grant rw access to/and NFSsec=systrusts whatever UID the client asserts. Widening that rule widens root access to the whole disk. - yt-dlp gets a copy of its cookie file. It opens
--cookiesfor writing to persist refreshed cookies, but agenix decrypts to0400, soscripts/ytdl.shcopies to a temp file first. Refreshed cookies are discarded; re-export from the browser andagenix -ewhen they expire. - The media apps set
openFirewall, so they answer directly on the LAN. The Caddy vhosts are convenience for off-LAN access, not the only door. - An agenix
ownerpointing at a user that doesn't exist breaks the secrets after it. The chown step walks the secrets in order and aborts the whole snippet on the first failure, leaving later ones root-owned.ddclientruns underDynamicUser, socloudflare.agecorrectly has noowner— its!-prefixed prestart reads it as root and copies it in. - Readeck runs as a static user on purpose. The nixpkgs module hardcodes
DynamicUser, and a dynamic UID can't own a directory outside systemd'sStateDirectory— which would strand the archive in/var/lib/private/readeck, on the root disk, where sanoid never looks.DynamicUser = lib.mkForce falseplus areadeckuser is what buys/mnt/data/Libraries/Readeck; the rest of the module's hardening is left alone.database.sourceis set next tomain.data_directorybecause its default is relative toWorkingDirectory, not to the data directory — set only one and the SQLite file stays behind while the bookmarks move. Unlike the media apps, it's loopback-only with no firewall port. - This repo is edited over NFS from the workstation at
/mnt/mawile, so a server reboot leaves the mount stale. Remount rather than debugging exports.
Layout #
| Path | |
|---|---|
flake.nix |
inputs (nixpkgs stable + unstable, home-manager, agenix), single mawile host |
configuration.nix |
everything system-level, including all Home Assistant config |
stalker.nix |
home-manager: beets library manager and its hourly import timer |
hardware-configuration.nix |
generated; don't hand-edit |
scripts/ytdl.sh |
yt-dlp wrapper, inlined as both a CLI tool and a daily timer |
scripts/health.sh |
host health checks (failed units, ZFS, SMART, disk space) pushed to Gatus every 5 minutes |
secrets/ |
agenix ciphertext + recipient list |
pkgs.unstable.<name> is available anywhere, via an overlay in flake.nix.