gunk-dev/the-valley

packages.nix

source4a982cac828cmain's published tip history
20.8 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{
7 pkgs,
8 lib,
9 # The commit the flake is built from: its full SHA, the SHA with -dirty
10 # after it for a tree with uncommitted changes, or null when nix knows no
11 # commit. Only the installed CLI's version stamp reads it.
12 rev ? null,
13}:
14rec {
15 # The CLI's version, which `valley --version` prints and self-update
16 # reads to tell which commit is installed (selfupdate/): cliVersion and
17 # the short commit, as 0.1.0-97db597. A dirty tree's has -dirty after
18 # it, and with no commit known it ends in -unknown, so neither names a
19 # commit.
20 cliVersion = "0.1.0";
21 version =
22 if rev == null then
23 "${cliVersion}-unknown"
24 else if lib.hasSuffix "-dirty" rev then
25 "${cliVersion}-${builtins.substring 0 7 rev}-dirty"
26 else
27 "${cliVersion}-${builtins.substring 0 7 rev}";
28
29 # The source of a Go program that uses modules of this repository: its own
30 # directory and those modules, which its go.mod replaces with their local
31 # directories. Nothing shared this way is fetched.
32 withLocal =
33 dir: modules:
34 lib.fileset.toSource {
35 root = ../.;
36 fileset = lib.fileset.unions ([ dir ] ++ modules);
37 };
38
39 # A program that reads signed notes takes the note module (../note). One
40 # reading of the envelope is shared by the programs that write, verify and
41 # admit attestations.
42 withNote = dir: withLocal dir [ ../note ];
43
44 # vendorHash = null builds in vendor mode, which has no reading of a local
45 # replace. The replaced module is a directory beside the program and
46 # nothing is fetched either way, so these builds read go.mod as written.
47 readsGoMod = ''
48 export GOFLAGS="''${GOFLAGS//-mod=vendor/-mod=mod}"
49 '';
50
51 # Markdown prose is filled paragraphs hard-wrapped at 100 columns
52 # (ida-1ec03b1). One flag string backs both the formatter app and the
53 # prose-format check, so the two cannot drift. Embedded-language
54 # formatting is off so fenced code blocks pass through byte-for-byte.
55 proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off";
56
57 # The integrator's CLI (bin/valley) wrapped for `nix run`. The script
58 # itself must keep running bare from any checkout — the package is the
59 # second of its two shipping modes, never a dependency of the first.
60 valley-script-unwrapped = pkgs.writeShellApplication {
61 name = "valley";
62 runtimeInputs = [
63 pkgs.git
64 pkgs.natscli # tail and replay only; every other verb needs just git
65 pkgs.cue # checks and approving — it composes the policy layers
66 pkgs.less # review only — its v key is what takes a note
67 attest # approving only — it runs the checks and signs them
68 valley-go # status, ready, unready, withdraw and rebase, naming a change by its ID, and pending's drafts
69 ];
70 text = builtins.readFile ../bin/valley;
71 };
72
73 # The shipping form. The verification schema travels with the script, as
74 # it does with the integrator: only the-valley's own tree carries
75 # schema/, so a project's checkout has none to read. --set-default leaves
76 # an explicit VALLEY_VERIFICATION_SCHEMA in charge.
77 valley-script =
78 pkgs.runCommand "valley"
79 {
80 nativeBuildInputs = [ pkgs.makeWrapper ];
81 meta.mainProgram = "valley";
82 }
83 ''
84 mkdir -p $out/bin
85 makeWrapper ${lib.getExe valley-script-unwrapped} $out/bin/valley \
86 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
87 '';
88
89 # The installed package: the wrapped script stamped with its version, the
90 # shell completions at the standard paths home-manager/NixOS auto-link,
91 # and the tiny policy self-update's smoke test derives checks from
92 # (examples/policy/smoke/). The stamp is set on this outer wrapper alone,
93 # so the packages the checks and the integrator use do not change with
94 # every commit. The name carries the version as well: nix profile names
95 # the element valley from it, and self-update reads a hand-installed
96 # valley's commit from it.
97 valley =
98 pkgs.runCommand "valley-${version}"
99 {
100 inherit version;
101 nativeBuildInputs = [
102 pkgs.makeWrapper
103 pkgs.installShellFiles
104 ];
105 meta.mainProgram = "valley";
106 }
107 ''
108 mkdir -p $out/bin $out/share/valley
109 makeWrapper ${lib.getExe valley-script} $out/bin/valley \
110 --set VALLEY_VERSION ${version}
111 installShellCompletion --bash --name valley ${../completions/valley.bash}
112 installShellCompletion --zsh --name _valley ${../completions/_valley}
113 cp -r --no-preserve=mode ${../examples/policy/smoke} $out/share/valley/smoke-policy
114 '';
115
116 # The verbs of the CLI written in Go (status/). status reads one change's
117 # request and outcome record from origin, and prints them as sentences or
118 # as the valley-status/v1 JSON object front ends read. ready, unready and
119 # withdraw write a change's record, with the one reading of a record the
120 # push hook shares (../change). rebase rebases a change's whole stack and
121 # adds each change's rebased head to its record, through the rebase engine
122 # (../rebase), which also reads how far each version has landed for the
123 # listings. knot reads and writes the plan graph, with the one reading of
124 # a graph the push hook shares (../knot), and plan shows that graph as a
125 # view. compare reads a version's file delta and its approved version's
126 # with the one reading of valley-delta-v1 (../delta), for review.
127 # self-update installs the tip of a branch of the-valley into the user's
128 # nix profile (../selfupdate). checks prints which checks a change
129 # requires, as the deriver (../derive) derives them from the policy the
130 # composed-policy reader (../composed) reads. Go,
131 # standard library only — hence vendorHash = null and no module fetch. It
132 # runs whatever git and cue are on PATH, which inside the CLI's package
133 # are the CLI's own. Its unit tests hold its shell quoting to the bash it
134 # replaced, so they need bash, lay pending out over real repositories,
135 # so they need git, and derive a change's checks under the verification
136 # schema, so they need cue and the schema.
137 valley-go = pkgs.buildGoModule {
138 pname = "valley-go";
139 version = "0";
140 src = withLocal ../status [
141 ../change
142 ../composed
143 ../delta
144 ../derive
145 ../knot
146 ../rebase
147 ../selfupdate
148 ../schema/verification.cue
149 ];
150 modRoot = "status";
151 vendorHash = null;
152 preBuild = readsGoMod;
153 nativeCheckInputs = [
154 pkgs.bash
155 pkgs.git
156 pkgs.cue
157 ];
158 # A test that cannot find a tool it needs fails rather than skipping.
159 preCheck = ''
160 export VALLEY_TEST_TOOLS=required
161 '';
162 meta.mainProgram = "valley-go";
163 };
164
165 # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns.
166 prose-fmt = pkgs.writeShellApplication {
167 name = "valley-fmt";
168 runtimeInputs = [
169 pkgs.git
170 pkgs.prettier
171 ];
172 text = ''
173 cd "$(git rev-parse --show-toplevel)"
174 git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \
175 prettier ${proseFmtArgs} --write
176 '';
177 };
178
179 # The Phase 2 attestation helper (dcr-0de694f). Go, standard library
180 # only, note format included — hence vendorHash = null, and hence no
181 # module fetch at build time. Its unit tests run in the checkPhase and
182 # need git and ssh-keygen.
183 attest-unwrapped = pkgs.buildGoModule {
184 pname = "valley-attest";
185 version = "0";
186 src = withNote ../attest;
187 modRoot = "attest";
188 vendorHash = null;
189 preBuild = readsGoMod;
190 nativeCheckInputs = [
191 pkgs.git
192 pkgs.openssh
193 ];
194 meta.mainProgram = "attest";
195 };
196
197 # The shipping form. git and cue are pinned to this flake's nixpkgs,
198 # so every host composes and vets a statement with the same tools.
199 # Signing needs no tool at all: a note is signed on crypto/ed25519
200 # inside the binary. `nix` is deliberately NOT pinned: the check being
201 # attested to must be built by the nix the machine actually runs, and
202 # a nix carried in here would be a second one.
203 attest =
204 pkgs.runCommand "valley-attest"
205 {
206 nativeBuildInputs = [ pkgs.makeWrapper ];
207 meta.mainProgram = "attest";
208 }
209 ''
210 mkdir -p $out/bin
211 makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \
212 --prefix PATH : ${
213 lib.makeBinPath [
214 pkgs.git
215 pkgs.cue
216 ]
217 } \
218 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue}
219 '';
220
221 # The identity registry compiler (dcr-b87f6e8). Go, standard library
222 # only, same trade as attest and the integrator — hence vendorHash = null
223 # and no module fetch. Its unit tests pin the note format's key hash
224 # against the reference vector attest is held to.
225 identity-unwrapped = pkgs.buildGoModule {
226 pname = "valley-identity";
227 version = "0";
228 src = ../identity;
229 vendorHash = null;
230 meta.mainProgram = "identity";
231 };
232
233 # The shipping form. git reads the registry out of the instance
234 # repository's tip and cue vets and exports it, both pinned to this
235 # flake's nixpkgs so every host compiles a registry the same way. The
236 # schema travels with the binary: a compiler judging a registry against a
237 # schema the consumer supplied would be no floor at all.
238 identity =
239 pkgs.runCommand "valley-identity"
240 {
241 nativeBuildInputs = [ pkgs.makeWrapper ];
242 meta.mainProgram = "identity";
243 }
244 ''
245 mkdir -p $out/bin
246 makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \
247 --prefix PATH : ${
248 lib.makeBinPath [
249 pkgs.git
250 pkgs.cue
251 ]
252 } \
253 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
254 '';
255
256 # The security-key signature verifier (sigverify/README.md). Go,
257 # standard library only — hence vendorHash = null and no module fetch.
258 #
259 # The git its git-tag command runs is fixed at build time to this
260 # flake's git, as an absolute store path compiled into the binary. A
261 # wrapper that puts git on PATH would leave the binary inside it looking
262 # git up on PATH, and a caller's environment could change which git that
263 # is. Compiled in, there is no PATH lookup to change.
264 #
265 # Its tests run in the checkPhase against real ssh-keygen and git. The
266 # security-key cases among them need OpenSSH's sk-dummy authenticator,
267 # which only the sigverify-unit check supplies; here they skip, so a
268 # consumer building this package never builds OpenSSH.
269 sigverify = pkgs.buildGoModule {
270 pname = "valley-sigverify";
271 version = "0";
272 src = ../sigverify;
273 vendorHash = null;
274 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
275 nativeCheckInputs = [
276 pkgs.git
277 pkgs.openssh
278 ];
279 meta.mainProgram = "sigverify";
280 };
281
282 # The pre-receive hook's policy (valleyhook/): what a push may write to a
283 # project a valley host serves. It validates a push to the plan graph
284 # with the one reading of a graph the CLI shares (../knot). The same binary is the git user's login
285 # shell, which derives the pushing principal and pauses pushes until the
286 # host has converged. Go, standard library only — hence
287 # vendorHash = null and no module fetch. Its unit tests run in the
288 # checkPhase.
289 #
290 # The git it reads the repository with is fixed at build time, the way
291 # sigverify's is: an absolute store path compiled into the binary, so no
292 # PATH a hook runs under can change which git that is.
293 valleyhook = pkgs.buildGoModule {
294 pname = "valley-valleyhook";
295 version = "0";
296 src = withLocal ../valleyhook [
297 ../note
298 ../change
299 ../knot
300 ];
301 modRoot = "valleyhook";
302 vendorHash = null;
303 preBuild = readsGoMod;
304 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
305 # The walk's and the change records' tests build hostile objects with
306 # real git.
307 nativeCheckInputs = [ pkgs.git ];
308 meta.mainProgram = "valleyhook";
309 };
310
311 # The note envelope's own tests, which hold it to refusing every line a
312 # relayer could append that one of its readers would refuse.
313 note = pkgs.buildGoModule {
314 pname = "valley-note";
315 version = "0";
316 src = ../note;
317 vendorHash = null;
318 };
319
320 # The plan graph (knot/): its canonical form, its layout, cycles and
321 # transitions, held by its own tests. The package is knotcheck, which
322 # knot-agree runs to hold the Go validator to the CUE schema and the
323 # knowledge lint.
324 knot = pkgs.buildGoModule {
325 pname = "valley-knot";
326 version = "0";
327 src = ../knot;
328 vendorHash = null;
329 meta.mainProgram = "knotcheck";
330 };
331
332 # The rebase engine (rebase/): a family of stacked changes captured from
333 # one snapshot of origin, rebased in a throwaway worktree, and published
334 # as one atomic branch push and one record append per change. Building
335 # valley-go does not run a module's tests that it only imports, so this
336 # package runs the engine's: go test, against real repositories, real
337 # pushes and real git, and go vet. Go, standard library only — hence
338 # vendorHash = null and no module fetch.
339 rebase = pkgs.buildGoModule {
340 pname = "valley-rebase";
341 version = "0";
342 src = withLocal ../rebase [ ../change ];
343 modRoot = "rebase";
344 vendorHash = null;
345 preBuild = readsGoMod;
346 nativeCheckInputs = [ pkgs.git ];
347 postCheck = ''
348 go vet ./...
349 '';
350 };
351
352 # Self-update (selfupdate/): the installed CLI brought up to the tip of a
353 # branch, built with nix and installed into the user's profile. Building
354 # valley-go does not run the tests of a module it only imports, so this
355 # package runs them, and go vet: a real bare repository for ls-remote,
356 # and a stand-in for nix whose builds are small scripts, so nothing is
357 # built and no profile is touched. Go, standard library only — hence
358 # vendorHash = null and no module fetch.
359 selfupdate = pkgs.buildGoModule {
360 pname = "valley-selfupdate";
361 version = "0";
362 src = ../selfupdate;
363 vendorHash = null;
364 nativeCheckInputs = [ pkgs.git ];
365 postCheck = ''
366 go vet ./...
367 '';
368 };
369
370 # The deriver of required checks (derive/): a change's paths read from
371 # git diff -z, globs matched byte by byte, and the required checks, pins,
372 # carry mode and unclassified paths derived from the whole composed
373 # policy (../composed). Building valley-go does not run the tests of a
374 # module it only imports, so this package runs them, and go vet. They
375 # hold it to bin/valley's deriver, run over the repository's own policy,
376 # the example policies and paths drawn from a fixed seed, so the source
377 # carries bin/valley, the schema and those policies, and the tests need
378 # bash, git and cue. bin/valley is run under C.UTF-8 and en_US.UTF-8 as
379 # well as C, so the tests read the UTF-8 locales' archive. Go, standard
380 # library only — hence vendorHash = null and no module fetch.
381 derive = pkgs.buildGoModule {
382 pname = "valley-derive";
383 version = "0";
384 src = withLocal ../derive [
385 ../composed
386 ../bin/valley
387 ../schema/verification.cue
388 ../examples/policy
389 ../policy/project
390 ];
391 modRoot = "derive";
392 vendorHash = null;
393 preBuild = readsGoMod;
394 nativeCheckInputs = [
395 pkgs.bash
396 pkgs.git
397 pkgs.cue
398 ];
399 # A test that cannot find a tool it needs, or a locale, fails rather
400 # than skipping.
401 preCheck = ''
402 export VALLEY_TEST_TOOLS=required
403 export LOCALE_ARCHIVE=${pkgs.glibcLocalesUtf8}/lib/locale/locale-archive
404 '';
405 postCheck = ''
406 go vet ./...
407 '';
408 };
409
410 # The change record's own tests: its strict reading, its fuzz target's
411 # seed corpus, and its branch-name rule, which they hold to real git's
412 # check-ref-format.
413 change = pkgs.buildGoModule {
414 pname = "valley-change";
415 version = "0";
416 src = ../change;
417 vendorHash = null;
418 nativeCheckInputs = [ pkgs.git ];
419 };
420
421 # valley-delta-v1 (delta/): the encoding's strict reading, and the delta
422 # read from real trees. Its checkPhase runs go vet, the conformance
423 # vectors' script in its --check mode, which holds the committed expected
424 # results to what the script computes, and go test, which builds each
425 # vector's trees with real git and holds the Go code to those results.
426 delta = pkgs.buildGoModule {
427 pname = "valley-delta";
428 version = "0";
429 src = ../delta;
430 vendorHash = null;
431 nativeCheckInputs = [
432 pkgs.git
433 pkgs.python3
434 ];
435 preCheck = ''
436 go vet ./...
437 python3 -I testdata/vectors.py --check testdata
438 '';
439 };
440
441 # The restacker (restack/): `valley-restacker once --report-only` reads
442 # one snapshot of origin through the rebase engine (../rebase), says
443 # which families it would restack and which it would hold, and builds the
444 # writes a pass would push, pushing none. It reads the cap on its
445 # automatic versions through the composed-policy reader it shares with
446 # the integrator (../composed). Its unit tests cover request association,
447 # outcome currency, capacity and report bounds, and, against real git,
448 # the journal of writes and recovery's decisions, its environment, its
449 # scratch repository's identity, its state directory's lock and binding,
450 # and the deadline on what it runs. go vet runs after them. Go, standard
451 # library only — hence vendorHash = null and no module fetch.
452 restack-unwrapped = pkgs.buildGoModule {
453 pname = "valley-restacker";
454 version = "0";
455 src = withLocal ../restack [
456 ../change
457 ../composed
458 ../rebase
459 ];
460 modRoot = "restack";
461 # The program alone: e2e/transport is a module of the tests' own.
462 subPackages = [ "." ];
463 vendorHash = null;
464 preBuild = readsGoMod;
465 nativeCheckInputs = [ pkgs.git ];
466 postCheck = ''
467 go vet ./...
468 '';
469 # The module is named for its directory; the program, for what it is.
470 postInstall = ''
471 mv $out/bin/restack $out/bin/valley-restacker
472 '';
473 meta.mainProgram = "valley-restacker";
474 };
475
476 # The shipping form: git to read origin, cue to compose the policy, and
477 # attest to check a transfer statement, all pinned to this flake's
478 # nixpkgs, with the packaged verification schema as the default.
479 valley-restacker =
480 pkgs.runCommand "valley-restacker"
481 {
482 nativeBuildInputs = [ pkgs.makeWrapper ];
483 meta.mainProgram = "valley-restacker";
484 }
485 ''
486 mkdir -p $out/bin
487 makeWrapper ${lib.getExe restack-unwrapped} $out/bin/valley-restacker \
488 --prefix PATH : ${
489 lib.makeBinPath [
490 pkgs.git
491 pkgs.cue
492 attest
493 ]
494 } \
495 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
496 '';
497
498 # The Phase 3 integrator (dcr-439b771). Go, standard library only,
499 # same trade as attest — hence vendorHash = null and no module fetch. It
500 # reads change records with the one reading of a record the push hook
501 # shares (../change).
502 integrator-unwrapped = pkgs.buildGoModule {
503 pname = "valley-integrator";
504 version = "0";
505 # The verification schema is in the source for the policy bridge's
506 # tests, which compose a policy under it.
507 src = withLocal ../integrator [
508 ../change
509 ../composed
510 ../schema/verification.cue
511 ];
512 modRoot = "integrator";
513 vendorHash = null;
514 preBuild = readsGoMod;
515 # The policy bridge's tests check a deriver's report against real git,
516 # and compose a policy with real cue.
517 nativeCheckInputs = [
518 pkgs.git
519 pkgs.cue
520 ];
521 meta.mainProgram = "integrator";
522 };
523
524 # The shipping form. The integrator drives four programs rather than
525 # reimplementing what they do: attest judges every statement and
526 # every note, the valley deriver and cue compose the policy, and git
527 # holds the requests and the refs. All four are pinned to this
528 # flake's nixpkgs so an instance's integrators agree; `nix` is
529 # deliberately absent for the same reason it is absent from attest —
530 # a closure must be recomputed by the nix the machine actually runs.
531 integrator =
532 pkgs.runCommand "valley-integrator"
533 {
534 nativeBuildInputs = [ pkgs.makeWrapper ];
535 meta.mainProgram = "integrator";
536 }
537 ''
538 mkdir -p $out/bin
539 makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \
540 --prefix PATH : ${
541 lib.makeBinPath [
542 pkgs.git
543 pkgs.cue
544 pkgs.natscli
545 attest
546 valley-script
547 ]
548 } \
549 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \
550 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \
551 --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue}
552 '';
553}