work // nixos / systemd
Machines that update themselves without compiling
contents
The last piece of celestial is the one I notice least, which is the point. Twice a day every machine checks main, downloads the system CI built for it, and makes it the boot default. No compiling, no SSH from a central box, no deploy tool. It lives in one module, diy-modules/host_upgrade.nix, behind host.upgrade.enable.
Pull, not push
Push-based deploy tools need the target online when you deploy. Two of my machines are laptops that spend days asleep, so anything that pushes would mostly be retrying. Each machine pulling for itself means a laptop that was off for a week catches up the next time it boots.
What one run does
- Ask GitHub where main is.
git ls-remotereturns one commit hash. With nothing new, that’s the whole run. - Work out which system that commit means for this host.
nix evalonnixosConfigurations.<host>...toplevel.outPathat that exact revision gives a store path. The result is cached per commit, so evaluation only happens once per push. - Compare. Already on it: done.
- Check the running system was made by this unit. If not, leave it (more below).
- Download, never build.
nix-store --realiseon the store path, not the derivation. A store path can only be substituted, so if CI hasn’t pushed it there is nothing to download and nothing is compiled. - Install.
nixos-rebuild boot --store-pathmakes it the default for the next boot.
The --store-path trick in step 5 is the whole design. It turns “the cache doesn’t have this” from “build it locally for an hour” into “not yet, try later”.
Telling failures apart
When the download fails, the unit asks the cache for the system’s .narinfo and reads the status code:
| Answer | Meaning | What happens |
|---|---|---|
| 404 | CI hasn’t built this commit yet | stop quietly; notify if it’s still missing after 20 hours |
| 200 | it’s there but the download failed | stop, report it |
| anything else | the cache isn’t reachable | stop, ask whether Pangolin is connected |
The 20-hour clock starts at the first miss, not the newest commit. If CI is red and I keep pushing every day, the warning still fires instead of resetting each time.
Leaving my hand-made generations alone
If I’ve run nh os switch to try something, or rolled back after a bad update, the timer must not undo that at midnight. The unit records the last system it installed in followed. When the running system isn’t that one, it assumes a human chose it, sends one notification per commit (“this machine runs a generation made by hand”) and does nothing.
upgrade in my shell runs the same unit in a “now” mode that takes main regardless. A polkit rule lets anyone in wheel start that one service without a password, and nothing else.
Small things that took a bug each
A run that dies mid-install. nixos-rebuild sets the system profile before it writes the boot entry. If power goes in between, the profile says “new” and the bootloader says “old”, and the “already on it” check in step 3 would never fix it. The unit touches a pending file before installing and removes it after; a leftover pending forces the install to run again.
The timer fires before wifi. Persistent=true means a missed run fires at boot, usually before the network is up. In automatic mode the ls-remote gets five tries a minute apart.
Two slots, not one. The timer runs at midnight and 13:00, with up to an hour of random delay. The second slot exists for laptops that were off at midnight, since the catch-up at boot often happens before I’ve logged in and brought the Pangolin tunnel up.
A switch must not kill its own upgrade. The service has restartIfChanged = false, otherwise activating a new generation could stop the unit that’s activating it.
Be quiet when no one is looking. Notifications go to every logged-in user’s session bus. If nobody is logged in the unit doesn’t mark the commit as “told”, so the message arrives when someone is.
Trade-offs
Booting into the new system rather than switching live means a fix doesn’t land until a reboot. I took that because a live switch under a running desktop is where most of my breakages happened, and the default is one option away (host.upgrade.operation = "switch").
It trusts CI completely. If a broken system builds, every machine takes it at next boot. The previous generation is one key press away in the boot menu, and tests/upgrade.nix runs the unit itself in a VM on every nix flake check, but there’s no canary host. With four machines I haven’t needed one; with forty I would.