| 1 | // Package valley is the canonical domain model of a valley host: what the |
| 2 | // host serves, independent of any machine that serves it. |
| 3 | // |
| 4 | // This file is deliberately not Nix. The NixOS module in this repo is one |
| 5 | // installer that consumes the model (`cue vet` + `cue export` at build |
| 6 | // time); other installers and tools read the same file. Deployment concerns |
| 7 | // — data directory, unix user, SSH keys — are machine integration and live |
| 8 | // with the installer, never here. |
| 9 | package valley |
| 10 | |
| 11 | // #Host is a complete valley host declaration: the set of projects the |
| 12 | // host serves, and the durability policy for the data behind them. |
| 13 | #Host: { |
| 14 | // projects maps a project name to its declaration. Names must be |
| 15 | // filesystem- and URL-safe because they become on-disk directory |
| 16 | // names ("<name>.git") and clone paths. |
| 17 | projects: [=~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"]: #Project |
| 18 | |
| 19 | // The host's offsite-backup policy. Optional: a declaration without |
| 20 | // it is a host without offsite backup, and evaluates exactly as it |
| 21 | // did before this field existed. |
| 22 | backup?: #Backup |
| 23 | } |
| 24 | |
| 25 | // #Project is the unit of declaration. A project *has* stores; git is the |
| 26 | // only store type today, and it is nested rather than top-level on |
| 27 | // purpose — the valley is not committed to being git-only. |
| 28 | #Project: { |
| 29 | // The project's git store. |
| 30 | git: { |
| 31 | // Whether the host serves a git repository for this project. |
| 32 | // Disabling never deletes anything: an existing repository is |
| 33 | // left untouched on disk, merely unmanaged. |
| 34 | enable: bool | *true |
| 35 | } |
| 36 | |
| 37 | // Publication-mirror URLs. Every push to the primary replicates main |
| 38 | // and the tags to each URL, force-updating them. Only those: a mirror |
| 39 | // publishes what has been integrated, and topic branches awaiting |
| 40 | // review are not published. The host changes nothing else on a |
| 41 | // mirror: the only deletions it makes there are tags deleted on the |
| 42 | // primary. So a repository that becomes a mirror keeps every ref it |
| 43 | // already holds; delete what must not be published when declaring it |
| 44 | // (docs/architecture.md shows how). Replication is best-effort: a dead |
| 45 | // mirror never rejects the primary push. Credentials are the host's |
| 46 | // concern (the installer documents how); they are not declared here. |
| 47 | mirrors: [...string] | *[] |
| 48 | |
| 49 | // The project's write protection. Optional: a project without it has |
| 50 | // no protected refs, so its branches are open to every push. Every |
| 51 | // other rule of the push policy (#Protection describes them) applies |
| 52 | // to it unchanged. |
| 53 | protection?: #Protection |
| 54 | |
| 55 | // Named grants, each opening ref patterns the push policy closes by |
| 56 | // default to the principals it names. A signed release tag pushed by a |
| 57 | // person is the case they exist for: tags take no push except through |
| 58 | // one. Empty by default, so a project opens nothing it does not name. |
| 59 | grants: [#GrantName]: #RefGrant |
| 60 | } |
| 61 | |
| 62 | // #RefGrant is one named grant: ref patterns, and the principals who may |
| 63 | // write the refs they match. A grant only adds writers. A protected ref it |
| 64 | // covers still takes a declared writer too. |
| 65 | #RefGrant: { |
| 66 | // The refs opened, as full refnames with `*` matching any characters. |
| 67 | refs: [#GrantPattern, ...#GrantPattern] |
| 68 | |
| 69 | // Who may write them. At least one: a grant that opens a pattern to |
| 70 | // nobody opens nothing. |
| 71 | writers: [#PrincipalName, ...#PrincipalName] |
| 72 | } |
| 73 | |
| 74 | // #GrantName names a grant. It appears in what a refusal says. |
| 75 | #GrantName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" |
| 76 | |
| 77 | // #GrantPattern is a pattern a grant may name. The namespaces whose rule |
| 78 | // is fixed are refused: branches are open already, replacement refs and |
| 79 | // notes are closed to every push, and the valley's own namespace has rules |
| 80 | // of its own. A declaration that tries to open one fails rather than |
| 81 | // reading as a grant the hook never applies. |
| 82 | #GrantPattern: #RefPattern & !~"^refs/(heads|replace|notes|the-valley)/" |
| 83 | |
| 84 | // #Protection states which of a project's refs are protected: closed to |
| 85 | // every push but a declared writer's. It is the declared half of the one |
| 86 | // structural git invariant, which the pre-receive hook (valleyhook/) |
| 87 | // enforces on every project a host serves, protected or not. |
| 88 | // |
| 89 | // The hook holds every push to an allowlist, and a write must pass every |
| 90 | // rule that applies to its ref: |
| 91 | // |
| 92 | // - replacement refs (refs/replace/*), notes (refs/notes/*) and the |
| 93 | // valley's own namespace (refs/the-valley/*, but for the four below) |
| 94 | // take no push at all; |
| 95 | // - a ref that is symbolic in the repository takes no push; |
| 96 | // - topic branches (refs/heads/*) are open; |
| 97 | // - attestation refs (refs/the-valley/attestations/<digest>/<key hash>) |
| 98 | // may be created by anyone, never updated or deleted, and only |
| 99 | // pointing at notes about that digest signed under that key hash; |
| 100 | // - integration requests (refs/the-valley/integration-requests/*) take |
| 101 | // writes only from a principal holding the approve grant, also |
| 102 | // accepted as request (dcr-e544f20); |
| 103 | // - change records (refs/the-valley/changes/main/<change ID>) move only |
| 104 | // by the transitions the hook allows; |
| 105 | // - the plan graph (refs/the-valley/knots, that name exactly) takes a |
| 106 | // named principal's write of one commit on its tip that leaves the |
| 107 | // graph valid, and is never deleted; |
| 108 | // - every other ref, tags included, takes a named grant of the project |
| 109 | // (#Project.grants) that opens it to the pusher; |
| 110 | // - a protected ref also takes a declared writer. |
| 111 | // |
| 112 | // Only the protected set, its writers and the project's grants are |
| 113 | // declared. The other rules are not choices: the namespaces they cover are |
| 114 | // fixed by the contributor protocol (archive/design/contribute.md), and the closed |
| 115 | // ones change what every reader of the repository sees. Protection only |
| 116 | // adds a requirement — a writer of a pattern covering integration requests |
| 117 | // still needs the approve grant. All policy beyond the invariant lives in |
| 118 | // the integrator, never here (archive/design/architecture.md, _a pull-based |
| 119 | // integrator_). |
| 120 | #Protection: { |
| 121 | // The refs closed to everyone but a declared writer. Patterns are |
| 122 | // matched against the full refname, so they begin with "refs/" — a |
| 123 | // bare branch name would protect nothing — and `*` matches any |
| 124 | // characters, path separators included. |
| 125 | refs: [#RefPattern, ...#RefPattern] | *["refs/heads/main"] |
| 126 | |
| 127 | // The principals allowed to push them. Empty by default: a protected |
| 128 | // ref admits no pushes at all. The integrator's local ref write is |
| 129 | // then the only path onto it, and that privilege is the machinery's |
| 130 | // — it is a write made on the host, which no push hook governs — so |
| 131 | // it is never declared here. A writers list is the deliberate |
| 132 | // exception that punches a named hole in the wall; the transition |
| 133 | // arrangement used one for the operator. |
| 134 | writers: [...#PrincipalName] | *[] |
| 135 | } |
| 136 | |
| 137 | // #RefPattern is a full refname, optionally with `*` wildcards. The |
| 138 | // characters git forbids in a refname — space, ~, ^, :, ?, [, \ — are |
| 139 | // rejected here too, so a pattern that could never match a real ref fails |
| 140 | // the declaration rather than sitting dead in a hook. |
| 141 | #RefPattern: =~"^refs/[^ ~^:?\\\\[]+$" |
| 142 | |
| 143 | // #PrincipalName names a principal — a human, machine, or service the |
| 144 | // instance grants something to — in the instance's identity registry |
| 145 | // (dcr-b87f6e8). The registry's compilation (identity/) binds each name to |
| 146 | // the keys that act as it, and on a host that compiles no registry the |
| 147 | // installer does — the NixOS module in this repo maps each name to its |
| 148 | // keys by hand. The shape is the project-name shape, for the same reason: a |
| 149 | // name travels into files and command lines. |
| 150 | #PrincipalName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" |
| 151 | |
| 152 | // #Backup is the durability policy for the host's data: that an offsite |
| 153 | // backup exists, how often it runs, and how long snapshots are kept. It is |
| 154 | // deliberately host-level — one data directory, one repository — because |
| 155 | // that is the host's reality today; per-project backup is a possible |
| 156 | // growth path (a `backup` on #Project), not a present need. Only policy |
| 157 | // is declared here: the repository URL, credentials, and host-key pinning |
| 158 | // are machine integration and live with the installer, never here. |
| 159 | #Backup: { |
| 160 | // Whether offsite backup runs for the host's data. Disabling never |
| 161 | // deletes anything: an existing backup repository is left untouched, |
| 162 | // merely no longer written to or pruned. |
| 163 | enable: bool | *true |
| 164 | |
| 165 | // The backup target kind. restic over sftp (dcr-d7952bc) is the |
| 166 | // only target today; an object-store target |
| 167 | // would widen this to a disjunction ("restic-s3") — deliberately not |
| 168 | // before an implementation exists. |
| 169 | target: "restic-sftp" |
| 170 | |
| 171 | // How often a snapshot is taken. "nightly" is the only cadence |
| 172 | // today; its exact wall-clock rendering is the installer's choice. |
| 173 | cadence: "nightly" |
| 174 | |
| 175 | // How many snapshots to keep per tier when pruning (restic |
| 176 | // --keep-daily/--keep-weekly/--keep-monthly semantics). Every tier |
| 177 | // keeps at least one snapshot — a zero tier would silently thin |
| 178 | // history, so it is rejected rather than rendered. |
| 179 | retention: { |
| 180 | daily: int & >0 | *7 |
| 181 | weekly: int & >0 | *4 |
| 182 | monthly: int & >0 | *6 |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // The top level of this package is itself a host declaration: a config |
| 187 | // file evaluated together with this schema is validated against #Host — |
| 188 | // unknown fields and unsafe project names are rejected. |
| 189 | // |
| 190 | // cue vet -c schema/valley.cue <config>.cue |
| 191 | // cue export schema/valley.cue <config>.cue |
| 192 | projects: #Host.projects |
| 193 | |
| 194 | // Mirrors #Host.backup. CUE cannot reference an optional field, so the |
| 195 | // constraint is restated against the same definition. |
| 196 | backup?: #Backup |
| 197 | |
| 198 | // A file's top level cannot be closed (embedding #Host would open it |
| 199 | // instead — embedding lifts closedness), so reject stray top-level fields |
| 200 | // explicitly: anything but the #Host fields — a `project:` typo, a |
| 201 | // deployment concern like `user:` — conflicts with this sentinel and |
| 202 | // fails vet with an error naming the field. |
| 203 | [!="projects" & !="backup"]: "INVALID: unknown top-level field; only \"projects\" and \"backup\" are allowed" |