Skip to content
tomwrwPublic

About

megadots - a denful NixOS and Home Manager configuration by tomwrw.

Topics

Resources

Stars

12 stars

Watchers

0 watching

Forks

Latest commit

 

History

232 Commits

Folders and files

Repository files navigation

Introduction

check

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 main should be considered a work in progress. Hardware configs, hostnames, secrets and user attributes are unique to me - you'll need to bring your own.

About

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.

Features

  • 🖥️ 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 theme quirk.
  • 🚀 nixos-anywhere for bare metal remote deployment.

The hosts

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.

What den gives you

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 nixos block, a homeManager block, 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 bare homeManager block on a host-scope aspect is silently dropped, so gnome hands its dconf settings down with to-users; a user aspect's nixos block stays with the user (den #694), so ssh sends its one system setting up with to-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 lanInterface and lanSubnets - 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.

Start here

The shortest path through the repo, in reading order:

  1. den/defaults.nix - what every host and user gets, unasked.
  2. den/hosts.nix - the roster: per-host facts and each host's aspect settings, and nothing else.
  3. hosts/endgame/default.nix - a host as a readable manifest of the aspects it takes.
  4. aspects/roles/base.nix - a role is just an aspect that is all includes.
  5. aspects/system/networking.nix - a real aspect, and the single consumer of the firewall quirk.
  6. den/quirks.nix - every channel that crosses a scope.

Layout

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.

Quirks: how anything crosses a scope

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.

Checks

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-user persist routes 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.

Deliberate Nix settings

system/nix.nix sets two options worth calling out, both security trade-offs:

  • nix.settings.trusted-users = [ "root" "@wheel" ] - lets any wheel member build/substitute arbitrary derivations and push closures via nixos-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 the base16-schemes derivation at eval time). Without it, evaluation fails outright.

Known trade-offs

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 theme quirk. Leaving homeManagerIntegration.autoImport at its default has Stylix push its Home Manager module into home-manager.sharedModules as well; the module system keys that differently from den's import, evaluates it twice, and dies on stylix.base16 being 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). On flatmate the 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-users includes @wheel, on a host that signs its own boot chain. A trusted user can get arbitrary content into the store, and endgame is 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 endgame a 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, and flatmate has neither.
  • Real hardware serials are in the roster. settings.disko.device in 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.

Usage

This configuration has multiple system entry points. At the moment, I am a single user (tomwrw) managing multiple machines.

Prerequisites

  • Nix with flakes enabled (experimental-features = nix-command flakes).
  • Nothing else. nix develop provides sops, age, nixfmt, nvd and ssh-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. deploy expects these on a removable drive at usbDefault in tasks.nix, overridable with MEGADOTS_USB.

Getting Started

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 says

Inside 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 flatmate

These 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.

Bootstrapping a host from scratch

Six things have to be in place before nix run .#deploy <name> will produce a working machine.

  1. 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 token
    

    Modes are copied from the USB with cp -a, so a private key has to be 0600 there. Ownership is handled by nixos-anywhere's --chown, which runs during the install - --extra-files copies as root - with each user's uid:gid read from the host's own config, so the uid has to be pinned. A users/<name>/ for someone the host does not have stops the deploy.

    The USB path itself is usbDefault at the top of tasks.nix - /run/media/$USER/SURVIVOR/keys, expanded when the task runs - overridable with MEGADOTS_USB.

  2. The host's public age recipient as settings.sops.recipient in the roster, then nix run .#write-sops-config and nix run .#secrets-updatekeys. .sops.yaml is rendered from the roster by flake/sops-config.nix, so the host's rule comes with its entry, and check-sops-config fails while the committed file is out of date.

  3. modules/hosts/<name>/secrets.yaml with at least users/<user>/password. Evaluation interpolates this path from the hostname, so a missing file fails the build.

  4. syncthing/<name>/{key,cert,guiPassword} in modules/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.

  5. 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 from ip -br link and the LAN's subnets under settings.networking, the Syncthing device id and folder under settings.syncthing, the age recipient from step 2 under settings.sops, and x86_64Level if 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.

  6. modules/hosts/<name>/ with a default.nix listing the aspects it takes and a _hardware.nix from nixos-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.

Adapting this for yourself

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.

Community

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.

And more inspiration

About

megadots - a denful NixOS and Home Manager configuration by tomwrw.

Topics

Resources

Stars

12 stars

Watchers

0 watching

Forks

Contributors

Languages