| 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. |
| 50 | protection?: #Protection |
| 51 | } |
| 52 | |
| 53 | // #Protection states which of a project's refs are closed to pushes. It is |
| 54 | // the declared half of the one structural git invariant: a protected ref |
| 55 | // takes a push only from a declared writer, and attestation refs |
| 56 | // (refs/the-valley/attestations/*) are create-only for everyone. The |
| 57 | // invariant's other half is not declared because it is not a choice — the |
| 58 | // attestation clause holds for every protected project, and the namespace |
| 59 | // is fixed by the contributor protocol (design/contribute.md). |
| 60 | // |
| 61 | // Everything this does not name stays open: topic branches, tags, new |
| 62 | // attestation refs. All policy beyond the invariant lives in the |
| 63 | // integrator, never here (design/architecture.md, _a pull-based |
| 64 | // integrator_). |
| 65 | #Protection: { |
| 66 | // The refs closed to everyone but a declared writer. Patterns are |
| 67 | // matched against the full refname, so they begin with "refs/" — a |
| 68 | // bare branch name would protect nothing — and `*` matches any |
| 69 | // characters, path separators included. |
| 70 | refs: [#RefPattern, ...#RefPattern] | *["refs/heads/main"] |
| 71 | |
| 72 | // The principals allowed to push them. Empty by default: a protected |
| 73 | // ref admits no pushes at all. The integrator's local ref write is |
| 74 | // then the only path onto it, and that privilege is the machinery's |
| 75 | // — it is a write made on the host, which no push hook governs — so |
| 76 | // it is never declared here. A writers list is the deliberate |
| 77 | // exception that punches a named hole in the wall; the transition |
| 78 | // arrangement used one for the operator. |
| 79 | writers: [...#PrincipalName] | *[] |
| 80 | } |
| 81 | |
| 82 | // #RefPattern is a full refname, optionally with `*` wildcards. The |
| 83 | // characters git forbids in a refname — space, ~, ^, :, ?, [, \ — are |
| 84 | // rejected here too, so a pattern that could never match a real ref fails |
| 85 | // the declaration rather than sitting dead in a hook. |
| 86 | #RefPattern: =~"^refs/[^ ~^:?\\\\[]+$" |
| 87 | |
| 88 | // #PrincipalName names a principal — a human, machine, or service the |
| 89 | // instance grants something to — in the instance's identity registry |
| 90 | // (dcr-b87f6e8). That registry is not an artifact yet, and these names are |
| 91 | // deliberately ahead of it: genesis before enforcement. The names declared |
| 92 | // here are what the registry's compilation will bind to keys, and until it |
| 93 | // exists the binding is the installer's — the NixOS module in this repo |
| 94 | // maps each name to the keys that act as it. The shape is the project-name |
| 95 | // shape, for the same reason: a name travels into files and command lines. |
| 96 | #PrincipalName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" |
| 97 | |
| 98 | // #Backup is the durability policy for the host's data: that an offsite |
| 99 | // backup exists, how often it runs, and how long snapshots are kept. It is |
| 100 | // deliberately host-level — one data directory, one repository — because |
| 101 | // that is the host's reality today; per-project backup is a possible |
| 102 | // growth path (a `backup` on #Project), not a present need. Only policy |
| 103 | // is declared here: the repository URL, credentials, and host-key pinning |
| 104 | // are machine integration and live with the installer, never here. |
| 105 | #Backup: { |
| 106 | // Whether offsite backup runs for the host's data. Disabling never |
| 107 | // deletes anything: an existing backup repository is left untouched, |
| 108 | // merely no longer written to or pruned. |
| 109 | enable: bool | *true |
| 110 | |
| 111 | // The backup target kind. restic over sftp (a Hetzner Storage Box, |
| 112 | // dcr-d7952bc) is the only target today; an object-store target |
| 113 | // would widen this to a disjunction ("restic-s3") — deliberately not |
| 114 | // before an implementation exists. |
| 115 | target: "restic-sftp" |
| 116 | |
| 117 | // How often a snapshot is taken. "nightly" is the only cadence |
| 118 | // today; its exact wall-clock rendering is the installer's choice. |
| 119 | cadence: "nightly" |
| 120 | |
| 121 | // How many snapshots to keep per tier when pruning (restic |
| 122 | // --keep-daily/--keep-weekly/--keep-monthly semantics). Every tier |
| 123 | // keeps at least one snapshot — a zero tier would silently thin |
| 124 | // history, so it is rejected rather than rendered. |
| 125 | retention: { |
| 126 | daily: int & >0 | *7 |
| 127 | weekly: int & >0 | *4 |
| 128 | monthly: int & >0 | *6 |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // The top level of this package is itself a host declaration: a config |
| 133 | // file evaluated together with this schema is validated against #Host — |
| 134 | // unknown fields and unsafe project names are rejected. |
| 135 | // |
| 136 | // cue vet -c schema/valley.cue <config>.cue |
| 137 | // cue export schema/valley.cue <config>.cue |
| 138 | projects: #Host.projects |
| 139 | |
| 140 | // Mirrors #Host.backup. CUE cannot reference an optional field, so the |
| 141 | // constraint is restated against the same definition. |
| 142 | backup?: #Backup |
| 143 | |
| 144 | // A file's top level cannot be closed (embedding #Host would open it |
| 145 | // instead — embedding lifts closedness), so reject stray top-level fields |
| 146 | // explicitly: anything but the #Host fields — a `project:` typo, a |
| 147 | // deployment concern like `user:` — conflicts with this sentinel and |
| 148 | // fails vet with an error naming the field. |
| 149 | [!="projects" & !="backup"]: "INVALID: unknown top-level field; only \"projects\" and \"backup\" are allowed" |