| 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 and propagating deletions. |
| 39 | // Only those: a mirror publishes what has been integrated, and topic |
| 40 | // branches awaiting review are not published. Any other branch on a |
| 41 | // mirror is removed. Remote-only namespaces (e.g. GitHub's |
| 42 | // refs/pull/*) are never touched. Replication is best-effort: a dead |
| 43 | // mirror never rejects the primary push. Credentials are the host's |
| 44 | // concern (the installer documents how); they are not declared here. |
| 45 | mirrors: [...string] | *[] |
| 46 | |
| 47 | // The project's write protection. Optional: a project without it is a |
| 48 | // project whose refs are all open to anyone with push access, and |
| 49 | // evaluates exactly as it did before this field existed. That includes |
| 50 | // replacement refs (refs/replace/*). The programs that make decisions |
| 51 | // from a repository read it with replacement refs disabled, so one |
| 52 | // planted there changes nothing they decide. |
| 53 | protection?: #Protection |
| 54 | } |
| 55 | |
| 56 | // #Protection states what a push may write to the project. It is the |
| 57 | // declared half of the one structural git invariant, which the pre-receive |
| 58 | // hook enforces (valleyhook/) on every project that declares this block. |
| 59 | // |
| 60 | // The hook holds every push to an allowlist and refuses the rest: |
| 61 | // |
| 62 | // - a protected ref takes a push only from a declared writer; |
| 63 | // - attestation refs (refs/the-valley/attestations/*) may be created by |
| 64 | // anyone, and never updated or deleted; |
| 65 | // - integration requests (refs/the-valley/integration-requests/*) take |
| 66 | // writes only from a principal holding the request grant (dcr-e544f20); |
| 67 | // - topic branches (refs/heads/*) are open; |
| 68 | // - everything else is closed — tags, notes, replacement refs, and any |
| 69 | // namespace not named here — until an allow entry below opens a pattern |
| 70 | // of it to named principals. |
| 71 | // |
| 72 | // Only the protected set, its writers and the allow entries are declared. |
| 73 | // The other rules are not choices: the namespaces they cover are fixed by |
| 74 | // the contributor protocol (design/contribute.md), and replacement refs |
| 75 | // are closed to every push because they would change what every reader of |
| 76 | // the repository sees. All policy beyond the invariant lives in the |
| 77 | // integrator, never here (design/architecture.md, _a pull-based |
| 78 | // integrator_). |
| 79 | #Protection: { |
| 80 | // The refs closed to everyone but a declared writer. Patterns are |
| 81 | // matched against the full refname, so they begin with "refs/" — a |
| 82 | // bare branch name would protect nothing — and `*` matches any |
| 83 | // characters, path separators included. |
| 84 | refs: [#RefPattern, ...#RefPattern] | *["refs/heads/main"] |
| 85 | |
| 86 | // The principals allowed to push them. Empty by default: a protected |
| 87 | // ref admits no pushes at all. The integrator's local ref write is |
| 88 | // then the only path onto it, and that privilege is the machinery's |
| 89 | // — it is a write made on the host, which no push hook governs — so |
| 90 | // it is never declared here. A writers list is the deliberate |
| 91 | // exception that punches a named hole in the wall; the transition |
| 92 | // arrangement used one for the operator. |
| 93 | writers: [...#PrincipalName] | *[] |
| 94 | |
| 95 | // Refs the hook closes by default, opened to named principals. A |
| 96 | // signed release tag pushed by a person is the case this exists for: |
| 97 | // tags are closed to every push, and an entry opening |
| 98 | // "refs/tags/release/<project>/*" to the operator is how one becomes |
| 99 | // pushable. Empty by default. |
| 100 | // |
| 101 | // An entry only adds writers. It cannot open a protected ref, which |
| 102 | // stays its writers' alone, and it cannot name the three namespaces |
| 103 | // whose rule is fixed. |
| 104 | allow: [...#Opening] | *[] |
| 105 | } |
| 106 | |
| 107 | // #Opening is one allow entry: ref patterns, and the principals who may |
| 108 | // write the refs they match. |
| 109 | #Opening: { |
| 110 | // The refs opened, as full refnames with `*` matching any characters. |
| 111 | refs: [#OpenablePattern, ...#OpenablePattern] |
| 112 | |
| 113 | // Who may write them. At least one: an entry that opens a pattern to |
| 114 | // nobody opens nothing. |
| 115 | writers: [#PrincipalName, ...#PrincipalName] |
| 116 | } |
| 117 | |
| 118 | // #RefPattern is a full refname, optionally with `*` wildcards. The |
| 119 | // characters git forbids in a refname — space, ~, ^, :, ?, [, \ — are |
| 120 | // rejected here too, so a pattern that could never match a real ref fails |
| 121 | // the declaration rather than sitting dead in a hook. |
| 122 | #RefPattern: =~"^refs/[^ ~^:?\\\\[]+$" |
| 123 | |
| 124 | // #OpenablePattern is a pattern an allow entry may name. The namespaces |
| 125 | // whose rule is fixed — replacement refs, attestations and integration |
| 126 | // requests — are refused, so a declaration that tries to open one fails |
| 127 | // rather than reading as an opening the hook never applies. |
| 128 | #OpenablePattern: #RefPattern & !~"^refs/(replace|the-valley/(attestations|integration-requests))/" |
| 129 | |
| 130 | // #PrincipalName names a principal — a human, machine, or service the |
| 131 | // instance grants something to — in the instance's identity registry |
| 132 | // (dcr-b87f6e8). The registry's compilation (identity/) binds each name to |
| 133 | // the keys that act as it, and on a host that compiles no registry the |
| 134 | // installer does — the NixOS module in this repo maps each name to its |
| 135 | // keys by hand. The shape is the project-name shape, for the same reason: a |
| 136 | // name travels into files and command lines. |
| 137 | #PrincipalName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" |
| 138 | |
| 139 | // #Backup is the durability policy for the host's data: that an offsite |
| 140 | // backup exists, how often it runs, and how long snapshots are kept. It is |
| 141 | // deliberately host-level — one data directory, one repository — because |
| 142 | // that is the host's reality today; per-project backup is a possible |
| 143 | // growth path (a `backup` on #Project), not a present need. Only policy |
| 144 | // is declared here: the repository URL, credentials, and host-key pinning |
| 145 | // are machine integration and live with the installer, never here. |
| 146 | #Backup: { |
| 147 | // Whether offsite backup runs for the host's data. Disabling never |
| 148 | // deletes anything: an existing backup repository is left untouched, |
| 149 | // merely no longer written to or pruned. |
| 150 | enable: bool | *true |
| 151 | |
| 152 | // The backup target kind. restic over sftp (a Hetzner Storage Box, |
| 153 | // dcr-d7952bc) is the only target today; an object-store target |
| 154 | // would widen this to a disjunction ("restic-s3") — deliberately not |
| 155 | // before an implementation exists. |
| 156 | target: "restic-sftp" |
| 157 | |
| 158 | // How often a snapshot is taken. "nightly" is the only cadence |
| 159 | // today; its exact wall-clock rendering is the installer's choice. |
| 160 | cadence: "nightly" |
| 161 | |
| 162 | // How many snapshots to keep per tier when pruning (restic |
| 163 | // --keep-daily/--keep-weekly/--keep-monthly semantics). Every tier |
| 164 | // keeps at least one snapshot — a zero tier would silently thin |
| 165 | // history, so it is rejected rather than rendered. |
| 166 | retention: { |
| 167 | daily: int & >0 | *7 |
| 168 | weekly: int & >0 | *4 |
| 169 | monthly: int & >0 | *6 |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // The top level of this package is itself a host declaration: a config |
| 174 | // file evaluated together with this schema is validated against #Host — |
| 175 | // unknown fields and unsafe project names are rejected. |
| 176 | // |
| 177 | // cue vet -c schema/valley.cue <config>.cue |
| 178 | // cue export schema/valley.cue <config>.cue |
| 179 | projects: #Host.projects |
| 180 | |
| 181 | // Mirrors #Host.backup. CUE cannot reference an optional field, so the |
| 182 | // constraint is restated against the same definition. |
| 183 | backup?: #Backup |
| 184 | |
| 185 | // A file's top level cannot be closed (embedding #Host would open it |
| 186 | // instead — embedding lifts closedness), so reject stray top-level fields |
| 187 | // explicitly: anything but the #Host fields — a `project:` typo, a |
| 188 | // deployment concern like `user:` — conflicts with this sentinel and |
| 189 | // fails vet with an error naming the field. |
| 190 | [!="projects" & !="backup"]: "INVALID: unknown top-level field; only \"projects\" and \"backup\" are allowed" |