gunk-dev/the-valley

valley.cue

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