gunk-dev/the-valley

hosts.nix

source6bf8ca46b311main's published tip history
19.8 KiB
1# The hosts the module checks are about: eval-only instantiations of the
2# NixOS module, one per declaration worth holding it to. Evaluating one
3# catches option-surface and cue-export regressions without building or
4# booting anything, and the hook checks read the scripts these render.
5{
6 pkgs,
7 lib,
8 self,
9 # Whether the hosts run stand-ins for this flake's programs rather than
10 # the programs themselves.
11 stubbed ? false,
12}:
13let
14 # The stand-ins: the integrator, the identity compiler, valleyhook and
15 # the restacker, each a script with the program's name that fails if
16 # anything runs it.
17 # A host rendered over them renders the same units, hooks and scripts,
18 # with the stand-ins' store paths where the programs' would be. None of
19 # them changes when a program does, so a check that reads only what the
20 # module renders changes only when the module or the check does. A check
21 # that runs a program takes the hosts that run the real one.
22 stub =
23 name:
24 pkgs.writeShellScriptBin name ''
25 echo "${name}: a stand-in the module checks render hosts over, which runs nothing" >&2
26 exit 1
27 '';
28 stubs = lib.genAttrs [ "integrator" "identity" "valleyhook" ] stub // {
29 restacker = stub "valley-restacker";
30 };
31
32 # Each check host shares the machine baseline and supplies its own
33 # declaration (and, where the declaration asks for backup, the secret
34 # paths).
35 #
36 # Each host is also handed the package set the checks already use,
37 # rather than importing nixpkgs again for itself. It is the same nixpkgs
38 # with the same configuration, so every host renders the same
39 # derivations. module-eval evaluates every host here, and importing
40 # nixpkgs once per host was half of its evaluation time.
41 mkHost =
42 module:
43 lib.nixosSystem {
44 inherit pkgs;
45 modules = [
46 self.nixosModules.default
47 {
48 fileSystems."/" = {
49 device = "/dev/disk/by-label/nixos";
50 fsType = "ext4";
51 };
52 boot.loader.grub.enable = false;
53 system.stateVersion = "25.11";
54 services.valley = {
55 enable = true;
56 authorizedKeys = [
57 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck0 valley-check"
58 ];
59 };
60 }
61 (lib.mkIf stubbed { services.valley.packages = stubs; })
62 module
63 ];
64 };
65
66 # The same controllers as restrictedHost's with the check worker on and
67 # the build slice on at its defaults, writing neither the worker's
68 # projects nor its signer's.
69 workerBase = {
70 services.valley = {
71 config = ../../examples/hosts/protected.cue;
72 buildSlice.enable = true;
73 integrator = {
74 enable = true;
75 signingKeyFile = "/run/agenix/valley-integrator-key";
76 knownSignersFile = "/var/lib/valley-instance/known_signers";
77 instanceProject = "sealed";
78 };
79 worker = {
80 enable = true;
81 signerName = "worker.valley.invalid/attestations";
82 };
83 };
84 };
85
86 # The check worker for two of the projects the controllers serve, its
87 # signer restricted to exactly those two. It is bound here so the hosts
88 # the module must refuse can be made from it.
89 workerHost = mkHost {
90 imports = [ workerBase ];
91 services.valley = {
92 integrator.signerProjects."worker.valley.invalid/attestations" = [
93 "sealed"
94 "guarded"
95 ];
96 worker.projects = [
97 "guarded"
98 "sealed"
99 ];
100 };
101 };
102in
103{
104 # The example declaration enables backup, so this host supplies
105 # the four secret paths (eval-only placeholders).
106 host = mkHost {
107 services.valley = {
108 config = ../../examples/host.cue;
109 backup = {
110 repositoryFile = "/run/agenix/valley-restic-repo";
111 passwordFile = "/run/agenix/valley-restic-password";
112 sshKeyFile = "/run/agenix/valley-git-ssh-key";
113 knownHostsFile = "/var/lib/valley-backup/known_hosts";
114 };
115 };
116 };
117
118 # A declaration with no optional field set at all — no backup, no
119 # protection — must keep evaluating exactly as it did before those
120 # fields existed: zero restic machinery, no hook.
121 noBackupHost = mkHost {
122 services.valley.config = ../../examples/hosts/no-backup.cue;
123 };
124
125 # The example declaration with the secret paths left unset: the
126 # module must refuse with assertions naming the missing options.
127 noSecretsHost = mkHost {
128 services.valley.config = ../../examples/host.cue;
129 };
130
131 # Bus enabled, minimal declaration. The bus-e2e check drives this
132 # host's rendered hooks and stream-init against a real server.
133 busHost = mkHost {
134 services.valley = {
135 config = ../../examples/hosts/bus.cue;
136 bus.enable = true;
137 };
138 };
139
140 # Two mirror URLs the sandbox can serve: git resolves a relative
141 # URL against the pushing repo's directory, so the mirror-e2e
142 # check drives the rendered script with no substitution at all —
143 # one reachable sibling repo, one that does not exist.
144 mirrorHost = mkHost {
145 services.valley.config = ../../examples/hosts/mirrors.cue;
146 # Client environment the push boundary does not read stays acceptable.
147 services.openssh.settings.AcceptEnv = "LANG LC_*";
148 };
149
150 # The protection declaration, plus the machine half it needs: keys
151 # bound to the principal names it uses, and the approve and restack
152 # grants held by hand. requester holds both grants, so the hook checks
153 # read a principal whose approve grant the restack grant leaves as it
154 # is. The approve grant is declared under both its names: requester is
155 # named under both, and approver under approve alone. The baseline key
156 # above is anonymous and stays in the list, so this host also renders
157 # the two forms of key side by side.
158 protectedHost = mkHost {
159 services.valley = {
160 config = ../../examples/hosts/protected.cue;
161 authorizedKeys = [
162 {
163 principal = "integrator";
164 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck1 integrator";
165 }
166 {
167 principal = "contributor";
168 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
169 }
170 {
171 principal = "requester";
172 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck3 requester";
173 }
174 {
175 principal = "restacker";
176 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck4 restacker";
177 }
178 {
179 principal = "approver";
180 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck5 approver";
181 }
182 ];
183 grants.request = [ "requester" ];
184 grants.approve = [
185 "approver"
186 "requester"
187 ];
188 grants.restack = [
189 "restacker"
190 "requester"
191 ];
192 # The keys attestations pushed here are checked against: the ones a
193 # controller would accept evidence from, though this host runs none.
194 integrator.knownSignersFile = "/var/lib/valley-instance/known_signers";
195 # released composes a hook of its own after the push policy: it
196 # refuses branches under frozen/, which the policy would accept.
197 extraPreReceive.released = pkgs.writeShellScript "valley-check-released-pre-receive" ''
198 while read -r _ _ ref; do
199 case "$ref" in
200 refs/heads/frozen/*)
201 echo "released: $ref is frozen" >&2
202 exit 1
203 ;;
204 esac
205 done
206 '';
207 };
208 };
209
210 # The same protection declaration with the controllers switched on. The
211 # integrator serves exactly what that declaration protects, so this host
212 # renders two controllers and no third; the paths are eval-only
213 # placeholders, like the backup host's.
214 integratorHost = mkHost {
215 services.valley = {
216 config = ../../examples/hosts/protected.cue;
217 integrator = {
218 enable = true;
219 signingKeyFile = "/run/agenix/valley-integrator-key";
220 knownSignersFile = "/var/lib/valley-instance/known_signers";
221 # The floor comes from "sealed", which declares protection, as the
222 # instance repository has to. Every other controller here reads the
223 # floor out of a repository that is not the one it serves, which is
224 # the cross-repository read the unit has to grant.
225 instanceProject = "sealed";
226 };
227 };
228 };
229
230 # The same controllers with a check worker's signer restricted to two of
231 # the projects they serve, and a retired signer restricted to none.
232 restrictedHost = mkHost {
233 services.valley = {
234 config = ../../examples/hosts/protected.cue;
235 integrator = {
236 enable = true;
237 signingKeyFile = "/run/agenix/valley-integrator-key";
238 knownSignersFile = "/var/lib/valley-instance/known_signers";
239 instanceProject = "sealed";
240 signerProjects = {
241 "worker.valley.invalid/attestations" = [
242 "guarded"
243 "sealed"
244 ];
245 "retired.valley.invalid/attestations" = [ ];
246 };
247 };
248 };
249 };
250
251 inherit workerHost;
252
253 # The check worker with neither list written: it builds for every project
254 # the controllers serve, and its signer counts in exactly those.
255 defaultWorkerHost = mkHost workerBase;
256
257 # The check worker narrowed to one project, its signer's list unwritten:
258 # the signer counts in that one project alone.
259 narrowedWorkerHost = mkHost {
260 imports = [ workerBase ];
261 services.valley.worker.projects = [ "guarded" ];
262 };
263
264 # Both lists written, the signer's narrower than the worker's. A module
265 # entry merged into the host's, rather than replaced by it, would make
266 # the two lists match.
267 mismatchedWorkerHost = mkHost {
268 imports = [ workerBase ];
269 services.valley = {
270 integrator.signerProjects."worker.valley.invalid/attestations" = [ "sealed" ];
271 worker.projects = [
272 "guarded"
273 "sealed"
274 ];
275 };
276 };
277
278 # The restacker on for one of the projects the controllers serve, with
279 # the floor read from the instance repository.
280 restackerHost = mkHost {
281 services.valley = {
282 config = ../../examples/hosts/protected.cue;
283 integrator = {
284 enable = true;
285 signingKeyFile = "/run/agenix/valley-integrator-key";
286 knownSignersFile = "/var/lib/valley-instance/known_signers";
287 instanceProject = "sealed";
288 };
289 restacker = {
290 enable = true;
291 projects = [ "guarded" ];
292 sshKeyFile = "/run/agenix/valley-restacker-key";
293 };
294 };
295 };
296
297 # The restacker with no list of projects: it serves every project the
298 # host integrates.
299 defaultRestackerHost = mkHost {
300 services.valley = {
301 config = ../../examples/hosts/protected.cue;
302 integrator = {
303 enable = true;
304 signingKeyFile = "/run/agenix/valley-integrator-key";
305 knownSignersFile = "/var/lib/valley-instance/known_signers";
306 instanceProject = "sealed";
307 };
308 restacker = {
309 enable = true;
310 sshKeyFile = "/run/agenix/valley-restacker-key";
311 };
312 };
313 };
314
315 # The restacker for a project this host does not serve.
316 unservedRestackerHost = mkHost {
317 services.valley = {
318 config = ../../examples/hosts/protected.cue;
319 integrator.instanceProject = "sealed";
320 restacker = {
321 enable = true;
322 projects = [ "nowhere" ];
323 sshKeyFile = "/run/agenix/valley-restacker-key";
324 };
325 };
326 };
327
328 # The restacker with no key to push with.
329 keylessRestackerHost = mkHost {
330 services.valley = {
331 config = ../../examples/hosts/protected.cue;
332 integrator.instanceProject = "sealed";
333 restacker = {
334 enable = true;
335 projects = [ "guarded" ];
336 };
337 };
338 };
339
340 # The collector on, with one unit of the host's own declared as a source.
341 telemetryHost = mkHost {
342 services.valley = {
343 config = ../../examples/hosts/protected.cue;
344 telemetry = {
345 enable = true;
346 observers = [ "operator" ];
347 localSources = [
348 {
349 name = "converge";
350 description = "the service that converges this host";
351 units = [ "converge.service" ];
352 }
353 ];
354 };
355 };
356 };
357
358 # The build slice alone, at caps of its own: every build of the daemon
359 # capped, and no worker.
360 buildSliceHost = mkHost {
361 services.valley = {
362 config = ../../examples/hosts/protected.cue;
363 buildSlice = {
364 enable = true;
365 cpuQuota = "200%";
366 memoryMax = "4G";
367 };
368 };
369 };
370
371 # The registry machinery switched on, over the same declaration. The
372 # signers file is not supplied: the compiler renders it, and a consumer
373 # that also named one would be asking which of the two won. The
374 # integrator's signing name is set here too, because a host that compiles
375 # a registry is a host whose registry publishes the integrator's key under
376 # a name, and the controller has to sign under that same name.
377 identityHost = mkHost {
378 services.valley = {
379 config = ../../examples/hosts/protected.cue;
380 identity.enable = true;
381 integrator = {
382 enable = true;
383 signingKeyFile = "/run/agenix/valley-integrator-key";
384 signingName = "integrator";
385 instanceProject = "sealed";
386 };
387 };
388 };
389
390 # The same host with the instance repository among the served projects —
391 # the shape a group's own instance takes, where the repository carrying
392 # the floor is itself integrated. The controller serving it reads the
393 # floor from the repository it is already serving.
394 integratorSelfHost = mkHost {
395 services.valley = {
396 config = ../../examples/hosts/protected.cue;
397 integrator = {
398 enable = true;
399 signingKeyFile = "/run/agenix/valley-integrator-key";
400 knownSignersFile = "/var/lib/valley-instance/known_signers";
401 instanceProject = "guarded";
402 };
403 };
404 };
405
406 # Hosts the module must refuse. Each is one way for the push policy to be
407 # quietly wrong, and each must fail an assertion naming what is wrong.
408 #
409 # The instance repository unprotected: the floor and the registry would
410 # be read from a main any key could push.
411 unprotectedInstanceHost = mkHost {
412 services.valley = {
413 config = ../../examples/hosts/protected.cue;
414 integrator = {
415 enable = true;
416 signingKeyFile = "/run/agenix/valley-integrator-key";
417 knownSignersFile = "/var/lib/valley-instance/known_signers";
418 instanceProject = "open";
419 };
420 };
421 };
422
423 # sshd accepting the principal variable from the client.
424 acceptEnvHost = mkHost {
425 services.valley.config = ../../examples/hosts/protected.cue;
426 services.openssh.settings.AcceptEnv = "LANG LC_* VALLEY_*";
427 };
428
429 # The same, in the git user's own Match block, with the keyword in another
430 # case: sshd reads keywords in any case.
431 matchAcceptEnvHost = mkHost {
432 services.valley.config = ../../examples/hosts/protected.cue;
433 services.openssh.extraConfig = lib.mkAfter ''
434 Match User git
435 ACCEPTENV GIT_CONFIG_*
436 Match All
437 '';
438 };
439
440 # The principal, single-quoted: sshd strips the quotes and admits it.
441 quotedAcceptEnvHost = mkHost {
442 services.valley.config = ../../examples/hosts/protected.cue;
443 services.openssh.extraConfig = lib.mkAfter "AcceptEnv LANG 'VALLEY_PRINCIPAL'";
444 };
445
446 # The principal, separated from the pattern before it by a tab.
447 tabAcceptEnvHost = mkHost {
448 services.valley.config = ../../examples/hosts/protected.cue;
449 services.openssh.settings.AcceptEnv = "LANG\tVALLEY_PRINCIPAL";
450 };
451
452 # A pattern that names neither variable outright and still admits one.
453 wildcardAcceptEnvHost = mkHost {
454 services.valley.config = ../../examples/hosts/protected.cue;
455 services.openssh.settings.AcceptEnv = "LANG G?T_*";
456 };
457
458 # sshd setting the principal itself, over every key.
459 setEnvHost = mkHost {
460 services.valley.config = ../../examples/hosts/protected.cue;
461 services.openssh.extraConfig = lib.mkAfter "SetEnv VALLEY_PRINCIPAL=operator";
462 };
463
464 # sshd honouring every variable a key's entry or ~/.ssh/environment sets.
465 permitUserEnvironmentHost = mkHost {
466 services.valley.config = ../../examples/hosts/protected.cue;
467 services.openssh.settings.PermitUserEnvironment = lib.mkForce "yes";
468 };
469
470 # Two projects written as one: rendered one signer per line, the worker
471 # would read back as restricted to guarded and sealed, two projects the
472 # declaration never listed for it.
473 spacedSignerProjectsHost = mkHost {
474 services.valley = {
475 config = ../../examples/hosts/protected.cue;
476 integrator = {
477 enable = true;
478 signingKeyFile = "/run/agenix/valley-integrator-key";
479 knownSignersFile = "/var/lib/valley-instance/known_signers";
480 instanceProject = "sealed";
481 signerProjects."worker.valley.invalid/attestations" = [ "guarded sealed" ];
482 };
483 };
484 };
485
486 # The same two projects joined by a no-break space, U+00A0, which Nix's
487 # [:space:] does not match and Go's unicode.IsSpace does. Nix strings
488 # have no escape for it, so it is decoded from JSON.
489 unicodeSpacedSignerProjectsHost = mkHost {
490 services.valley = {
491 config = ../../examples/hosts/protected.cue;
492 integrator = {
493 enable = true;
494 signingKeyFile = "/run/agenix/valley-integrator-key";
495 knownSignersFile = "/var/lib/valley-instance/known_signers";
496 instanceProject = "sealed";
497 signerProjects."worker.valley.invalid/attestations" = [
498 "guarded${builtins.fromJSON ''"\u00a0"''}sealed"
499 ];
500 };
501 };
502 };
503
504 # The check worker with its signer restricted to no project list at all:
505 # every controller holding its key would count its evidence everywhere.
506 unrestrictedWorkerHost = workerHost.extendModules {
507 modules = [ { services.valley.integrator.signerProjects = lib.mkForce { }; } ];
508 };
509
510 # The check worker given build capacity of its own: three batches at
511 # once, three checks at once in each, each building two derivations at
512 # once on two cores, read again every five minutes, keeping no outputs,
513 # and evaluating within a budget of its own.
514 capacityWorkerHost = workerHost.extendModules {
515 modules = [
516 {
517 services.valley.worker = {
518 batches = 3;
519 interval = "5min";
520 buildSlots = 3;
521 maxJobs = 2;
522 cores = 2;
523 keepOutputs = null;
524 evalMemoryBudget = "12G";
525 };
526 }
527 ];
528 };
529
530 # The check worker on a host whose builds are not capped: turning it on
531 # must not be what caps every build of the daemon.
532 noSliceWorkerHost = workerHost.extendModules {
533 modules = [ { services.valley.buildSlice.enable = lib.mkForce false; } ];
534 };
535
536 # The worker's signer restricted to a project the worker does not build:
537 # its key would be trusted where it does no work.
538 widerWorkerHost = workerHost.extendModules {
539 modules = [
540 {
541 services.valley.integrator.signerProjects = lib.mkForce {
542 "worker.valley.invalid/attestations" = [
543 "guarded"
544 "released"
545 "sealed"
546 ];
547 };
548 }
549 ];
550 };
551
552 # The worker signing under the controllers' own name.
553 integratorNamedWorkerHost = workerHost.extendModules {
554 modules = [
555 {
556 services.valley.integrator.signingName = "integrator";
557 services.valley.integrator.signerProjects = lib.mkForce {
558 integrator = [
559 "guarded"
560 "sealed"
561 ];
562 };
563 services.valley.worker.signerName = lib.mkForce "integrator";
564 }
565 ];
566 };
567
568 # One key declared untagged and tagged: which principal it pushes as
569 # would depend on which line sshd reads first.
570 conflictingKeysHost = mkHost {
571 services.valley = {
572 config = ../../examples/hosts/protected.cue;
573 authorizedKeys = [
574 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor"
575 {
576 principal = "contributor";
577 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
578 }
579 ];
580 };
581 };
582}