# enstate: state next to the store, a design draft Draft 3, 2026-11-27. Nothing is built yet. Section 11 lists what is open. The name, enstate, may change. An older model of state objects is in [state-object.md](state-object.md); section 9 says how it fits in. [migrations.md](migrations.md) walks through the migration model with three made-up cases. ## 1. The problem A NixOS host is built from two kinds of data. - **Generations**: the system, in the store. They move forward and roll back, get copied to builders and caches, and are readable by everyone on the host. - **State**: keys, passwords, tokens, and also public data about other hosts (their keys, addresses). It has one current value. It moves forward only, on its own schedule (a rotation, a new peer), and each piece has its own audience. NixOS has no facility for the second kind, so it ends up in one of three wrong places: in the store (readable, copied around, and rolled back with the generation: sops-nix and agenix fix the first and not the last), in files nobody declared (`/var/src/secrets`, where a missing file shows up as a failed unit), or evaluated into the system (the host database: a new peer means deploying every host). ## 2. Goals 1. Change state without deploying: rotate a key, add a peer, update known hosts, authorized keys, host names, and have the running system pick it up. 2. A system declares what state it can consume. State that doesn't fit is refused, with the reason, before anything changes. 3. An operator can hand over a part of the state, see what it would do before it is done, and say for each piece whether it must go in or may be left out. 4. State never rolls back when the generation does. Between two generations that expect different state there is a migration. 5. Secrets are never in the store, never on a builder, never evaluated. 6. Independent of hrm and of NixOS: a program and file formats of its own. NixOS integrates it with a module; another system could, too. Scope: the system's state first. Users' state (a user's keys, tokens) and early state (the initrd's ssh host key, a disk's key) come later, by the same mechanism where possible; nothing here may prevent them. Not goals: storing or generating secrets at the source (password-store and the like stay), and configuration management (no code in what gets handed over, see 5.6). ## 3. Words One word per concept, and no word used for two things, so that renaming stays a search and replace. All of them are working names. | word | meaning | |---|---| | **state** | all of it, as opposed to generations; never one piece | | **channel** | one of "the other databases": state with its own provider, order and cadence | | **slot** | a named place in a channel that a generation offers | | **entry** | what a file slot holds: one value | | **record** | one element of a structured slot, under a key | | **type** | what a slot holds, and how to check it; types have **versions** | | **format** | how a value is encoded, named by a media type | | **snapshot** | the whole content of one channel on one host at one moment | | **submission** | what gets handed to the host: a set of items for one channel, complete or partial | | **item** | one piece of a submission: an entry or a record to set or remove, and whether it is required | | **group** | items of a submission that go in together or not at all | | **plan** | what a submission would do: which items go in, which don't and why, how the views change, what reacts | | **order** | what makes one snapshot the successor of another (a channel's choice) | | **schema** | what one generation accepts: its channels, slots, types, views, reactions | | **view** | a file made from one or more slots, at a stable path, for programs to read | | **renderer** | the program that makes a view | | **validator** | the program that checks a slot's content | | **converter** | the program that turns one version of a type into another | | **reaction** | what the system does when a view changes: reload or restart units | | **receptor** | the program on the host that plans, accepts or refuses submissions, and migrates | | **provider** | whatever makes submissions and delivers them | | **migration** | how state accepted under one schema serves another | | **policy** | what to do with a slot that can't be filled | ## 4. The parts | part | where it lives | |---|---| | schema | in the generation (store); it is code, like the system | | submission | made outside, handed to the host | | snapshot | on the host, kept by the receptor | | receptor | on the host, set up by activation; a small program run on demand, like an activation script, not a service; language secondary, Rust a candidate | | provider | outside the host (a deploy tool, an operator), or an agent on it (a sync) | A host would start with two channels: its own secrets (from the deploying host), and the fleet's public data (from a registry). ## 5. How it works ### 5.1 Slots, types and formats - A **file slot** holds one entry: a private key, a certificate, a ready-made file. - A **structured slot** holds records under keys: hosts with their addresses and keys, users with their authorized keys. Every piece of state has an address: `CHANNEL/SLOT` for an entry, `CHANNEL/SLOT/KEY` for a record. A slot has a type, and a type has versions. A type says which formats it accepts, and comes with a validator. Formats are named by media types (`application/json`, `application/x-pem-file`), so that the encoding can change, or a type can accept several, without the design changing. Records are JSON to start with. A validator sees a slot's whole content, not one record at a time, so it can check what holds between records (no two hosts with one address), and it names the addresses it objects to. ### 5.2 Views and renderers Programs don't read slots, they read views: files at stable paths. A file slot can be its own view. For anything else, the generation declares a view with a renderer. What a renderer does depends on the data and on who reads it: host records become known hosts for ssh, a zone file for a resolver, host files for tincr. (Host names go to a resolver, not into `/etc/hosts`: that file is the generation's in NixOS, and enstate doesn't touch what a generation owns. How names get resolved can change.) So enstate doesn't define renderers, only the protocol they follow: - the receptor hands the renderer the content of the slots it asked for, in their formats, and a directory to write into; - the renderer writes the view's files there and exits; it reads nothing else, reaches no network, and gives the same output for the same input; - the receptor compares what was written with what is in place, installs it atomically, and runs the reactions of the views that changed. Because a renderer only writes into the directory it is given, the receptor can run it without installing anything. That is what makes plans (5.4) possible without each renderer having a dry-run mode of its own. A sketch of the NixOS side, names not final: ```nix enstate.channels.secrets.slots."retiolum.key" = { type = "ed25519-private-key"; secret = true; credentialFor = [ "tincr-retiolum" ]; # LoadCredential= follows reaction.restart = [ "tincr-retiolum" ]; }; enstate.channels.fleet.slots.hosts = { type = "hosts"; # records under host names }; enstate.views.ssh-known-hosts = { from = [ "fleet/hosts" ]; renderer = ./render-known-hosts; }; ``` From the declarations the module generates the schema (a file in the system), the units' credentials, and the receptor's setup. Static configuration points at the views' stable paths (`GlobalKnownHostsFile /run/…/ssh-known-hosts`); what is behind them changes. ### 5.3 Submissions A provider hands the receptor a submission for one channel: items, and a manifest (the channel, optionally the snapshot it builds on, a checksum per item). How it gets to the host is not the receptor's business: over ssh from a deploy tool or an operator, or fetched by an agent. A submission is **complete** or **partial**: - complete: it is the channel's new content; what it doesn't mention goes. For a provider that owns a channel, like a deploy tool with a host's secrets. - partial: it sets or removes the items it names and leaves the rest. For an operator who adds a host and a key, or rotates one key. Each item says whether it is **required** or **optional**: - required: if this item can't go in, the whole submission is refused. - optional: if this item can't go in, it is left out, with a warning, and the rest goes on. Items that belong together can be put in a **group**: the items of a group go in together or not at all, and the group is required or optional as a whole. Grouping is the submission's choice. Items outside a group stand alone, so a submission can let an item in without its companion (a key for a host whose record was left out), if that is what the operator wants. Example: an operator submits a new host record and a new authorized key, both required. The validator objects to the host record (its address is taken), so the submission is refused, with that message. The key still has to change, so the operator submits the key alone; or submits both again with the host record marked optional, and gets the key in and a warning for the record. Had the two been an optional group, neither would have gone in, with one warning. ### 5.4 Plan, then apply The receptor works in two steps, and the first can be run alone, as a dry run. **Plan.** Without changing anything, the receptor: 1. checks that the channel exists in the running generation's schema, and the order (5.8); 2. applies the items to a copy of the current snapshot; 3. runs the validators on the result. Optional items that an objection names are taken out, with their groups, and the check runs again; an objection to a required item, or to something already in place, refuses the submission; 4. checks that every slot the schema requires is filled; 5. runs the renderers into a scratch directory, and compares. The plan says, for each item, whether it goes in, and if not, why; for each view, how it would change (a diff for public views, only "changes" for secret ones); and which reactions would run. **Apply.** The same, and then: the new snapshot becomes current, the views are installed atomically, and each reaction runs once (reload or restart per unit, deduplicated). A submission that is refused changes nothing. ### 5.5 Forcing Not in the first version. Later, a forced submission would go in although it doesn't fit, and policies (section 7) would say what happens to what it leaves unfilled. ### 5.6 No code in submissions A submission is data. Validators, renderers, converters, migrations and reactions all come from generations, never from a submission. So whoever can deliver state can change values, but can't run anything, and a submission can be inspected, and planned, before it is applied. ### 5.7 Where things are on the host - The current snapshot of each channel is kept on disk, so it survives a reboot, together with the schema it was accepted under (section 6 needs it). Secrets there are root's, as `/var/src/secrets` is today; later they can be encrypted to the host's key. - At boot and on each apply, the receptor materializes the views under `/run` (memory): secret ones readable by root only and handed to services as systemd credentials; public ones readable by everyone. ### 5.8 Order Each channel chooses what makes a snapshot the successor of another; maybe a format per channel. An identifier alone (a UUID) doesn't order. Candidates: - a **chain**: each snapshot has an id, and a submission may name the snapshot it builds on. If it does, it goes in only if that is the current one, so two providers can't overwrite each other unnoticed. A partial submission that names none applies to whatever is current. - a **counter** or a **timestamp**: for a provider that can't ask the host what is current, like a registry many hosts fetch from. ## 6. Migrations Generations differ in what they accept: a slot gets added, renamed, removed, split, or its type changes. The state on the host was accepted under one schema and has to serve another whenever the generation changes (switch, rollback, boot) and no fresh submission is at hand. Between two schemas there is a migration, ideally in both directions; that can't be demanded, so there are fallbacks. Generations don't form a line: any one may follow any other. So a migration can't be a numbered script per release. Instead it is put together from parts, most of them implicit, so that having migrations is simple: 1. **Same name, same type**: carries over. Most slots, most of the time; nothing to declare. 2. **Renames**: a slot says `renamedFrom`. 3. **Type versions**: a type carries converters between its versions, upward and, where possible, downward. They belong to the type, not to a host's configuration, so they're written once. 4. **Derived slots**: a slot that can be computed from another (a public key from its private key, as in the state objects) gets filled on the host. 5. **Custom steps**: a program from the generation, for what the above can't say (splitting one slot in two). The escape hatch. 6. **Fallback**: what no slot wants is kept and reported (it may be wanted again after a rollback); a slot that can't be filled falls to the policy (section 7). Two rules make this work in both directions, and [migrations.md](migrations.md) shows them at work: - **The receptor keeps the schema the snapshot was accepted under.** A rename or a custom step declared by a newer schema can then be undone for an older generation that has never heard of it: the way back comes from the newer schema, which the state still carries. - **A generation change never rewrites the snapshot.** The receptor computes what the new generation sees from what is stored, and stores that only when the next submission is applied under the new generation. So switching to an older generation and back loses nothing, and a conversion that drops something happens only when an operator hands something over under the older generation, and the plan says what would be dropped. Migrations are code from generations that ran or are about to run on this host, and they run on the host, so secrets don't leave it. The receptor keeps what it needs of the stored schema alive in the store. A fresh submission made for the new schema beats a migration, and the provider knows what to make: the schema is a file in the system it deploys. The order for a change that needs both: first submit what the new generation wants (the old one keeps and ignores it), then switch. With that, most switches need no migration, and migrations are for the times nobody is there: rollbacks and boots. ## 7. Policy One policy to start with: **refuse**. A submission that leaves a required slot unfilled is refused; a switch to a generation whose required slots the state can't fill, even by migrating, is refused before switching, with the reason. At boot nobody can refuse. For now, a slot that can't be filled then has no views, and what needs them fails to start. More policies (stop what can't work, keep the last value that fit), and forcing, come when they turn out to be needed. ## 8. Trust (all of it, eventually) Three steps, each usable on its own: 1. **As today.** The host's ssh host key is the first key. It gets to the host as it does now (rsync over ssh), the authorized keys that allow this are part of the generation, and root on the host hands submissions to the receptor. Transport is trusted because it is ssh. 2. **Signed submissions.** The schema names the keys a channel accepts signatures from. Then the fleet channel can be fetched from somewhere untrusted, like a federation, by an agent without privileges. 3. **Encrypted items.** Secrets encrypted to the host's key, for transport and at rest. The host key itself stays the exception that step 1 provides, until that evolves. ## 9. How the state objects fit in [state-object.md](state-object.md) describes objects (a key pair, its public half, a certificate), where they may exist (SOURCE, TARGET, REGISTRY, ACTIVE) and the pipelines that move them. That is the provider's side, and this design is the last step of it: - SOURCE and the pipelines (generate, derive the public key, publish it) happen outside the host, in the tool that makes submissions. - TARGET is the receptor's snapshot; ACTIVE is the views. - REGISTRY is where the fleet channel's submissions come from: every host's public halves, composed per host. So one private key appears twice: in its own host's secrets channel, and, as its derived public half, in everyone's fleet channel. A rotation is one pipeline run that produces both. The dependencies there (derived-from) are the derived slots of section 6. Types here and object types there should be one list. ## 10. Cases to cover, and hrm as a test | case | channel | view, and how the host uses it | reaction | |---|---|---|---| | rotate a service's key | secrets | the key, as a systemd credential | restart or reload the service | | ssh known hosts | fleet | rendered from host records; `GlobalKnownHostsFile` | none, read at each use | | authorized keys | fleet | rendered from user records; `AuthorizedKeysFile` | none | | host names | fleet | a zone file for a local resolver; `/etc/hosts` stays the generation's | reload the resolver | | retiolum peers | fleet | tincr's host files, rendered from host records | reload tincr | | passwords (`shadow-overrides`) | secrets | read at activation today | needs a reaction of its own | What it would replace in hrm: - `/var/src/secrets`, delivered by krops' populate: becomes the secrets channel. The `secrets*` rule in `krops.nix`, `inputs.secrets`, and `` in modules go away. - `modules/systemd-credentials.nix` (a path unit and checksum per service): the receptor does this once for all. - The host database evaluated from kartei (retiolum's host files, wiregrill's peers, known hosts): becomes the fleet channel, with the continuum document as its registry. ## 11. Open questions 1. **Structured types**: how the shape of records is declared in the schema and checked. Generated from NixOS option types, a small language of its own, an existing one (JSON Schema), or left entirely to the type's validator? 2. **The protocols** for validators, renderers and converters: how content enters (files in a directory, or one document on standard input), and how objections name addresses. 3. **Removing** in a partial submission, and removing the last record a required slot has. 4. **Order**: chain, counter, or timestamp, per channel. 5. **At boot** with a slot that can't be filled: is "no views, consumers fail" good enough until there are more policies? 6. **Names** (section 3). ## 12. A first version Small enough to try on one host, large enough to replace something real: - one channel; file slots with type `opaque` and one or two with validators; one structured slot with JSON records, a validator and a renderer; - submissions complete and partial, items required and optional, groups; - the receptor as a command: `plan`, `apply`, `migrate` (on a generation change), `materialize` (at boot), `status`; - migrations: same name carries over, `renamedFrom`, kept and reported; the two rules of section 6; policy: refuse; - a NixOS module: slot and view options, schema file, credentials, reactions, the check before switching; - a provider as a few lines in a deploy tool: pack a directory, copy it, call `plan` or `apply`; - tested in a NixOS VM test: apply, refuse (missing slot, wrong order, a validator's objection), an optional item left out with a warning, a plan that changes nothing, rotate without switching, switch and roll back across a rename with state in place. Then: more views, the fleet channel and a registry, signing, type versions with converters, custom steps, forcing and policies, encryption.