My NixOS configuration, built on the den framework + Home Manager + Flakes. This framework provides libraries that make implementing the dendritic pattern a breeze. I publish this repo to help others, as I found other people's repos extremely helpful when learning Nix/NixOS. Hopefully I can return the favour.
Note: This is my personal config. Any branch other than
mainshould be considered a work in progress. Hardware configs, hostnames, secrets and user attributes are unique to me - you'll need to bring your own.
This is the fourth iteration of my NixOS configuration. I've been daily driving NixOS for nearly 2 years. Most recently, I have dabbled with the dendritic pattern and with the den framework, I have found a suitable home for my configs.
You can find my other configs archived in named branches for review if you want to check out other management styles, like megadots-classic and megadots-dendritic.
I'm not a developer. I'm a tinkerer with a consultancy job in a technical field who got curious about declarative system management and fell down the NixOS rabbit hole. This project has genuinely brought some fun back into computing for me.
- 🖥️ NixOS aspects for multiple hosts.
- 🏠 Home Manager as a NixOS module.
- 👻 sops-nix for secrets management, with dedicated age key support for hosts and users.
- 🔑 FIDO2 hardware keys (Token2 PIN+) for SSH, with an optional extra LUKS unlock keyslot alongside the passphrase.
- 📸 Impermanence on an ephemeral btrfs root -
/is restored from a blank snapshot on every boot, and state is opt-in per aspect. - 👮 Secure Boot via lanzaboote with automatic key generation and enrollment.
- ❄️ Flake with the den framework for modular, composable host and user aspects.
- 💾 Disko for declarative disk partitioning.
- 💢 CachyOS kernel for a gaming optimised kernel (opt-in per host).
- 🎨 Stylix for consistent base16 theming at both scopes - the login screen, TTY
and plymouth as well as the user session - driven by a single
themequirk. - 🚀 nixos-anywhere for bare metal remote deployment.
| Host | Machine | Desktop | Bootloader | Kernel | Aspects |
|---|---|---|---|---|---|
endgame |
AMD desktop (zen4) | GNOME | lanzaboote (Secure Boot) | CachyOS lto-znver4 |
base, workstation, gaming, dev |
flatmate |
Microsoft Surface Pro (Intel) | GNOME | lanzaboote (Secure Boot) | nixpkgs default | base, workstation, dev |
Both run an ephemeral btrfs root on LUKS, the same user and the same desktop. / is its
own subvolume, deleted and restored from a read-only root-blank snapshot by an initrd
service on every boot; /nix, /persist and swap are separate subvolumes that survive.
/home deliberately sits inside the rolled-back root, so user state is opt-in through
persist.home in the aspect that owns it, exactly as system state is opt-in through
persist.system. Touch dont-wipe at the top of the btrfs volume to skip the wipe for a
boot.
The desktop is a host's choice, not the workstation aspect's - the same way the
bootloader is - so moving a machine to another desktop is an edit to that host's include
list and nothing shared.
If you have not met den before, five terms explain almost everything in this repo:
- aspect - a named, self-contained feature. It can carry a
nixosblock, ahomeManagerblock, or both. Aspects are never split by class, only by concern: bluetooth owns its NixOS options and the state it needs persisted, in one file. includes- how a host, a user or another aspect opts in. roles/base.nix is nothing but a list of aspects every host takes.provides.to-users/provides.to-hosts- crossing between a host and its users. A barehomeManagerblock on a host-scope aspect is silently dropped, so gnome hands its dconf settings down withto-users; a user aspect'snixosblock stays with the user (den #694), so ssh sends its one system setting up withto-hosts.- quirk + policy - a quirk is a named data channel an aspect writes to; a policy routes it. All of both live in den/quirks.nix.
- settings - a value an aspect needs that differs per machine. The aspect declares it
with a type and a default where one makes sense -
networking declares
lanInterfaceandlanSubnets- and each host sets it in the roster. A required one left unset fails eval naming the option. This is den's documented pattern, wired up in den/settings.nix.
The shortest path through the repo, in reading order:
- den/defaults.nix - what every host and user gets, unasked.
- den/hosts.nix - the roster: per-host facts and each host's aspect settings, and nothing else.
- hosts/endgame/default.nix - a host as a readable manifest of the aspects it takes.
- aspects/roles/base.nix - a role is just an aspect
that is all
includes. - aspects/system/networking.nix - a real aspect, and the single consumer of the firewall quirk.
- den/quirks.nix - every channel that crosses a scope.
Everything lives under modules/, discovered automatically by
import-tree - there are no manual import lists;
dropping a file in is enough. flake.nix is generated by
flake-file (nix run .#write-flake), so each module
declares the flake inputs it needs right next to the code that uses them via
flake-file.inputs.
modules/
├── aspects/ # every aspect, filed by what it is
│ ├── roles/ # base, workstation, gaming, dev
│ ├── system/ # the OS itself: boot, disks, kernel, nix, networking, security
│ ├── desktop/ # gnome, stylix, fonts, networkmanager
│ ├── hardware/ # opt-in support, and per-model profiles
│ └── apps/ # every user-facing app, one file each
├── den/ # defaults, the host roster, the schema, settings, the quirks
├── flake/ # flake plumbing: inputs, treefmt, checks, devShell, tasks, .sops.yaml
├── hosts/ # one directory per host: its aspects, _hardware.nix and secrets.yaml
│ ├── endgame/
│ └── flatmate/
└── users/tomwrw/ # the user, itself just another aspect, and its secrets.yaml
Aspect names are short and flat: modules/aspects/system/sops.nix declares
den.aspects.sops, and modules/aspects/apps/signal.nix declares den.aspects.signal.
The directories are filing, not namespace - an includes list reads as a list of names
rather than a list of paths. den never reads a path: an aspect is identified by its
attribute path, so a file can be moved between these directories without changing a line
of configuration.
A directory says what a thing is; roles and hosts say who takes it. That is the whole
filing rule, and the reason system/ is not called core/ as it was previously: membership is already stated,
precisely and in exactly two enforced places, by roles/ and by
each host's default.nix. A directory that also implies membership is an unenforced third
copy, and it drifts - system/scheduler.nix is on endgame alone, which is a fact about
endgame's include list, not about where the file sits.
This on it's own took me some time to wrap my head around when moving to den (and more broadly, dendritic patterns).
Host-specific hardware is not an aspect: each host imports its own _hardware.nix
directly, and the _ prefix is what stops
import-tree picking it up as a module of its own.
Roles are aspects too. base, workstation, gaming and dev are ordinary aspects that
happen to be mostly includes, so there is one concept to learn rather than two.
An aspect says what it needs; something else decides how to apply it. All five channels, and every policy that routes one, live in den/quirks.nix.
# aspects/apps/sunshine.nix says only this...
firewall.tcp = [ 47984 47989 47990 48010 ];
# ...and aspects/system/networking.nix is the single place that turns every such
# declaration into nftables rules: on the host's LAN interface, from its LAN subnets.| Quirk | Carries |
|---|---|
persist |
system paths for environment.persistence, home paths for home.persistence |
firewall |
LAN-only ports, accepted on the host's LAN interface from its LAN subnets |
theme |
the base16 scheme and wallpaper, read by Stylix at both scopes |
syncthing-peer |
one device in the mesh, produced per host and consumed per user |
terminal |
how to run a command in a terminal window, so no aspect names one |
Note the trap: a quirk emitted from a user-scope aspect only reaches the host if an
expose policy is registered for it in den.schema.user.includes - without one it is
discarded silently, with no error. syncthing is included at user scope, so its firewall
ports depend on exactly that. Expose copies rather than moves, which is why one
persist quirk can carry both halves: the home entries stay readable in the user scope
they are consumed in.
The Syncthing mesh is the same machinery at fleet scale, and is built without the aspect
knowing the fleet exists: a producer on den.schema.host.includes makes every host
announce its own id, a pipe.collectAll policy gathers all of them into the user scope,
and apps/syncthing.nix reads the pool it is handed.
Peers that aren't den hosts - the NAS - are appended to the same pipe from
fleet.externalPeers in den/hosts.nix, so they arrive
indistinguishable from a fleet host.
nix flake check builds both hosts, runs treefmt, greps the tree for plaintext key
material, and runs routing and controls - all in
flake/checks.nix - and check-sops-config, which fails while
.sops.yaml is not what flake/sops-config.nix renders
from the roster.
routing is for what still builds but is wrong - the failures the comments here call
silent, because nothing errors when they happen. It reads every host and fails on:
- a port open anywhere but the LAN interface, or a rule there without the subnet match;
- a feature and its port disagreeing - sshd and 22, Sunshine and 47989, a user's Syncthing and 22000 reaching the host through the expose policy;
- a home-relative path in
environment.persistence, or a user missing what the user-scope and host-to-userpersistroutes should have put in their home; - a user's Syncthing devices not being every announcing host plus
fleet.externalPeers.
It also forces each host's disko script, the only place that reads the disk path, and
builds each nftables.service, which runs nft --check on its ruleset.
controls is what makes those green results mean something. A check that only ever sees
healthy hosts passes whether or not it can still fail, so this one builds the real hosts
with something deliberately broken (through extendModules) and fails if any of these
stops firing: the Zen 4 kernel guard, the workstation desktop guard, the Surface standby
guard, and the routing facts for a port open everywhere, Sunshine without its port, and
a port listed twice in one rule. It also fails if a host it breaks has been renamed away,
rather than passing with nothing left to test.
The secrets scan guards the one mistake a public repo cannot take back: a committed private key. It assembles its own search strings from fragments so that the file does not match itself.
system/nix.nix sets two options worth calling out, both security trade-offs:
nix.settings.trusted-users = [ "root" "@wheel" ]- lets anywheelmember build/substitute arbitrary derivations and push closures vianixos-rebuild --target-host. A single-admin-LAN trade-off: fine as the sole admin of this fleet, not something to carry into a multi-user or shared-admin setup without thought.nix.settings.allow-import-from-derivation = true- required because Stylix's base16 scheme reader does an IFD (readFiles a YAML out of thebase16-schemesderivation at eval time). Without it, evaluation fails outright.
Things that are deliberate rather than missed, so you can judge whether they suit you:
- Stylix's own Home Manager auto-import is switched off. Stylix is applied at both
scopes, each half reading the same
themequirk. LeavinghomeManagerIntegration.autoImportat its default has Stylix push its Home Manager module intohome-manager.sharedModulesas well; the module system keys that differently from den's import, evaluates it twice, and dies onstylix.base16being read-only and set twice. - The LAN boundary is a source subnet, so it has one residual edge. Every port an
aspect asks for is accepted only on the host's LAN interface and only from its
settings.networking.lanSubnets(nftables). Onflatmatethe interface is the Wi-Fi adapter, so it is the subnet that keeps SSH and Syncthing closed on a cafe network - a foreign network that happens to reuse the same subnet would still match. The subnets are IPv4 only, so IPv6 mDNS and link-local Syncthing addresses from the LAN are refused; IPv4 carries both. trusted-usersincludes@wheel, on a host that signs its own boot chain. A trusted user can get arbitrary content into the store, andendgameis the machine that then signs whatever it boots with its Secure Boot key.- A third-party binary cache supplies that host's kernel.
system/nyx-cache.nix trusts
chaotic-cx/nyx for prebuilt CachyOS kernels, and for
proton-cachyos in apps/steam.nix. It composes with the
point above: a compromised cache could hand
endgamea kernel its own Secure Boot chain would then sign and boot without complaint. Accepted so that a znver4 LTO kernel does not have to be compiled locally on every bump. Only hosts that take a package from nyx trust it - the aspect is included by the two that do, andflatmatehas neither. - Real hardware serials are in the roster.
settings.disko.devicein den/hosts.nix carries the NVMe serial of each machine./dev/disk/by-id/is the correct stable identifier - disko partitions on it - so it cannot come from sops, which is decrypted far too late to place a partition. A serial is an identifier, not a credential; the realistic cost is that it fingerprints the hardware.
This configuration has multiple system entry points. At the moment, I am a single user (tomwrw) managing multiple machines.
- Nix with flakes enabled (
experimental-features = nix-command flakes). - Nothing else.
nix developprovidessops,age,nixfmt,nvdandssh-to-age, and every task below - see devshell.nix. - An age keypair per host and per user, and a FIDO2 token if you want the extra LUKS
keyslot.
deployexpects these on a removable drive atusbDefaultin tasks.nix, overridable withMEGADOTS_USB.
The repo's tasks are flake apps, defined in
tasks.nix. nix flake show lists them with a
description each.
nix flake show # list the tasks
nix run .#build endgame # build a host's closure locally (no activation)
nix run .#diff-host endgame # build, then nvd diff against the running system
nix run .#rebuild endgame # switch a remote host (pushes a locally-built closure)
nix run .#deploy endgame # bare-metal install via nixos-anywhere (formats disks)
nix run .#enroll-fido2 endgame # add the inserted token to the LUKS header
nix run .#luks-device endgame # print the host's LUKS device path
nix run .#write-sops-config # render .sops.yaml from the roster's recipients
nix run .#secrets-updatekeys # re-encrypt every secrets file to what .sops.yaml now saysInside nix develop the same tasks are on $PATH as bare commands, so the
above is build endgame, diff-host endgame, and so on. It is diff-host and
not diff because that name belongs to diffutils, and shadowing it inside the
shell would be a nasty surprise.
A positional host passes straight through, but a flag for the task needs --
first, or nix consumes it: nix run .#rebuild -- endgame --flag.
An unknown or missing host is refused up front, with the valid list:
$ nix run .#deploy typo
no such host: typo
valid hosts: endgame flatmateThese run from the checkout and can drive either host. For operating on the
machine you are sitting at, from any directory, use the n* aliases in
system/nix.nix - nr, nb, nd, nu, ncheck,
nfmt, nclean.
There is no wrapper for the things nix already does. nix flake check builds
both hosts and runs the checks above; nix fmt runs nixfmt, deadnix and statix;
nix flake update updates the inputs.
CI does not build the closures. It runs the six checks, then instantiates every
host - forcing toplevel.drvPath evaluates the whole module system, and every assertions
entry in the tree with it, without realising a single output. The host list is derived from
nixosConfigurations, so a new host is covered without touching
check.yml.
Six things have to be in place before nix run .#deploy <name> will produce a working
machine.
-
USB key material. The USB mirrors the destination: whatever is under
<usb>/users/<username>/is copied to that user's home, at the same relative path. There is no manifest anywhere in this repo, so adding a key is a copy on the USB and no config change at all.<usb>/hosts/<hostname>/age.txt # -> <persistRoot>/var/lib/sops-nix/key.txt <usb>/users/<username>/.config/sops/age/keys.txt # -> ~/.config/sops/age/keys.txt <usb>/users/<username>/.ssh/id_ed25519{,.pub} <usb>/users/<username>/.ssh/id_ed25519_sk_primary{,.pub} # FIDO2 handles; useless <usb>/users/<username>/.ssh/id_ed25519_sk_backup{,.pub} # without the physical tokenModes are copied from the USB with
cp -a, so a private key has to be0600there. Ownership is handled by nixos-anywhere's--chown, which runs during the install ---extra-filescopies as root - with each user's uid:gid read from the host's own config, so the uid has to be pinned. Ausers/<name>/for someone the host does not have stops the deploy.The USB path itself is
usbDefaultat the top of tasks.nix -/run/media/$USER/SURVIVOR/keys, expanded when the task runs - overridable withMEGADOTS_USB. -
The host's public age recipient as
settings.sops.recipientin the roster, thennix run .#write-sops-configandnix run .#secrets-updatekeys. .sops.yaml is rendered from the roster by flake/sops-config.nix, so the host's rule comes with its entry, andcheck-sops-configfails while the committed file is out of date. -
modules/hosts/<name>/secrets.yamlwith at leastusers/<user>/password. Evaluation interpolates this path from the hostname, so a missing file fails the build. -
syncthing/<name>/{key,cert,guiPassword}inmodules/users/<user>/secrets.yaml- the syncthing secrets are keyed by host but live in the user file (apps/syncthing.nix). Miss these and the host builds fine, then Home Manager activation fails on the new machine. -
The roster entry in modules/den/hosts.nix: the system disk and swap size under
settings.disko(a stable/dev/disk/by-id/path), the LAN interface name fromip -br linkand the LAN's subnets undersettings.networking, the Syncthing device id and folder undersettings.syncthing, the age recipient from step 2 undersettings.sops, andx86_64Levelif the CPU goes past x86-64-v1 (check with/lib64/ld-linux-x86-64.so.2 --help | grep supported). That one fact picks the Proton-CachyOS build and is what the Zen 4 kernel guard checks. -
modules/hosts/<name>/with adefault.nixlisting the aspects it takes and a_hardware.nixfromnixos-generate-config.
Then boot the target from a NixOS installer ISO, set a password for the nixos user so
SSH works, and run nix run .#deploy <name>. nixos-anywhere partitions with disko, copies the
USB tree into /persist, chowns it to the user, and installs. The machine comes up with its
secrets decryptable and its keys in place - no second pass, nothing to do by hand.
Fork it, then: replace modules/users/ and modules/hosts/ with your own, empty the
roster in modules/den/hosts.nix, put your own age recipients there (fleet.sops and each
host's settings.sops.recipient) and run nix run .#write-sops-config, and
replace assets/ (the wallpapers are not covered by this repo's licence - see
LICENSE). The parts worth keeping are modules/aspects/ and modules/den/ -
no aspect names a host or a user, and what differs per machine is a setting in the
roster. Even where the persist store is mounted is a host fact, persistRoot
(/persist unless set, in den/schema.nix): disko mounts it,
impermanence collects into it, and ephemeral-btrfs, sops (its age key), openssh (host
keys), gnome (monitors.xml) and the deploy task read it for the files they need before
or outside impermanence's bind mounts.
I learn by doing. None of this would be possible without the copious amounts of developers and repos that share their content freely for others like me to dissect and study. There are many, but to name a few - shout outs go to:
ryan4yin for their awesome book on NixOS (if you haven't started here, then give it a whirl - it really was great) and the i3 Kickstarter repo. Both excellent resources to help me understand the power of NixOS.
The majority of my config structure was heavily influenced by the awesome Misterio77. Not only did his Nix Starter Configs help guide me early on, but his own Nix Config repo was a great inspiration on how to construct and model a modular Nix configuration.
- Nvim - https://github.com/Nvim (https://github.com/Nvim/snowfall)
- mtrsk - https://github.com/mtrsk (https://github.com/mtrsk/nixos-config)
- runarsf - https://github.com/runarsf (https://github.com/runarsf/dotfiles)
- librephoenix - https://github.com/librephoenix (https://github.com/librephoenix/nixos-config)
- frost-phoenix - https://github.com/Frost-Phoenix (https://github.com/Frost-Phoenix/nixos-config)
