gunk-dev/the-valley

packages.nix

sourcedbe26c5ba15amain's published tip history
41.1 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 # What no Go build or test here reads: prose and .gitignore files. A Go
30 # package's source leaves them out, so editing a module's README rebuilds
31 # nothing.
32 unread = lib.fileset.fileFilter (f: f.hasExt "md" || f.name == ".gitignore") ../.;
33
34 # Every Go test file in the tree. No package's build reads one.
35 goTests = lib.fileset.fileFilter (f: lib.hasSuffix "_test.go" f.name) ../.;
36
37 # A Go module of this repository, built as a package that runs no tests,
38 # with its unit tests beside it as the package's unit attribute.
39 #
40 # The package's source is what its build reads: the module's directory
41 # (dir) and the modules of this repository it uses (modules), which its
42 # go.mod replaces with their local directories, each in its place in the
43 # tree. Nothing shared this way is fetched. The source holds no test file
44 # and none of the files only the tests read (tests.reads), and the
45 # package's checkPhase is off. So an edit to a test, or to a file only the
46 # tests read, changes neither the package nor any check built on it.
47 #
48 # unit is the same build over the same source with the module's own tests
49 # and tests.reads added, and with its checkPhase on. The checkPhase runs
50 # go test, with the rest of tests: the tools the tests need, and what runs
51 # before and after them, such as go vet. Each module's unit is its unit
52 # check (nix/checks/), so its tests run in that check and in no package
53 # build.
54 #
55 # A file the tests read is in unit's source though it is prose, so a test
56 # may read the repository's documents.
57 #
58 # excluded names files of dir that neither the build nor the tests read:
59 # files only a flake check reads, or the part of a shared module that
60 # another package builds and tests.
61 goModule =
62 {
63 dir,
64 modules ? [ ],
65 excluded ? [ ],
66 tests ? { },
67 ...
68 }@args:
69 let
70 testReads = tests.reads or [ ];
71 source =
72 fileset:
73 lib.fileset.toSource {
74 root = ../.;
75 inherit fileset;
76 };
77 build = lib.fileset.difference (lib.fileset.unions ([ dir ] ++ modules)) (
78 lib.fileset.unions ([ unread goTests ] ++ excluded ++ testReads)
79 );
80 withTests = lib.fileset.union (lib.fileset.difference (lib.fileset.unions [
81 build
82 (lib.fileset.intersection goTests dir)
83 ]) (lib.fileset.unions ([ unread ] ++ excluded))) (lib.fileset.unions testReads);
84 in
85 pkgs.buildGoModule (
86 finalAttrs:
87 removeAttrs args [
88 "dir"
89 "modules"
90 "excluded"
91 "tests"
92 ]
93 // {
94 version = "0";
95 src = source build;
96 modRoot = lib.path.removePrefix ../. dir;
97 # Standard library only, so no module is fetched.
98 vendorHash = null;
99 doCheck = false;
100 passthru.unit = finalAttrs.finalPackage.overrideAttrs (
101 prev:
102 removeAttrs tests [ "reads" ]
103 // {
104 pname = "${prev.pname}-unit";
105 src = source withTests;
106 doCheck = true;
107 }
108 );
109 }
110 );
111
112 # vendorHash = null builds in vendor mode, which has no reading of a local
113 # replace. The replaced module is a directory beside the program and
114 # nothing is fetched either way, so these builds read go.mod as written.
115 readsGoMod = ''
116 export GOFLAGS="''${GOFLAGS//-mod=vendor/-mod=mod}"
117 '';
118
119 # Markdown prose is filled paragraphs hard-wrapped at 100 columns
120 # (ida-1ec03b1). One flag string backs both the formatter app and the
121 # prose-format check, so the two cannot drift. Embedded-language
122 # formatting is off so fenced code blocks pass through byte-for-byte.
123 proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off";
124
125 # The integrator's CLI (bin/valley) wrapped for `nix run`. The script
126 # itself must keep running bare from any checkout — the package is the
127 # second of its two shipping modes, never a dependency of the first.
128 valley-script-unwrapped = pkgs.writeShellApplication {
129 name = "valley";
130 runtimeInputs = [
131 pkgs.git
132 pkgs.natscli # tail and replay only; every other verb needs just git
133 pkgs.cue # checks and approving — it composes the policy layers
134 pkgs.less # review only — its v key is what takes a note
135 attest # approving only — it runs the checks and signs them
136 valley-go # status, ready, unready, withdraw and rebase, naming a change by its ID, and pending's drafts
137 ];
138 text = builtins.readFile ../bin/valley;
139 };
140
141 # The shipping form. The verification schema travels with the script, as
142 # it does with the integrator: only the-valley's own tree carries
143 # schema/, so a project's checkout has none to read. --set-default leaves
144 # an explicit VALLEY_VERIFICATION_SCHEMA in charge.
145 valley-script =
146 pkgs.runCommand "valley"
147 {
148 nativeBuildInputs = [ pkgs.makeWrapper ];
149 meta.mainProgram = "valley";
150 }
151 ''
152 mkdir -p $out/bin
153 makeWrapper ${lib.getExe valley-script-unwrapped} $out/bin/valley \
154 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
155 '';
156
157 # The installed package: the wrapped script stamped with its version, the
158 # shell completions at the standard paths home-manager/NixOS auto-link,
159 # and the tiny policy self-update's smoke test derives checks from
160 # (examples/policy/smoke/). The stamp is set on this outer wrapper alone,
161 # so the packages the checks and the integrator use do not change with
162 # every commit. The name carries the version as well: nix profile names
163 # the element valley from it, and self-update reads a hand-installed
164 # valley's commit from it.
165 valley =
166 pkgs.runCommand "valley-${version}"
167 {
168 inherit version;
169 nativeBuildInputs = [
170 pkgs.makeWrapper
171 pkgs.installShellFiles
172 ];
173 meta.mainProgram = "valley";
174 }
175 ''
176 mkdir -p $out/bin $out/share/valley
177 makeWrapper ${lib.getExe valley-script} $out/bin/valley \
178 --set VALLEY_VERSION ${version}
179 installShellCompletion --bash --name valley ${../completions/valley.bash}
180 installShellCompletion --zsh --name _valley ${../completions/_valley}
181 cp -r --no-preserve=mode ${../examples/policy/smoke} $out/share/valley/smoke-policy
182 '';
183
184 # The verbs of the CLI written in Go (status/). status reads one change's
185 # request and outcome record from origin, and prints them as sentences or
186 # as the valley-status/v1 JSON object front ends read. ready, unready and
187 # withdraw write a change's record, with the one reading of a record the
188 # push hook shares (../change). rebase rebases a change's whole stack and
189 # adds each change's rebased head to its record, through the rebase engine
190 # (../rebase), which also reads how far each version has landed for the
191 # listings. knot reads and writes the plan graph, with the one reading of
192 # a graph the push hook shares (../knot), and plan shows that graph as a
193 # view. compare reads a version's file delta and its approved version's
194 # with the one reading of valley-delta-v1 (../delta), for review.
195 # self-update installs the tip of a branch of the-valley into the user's
196 # nix profile (../selfupdate). checks prints which checks a change
197 # requires, as the deriver (../derive) derives them from the policy the
198 # composed-policy reader (../composed) reads. Go, standard library only.
199 # It runs whatever git and cue are on PATH, which inside the CLI's package
200 # are the CLI's own. Its unit tests hold its shell quoting to the bash it
201 # replaced, so they need bash, lay pending out over real repositories,
202 # so they need git, and derive a change's checks under the verification
203 # schema, so they need cue and the schema. The plan's golden files
204 # (status/testdata/plan/) are valley-plan's, and no Go test reads them.
205 valley-go = goModule {
206 pname = "valley-go";
207 dir = ../status;
208 modules = [
209 ../change
210 ../composed
211 ../delta
212 ../derive
213 ../knot
214 ../note
215 ../rebase
216 ../selfupdate
217 ../telemetry
218 ];
219 excluded = [ ../status/testdata/plan ];
220 preBuild = readsGoMod;
221 tests = {
222 reads = [ ../schema/verification.cue ];
223 nativeCheckInputs = [
224 pkgs.bash
225 pkgs.git
226 pkgs.cue
227 ];
228 # A test that cannot find a tool it needs fails rather than skipping.
229 preCheck = ''
230 export VALLEY_TEST_TOOLS=required
231 '';
232 };
233 meta.mainProgram = "valley-go";
234 };
235
236 # The check worker: one pass of `valley-go work` (status/worker.go), which
237 # builds the nix checks ready changes require and publishes the evidence
238 # before anyone approves them. It runs this flake's git, cue and attest,
239 # and the verification schema travels with it, as it does with the CLI.
240 # nix is the machine's own, for the reason attest gives.
241 valley-worker =
242 pkgs.runCommand "valley-worker"
243 {
244 nativeBuildInputs = [ pkgs.makeWrapper ];
245 meta.mainProgram = "valley-worker";
246 }
247 ''
248 mkdir -p $out/bin
249 makeWrapper ${lib.getExe valley-go} $out/bin/valley-worker \
250 --add-flags work \
251 --prefix PATH : ${
252 lib.makeBinPath [
253 pkgs.git
254 pkgs.cue
255 attest
256 ]
257 } \
258 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
259 '';
260
261 # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns.
262 prose-fmt = pkgs.writeShellApplication {
263 name = "valley-fmt";
264 runtimeInputs = [
265 pkgs.git
266 pkgs.prettier
267 ];
268 text = ''
269 cd "$(git rev-parse --show-toplevel)"
270 git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \
271 prettier ${proseFmtArgs} --write
272 '';
273 };
274
275 # The Phase 2 attestation helper (dcr-0de694f). Go, standard library
276 # only, note format included, and hence no module fetch at build time.
277 # Its unit tests need git and ssh-keygen. It reads signed notes with the
278 # note module (../note), the one reading of the envelope shared by the
279 # programs that write, verify and admit attestations. The conformance
280 # vectors (attest/conformance/) are attest-conformance's, and no Go test
281 # reads them.
282 attest-unwrapped = goModule {
283 pname = "valley-attest";
284 dir = ../attest;
285 modules = [
286 ../note
287 ../telemetry
288 ];
289 excluded = [ ../attest/conformance ];
290 preBuild = readsGoMod;
291 tests.nativeCheckInputs = [
292 pkgs.git
293 pkgs.openssh
294 ];
295 meta.mainProgram = "attest";
296 };
297
298 # The shipping form. git and cue are pinned to this flake's nixpkgs,
299 # so every host composes and vets a statement with the same tools.
300 # Signing needs no tool at all: a note is signed on crypto/ed25519
301 # inside the binary. `nix` is deliberately NOT pinned: the check being
302 # attested to must be built by the nix the machine actually runs, and
303 # a nix carried in here would be a second one.
304 attest =
305 pkgs.runCommand "valley-attest"
306 {
307 nativeBuildInputs = [ pkgs.makeWrapper ];
308 meta.mainProgram = "attest";
309 }
310 ''
311 mkdir -p $out/bin
312 makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \
313 --prefix PATH : ${
314 lib.makeBinPath [
315 pkgs.git
316 pkgs.cue
317 ]
318 } \
319 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue}
320 '';
321
322 # The identity registry compiler (dcr-b87f6e8). Go, standard library
323 # only, note format included, same trade as attest and the integrator, so
324 # no module is fetched. It reads the registry's export with exactjson
325 # (../exactjson). Its unit tests pin the note format's key hash
326 # against the reference vector attest is held to, and verify a real
327 # YubiKey attestation against the Yubico roots it carries. The test
328 # vectors (identity/testdata/) are read by the tests and by identity-e2e,
329 # never by the build.
330 identity-unwrapped = goModule {
331 pname = "valley-identity";
332 dir = ../identity;
333 modules = [
334 ../note
335 ../exactjson
336 ];
337 preBuild = readsGoMod;
338 tests.reads = [ ../identity/testdata ];
339 meta.mainProgram = "identity";
340 };
341
342 # The shipping form. git reads the registry out of the instance
343 # repository's tip and cue vets and exports it, both pinned to this
344 # flake's nixpkgs so every host compiles a registry the same way. The
345 # schema travels with the binary: a compiler judging a registry against a
346 # schema the consumer supplied would be no floor at all.
347 identity =
348 pkgs.runCommand "valley-identity"
349 {
350 nativeBuildInputs = [ pkgs.makeWrapper ];
351 meta.mainProgram = "identity";
352 }
353 ''
354 mkdir -p $out/bin
355 makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \
356 --prefix PATH : ${
357 lib.makeBinPath [
358 pkgs.git
359 pkgs.cue
360 ]
361 } \
362 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
363 '';
364
365 # The security-key signature verifier (sigverify/README.md). Go,
366 # standard library only, so no module is fetched.
367 #
368 # The git its git-tag command runs is fixed at build time to this
369 # flake's git, as an absolute store path compiled into the binary. A
370 # wrapper that puts git on PATH would leave the binary inside it looking
371 # git up on PATH, and a caller's environment could change which git that
372 # is. Compiled in, there is no PATH lookup to change.
373 #
374 # Its tests run against real ssh-keygen and git. The security-key cases
375 # among them need OpenSSH's sk-dummy authenticator, which the
376 # sigverify-unit check supplies. Building this package runs no test, so a
377 # consumer building it never builds OpenSSH.
378 sigverify = goModule {
379 pname = "valley-sigverify";
380 dir = ../sigverify;
381 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
382 tests.nativeCheckInputs = [
383 pkgs.git
384 pkgs.openssh
385 ];
386 meta.mainProgram = "sigverify";
387 };
388
389 # Telemetry (../telemetry): the diagnostic records, the bounded streams
390 # that hold them and the telemetry command's grammar, which the
391 # producers, the git user's shell and the collector share, and the
392 # collector itself, valley-telemetry serve, which the host runs per
393 # connection to its socket. Go, standard library only.
394 telemetry = goModule {
395 pname = "valley-telemetry";
396 dir = ../telemetry;
397 meta.mainProgram = "valley-telemetry";
398 };
399
400 # The pre-receive hook's policy (valleyhook/): what a push may write to a
401 # project a valley host serves. It validates a push to the plan graph
402 # with the one reading of a graph the CLI shares (../knot), and reads the
403 # project's push policy with exactjson (../exactjson). The same
404 # binary is the git user's login shell, which derives the pushing
405 # principal and pauses pushes until the host has converged. Go, standard
406 # library only, so no module is fetched.
407 #
408 # The git it reads the repository with is fixed at build time, the way
409 # sigverify's is: an absolute store path compiled into the binary, so no
410 # PATH a hook runs under can change which git that is.
411 valleyhook = goModule {
412 pname = "valley-valleyhook";
413 dir = ../valleyhook;
414 modules = [
415 ../note
416 ../change
417 ../knot
418 ../exactjson
419 ../telemetry
420 ];
421 preBuild = readsGoMod;
422 ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ];
423 # The walk's and the change records' tests build hostile objects with
424 # real git.
425 tests.nativeCheckInputs = [ pkgs.git ];
426 meta.mainProgram = "valleyhook";
427 };
428
429 # The PIV session signer (approval/piv/): a small C program on libykpiv
430 # that holds a YubiKey exclusively for a run of approval signatures,
431 # asks for the PIN once on the terminal, and signs each statement with a
432 # touch. It is C so that the Go modules stay standard library only: the
433 # approval command runs it rather than linking a smart card library. It
434 # calls pcsc-lite, libykpiv's own transport, directly only to connect
435 # exclusively and to see a reset. libykpiv reconnects after a reset or a
436 # removal (_ykpiv_begin_transaction) in shared mode; the signer's
437 # libykpiv reconnects exclusively, so no command reaches the card over a
438 # shared connection even when the card changes between the helper's
439 # check and libykpiv's call. nixpkgs' default hardening applies.
440 piv-signer = pkgs.stdenv.mkDerivation {
441 pname = "valley-piv-signer";
442 version = "0";
443 src = ../approval/piv;
444 buildInputs = [
445 (pkgs.yubico-piv-tool.overrideAttrs (old: {
446 postPatch = (old.postPatch or "") + ''
447 substituteInPlace lib/ykpiv.c \
448 --replace-fail 'SCardReconnect(state->card, SCARD_SHARE_SHARED,' \
449 'SCardReconnect(state->card, SCARD_SHARE_EXCLUSIVE,' \
450 --replace-fail 'SCardConnect(state->context, state->reader, SCARD_SHARE_SHARED,' \
451 'SCardConnect(state->context, state->reader, SCARD_SHARE_EXCLUSIVE,'
452 '';
453 }))
454 pkgs.pcsclite
455 ];
456 buildPhase = ''
457 $CC -std=c11 -O2 -Wall -Wextra -Werror -o valley-piv-signer valley-piv-signer.c \
458 -I${lib.getDev pkgs.pcsclite}/include/PCSC -lykpiv -lpcsclite
459 '';
460 installPhase = ''
461 install -Dm755 valley-piv-signer $out/bin/valley-piv-signer
462 '';
463 meta.mainProgram = "valley-piv-signer";
464 };
465
466 # Approval statements (approval/): the-valley/approval/v1's written form,
467 # signing it with a software key or a YubiKey's PIV slot, and verifying
468 # an approval note under the approval signers files. Go, standard library
469 # only. The PIV signer it runs is fixed at build time, the way
470 # sigverify's git is: an absolute store path compiled into the binary.
471 # Its unit tests run that signer with no smart card service to reach, so
472 # no device is touched.
473 approval = goModule {
474 pname = "valley-approval";
475 dir = ../approval;
476 modules = [
477 ../note
478 ../change
479 ];
480 excluded = [ ../approval/piv ];
481 subPackages = [ "cmd/approval" ];
482 preBuild = readsGoMod;
483 ldflags = [ "-X main.pivSigner=${lib.getExe piv-signer}" ];
484 tests = {
485 reads = [ ../approval/testdata ];
486 subPackages = [
487 "."
488 "cmd/approval"
489 ];
490 VALLEY_PIV_SIGNER = lib.getExe piv-signer;
491 };
492 meta.mainProgram = "approval";
493 };
494
495 # The note envelope (note/). Its tests hold it to refusing every line a
496 # relayer could append that one of its readers would refuse.
497 note = goModule {
498 pname = "valley-note";
499 dir = ../note;
500 };
501
502 # The strict JSON reading (exactjson/) that the identity compiler, the
503 # pre-receive hook and read access read CUE's exports with: every key
504 # spelled exactly as the field it is read into, and no key twice in one
505 # object. Its tests
506 # hold it to each spelling encoding/json alone would fold into a field.
507 exactjson = goModule {
508 pname = "valley-exactjson";
509 dir = ../exactjson;
510 };
511
512 # The web UI (web/). Package export is the contract between the
513 # publisher on a valley host and the web server it pushes to: the public
514 # export and the projection onto it, the signed manifest, the upload and
515 # the receiver's state directory. It reads names and IDs by the plan
516 # graph's rules (../knot), the manifest with the note module (../note),
517 # and who may read a project through ../readaccess, which reads with
518 # ../exactjson, as the publisher reads the host declaration and package
519 # export reads export.json. The program
520 # built from it is the publisher, cmd/valley-web-publish, which
521 # valley-web-publish below wraps. The server and its command are built by
522 # valley-web below, and the receiver by valley-web-receive. The export's
523 # tests accept bundles and build the shared fixture's repositories with
524 # real git. The publisher's tests run it against a valley built with real
525 # git and the CLI itself (bin/valley, run by bash, with valley-go on
526 # PATH), read each project's policy/read and the instance's identity
527 # registry with cue under schema/read.cue and schema/identity.cue, and
528 # upload to a receiver over a real store, and to the real receiver, whose
529 # state directory the real valley-web serves. One lands a change as the
530 # integrator does, with a transfer statement attest signs under a key
531 # ssh-keygen makes, which the CLI verifies with attest. Each test's host
532 # declaration is exported by cue under schema/valley.cue, as the module
533 # exports the host's. go vet runs after them. The fixture's expected
534 # export (export/exporttest/testdata/) is read by the tests alone, and so
535 # are README.md and docs/, whose links package markdown's tests resolve. Go,
536 # standard library only, so no module is fetched.
537 web = goModule {
538 pname = "valley-web-module";
539 dir = ../web;
540 modules = [
541 ../knot
542 ../note
543 ../readaccess
544 ../exactjson
545 ];
546 excluded = [
547 ../web/server
548 ../web/readerstate
549 ../web/cmd/valley-web
550 ../web/cmd/valley-web-receive
551 ];
552 preBuild = readsGoMod;
553 tests = {
554 reads = [
555 ../web/export/exporttest/testdata
556 ../README.md
557 ../docs
558 ../bin/valley
559 ../schema/read.cue
560 ../schema/identity.cue
561 ../schema/valley.cue
562 ];
563 nativeCheckInputs = [
564 pkgs.git
565 pkgs.bash
566 pkgs.cue
567 pkgs.openssh
568 valley-go
569 attest
570 ];
571 # A test that cannot find a tool it needs fails rather than skipping.
572 preCheck = ''
573 export VALLEY_TEST_TOOLS=required
574 export VALLEY_WEB=${lib.getExe valley-web}
575 export VALLEY_WEB_RECEIVE=${lib.getExe valley-web-receive}
576 '';
577 postCheck = ''
578 go vet ./...
579 '';
580 };
581 meta.mainProgram = "valley-web-publish";
582 };
583
584 # Read access (readaccess/): the two consents a principal needs to read a
585 # project, read out of git commits with cue under schema/read.cue and
586 # schema/identity.cue, and the evaluator that intersects them. The web
587 # publisher reads through it, and its command is the coverage report,
588 # which valley-read-coverage below wraps. It reads cue's exports with
589 # exactjson (../exactjson). Its tests read real commits with real git and
590 # the real cue under both schemas, and go vet runs after them. Go,
591 # standard library only, so no module is fetched.
592 readaccess = goModule {
593 pname = "valley-readaccess";
594 dir = ../readaccess;
595 modules = [ ../exactjson ];
596 preBuild = readsGoMod;
597 tests = {
598 reads = [
599 ../schema/read.cue
600 ../schema/identity.cue
601 ];
602 nativeCheckInputs = [
603 pkgs.git
604 pkgs.cue
605 ];
606 # A test that cannot find a tool it needs fails rather than skipping.
607 preCheck = ''
608 export VALLEY_TEST_TOOLS=required
609 '';
610 postCheck = ''
611 go vet ./...
612 '';
613 };
614 meta.mainProgram = "valley-read-coverage";
615 };
616
617 # The coverage report: which projects a principal cannot read at a
618 # destination, and which consent each lacks. It runs this flake's git and
619 # cue, and both schemas travel with it.
620 valley-read-coverage =
621 pkgs.runCommand "valley-read-coverage"
622 {
623 nativeBuildInputs = [ pkgs.makeWrapper ];
624 meta.mainProgram = "valley-read-coverage";
625 }
626 ''
627 mkdir -p $out/bin
628 makeWrapper ${lib.getExe readaccess} $out/bin/valley-read-coverage \
629 --prefix PATH : ${
630 lib.makeBinPath [
631 pkgs.git
632 pkgs.cue
633 ]
634 } \
635 --set-default VALLEY_READ_SCHEMA ${../schema/read.cue} \
636 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
637 '';
638
639 # The web UI's server (web/server/) and its command, valley-web
640 # (web/cmd/valley-web/), which serves the active export of a receiver's
641 # state directory to its readers, and keeps their sessions in the
642 # browser state (web/readerstate/). It reads the export and the
643 # state directory with package export, so its source is the web
644 # module, less the fixture's expected export, which only web's tests
645 # read. Package export reads export.json with ../exactjson, so its
646 # source holds that module too. Of ../readaccess, which the publisher
647 # reads through and the server does not, it reads the go.mod alone,
648 # which go reads to resolve the module. The git it reads repositories
649 # with is fixed at build time, the way valleyhook's is. Its tests run the real program over the shared
650 # fixture and real git, and the server over the real publisher's output
651 # (web-published), and go vet runs after them. The crash tests and the
652 # browser test are skipped there: web-reader-state-crash and
653 # web-reader-browser-e2e (nix/checks/) run them from the same tests.
654 # Go, standard library only, so no module is fetched.
655 valley-web = goModule {
656 pname = "valley-web";
657 dir = ../web;
658 modules = [
659 ../knot
660 ../note
661 ../readaccess/go.mod
662 ../exactjson
663 ];
664 excluded = [
665 ../web/export/exporttest/testdata
666 ../web/cmd/valley-web-publish
667 ../web/cmd/valley-web-receive
668 ];
669 subPackages = [ "cmd/valley-web" ];
670 preBuild = readsGoMod;
671 ldflags = [ "-X the-valley/web/export.GitProgram=${lib.getExe pkgs.git}" ];
672 tests = {
673 subPackages = [
674 "server"
675 "readerstate"
676 "cmd/valley-web"
677 ];
678 checkFlags = [ "-skip=^(TestCrash|TestBrowser)" ];
679 nativeCheckInputs = [ pkgs.git ];
680 # A test that cannot find the publisher's output fails rather than
681 # skipping.
682 preCheck = ''
683 export VALLEY_TEST_TOOLS=required
684 export VALLEY_WEB_PUBLISHED=${web-published}
685 '';
686 postCheck = ''
687 go vet ./server/... ./readerstate/... ./cmd/valley-web/...
688 '';
689 };
690 meta.mainProgram = "valley-web";
691 };
692
693 # The real publisher's signed output over a small valley with a public
694 # project, two private projects each one named reader may read, and a
695 # project with no effective reader, written with --out by the packaged publisher and CLI
696 # (nix/checks/web-published.sh). valley-web's tests read it, so the
697 # server is tested against what the publisher writes. It is no check of
698 # its own.
699 web-published =
700 pkgs.runCommand "valley-web-published"
701 {
702 nativeBuildInputs = [
703 pkgs.git
704 pkgs.cue
705 pkgs.jq
706 valley-script
707 valley-web-publish
708 ];
709 schema = ../schema/valley.cue;
710 }
711 (builtins.readFile ./checks/web-published.sh);
712
713 # The web export receiver, valley-web-receive (web/cmd/valley-web-receive/),
714 # which takes the publisher's uploads into a state directory that
715 # valley-web serves. Its source is the web module less the publisher,
716 # the server and its command, which it does not build, and the fixture's
717 # expected export. Package export reads export.json with ../exactjson, so
718 # its source holds that module too; of ../readaccess it reads the go.mod
719 # alone, as valley-web does. The git it accepts bundles with is fixed at
720 # build time. Its tests run the real program over the shared fixture and
721 # real git, with the real valley-web serving its state directory, and go
722 # vet runs after them. Go, standard library only.
723 valley-web-receive = goModule {
724 pname = "valley-web-receive";
725 dir = ../web;
726 modules = [
727 ../knot
728 ../note
729 ../readaccess/go.mod
730 ../exactjson
731 ];
732 excluded = [
733 ../web/export/exporttest/testdata
734 ../web/cmd/valley-web-publish
735 ../web/cmd/valley-web
736 ../web/server
737 ../web/readerstate
738 ];
739 subPackages = [ "cmd/valley-web-receive" ];
740 preBuild = readsGoMod;
741 ldflags = [ "-X the-valley/web/export.GitProgram=${lib.getExe pkgs.git}" ];
742 tests = {
743 nativeCheckInputs = [ pkgs.git ];
744 # A test that cannot find valley-web fails rather than skipping.
745 preCheck = ''
746 export VALLEY_TEST_TOOLS=required
747 export VALLEY_WEB=${lib.getExe valley-web}
748 '';
749 postCheck = ''
750 go vet ./cmd/valley-web-receive/...
751 '';
752 };
753 meta.mainProgram = "valley-web-receive";
754 };
755
756 # The edge's container image, for a web server that does not run NixOS:
757 # valley-web and valley-web-receive, and the git both run, in one image.
758 # The two run as two containers of it over one state directory, as two
759 # users of one group: the receiver as valley-web-receive, which writes
760 # the directory, and valley-web as valley-web, which reads it. The image
761 # names both users in its /etc/passwd, and holds the state directory,
762 # owned by the receiver and readable by the group alone, so a volume
763 # mounted there starts with that owner and mode. In it, browser/ is
764 # valley-web's alone, mode 0700: the browser state, the one place
765 # valley-web writes. It runs as valley-web unless it is told otherwise,
766 # and starts no program of its own: each container names its command.
767 edgeUsers = {
768 receiver = {
769 name = "valley-web-receive";
770 uid = 990;
771 };
772 web = {
773 name = "valley-web";
774 uid = 991;
775 };
776 gid = 990;
777 };
778 valley-web-image = pkgs.dockerTools.buildLayeredImage {
779 name = "valley-web";
780 contents = [
781 valley-web
782 valley-web-receive
783 (pkgs.dockerTools.fakeNss.override {
784 extraPasswdLines = with edgeUsers; [
785 "${receiver.name}:x:${toString receiver.uid}:${toString gid}:valley-web-receive:/var/empty:/bin/false"
786 "${edgeUsers.web.name}:x:${toString edgeUsers.web.uid}:${toString gid}:valley-web:/var/empty:/bin/false"
787 ];
788 extraGroupLines = [ "valley-web:x:${toString edgeUsers.gid}:" ];
789 })
790 ];
791 fakeRootCommands = ''
792 mkdir -p var/lib/valley-web/browser tmp
793 chown ${toString edgeUsers.receiver.uid}:${toString edgeUsers.gid} var/lib/valley-web
794 chmod 0750 var/lib/valley-web
795 chown ${toString edgeUsers.web.uid}:${toString edgeUsers.gid} var/lib/valley-web/browser
796 chmod 0700 var/lib/valley-web/browser
797 chmod 1777 tmp
798 '';
799 config = {
800 User = edgeUsers.web.name;
801 WorkingDirectory = "/";
802 Env = [ "PATH=/bin" ];
803 Volumes."/var/lib/valley-web" = { };
804 };
805 };
806
807 # The web publisher: one pass of valley-web-publish, which reads a valley
808 # host's public projects through the CLI's JSON commands and uploads them
809 # to a web receiver. It runs this flake's git, cue and CLI, and the two
810 # schemas it reads read access under travel with it, as the verification
811 # schema does with the CLI.
812 valley-web-publish =
813 pkgs.runCommand "valley-web-publish"
814 {
815 nativeBuildInputs = [ pkgs.makeWrapper ];
816 meta.mainProgram = "valley-web-publish";
817 }
818 ''
819 mkdir -p $out/bin
820 makeWrapper ${lib.getExe web} $out/bin/valley-web-publish \
821 --prefix PATH : ${
822 lib.makeBinPath [
823 pkgs.git
824 pkgs.cue
825 valley-script
826 ]
827 } \
828 --set-default VALLEY_READ_SCHEMA ${../schema/read.cue} \
829 --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue}
830 '';
831
832 # The plan graph (knot/): its canonical form, its layout, cycles and
833 # transitions, held by its own tests. The package is knotcheck, which
834 # knot-agree runs to hold the Go validator to the CUE schema and the
835 # knowledge lint.
836 knot = goModule {
837 pname = "valley-knot";
838 dir = ../knot;
839 meta.mainProgram = "knotcheck";
840 };
841
842 # The rebase engine (rebase/): a family of stacked changes captured from
843 # one snapshot of origin, rebased in a throwaway worktree, and published
844 # as one atomic branch push and one record append per change. Its tests
845 # run against real repositories, real pushes and real git, with go vet
846 # after them. Go, standard library only, so no module is fetched.
847 rebase = goModule {
848 pname = "valley-rebase";
849 dir = ../rebase;
850 modules = [ ../change ];
851 preBuild = readsGoMod;
852 tests = {
853 nativeCheckInputs = [ pkgs.git ];
854 postCheck = ''
855 go vet ./...
856 '';
857 };
858 };
859
860 # Self-update (selfupdate/): the installed CLI brought up to the tip of a
861 # branch, built with nix and installed into the user's profile. Its tests,
862 # with go vet after them, use a real bare repository for ls-remote and a
863 # stand-in for nix whose builds are small scripts, so nothing is built and
864 # no profile is touched. Go, standard library only, so no module is
865 # fetched.
866 selfupdate = goModule {
867 pname = "valley-selfupdate";
868 dir = ../selfupdate;
869 tests = {
870 nativeCheckInputs = [ pkgs.git ];
871 postCheck = ''
872 go vet ./...
873 '';
874 };
875 };
876
877 # The deriver of required checks (derive/): a change's paths read from
878 # git diff -z, globs matched byte by byte, and the required checks, pins,
879 # carry mode and unclassified paths derived from the whole composed
880 # policy (../composed). Its tests, with go vet after them, derive over the
881 # repository's own policy and the example policies, with changes drawn
882 # from a fixed seed, so they read the schema and those policies, and need
883 # git and cue. Go, standard library only, so no module is fetched.
884 derive = goModule {
885 pname = "valley-derive";
886 dir = ../derive;
887 modules = [ ../composed ];
888 preBuild = readsGoMod;
889 tests = {
890 reads = [
891 ../schema/verification.cue
892 ../examples/policy
893 ../policy/project
894 ];
895 nativeCheckInputs = [
896 pkgs.git
897 pkgs.cue
898 ];
899 # A test that cannot find a tool it needs fails rather than skipping.
900 preCheck = ''
901 export VALLEY_TEST_TOOLS=required
902 '';
903 postCheck = ''
904 go vet ./...
905 '';
906 };
907 };
908
909 # The composed-policy reader (composed/): each layer read out of git,
910 # present, absent or refused. Its tests
911 # read layers from real repositories, some with objects removed, and go
912 # vet runs after them.
913 composed = goModule {
914 pname = "valley-composed";
915 dir = ../composed;
916 tests = {
917 nativeCheckInputs = [ pkgs.git ];
918 # A test that cannot find git fails rather than skipping.
919 preCheck = ''
920 export VALLEY_TEST_TOOLS=required
921 '';
922 postCheck = ''
923 go vet ./...
924 '';
925 };
926 };
927
928 # The change record (change/). Its tests cover its strict reading, its
929 # fuzz target's seed corpus, and its branch-name rule, which they hold to
930 # real git's check-ref-format.
931 change = goModule {
932 pname = "valley-change";
933 dir = ../change;
934 tests.nativeCheckInputs = [ pkgs.git ];
935 };
936
937 # valley-delta-v1 (delta/): the encoding's strict reading, and the delta
938 # read from real trees. Its unit tests run go vet, the conformance
939 # vectors' script in its --check mode, which holds the committed expected
940 # results to what the script computes, and go test, which builds each
941 # vector's trees with real git and holds the Go code to those results.
942 delta = goModule {
943 pname = "valley-delta";
944 dir = ../delta;
945 tests = {
946 reads = [ ../delta/testdata ];
947 nativeCheckInputs = [
948 pkgs.git
949 pkgs.python3
950 ];
951 preCheck = ''
952 go vet ./...
953 python3 -I testdata/vectors.py --check testdata
954 '';
955 };
956 };
957
958 # Review records (review/): the-valley/review/v1's strict reading, a
959 # review's binding to the version it judged, recomputed from git within a
960 # budget, and the append-only history a review is kept in, with the check
961 # an append is held to. It reads change records with the one reading of a
962 # record the push hook shares (../change), and the delta with
963 # valley-delta-v1's bounded reading (../delta). Its tests build every
964 # repository they read with real git and run its fuzz target's seed
965 # corpus, and go vet runs after them. Go, standard library only, so no
966 # module is fetched.
967 review = goModule {
968 pname = "valley-review";
969 dir = ../review;
970 modules = [
971 ../change
972 ../delta
973 ];
974 preBuild = readsGoMod;
975 tests = {
976 nativeCheckInputs = [ pkgs.git ];
977 postCheck = ''
978 go vet ./...
979 '';
980 };
981 };
982
983 # The restacker (restack/): `valley-restacker run` reconciles on an
984 # interval, and `once` is one pass. A pass reads origin through the
985 # rebase engine (../rebase), plans which families to restack and which
986 # to hold, keeps the writes in its journal, and pushes them;
987 # `once --report-only` pushes none. It reads the cap on its
988 # automatic versions through the composed-policy reader it shares with
989 # the integrator (../composed), and a signer projects file through the
990 # note module (../note). Its unit tests cover request association,
991 # outcome currency, capacity and report bounds, and, against real git,
992 # the journal of writes, recovery's decisions and publishing through the
993 # rendered hook, its environment, its
994 # scratch repository's identity, its state directory's lock and binding,
995 # and the deadline on what it runs. go vet runs after them. Go, standard
996 # library only, so no module is fetched.
997 restack-unwrapped = goModule {
998 pname = "valley-restacker";
999 dir = ../restack;
1000 modules = [
1001 ../change
1002 ../composed
1003 ../note
1004 ../rebase
1005 ];
1006 # The program alone: e2e/transport is a module of restack-e2e's own,
1007 # which neither this build nor its tests read.
1008 excluded = [ ../restack/e2e ];
1009 subPackages = [ "." ];
1010 preBuild = readsGoMod;
1011 tests = {
1012 nativeCheckInputs = [ pkgs.git ];
1013 postCheck = ''
1014 go vet ./...
1015 '';
1016 };
1017 # The module is named for its directory; the program, for what it is.
1018 postInstall = ''
1019 mv $out/bin/restack $out/bin/valley-restacker
1020 '';
1021 meta.mainProgram = "valley-restacker";
1022 };
1023
1024 # The shipping form: git to read origin, cue to compose the policy, and
1025 # attest to check a transfer statement, all pinned to this flake's
1026 # nixpkgs, with the packaged verification schema as the default.
1027 valley-restacker =
1028 pkgs.runCommand "valley-restacker"
1029 {
1030 nativeBuildInputs = [ pkgs.makeWrapper ];
1031 meta.mainProgram = "valley-restacker";
1032 }
1033 ''
1034 mkdir -p $out/bin
1035 makeWrapper ${lib.getExe restack-unwrapped} $out/bin/valley-restacker \
1036 --prefix PATH : ${
1037 lib.makeBinPath [
1038 pkgs.git
1039 pkgs.cue
1040 attest
1041 ]
1042 } \
1043 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue}
1044 '';
1045
1046 # The Phase 3 integrator (dcr-439b771). Go, standard library only,
1047 # same trade as attest, so no module is fetched. It reads change records
1048 # with the one reading of a record the push hook shares (../change),
1049 # verifier keys with the one reading of a key the note format defines
1050 # (../note), and derives required checks with the deriver (../derive).
1051 integrator-unwrapped = goModule {
1052 pname = "valley-integrator";
1053 dir = ../integrator;
1054 modules = [
1055 ../change
1056 ../composed
1057 ../derive
1058 ../note
1059 ../telemetry
1060 ];
1061 # The scenarios (integrator/e2e/) are integrator-e2e's, and no Go test
1062 # reads them.
1063 excluded = [ ../integrator/e2e ];
1064 preBuild = readsGoMod;
1065 tests = {
1066 # The policy bridge's tests compose a policy under the verification
1067 # schema with real cue, and derive over diffs in real git.
1068 reads = [ ../schema/verification.cue ];
1069 nativeCheckInputs = [
1070 pkgs.git
1071 pkgs.cue
1072 ];
1073 };
1074 meta.mainProgram = "integrator";
1075 };
1076
1077 # The shipping form. The integrator drives four programs rather than
1078 # reimplementing what they do: attest judges every statement and
1079 # every note, cue composes the policy, git holds the requests and the
1080 # refs, and nats publishes its events. All four are pinned to this
1081 # flake's nixpkgs so an instance's integrators agree; `nix` is
1082 # deliberately absent for the same reason it is absent from attest —
1083 # a closure must be recomputed by the nix the machine actually runs.
1084 integrator =
1085 pkgs.runCommand "valley-integrator"
1086 {
1087 nativeBuildInputs = [ pkgs.makeWrapper ];
1088 meta.mainProgram = "integrator";
1089 }
1090 ''
1091 mkdir -p $out/bin
1092 makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \
1093 --prefix PATH : ${
1094 lib.makeBinPath [
1095 pkgs.git
1096 pkgs.cue
1097 pkgs.natscli
1098 attest
1099 ]
1100 } \
1101 --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \
1102 --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \
1103 --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue}
1104 '';
1105}