gunk-dev/the-valley

packages.nix

source2886163c0514main's published tip history
14.4 KiB
1# Everything this flake builds: the CLI and its Go verbs, the
2# formatter, the attestation helper, the integrator, the identity
3# compiler, the pre-receive hook's policy, the security-key signature
4# verifier. The flake wires these into packages, apps, and the checks that
5# drive them; nothing here knows about any of those outputs.
6{ pkgs, lib }:
7rec {
8 # The source of a Go program that uses modules of this repository: its own
9 # directory and those modules, which its go.mod replaces with their local
10 # directories. Nothing shared this way is fetched.
11 withLocal =
12 dir: modules:
13 lib.fileset.toSource {
14 root = ../.;
15 fileset = lib.fileset.unions ([ dir ] ++ modules);
16 };
17
18 # A program that reads signed notes takes the note module (../note). One
19 # reading of the envelope is shared by the programs that write, verify and
20 # admit attestations.
21 withNote = dir: withLocal dir [ ../note ];
22
23 # vendorHash = null builds in vendor mode, which has no reading of a local
24 # replace. The replaced module is a directory beside the program and
25 # nothing is fetched either way, so these builds read go.mod as written.
26 readsGoMod = ''
27 export GOFLAGS="''${GOFLAGS//-mod=vendor/-mod=mod}"
28 '';
29
30 # Markdown prose is filled paragraphs hard-wrapped at 100 columns
31 # (ida-1ec03b1). One flag string backs both the formatter app and the
32 # prose-format check, so the two cannot drift. Embedded-language
33 # formatting is off so fenced code blocks pass through byte-for-byte.
34 proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off";
35
36 # The integrator's CLI (bin/valley) wrapped for `nix run`. The script
37 # itself must keep running bare from any checkout — the package is the
38 # second of its two shipping modes, never a dependency of the first.
39 valley-script-unwrapped = pkgs.writeShellApplication {
40 name = "valley";
41 runtimeInputs = [
42 pkgs.git
43 pkgs.natscli # tail and replay only; every other verb needs just git
44 pkgs.cue # checks and approving — it composes the policy layers
45 pkgs.less # review only — its v key is what takes a note
46 attest # approving only — it runs the checks and signs them
47 valley-go # status, ready, unready, withdraw and rebase, naming a change by its ID, and pending's drafts
48 ];
49 text = builtins.readFile ../bin/valley;
50 };
51
52 # The shipping form. The verification schema travels with the script, as
53 # it does with the integrator: only the-valley's own tree carries
54 # schema/, so a project's checkout has none to read. --set-default leaves
55 # an explicit VALLEY_VERIFICATION_SCHEMA in charge.
56 valley-script =
57 pkgs.runCommand "valley"
58 {
59 nativeBuildInputs = [ pkgs.makeWrapper ];
60 meta.mainProgram = "valley";
61 }
62 ''
63 mkdir -p $out/bin
64 makeWrapper ${lib.getExe valley-script-unwrapped} $out/bin/valley \
65 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
66 '';
67
68 # The installed package: the wrapped script plus the shell completions,
69 # at the standard paths home-manager/NixOS auto-link.
70 valley = pkgs.symlinkJoin {
71 name = "valley";
72 paths = [ valley-script ];
73 nativeBuildInputs = [ pkgs.installShellFiles ];
74 postBuild = ''
75 installShellCompletion --bash --name valley ${../completions/valley.bash}
76 installShellCompletion --zsh --name _valley ${../completions/_valley}
77 '';
78 };
79
80 # The verbs of the CLI written in Go (status/). status reads one change's
81 # request and outcome record from origin, and prints them as sentences or
82 # as the valley-status/v1 JSON object front ends read. ready, unready and
83 # withdraw write a change's record, with the one reading of a record the
84 # push hook shares (../change). rebase rebases a change's whole stack and
85 # adds each change's rebased head to its record, through the rebase engine
86 # (../rebase), which also reads how far each version has landed for the
87 # listings. knot reads and writes the plan graph, with the one reading of
88 # a graph the push hook shares (../knot), and plan shows that graph as a
89 # view. compare reads a version's file delta and its approved version's
90 # with the one reading of valley-delta-v1 (../delta), for review. Go,
91 # standard library only — hence vendorHash = null and no module fetch. It
92 # runs whatever git is on PATH, which inside the CLI's package is the
93 # CLI's own. Its unit tests hold its shell quoting to the bash it
94 # replaced, so they need bash, and lay pending out over real repositories,
95 # so they need git.
96 valley-go = pkgs.buildGoModule {
97 pname = "valley-go";
98 version = "0";
99 src = withLocal ../status [
100 ../change
101 ../delta
102 ../knot
103 ../rebase
104 ];
105 modRoot = "status";
106 vendorHash = null;
107 preBuild = readsGoMod;
108 nativeCheckInputs = [
109 pkgs.bash
110 pkgs.git
111 ];
112 meta.mainProgram = "valley-go";
113 };
114
115 # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns.
116 prose-fmt = pkgs.writeShellApplication {
117 name = "valley-fmt";
118 runtimeInputs = [
119 pkgs.git
120 pkgs.prettier
121 ];
122 text = ''
123 cd "$(git rev-parse --show-toplevel)"
124 git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \
125 prettier ${proseFmtArgs} --write
126 '';
127 };
128
129 # The Phase 2 attestation helper (dcr-0de694f). Go, standard library
130 # only, note format included — hence vendorHash = null, and hence no
131 # module fetch at build time. Its unit tests run in the checkPhase and
132 # need git and ssh-keygen.
133 attest-unwrapped = pkgs.buildGoModule {
134 pname = "valley-attest";
135 version = "0";
136 src = withNote ../attest;
137 modRoot = "attest";
138 vendorHash = null;
139 preBuild = readsGoMod;
140 nativeCheckInputs = [
141 pkgs.git
142 pkgs.openssh
143 ];
144 meta.mainProgram = "attest";
145 };
146
147 # The shipping form. git and cue are pinned to this flake's nixpkgs,
148 # so every host composes and vets a statement with the same tools.
149 # Signing needs no tool at all: a note is signed on crypto/ed25519
150 # inside the binary. `nix` is deliberately NOT pinned: the check being
151 # attested to must be built by the nix the machine actually runs, and
152 # a nix carried in here would be a second one.
153 attest =
154 pkgs.runCommand "valley-attest"
155 {
156 nativeBuildInputs = [ pkgs.makeWrapper ];
157 meta.mainProgram = "attest";
158 }
159 ''
160 mkdir -p $out/bin
161 makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \
162 --prefix PATH : ${
163 lib.makeBinPath [
164 pkgs.git
165 pkgs.cue
166 ]
167 } \
168 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue}
169 '';
170
171 # The identity registry compiler (dcr-b87f6e8). Go, standard library
172 # only, same trade as attest and the integrator — hence vendorHash = null
173 # and no module fetch. Its unit tests pin the note format's key hash
174 # against the reference vector attest is held to.
175 identity-unwrapped = pkgs.buildGoModule {
176 pname = "valley-identity";
177 version = "0";
178 src = ../identity;
179 vendorHash = null;
180 meta.mainProgram = "identity";
181 };
182
183 # The shipping form. git reads the registry out of the instance
184 # repository's tip and cue vets and exports it, both pinned to this
185 # flake's nixpkgs so every host compiles a registry the same way. The
186 # schema travels with the binary: a compiler judging a registry against a
187 # schema the consumer supplied would be no floor at all.
188 identity =
189 pkgs.runCommand "valley-identity"
190 {
191 nativeBuildInputs = [ pkgs.makeWrapper ];
192 meta.mainProgram = "identity";
193 }
194 ''
195 mkdir -p $out/bin
196 makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \
197 --prefix PATH : ${
198 lib.makeBinPath [
199 pkgs.git
200 pkgs.cue
201 ]
202 } \
203 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
204 '';
205
206 # The security-key signature verifier (sigverify/README.md). Go,
207 # standard library only — hence vendorHash = null and no module fetch.
208 #
209 # The git its git-tag command runs is fixed at build time to this
210 # flake's git, as an absolute store path compiled into the binary. A
211 # wrapper that puts git on PATH would leave the binary inside it looking
212 # git up on PATH, and a caller's environment could change which git that
213 # is. Compiled in, there is no PATH lookup to change.
214 #
215 # Its tests run in the checkPhase against real ssh-keygen and git. The
216 # security-key cases among them need OpenSSH's sk-dummy authenticator,
217 # which only the sigverify-unit check supplies; here they skip, so a
218 # consumer building this package never builds OpenSSH.
219 sigverify = pkgs.buildGoModule {
220 pname = "valley-sigverify";
221 version = "0";
222 src = ../sigverify;
223 vendorHash = null;
224 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
225 nativeCheckInputs = [
226 pkgs.git
227 pkgs.openssh
228 ];
229 meta.mainProgram = "sigverify";
230 };
231
232 # The pre-receive hook's policy (valleyhook/): what a push may write to a
233 # project a valley host serves. It validates a push to the plan graph
234 # with the one reading of a graph the CLI shares (../knot). The same binary is the git user's login
235 # shell, which derives the pushing principal and pauses pushes until the
236 # host has converged. Go, standard library only — hence
237 # vendorHash = null and no module fetch. Its unit tests run in the
238 # checkPhase.
239 #
240 # The git it reads the repository with is fixed at build time, the way
241 # sigverify's is: an absolute store path compiled into the binary, so no
242 # PATH a hook runs under can change which git that is.
243 valleyhook = pkgs.buildGoModule {
244 pname = "valley-valleyhook";
245 version = "0";
246 src = withLocal ../valleyhook [
247 ../note
248 ../change
249 ../knot
250 ];
251 modRoot = "valleyhook";
252 vendorHash = null;
253 preBuild = readsGoMod;
254 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
255 # The walk's and the change records' tests build hostile objects with
256 # real git.
257 nativeCheckInputs = [ pkgs.git ];
258 meta.mainProgram = "valleyhook";
259 };
260
261 # The note envelope's own tests, which hold it to refusing every line a
262 # relayer could append that one of its readers would refuse.
263 note = pkgs.buildGoModule {
264 pname = "valley-note";
265 version = "0";
266 src = ../note;
267 vendorHash = null;
268 };
269
270 # The plan graph (knot/): its canonical form, its layout, cycles and
271 # transitions, held by its own tests. The package is knotcheck, which
272 # knot-agree runs to hold the Go validator to the CUE schema and the
273 # knowledge lint.
274 knot = pkgs.buildGoModule {
275 pname = "valley-knot";
276 version = "0";
277 src = ../knot;
278 vendorHash = null;
279 meta.mainProgram = "knotcheck";
280 };
281
282 # The rebase engine (rebase/): a family of stacked changes captured from
283 # one snapshot of origin, rebased in a throwaway worktree, and published
284 # as one atomic branch push and one record append per change. Building
285 # valley-go does not run a module's tests that it only imports, so this
286 # package runs the engine's: go test, against real repositories, real
287 # pushes and real git, and go vet. Go, standard library only — hence
288 # vendorHash = null and no module fetch.
289 rebase = pkgs.buildGoModule {
290 pname = "valley-rebase";
291 version = "0";
292 src = withLocal ../rebase [ ../change ];
293 modRoot = "rebase";
294 vendorHash = null;
295 preBuild = readsGoMod;
296 nativeCheckInputs = [ pkgs.git ];
297 postCheck = ''
298 go vet ./...
299 '';
300 };
301
302 # The change record's own tests: its strict reading, its fuzz target's
303 # seed corpus, and its branch-name rule, which they hold to real git's
304 # check-ref-format.
305 change = pkgs.buildGoModule {
306 pname = "valley-change";
307 version = "0";
308 src = ../change;
309 vendorHash = null;
310 nativeCheckInputs = [ pkgs.git ];
311 };
312
313 # valley-delta-v1 (delta/): the encoding's strict reading, and the delta
314 # read from real trees. Its checkPhase runs go vet, the conformance
315 # vectors' script in its --check mode, which holds the committed expected
316 # results to what the script computes, and go test, which builds each
317 # vector's trees with real git and holds the Go code to those results.
318 delta = pkgs.buildGoModule {
319 pname = "valley-delta";
320 version = "0";
321 src = ../delta;
322 vendorHash = null;
323 nativeCheckInputs = [
324 pkgs.git
325 pkgs.python3
326 ];
327 preCheck = ''
328 go vet ./...
329 python3 -I testdata/vectors.py --check testdata
330 '';
331 };
332
333 # The Phase 3 integrator (dcr-439b771). Go, standard library only,
334 # same trade as attest — hence vendorHash = null and no module fetch. It
335 # reads change records with the one reading of a record the push hook
336 # shares (../change).
337 integrator-unwrapped = pkgs.buildGoModule {
338 pname = "valley-integrator";
339 version = "0";
340 # The verification schema is in the source for the policy bridge's
341 # tests, which compose a policy under it.
342 src = withLocal ../integrator [
343 ../change
344 ../schema/verification.cue
345 ];
346 modRoot = "integrator";
347 vendorHash = null;
348 preBuild = readsGoMod;
349 # The policy bridge's tests check a deriver's report against real git,
350 # and compose a policy with real cue.
351 nativeCheckInputs = [
352 pkgs.git
353 pkgs.cue
354 ];
355 meta.mainProgram = "integrator";
356 };
357
358 # The shipping form. The integrator drives four programs rather than
359 # reimplementing what they do: attest judges every statement and
360 # every note, the valley deriver and cue compose the policy, and git
361 # holds the requests and the refs. All four are pinned to this
362 # flake's nixpkgs so an instance's integrators agree; `nix` is
363 # deliberately absent for the same reason it is absent from attest —
364 # a closure must be recomputed by the nix the machine actually runs.
365 integrator =
366 pkgs.runCommand "valley-integrator"
367 {
368 nativeBuildInputs = [ pkgs.makeWrapper ];
369 meta.mainProgram = "integrator";
370 }
371 ''
372 mkdir -p $out/bin
373 makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \
374 --prefix PATH : ${
375 lib.makeBinPath [
376 pkgs.git
377 pkgs.cue
378 pkgs.natscli
379 attest
380 valley-script
381 ]
382 } \
383 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \
384 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \
385 --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue}
386 '';
387}