aboutsummaryrefslogtreecommitdiffstats
path: root/docs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-27 23:26:45 +0000
committertv <tv@krebsco.de>2026-11-27 23:26:45 +0000
commit2d3d68b622ff62ee1404eda734a73c5e5c8d1397 (patch)
treead986bda63a2f71ad1c364e7b0dd5b6f087318a1 /docs
init: design draft 2, state objects
Diffstat (limited to 'docs')
-rw-r--r--docs/design.md341
-rw-r--r--docs/state-object.md2
2 files changed, 343 insertions, 0 deletions
diff --git a/docs/design.md b/docs/design.md
new file mode 100644
index 0000000..6bfa168
--- /dev/null
+++ b/docs/design.md
@@ -0,0 +1,341 @@
+# enstate: state next to the store, a design draft
+
+Draft 2, 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.
+
+## 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
+ rejected as a whole, before anything changes, unless a policy says how
+ to go on without the part that doesn't fit.
+3. State never rolls back when the generation does. Between two
+ generations that expect different state there is a migration.
+4. Secrets are never in the store, never on a builder, never evaluated.
+5. 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 snapshots, see
+5.4).
+
+## 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": a stream of snapshots with its own provider, order and cadence |
+| **slot** | a named place in a channel that a generation offers |
+| **type** | what a slot holds: its format, how to validate it; types have **versions** |
+| **entry** | the value of one slot in one snapshot |
+| **snapshot** | a complete set of entries for one channel of one host |
+| **order** | what makes one snapshot newer than another (a channel's choice) |
+| **schema** | what one generation accepts: its channels, slots, types, views, reactions, policies |
+| **view** | a file made from one or more slots, at a stable path, for programs to read |
+| **reaction** | what the system does when a view changes: reload or restart units |
+| **receptor** | the program on the host that accepts or rejects snapshots, and migrates |
+| **provider** | whatever makes snapshots and delivers them |
+| **migration** | how state accepted under one schema becomes state for 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 |
+| snapshot | made outside, handed to the host |
+| 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), 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: files and structured data (both)
+
+- A **file slot** holds bytes of some type: a private key, a certificate,
+ a ready-made file. Its type has a validator (the key parses).
+- A **structured slot** holds records of a declared shape: host names with
+ their addresses, peers with their keys, users with their authorized
+ keys. It gets validated against that shape.
+
+Programs don't read slots, they read views. A file slot is its own view.
+For structured slots the generation declares views with a renderer: a
+pure function from records to a file (known hosts, a zone file, tincr's
+host files), which the receptor runs. So one slot of host records can feed
+several views, and each module renders the format it needs.
+
+A sketch of the NixOS side, names not final:
+
+```nix
+state.channels.secrets.slots."retiolum.ed25519_key" = {
+ type = "ed25519-private-key";
+ secret = true;
+ credentialFor = [ "tincr-retiolum" ]; # LoadCredential= follows
+ reaction.restart = [ "tincr-retiolum" ];
+ policy = "stop-consumers";
+};
+
+state.channels.fleet.slots.hosts = {
+ type = "hosts"; # records: name, addresses, keys
+};
+
+state.views.ssh-known-hosts = {
+ from = [ "fleet.hosts" ];
+ render = ./known-hosts.jq; # records -> file
+};
+```
+
+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.2 Handing over
+
+A provider gives the receptor a snapshot: one entry per slot, and a
+manifest (channel, the schema it was made for, its place in the channel's
+order, a checksum per entry). How it gets to the host is not the
+receptor's business: over ssh from a deploy tool, or fetched by an agent.
+
+### 5.3 Accepting or rejecting
+
+The receptor checks, without changing anything:
+
+- the channel exists in the running generation's schema;
+- the snapshot is newer than the current one, by the channel's order;
+- every entry has its slot's type and version, or can be migrated to it
+ (section 6);
+- every slot is filled, or its policy says how to go on (section 7);
+- where the schema asks for it, the snapshot is signed (section 8).
+
+If a check fails, the snapshot is rejected as a whole, with the reasons.
+Otherwise the receptor puts the new entries in place atomically, renders
+the views, works out which views changed, and runs each reaction once
+(reload or restart per unit, deduplicated).
+
+A snapshot can be **forced**: it is then accepted although it doesn't
+fit, and the policies decide what happens to what it leaves unfilled.
+
+### 5.4 No code in snapshots
+
+A snapshot is data. Validators, renderers, migrations, reactions and
+policies all come from generations, never from a snapshot. So whoever can
+deliver state can change values, but can't run anything, and a snapshot
+can be inspected before it is accepted.
+
+### 5.5 Where things are on the host
+
+- Accepted snapshots are kept on disk, so they survive a reboot, together
+ with the schema they were 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 accept, 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.6 Order
+
+Each channel chooses what makes a snapshot newer (maybe a format per
+channel; UUIDs came to mind). An identifier alone doesn't order.
+Candidates:
+
+- a **chain**: each snapshot has an id (a UUID, or the hash of its
+ content) and names the id it replaces. It is accepted only if that is
+ the current one. No clocks, no counters, and two providers can't
+ overwrite each other unnoticed. The provider has to ask the host what
+ is current.
+- a **counter** or a **timestamp**: simpler for a provider that can't ask,
+ like a registry many hosts fetch from.
+
+Forcing skips the order check, and is recorded.
+
+## 6. Migrations
+
+Generations differ in what they accept: a slot gets added, renamed,
+removed, 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 snapshot 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, to make having migrations
+simple:
+
+1. **Same name, same type**: the entry carries over. This is most slots,
+ most of the time, and costs nothing to declare.
+2. **Renames**: a slot says `renamedFrom`. Because the receptor keeps the
+ schema the state was accepted under, a rename declared by the newer
+ schema can also be undone when going back to the older generation,
+ which has never heard of the new name.
+3. **Type versions**: a type's definition 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. The escape hatch.
+6. **Fallbacks**, per slot: keep an entry no slot wants any more (ignore
+ and report; it may be wanted again after a rollback), or drop it; and
+ for a slot left unfilled, its policy.
+
+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 the previous schema (and what it needs of it) alive in the store.
+
+A fresh snapshot made for the new schema beats a migration. The provider
+knows what to make: the schema is a file in the system it deploys.
+
+The order for a change that needs both state and system: first deliver a
+snapshot that already has what the new generation wants (the old one
+keeps and ignores it), then switch. With that, most switches need no
+migration at all, and migrations are for the times nobody is there:
+rollbacks and boots.
+
+## 7. Policies
+
+A policy says what happens when a slot can't be filled: after a
+migration that had no way, at boot into a generation that doesn't fit,
+or on a forced snapshot. The same policies apply in all three cases.
+
+- **refuse**: don't switch, don't accept. The default where refusing is
+ possible (before a switch, on a snapshot that isn't forced).
+- **stop-consumers**: go on, and stop, or don't start, the units that need
+ the slot ("stop everything that doesn't work"). The default where
+ refusing isn't possible (at boot, when forced).
+- **keep-last**: go on with the last entry that fit, where there is one.
+
+The schema knows each slot's consumers (credentials, views, reactions),
+so the receptor can work out what to stop, and report it.
+
+## 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 snapshots
+ to the receptor. Transport is trusted because it is ssh.
+2. **Signed snapshots.** 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 entries.** 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 snapshots.
+- TARGET is the receptor's stored snapshot; ACTIVE is the views.
+- REGISTRY is where the fleet channel's snapshots 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 (DNS is the natural way), or `/etc/hosts` | 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
+ `<secrets>` 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. **Does the migration model hold?** It rests on three things: the
+ receptor keeps the schema the state was accepted under; types carry
+ their own converters; structure changes are hints (`renamedFrom`,
+ derived). Cases to try it on, on paper: a renamed slot and a rollback;
+ a type with a new version and no way down; a slot split in two.
+2. **How are structured types written?** The shape of records has to be
+ declared in the schema and checked by the receptor. Generated from
+ NixOS option types, a small language of its own, or an existing one
+ (JSON Schema)? And the entries themselves: JSON?
+3. **What are renderers?** Pure functions from records to a file, run by
+ the receptor, from the generation. A fixed set of built-in formats, a
+ template language, jq, or any program in a sandbox?
+4. **Policies**: are refuse, stop-consumers and keep-last the right set,
+ and the right defaults? Does stop-consumers stop only the units a slot
+ names, or everything that depends on them?
+5. **Order**: chain, counter, or timestamp, per channel; what a forced
+ snapshot leaves behind for the next regular one.
+6. **Names** (section 3), and the project's.
+
+## 12. A first version
+
+Small enough to try on one host, large enough to replace something real:
+
+- one channel, file slots only, type `opaque` and one or two with
+ validators; order by chain;
+- the receptor as a command: `check`, `accept` (with force), `migrate`
+ (on a generation change), `materialize` (at boot), `status`;
+- migrations: same name carries over, `renamedFrom`, ignore and report;
+ policies: refuse and stop-consumers;
+- a NixOS module: slot 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 `accept`;
+- tested in a NixOS VM test: accept, reject (missing slot, wrong order,
+ bad type), rotate without switching, switch and roll back across a
+ rename with state in place, force and see consumers stopped.
+
+Then: structured slots and views, the fleet channel and a registry,
+signing, type versions with converters, encryption.
diff --git a/docs/state-object.md b/docs/state-object.md
new file mode 100644
index 0000000..a146a28
--- /dev/null
+++ b/docs/state-object.md
@@ -0,0 +1,2 @@
+A first write-up on state objects, older than the design. Not done, just an idea; kept as it is. design.md, section 9, says how it fits in.
+