# Using enstate instead of sops-nix or agenix Examples for moving a NixOS host's secrets from sops-nix or agenix to enstate, and for delivering them with the common deployment tools. [comparison.md](comparison.md) says how the approaches differ; [design.md](design.md) says why enstate works as it does. The examples use templates, locations, `--pack --dry-run` and the incoming directory, as the branches that build them have it (2026-12-01). ## The idea, in three steps 1. **The host declares what it accepts**, in its NixOS configuration: slots, with types, and views for the services, which read them where they are (`LoadCredential=`, a path in a configuration file). 2. **A source list says where the values come from** on the deploying machine: your existing secret store, or, while moving, the sops or age files you have. 3. **A deployment packs a submission, copies it to the host's incoming directory, and switches.** The check before the switch plans it against the new generation and refuses the switch if something's missing; the switch takes it. Changing a secret later needs no deployment: pack, copy, `enstate --apply` over ssh. Secrets never go into the Nix store, and don't roll back with a generation. ## From sops-nix A typical sops-nix configuration: ```nix sops.defaultSopsFile = ./secrets/web.yaml; sops.secrets."db/password" = { owner = "app"; restartUnits = [ "app.service" ]; }; sops.templates."app.env".content = '' DATABASE_PASSWORD=${config.sops.placeholder."db/password"} ''; systemd.services.app.serviceConfig.EnvironmentFile = config.sops.templates."app.env".path; ``` The same with enstate: ```nix enstate.enable = true; enstate.channels.secrets.slots."db.password" = { type = "text/plain"; secret = true; view = false; # only used by the template below }; enstate.views."app.env" = { from = [ "secrets/db.password" ]; template = '' DATABASE_PASSWORD=$(secrets/db.password) ''; reactions.restart = [ "app.service" ]; }; systemd.services.app.serviceConfig = { LoadCredential = "app.env:${config.enstate.views."app.env".path}/app.env"; EnvironmentFile = "%d/app.env"; }; ``` - `restartUnits` becomes `reactions.restart`, on the view. - `sops.templates` becomes a view with a `template`; `$(…)` needs no escaping in Nix strings (`${…}` works too, escaped as `''${…}`). - `owner = "app"`: handing a secret to a service is systemd's business, not enstate's. enstate's views are root's (0600 for secret ones); the service gets the file as a systemd credential, `LoadCredential=`, readable by its own user (`DynamicUser=` included) at `$CREDENTIALS_DIRECTORY/NAME`, `%d/NAME` in its unit, and enstate orders the service after the views by itself. While moving, the sops file can stay the source, read on the deploying machine (`secrets.json`): ```json { "channel": "secrets", "complete": true, "items": { "db.password": { "source": { "command": ["sops", "--decrypt", "--extract", "[\"db\"][\"password\"]", "secrets/web.yaml"] } } } } ``` Later the values can move to whatever store you use (pass, passage, a vault's command line), and the sops files go. The host's age key that sops needed isn't needed any more. ## From agenix (and ragenix, agenix-rekey) ```nix age.secrets.wireguard.file = ./secrets/wireguard.age; networking.wireguard.interfaces.wg0.privateKeyFile = config.age.secrets.wireguard.path; ``` The same with enstate: ```nix enstate.enable = true; enstate.channels.secrets.slots."wireguard.key" = { type = "application/x-wireguard-private-key"; secret = true; }; networking.wireguard.interfaces.wg0.privateKeyFile = config.enstate.channels.secrets.slots."wireguard.key".path; ``` The `.age` files can stay the source, decrypted on the deploying machine with your key, not the host's, with a location declared once for all of them (`secrets.json`): ```json { "channel": "secrets", "complete": true, "locations": { "age": { "source": { "command": ["age", "--decrypt", "-i", "/home/me/.ssh/id_ed25519", "secrets/$(var:name).age"] } } }, "items": { "wireguard.key": { "location": "age", "name": "wireguard" } } } ``` `secrets.nix` with the hosts' public keys isn't needed any more: only you need to decrypt. agenix-rekey's generators become your store's own (enstate reads values, it doesn't make them). ## Delivering, with the common deployment tools Whatever the tool, the steps are the same: pack, copy to the host's incoming directory, switch. None of the tools below runs a command of yours before it switches, so a small script does the first two, and the tool the third. It takes the host, the new generation's schema (to check the source lists against), and the source lists: ```sh #!/bin/sh # deliver HOST SCHEMA SOURCES...: pack, and copy to HOST's incoming directory. set -eu host=$1 schema=$2; shift 2 work=$(mktemp -d "${XDG_RUNTIME_DIR:-/tmp}/enstate.XXXXXXXX") trap 'rm -rf "$work"' EXIT stamp=$(date -u +%Y%m%dT%H%M%SZ)-$$ for sources; do # Refuses, with every problem, if a source can't be read or a slot is missing. enstate --pack "$sources" --schema "$schema" --out "$work/$stamp-$(basename "$sources" .json)" done # Under a dot name, which enstate skips, and then renamed, within the incoming # directory: a receptor looking at it meanwhile sees no half submission. rsync -a --chmod=D0700,F0600 \ --rsync-path='mkdir -p -m 0700 /var/lib/enstate /var/lib/enstate/incoming && rsync' \ "$work"/ "root@$host:/var/lib/enstate/incoming/.$stamp/" ssh "root@$host" "cd /var/lib/enstate/incoming && for s in .$stamp/*; do mv \"\$s\" \"\${s#.$stamp/}\"; done && rmdir .$stamp" ``` `enstate --pack SOURCES --dry-run --schema SCHEMA` says the same without writing anything, and `--json` makes it data for a script. The check before the switch runs on the host, with the new generation's enstate and schema, and plans what waits; a refused submission refuses the switch, with the reasons. A host's first deployment works the same, as the check runs even where the running system has no enstate yet. ### nixos-rebuild ```sh schema=$(nix build --no-link --print-out-paths \ '.#nixosConfigurations.web.config.environment.etc."enstate/schema.json".source') deliver web "$schema" secrets.json nixos-rebuild switch --flake .#web --target-host root@web ``` ### deploy-rs ```sh schema=$(nix build --no-link --print-out-paths \ '.#nixosConfigurations.web.config.environment.etc."enstate/schema.json".source') deliver web "$schema" secrets.json deploy .#web ``` If deploy-rs rolls the generation back (magic rollback), the state stays as the switch took it: the older generation sees it by enstate's migrations, as after any rollback. For several nodes, deliver to each before `deploy .`. ### Colmena Colmena's nodes aren't flake outputs of their own; `colmena eval` passes an expression the hive's `nodes` (and `pkgs`, `lib`), and with `--instantiate` prints the derivations it forces, which `nix-store --realise` builds: ```sh schema=$(nix-store --realise "$(colmena eval --instantiate -E \ '{ nodes, ... }: nodes.web.config.environment.etc."enstate/schema.json".source')") deliver web "$schema" secrets.json colmena apply --on web ``` Colmena's own `deployment.keys` upload files at deployment, outside the store, as enstate's submissions are, without a schema or a plan; they can stay for what isn't in enstate yet, and move one by one. ### wire A hive evaluates to its nodes (`wire.makeHive`'s result has them as `nodes`), so for a `hive.nix`: ```sh schema=$(nix-build --no-out-link hive.nix \ -A 'nodes.web.config.environment.etc."enstate/schema.json".source') deliver web "$schema" secrets.json wire apply switch --on web ``` wire's `deployment.keys` (`wire apply keys`) move files outside the store, from commands on the deploying machine, as enstate's submissions do, but without a schema, a plan, or state on the host: keys in `/run/keys` are gone at a reboot until the next deployment, where enstate puts its views back by itself. Keys can stay for what isn't in enstate yet, and move to slots one by one. ### Clan Clan generates and stores values itself, with its vars, which is where they can stay: a location reads them with `clan vars get MACHINE GENERATOR/VAR`, which writes the value's bytes as they are when its output isn't a terminal, and fails with "Var … has not been generated yet" for one that doesn't exist yet (which `--pack --dry-run` shows as it is). enstate delivers them, checked against the host's schema, and changes them without deploying: ```json { "channel": "secrets", "complete": true, "locations": { "clan": { "source": { "command": ["clan", "vars", "get", "$(env:MACHINE)", "$(var:var)"] } } }, "items": { "wireguard.key": { "location": "clan", "var": "wireguard/private-key" } } } ``` Clan's machines are flake outputs, so: ```sh schema=$(nix build --no-link --print-out-paths \ '.#nixosConfigurations.web.config.environment.etc."enstate/schema.json".source') MACHINE=web deliver web "$schema" secrets.json clan machines update web ``` `clan vars generate` makes what's missing, before; the services read enstate's views, so Clan doesn't need to put those secrets on the machine itself. `clan machines update` sets the system profile, runs `switch-to-configuration boot`, which makes the new generation the next boot's default, and only then `switch`. enstate's check runs for `boot` too, so a generation the state can't serve is refused at that first step, before it becomes the default, with the check's reasons. ### krops krops' populate delivers submissions itself, with its `pipe` source made for directories (`directory = true`): the command gets an empty directory (mode 0700, removed afterwards) and fills it, and populate delivers it with the modes it has. A populate run of its own, with a target directory in memory, packs each channel there: ```nix pkgs.populate { target = lib.mkTarget "root@web" // { path = "/run/enstate/krops"; }; source.pending.pipe = { directory = true; command = toString (pkgs.writers.writeDash "web-enstate-pack" '' set -efu enstate --pack ${secretsList} --out "$1"/secrets ''); }; } ``` The submissions stay in `/run/enstate/krops/pending` between deployments, packed by channel without a stamp, so populate (checksums, a fixed modification time) sends only what changed. After the build, a step on the host copies them into the incoming directory under a dot name, and renames them, as above (from memory to disk, `mv` would copy, and enstate could see half a submission): ```sh stamp=$(date -u +%Y%m%dT%H%M%SZ)-$$ mkdir -p -m 0700 /var/lib/enstate /var/lib/enstate/incoming for channel in $(ls /run/enstate/krops/pending); do cp -a /run/enstate/krops/pending/"$channel" /var/lib/enstate/incoming/."$stamp-$channel" mv /var/lib/enstate/incoming/."$stamp-$channel" /var/lib/enstate/incoming/"$stamp-$channel" done ``` Then the switch takes them, or, if there's nothing to switch to, `enstate --migrate`. hrm's `krops.nix` does so. ## Changing a secret, without deploying With any of the tools, a secret changes without a deployment: ```sh enstate --pack secrets.json --out sub rsync -a --chmod=D0700,F0600 sub/ root@web:/tmp/sub/ ssh root@web 'enstate --plan /tmp/sub && enstate --apply /tmp/sub; rm -r /tmp/sub' ``` ## What doesn't move (yet) - **Secrets needed before users exist** (sops-nix's `neededForUsers`, hashed passwords), and other early state (initrd keys): later. - **Secrets inside a closure for images or CI**: by design, state doesn't take Nix's paths; images preseeded with state sealed to the TPM are designed, not built ([design.md](design.md), 8.3). - **Users' secrets** (home-manager's sops-nix and agenix modules): the system's state first; users' state later.