Skip to content

About

A NixOS deploy that refuses to lie about what is running. Gates on clean+pushed, builds on a build host, tags generation→commit.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nix-deploy-gate

A NixOS deploy that refuses to lie about what is running.

$ ./deploy.sh vps
REFUSED: working tree dirty. Commit everything first.
 M hosts/vps/services/nginx.nix

$ git commit -am 'vps: rate-limit the api route' && git push
$ ./deploy.sh vps
>> gate ok: vps @ fcf4d11 (main, clean + pushed to origin)
>> building .#vps on builder ...
>> deployed: vps @ fcf4d11 (switch)
>> tagged vps-gen-39 -> fcf4d11
>> DONE: vps @ fcf4d11 live (generation 39)

Four steps: gate (refuse dirty or unpushed), build (on a build host, never the target), ship (copy the closure, activate), record (tag generation to commit). About 100 lines of bash. Host addresses live in a gitignored config, not in the script.

The invariant

Every deployed generation maps to a pushed commit.

That is the whole point, and it is worth being precise about why it matters, because "commit before you deploy" sounds like hygiene advice and it isn't.

NixOS makes a promise: the flake in git describes the live machine. That promise is what lets you rebuild a dead box from the repo plus a data restore. It is the entire disaster-recovery story.

A single deploy from a dirty tree silently voids it. The machine now runs something that exists nowhere but one laptop's working directory. Nothing breaks, no error appears, and you will not find out until the box is gone and you are trying to rebuild it from a flake that no longer matches what was running. The failure is silent at the moment it happens and expensive at the moment it matters.

So the gate is not about tidiness. It is about keeping the DR claim true, and it is cheap to enforce today and impossible to retrofit onto past deploys.

There is no --force. An escape hatch used once, at 3am, under pressure, is the same as no gate at all.

Three decisions that are easy to get wrong

1. The remote you check against must outlive the target

The gate asks "is HEAD pushed?" That question is meaningless unless you name the right remote.

If you self-host git (Gitea, Forgejo, GitLab) on one of the machines you deploy with this, then pushing your VPS's config to the Gitea running on that VPS is a loop with no exit. The box dies and takes the only copy of its own configuration with it. Your DR plan now depends on the thing you are recovering from.

So the flake carries at least two remotes: one that survives the target (canonical, and what GATE_REMOTE points at) and any mirrors you like for convenience. The gate only trusts the survivable one.

This is the sort of thing that looks obvious written down and is invisible while you are setting it up, because pushing to your own Gitea feels like a real backup right up until it isn't.

2. The target should never compile

nixos-rebuild --build-host X --target-host Y realises the closure on X and ships only the result to Y. Y just activates it.

This is not a preference when the target is small. A 2-core, 2GB VPS asked to compile a kernel or a Qt dependency will thrash, OOM, and take your services down with it, and it will do that during the deploy, when you are least able to diagnose it. Build on something with cores, ship a finished artifact.

Both ends must share an architecture or you are quietly cross-compiling.

3. Tag afterwards; the generation number does not exist yet

You want to answer "what exactly was running when the incident started?" from git alone. The natural instinct is to put the generation number in the commit message.

It cannot work. The generation number is assigned by the activation, which happens after the commit is written. Guessing previous + 1 holds right up until someone runs a manual nixos-rebuild on the box, or an activation fails, and now your numbering is silently wrong in a way nobody notices until they are relying on it.

So the tag is written after a successful switch, pointing back at the deployed commit:

git show vps-gen-39      # exactly what was running as generation 39

The build step has the same shape of reasoning: it builds from a fresh clone of the pushed commit rather than the working directory. The gate just proved those are identical, so this changes nothing today. It makes "what was built is what was committed" a property of the process instead of a coincidence that happens to hold.

Setup

cp deploy.conf.example deploy.conf   # add your hosts; it is gitignored
./deploy.sh <host> [switch|boot|test]

Requirements: nixos-rebuild, git, ssh access to the target as a user who can activate, and a build host of the same architecture.

deploy.conf in full:

GATE_REMOTE=origin            # the remote that outlives your targets
BUILD_HOST=builder            # where closures are realised; localhost is fine

TARGET_vps=root@100.64.0.1    # user@address
PORT_vps=2222                 # optional, default 22
ATTR_vps=vps                  # optional, defaults to the host name

What this is not

  • Not a deploy tool. deploy-rs and colmena do rollbacks, health checks, and parallel fleets properly. This is ~100 lines of bash around nixos-rebuild that enforces one invariant. If you want the real thing, use the real thing. The ideas here port to either.
  • No automatic rollback. A bad activation is yours to fix. NixOS keeps the previous generation bootable, which covers the worst case.
  • No secrets handling. Use sops-nix or agenix and encrypt to each host's key so plaintext never enters the repo. This script only cares that the repo is committed and pushed.

License

MIT, see LICENSE.

About

A NixOS deploy that refuses to lie about what is running. Gates on clean+pushed, builds on a build host, tags generation→commit.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages