gunk-dev/the-valley

host-module.nix

source223a2a63a778main's published tip history
57.7 KiB
1# What the NixOS modules render from a declaration, and what they must refuse.
2# This covers the valley host module, the web publisher module alongside it,
3# and the web receiver module on an edge host.
4# The refusals are eval-time: a host that must not evaluate is caught by a
5# throw here, since there is nothing to build once it has. What a host does
6# render is pinned by module-eval.sh over the rendered scripts.
7#
8# Every host here runs the stand-ins for this flake's programs
9# (nix/checks/hosts.nix). Nothing here runs a program: each assertion is
10# about the units, hooks and scripts the module renders, which name a
11# program's path and the flags it is given. So module-eval changes when the
12# module, a host declaration or this check does, and not when a program
13# does.
14{
15 pkgs,
16 lib,
17 self,
18 stubbedHosts,
19 ...
20}:
21let
22 inherit (stubbedHosts)
23 unprotectedInstanceHost
24 acceptEnvHost
25 matchAcceptEnvHost
26 wildcardAcceptEnvHost
27 quotedAcceptEnvHost
28 tabAcceptEnvHost
29 setEnvHost
30 permitUserEnvironmentHost
31 conflictingKeysHost
32 host
33 noBackupHost
34 noSecretsHost
35 protectedHost
36 busHost
37 mirrorHost
38 integratorHost
39 integratorSelfHost
40 restrictedHost
41 spacedSignerProjectsHost
42 unicodeSpacedSignerProjectsHost
43 identityHost
44 workerHost
45 buildSliceHost
46 noSliceWorkerHost
47 unrestrictedWorkerHost
48 widerWorkerHost
49 integratorNamedWorkerHost
50 restackerHost
51 unservedRestackerHost
52 keylessRestackerHost
53 ;
54
55 # The web publisher's module (nix/web-publisher.nix) beside the host
56 # module, over integratorHost: its instance repository is the one the
57 # controllers read the floor from. The host also has a backup key the
58 # publisher must not read. The publisher is a stand-in, as the host's
59 # programs are. webPublisherOffHost imports the module and leaves it off.
60 webPublisherStub = pkgs.writeShellScriptBin "valley-web-publish" ''
61 echo "valley-web-publish: a stand-in the module checks render hosts over, which runs nothing" >&2
62 exit 1
63 '';
64 webPublisherOffHost = stubbedHosts.integratorHost.extendModules {
65 modules = [
66 self.nixosModules.web-publisher
67 { services.valley.webPublisher.package = webPublisherStub; }
68 ];
69 };
70 webPublisherHost = webPublisherOffHost.extendModules {
71 modules = [
72 {
73 networking.hostName = "valley-host";
74 services.valley.backup.sshKeyFile = "/run/agenix/valley-git-ssh-key";
75 services.valley.webPublisher = {
76 enable = true;
77 destination = "example.org";
78 receiver = "https://example.org";
79 receiverTokenFile = "/run/keys/valley-web-receiver-token";
80 allowedAddresses = [
81 "192.0.2.10"
82 "2001:db8::10"
83 ];
84 principals = {
85 operator = "operator";
86 alice = "agent";
87 };
88 };
89 }
90 ];
91 };
92 # The publisher on a host whose declaration asks for a backup.
93 backupWebPublisherHost = webPublisherHost.extendModules {
94 modules = [
95 {
96 services.valley.config = lib.mkForce (
97 pkgs.runCommand "protected-with-backup.cue" { } ''
98 cat ${../../examples/hosts/protected.cue} > $out
99 printf '\nbackup: target: "restic-sftp"\n' >> $out
100 ''
101 );
102 services.valley.backup = {
103 repositoryFile = "/run/agenix/valley-restic-repo";
104 passwordFile = "/run/agenix/valley-restic-password";
105 knownHostsFile = "/var/lib/valley-backup/known_hosts";
106 };
107 }
108 ];
109 };
110 # The publisher reading the registry at a ref the instance repository's
111 # protection does not cover.
112 unprotectedRegistryWebPublisherHost = webPublisherHost.extendModules {
113 modules = [ { services.valley.identity.ref = "refs/heads/registry"; } ];
114 };
115 # The publisher reading the registry at a short ref name, which git would
116 # resolve by trying other names in turn.
117 shortRegistryRefWebPublisherHost = webPublisherHost.extendModules {
118 modules = [ { services.valley.identity.ref = "registry"; } ];
119 };
120 # The publisher with no instance repository to discover the projects and
121 # the signers through.
122 noInstanceWebPublisherHost = webPublisherHost.extendModules {
123 modules = [
124 {
125 services.valley.integrator = {
126 instanceProject = lib.mkForce null;
127 instancePolicy = "/var/lib/valley-instance/policy/instance";
128 };
129 }
130 ];
131 };
132
133 # The web receiver module (nix/web-receiver.nix) on a standalone edge
134 # host, evaluated with stand-in binaries. webReceiverOffHost imports the
135 # module while leaving it disabled.
136 edgeStub =
137 name:
138 pkgs.writeShellScriptBin name ''
139 echo "${name}: a stand-in the module checks render hosts over, which runs nothing" >&2
140 exit 1
141 '';
142 webReceiverOffHost = lib.nixosSystem {
143 inherit pkgs;
144 modules = [
145 self.nixosModules.web-receiver
146 {
147 fileSystems."/" = {
148 device = "/dev/disk/by-label/nixos";
149 fsType = "ext4";
150 };
151 boot.loader.grub.enable = false;
152 system.stateVersion = "25.11";
153 services.valley.webReceiver = {
154 receiverPackage = edgeStub "valley-web-receive";
155 webPackage = edgeStub "valley-web";
156 };
157 }
158 ];
159 };
160 webReceiverHost = webReceiverOffHost.extendModules {
161 modules = [
162 {
163 services.valley.webReceiver = {
164 enable = true;
165 instance = "valley-host";
166 destination = "example.org";
167 verifiers = [ "valley-host/web-export+1a2b3c4d+AQ" ];
168 tokenFile = "/run/keys/valley-web-receiver-token";
169 listen = "192.0.2.10:8081";
170 webListen = "127.0.0.1:8080";
171 };
172 }
173 ];
174 };
175 # The receiver and valley-web with readers signing in at an origin.
176 webReaderHost = webReceiverHost.extendModules {
177 modules = [ { services.valley.webReceiver.origin = "https://example.org"; } ];
178 };
179 # The receiver with its token in the Nix store, and with no verifier.
180 storeTokenWebReceiverHost = webReceiverHost.extendModules {
181 modules = [
182 { services.valley.webReceiver.tokenFile = lib.mkForce "${pkgs.writeText "token" "x"}"; }
183 ];
184 };
185 noVerifierWebReceiverHost = webReceiverHost.extendModules {
186 modules = [ { services.valley.webReceiver.verifiers = lib.mkForce [ ]; } ];
187 };
188 # The publisher allowed to connect to no address.
189 noAddressWebPublisherHost = webPublisherHost.extendModules {
190 modules = [ { services.valley.webPublisher.allowedAddresses = lib.mkForce [ ]; } ];
191 };
192 # The publisher allowed the canary its unit's check must be denied.
193 canaryWebPublisherHost = webPublisherHost.extendModules {
194 modules = [ { services.valley.webPublisher.allowedAddresses = [ "198.51.100.0/24" ]; } ];
195 };
196 # The publisher with its token in the Nix store.
197 storeTokenWebPublisherHost = webPublisherHost.extendModules {
198 modules = [
199 { services.valley.webPublisher.receiverTokenFile = lib.mkForce "${pkgs.writeText "token" "x"}"; }
200 ];
201 };
202
203 failedAssertions = builtins.filter (a: !a.assertion) (
204 host.config.assertions
205 ++ noBackupHost.config.assertions
206 ++ busHost.config.assertions
207 ++ mirrorHost.config.assertions
208 ++ protectedHost.config.assertions
209 ++ integratorHost.config.assertions
210 ++ integratorSelfHost.config.assertions
211 ++ restrictedHost.config.assertions
212 ++ identityHost.config.assertions
213 ++ workerHost.config.assertions
214 ++ restackerHost.config.assertions
215 ++ buildSliceHost.config.assertions
216 ++ webPublisherHost.config.assertions
217 ++ webPublisherOffHost.config.assertions
218 ++ webReceiverHost.config.assertions
219 ++ webReaderHost.config.assertions
220 ++ webReceiverOffHost.config.assertions
221 );
222
223 missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions;
224
225 # The hosts the module must refuse, each with the words its refusal must
226 # say. An assertion that stopped firing would leave the push policy
227 # quietly wrong, so each is held to naming what is wrong.
228 refusedHosts = [
229 {
230 host = unprotectedInstanceHost;
231 says = "whose refs/heads/main is not protected";
232 why = "an instance repository without protection would let any key that can push rewrite the floor and the registry";
233 }
234 {
235 host = acceptEnvHost;
236 says = "AcceptEnv";
237 why = "sshd accepting the principal variable from the client would let any key name any principal";
238 }
239 {
240 host = matchAcceptEnvHost;
241 says = "GIT_CONFIG_*";
242 why = "sshd accepting GIT_ variables in the git user's Match block, in whatever case the keyword is written, would let a client set git's configuration";
243 }
244 {
245 host = wildcardAcceptEnvHost;
246 says = "G?T_*";
247 why = "a wildcard that admits GIT_ variables admits them however it is spelled";
248 }
249 {
250 host = quotedAcceptEnvHost;
251 says = "holds a quote or a backslash";
252 why = "a quoted name is one sshd unquotes and admits, so a check that read it differently would pass it";
253 }
254 {
255 host = tabAcceptEnvHost;
256 says = "VALLEY_PRINCIPAL from the client";
257 why = "a name after a tab is a name sshd admits";
258 }
259 {
260 host = setEnvHost;
261 says = "SetEnv";
262 why = "sshd setting the principal itself would make every key push as it";
263 }
264 {
265 host = permitUserEnvironmentHost;
266 says = "PermitUserEnvironment";
267 why = "a PermitUserEnvironment wider than the principal variable would let a key's entry or ~/.ssh/environment set what the push policy does not expect";
268 }
269 {
270 host = spacedSignerProjectsHost;
271 says = "services.valley.integrator.signerProjects holds";
272 why = "a project with a space in it would be read back as two projects the declaration never listed";
273 }
274 {
275 host = unicodeSpacedSignerProjectsHost;
276 says = "services.valley.integrator.signerProjects holds";
277 why = "a project holding a no-break space would be read back by the integrator as two projects, or refused at start";
278 }
279 {
280 host = noSliceWorkerHost;
281 says = "services.valley.worker.enable needs services.valley.buildSlice.enable";
282 why = "a worker on a host without the build slice would build uncapped, and capping every build of the daemon is the host's decision, not the worker's";
283 }
284 {
285 host = unrestrictedWorkerHost;
286 says = "must restrict worker.valley.invalid/attestations, the worker's signer, to exactly the worker's projects";
287 why = "a worker whose signer no project list restricts would have its evidence counted in every project by every controller that holds its key";
288 }
289 {
290 host = widerWorkerHost;
291 says = "must restrict worker.valley.invalid/attestations, the worker's signer, to exactly the worker's projects";
292 why = "a worker trusted in a project it does not build would be trusted where it does no work";
293 }
294 {
295 host = integratorNamedWorkerHost;
296 says = "services.valley.worker.signerName must differ from integrator";
297 why = "a worker signing under the controllers' name would make every controller refuse to start";
298 }
299 {
300 host = unservedRestackerHost;
301 says = "services.valley.restacker.projects must name at least one project, and only projects this host serves, and it names nowhere";
302 why = "a controller for a project the host does not serve would fail every pass";
303 }
304 {
305 host = keylessRestackerHost;
306 says = "services.valley.restacker.sshKeyFile must be set";
307 why = "a restacker with no key cannot reach the repositories it serves, except as a user that could write them on disk";
308 }
309 {
310 host = conflictingKeysHost;
311 says = "more than one principal tag";
312 why = "a key declared under two tags would push as whichever entry sshd reads first";
313 }
314 {
315 host = unprotectedRegistryWebPublisherHost;
316 says = "whose refs/heads/registry is not protected";
317 why = "a publisher reading the registry at an unprotected ref would let any key that can push there grant anyone read";
318 }
319 {
320 host = shortRegistryRefWebPublisherHost;
321 says = "services.valley.identity.ref is registry, which is not a full ref name";
322 why = "git resolves a ref that is not a full name by trying several refs in turn, and a pusher can make one of them";
323 }
324 {
325 host = storeTokenWebPublisherHost;
326 says = "services.valley.webPublisher.receiverTokenFile is in the Nix store";
327 why = "the publisher's receiver token is in the Nix store";
328 }
329 {
330 host = noAddressWebPublisherHost;
331 says = "services.valley.webPublisher.allowedAddresses is empty: list the receiver's IP addresses";
332 why = "a publisher with no allowed address would either reach nothing or, allowed everything, reach any host";
333 }
334 {
335 host = canaryWebPublisherHost;
336 says = "services.valley.webPublisher.allowedAddresses covers 198.51.100.1";
337 why = "the check before each pass could not prove the filter in force if the filter allowed its canary";
338 }
339 {
340 host = storeTokenWebReceiverHost;
341 says = "services.valley.webReceiver.tokenFile is in the Nix store";
342 why = "the receiver's token is in the Nix store";
343 }
344 {
345 host = noVerifierWebReceiverHost;
346 says = "services.valley.webReceiver.verifiers names no verifier line";
347 why = "the receiver pins no verifier";
348 }
349 {
350 host = noInstanceWebPublisherHost;
351 says = "services.valley.webPublisher needs services.valley.integrator.instanceProject";
352 why = "a publisher with no instance repository has no publication to discover the projects and their signers through";
353 }
354 ];
355
356 unrefusedHosts = builtins.filter (
357 r: !(lib.any (a: !a.assertion && lib.hasInfix r.says a.message) r.host.config.assertions)
358 ) refusedHosts;
359
360 protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys;
361
362 resticRenderedWithoutDeclaration =
363 noBackupHost.config.systemd.services ? "restic-backups-valley"
364 || noBackupHost.config.systemd.timers ? "restic-backups-valley";
365
366 # The bus defaults off; a host that never asked for one must
367 # render zero bus machinery.
368 busRenderedWithoutEnable =
369 noBackupHost.config.systemd.services ? valley-bus
370 || noBackupHost.config.systemd.services ? valley-bus-init;
371
372 # A declaration with no protection block and no tagged key still gets
373 # the push policy on every project — the policy is the host's, and a
374 # protection block only adds protected refs to it — and nothing of the
375 # principal machinery: no tag on a key, no sshd change.
376 protectionRenderedWithoutDeclaration =
377 noBackupHost.config.services.openssh.settings ? PermitUserEnvironment
378 || lib.any (lib.hasInfix "environment=") (
379 noBackupHost.config.users.users.git.openssh.authorizedKeys.keys
380 );
381
382 policyMissingWithoutDeclaration =
383 !(lib.hasInfix "valley-protect-the-valley" noBackupHost.config.systemd.services.valley-init.script);
384
385 # The integrator's units. What a controller *does* is not checked here
386 # and no VM is booted: that is integrator-e2e's job (the real binaries
387 # over real repositories) and, past it, live exercise on a host. These
388 # assertions cover the wiring — that a controller is rendered for what
389 # the declaration protects and for nothing else, under an identity that
390 # is not the git user's, pointed at the key and the signers the consumer
391 # supplied.
392 #
393 # There is a seam between those two halves that neither reaches, and in a
394 # single day it produced five deployment failures. Every assertion below is
395 # an eval of the unit's text; integrator-e2e runs the binaries with no unit
396 # around them at all. What sits between is the unit's sandbox and identity
397 # meeting the filesystem the binaries actually use at runtime:
398 #
399 # - git refusing a repository the service does not own (bd-500adf7)
400 # - a push authenticating as the operator rather than as the run
401 # (bd-353c59d)
402 # - no writable temporary directory, so no worktree could be made
403 # - the worktree metadata root, created by whichever service reached it
404 # first and then unchmodable by the other, which failed valley-init and
405 # took every valley unit down with it
406 # - the state directory not writable under ProtectSystem=strict unless
407 # the unit names it, which the previous fix had made the scratch root
408 #
409 # Each was found by a host, minutes of a live service after a deploy that
410 # every check here passed. They compose: the third's fix set up the fourth,
411 # and the fourth's fix set up the fifth. That is the argument. A seam whose
412 # failures feed each other is not one to pin an assertion at a time, and
413 # each assertion here that marks a piece of it — the git ownership
414 # exception, TMPDIR, the worktrees root, ReadWritePaths — marks a place a
415 # smoke boot would have spoken first.
416 #
417 # A nixosTest that boots one host, starts the units and reconciles one
418 # change end to end is the next check this module needs. Nothing else
419 # closes the seam, and five incidents is enough evidence of what it costs
420 # to leave open. Not built here.
421 #
422 # Protection and integration are separable declarations: protectedHost
423 # declares the same protected projects with the service off, and must
424 # evaluate to no controller at all.
425 integratorRenderedWithoutEnable =
426 protectedHost.config.systemd.services ? "valley-integrator@"
427 || protectedHost.config.systemd.targets ? valley-integrators
428 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
429
430 # One target pulls in each controller, and another the path unit that
431 # publishes each project's queued moves, landings included.
432 integratorWants = integratorHost.config.systemd.targets.valley-integrators.wants;
433 controllers = lib.filter (lib.hasPrefix "valley-integrator@") integratorWants;
434 publishers = integratorHost.config.systemd.targets.valley-publish.wants;
435
436 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
437
438 # The controller is not the repositories' owner, which is the whole point
439 # of running it as itself — so git needs an ownership exception, and
440 # where that exception lives is load-bearing. GIT_CONFIG_* is command
441 # scope, the only scope besides system and global that safe.directory is
442 # read from, and the unit's environment is inherited by every git the
443 # controller drives. It names one repository: a wildcard here would hand
444 # a compromised controller every repository on the host.
445 integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment;
446
447 # The count is what git reads; an entry past it is an entry git ignores,
448 # silently. So it is checked against the entries actually rendered rather
449 # than against a number written here, which would have to be edited every
450 # time a grant is added and would go on passing if it were not.
451 integratorSafeDirectory =
452 integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory"
453 || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git"
454 || integratorGitEnv.GIT_CONFIG_COUNT or null
455 != toString (
456 lib.length (lib.filter (lib.hasPrefix "GIT_CONFIG_KEY_") (lib.attrNames integratorGitEnv))
457 );
458
459 integratorService = integratorHost.config.systemd.services."valley-integrator@".serviceConfig;
460
461 # Scratch goes under the state directory, because the sandbox leaves no
462 # writable temporary directory and a verdict needs a checkout on disk. Two
463 # halves, and each was a live failure on its own. TMPDIR naming a path
464 # outside the state directory is the first; the state directory not being
465 # named in ReadWritePaths is the second, which failed even though
466 # ProtectSystem=strict is documented to leave a declared state directory
467 # writable. Everything the unit may write is enumerated, so what a reader
468 # of the unit sees is the whole of it.
469 # The floor is read from the instance repository's integrated tip
470 # (dcr-f41f718, dcr-b87f6e8), so a controller is told which repository
471 # carries it and reads it there. Three things have to be true of that, and
472 # each was a way for it to be silently wrong.
473 #
474 # The controller has to be told at all — the failure this replaced was a
475 # binary looking for a materialized directory nothing populated.
476 #
477 # It has to work when the repository carrying the floor is not the one the
478 # controller serves, which is the ordinary case: on integratorHost every
479 # controller serves a protected project and reads the floor out of
480 # "open", which no controller serves. git refuses a repository the caller
481 # does not own, so that read needs its own safe.directory entry, named and
482 # not wildcarded — the same failure as the served repository's, one
483 # repository over.
484 #
485 # And it has to keep working when the two coincide, which is what an
486 # instance's own host looks like: integratorSelfHost's controller for
487 # "guarded" reads the floor from "guarded".
488 integratorFloor =
489 let
490 command = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
491 selfCommand = integratorSelfHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
492 env = integratorHost.config.systemd.services."valley-integrator@".environment;
493 in
494 !(lib.hasInfix "--instance-repo /srv/git/sealed.git" command)
495 || !(lib.hasInfix "--instance-repo /srv/git/guarded.git" selfCommand)
496 || env.GIT_CONFIG_COUNT or null != "2"
497 || env.GIT_CONFIG_KEY_1 or null != "safe.directory"
498 || env.GIT_CONFIG_VALUE_1 or null != "/srv/git/sealed.git";
499
500 integratorScratch =
501 integratorGitEnv.TMPDIR or null != "/var/lib/${integratorService.StateDirectory}"
502 || !(builtins.elem "/var/lib/${integratorService.StateDirectory}" integratorService.ReadWritePaths);
503
504 # The registry compiler. It is off by default and must stay inert: a
505 # consumer that has not asked for it renders the system it always did.
506 # integratorHost is the strongest witness available — the integrator on,
507 # the signers file supplied by hand, and every option of the identity
508 # block left alone.
509 identityRenderedWithoutEnable =
510 integratorHost.config.systemd.services ? valley-identity
511 || integratorHost.config.systemd.timers ? valley-identity
512 || integratorHost.config.services.openssh.settings ? PermitUserEnvironment
513 || lib.any (lib.hasInfix "valley-identity") integratorHost.config.services.openssh.authorizedKeysFiles
514 || lib.hasInfix "valley-identity" integratorHost.config.services.openssh.extraConfig
515 || lib.hasInfix "valley-identity" (
516 integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
517 )
518 || lib.hasInfix "--name " (
519 integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
520 );
521
522 identityService = identityHost.config.systemd.services.valley-identity.serviceConfig;
523
524 identityStateDir = "/var/lib/${identityService.StateDirectory or ""}";
525
526 # What the compiled artifacts have to be true of, and each was a way for
527 # the machinery to be quietly wrong.
528 #
529 # The compiler writes into its state directory and nowhere else, so that
530 # directory has to be the one path the unit may write.
531 #
532 # sshd walks every directory above an authorized-keys file and refuses a
533 # group- or world-writable one, so the state directory is 0755 rather than
534 # the 0700 a service's state usually takes — and the unit runs as the git
535 # user, because sshd equally refuses a file owned by anyone but root or
536 # the account it authorizes.
537 #
538 # The compiled file is added to what sshd reads for the git user rather
539 # than replacing it, and for the git user alone: the user's Match block
540 # names the declared keys and the compiled ones, and nothing else — no
541 # ~/.ssh/authorized_keys, no AuthorizedKeysCommand — so every key the git
542 # user accepts carries a tag this module or the compiler wrote. The
543 # compiler is handed the declared keys, so it can refuse a registry key
544 # declared under another tag.
545 identityCompiler =
546 identityService.User or null != identityHost.config.services.valley.user
547 || identityService.StateDirectoryMode or null != "0755"
548 || identityService.ReadWritePaths or [ ] != [ identityStateDir ]
549 || !(lib.hasInfix "--repo /srv/git/sealed.git" identityService.ExecStart)
550 || !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" identityService.ExecStart)
551 || !(lib.hasInfix "--authorized-keys ${identityStateDir}/authorized_keys.git" identityService.ExecStart)
552 || !(lib.hasInfix "--grants ${identityStateDir}/grants" identityService.ExecStart)
553 || !(lib.hasInfix "-valley-declared-keys" identityService.ExecStart)
554 || !(lib.hasInfix "AuthorizedKeysFile /etc/ssh/authorized_keys.d/%u ${identityStateDir}/authorized_keys.git\n" identityHost.config.services.openssh.extraConfig)
555 || !(lib.hasInfix "AuthorizedKeysCommand none" identityHost.config.services.openssh.extraConfig);
556
557 # A controller under a compiled registry reads the compiled signers file
558 # and signs under the name the registry publishes its key under. The key
559 # hash binds name to key, so a controller signing under the host's default
560 # name while the registry publishes it as "integrator" would produce notes
561 # no verifier can find a key for.
562 identityIntegrator =
563 let
564 command = identityHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
565 in
566 !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" command)
567 || !(lib.hasInfix "--name integrator" command)
568 || !(builtins.elem "valley-identity.service"
569 identityHost.config.systemd.services."valley-integrator@".after
570 );
571
572 # The signers publication. A host whose integrator runs and which serves
573 # the instance repository publishes there what a client verifies its
574 # transfer statements with. What it publishes has to be what the
575 # controllers use: the same signers file and the same --name, or none on
576 # either, in which case both take the binary's default. The publisher
577 # writes as the git user, which owns the instance repository, may write
578 # that repository and nothing else, runs after the compilation when there
579 # is one, and runs again whenever the signers file changes. The signer
580 # projects file is the controllers' too, or neither has one.
581 flagValue =
582 flag: command:
583 let
584 m = builtins.match ".*--${flag} ([^ ]+).*" command;
585 in
586 if m == null then null else lib.head m;
587
588 signersPublisher =
589 host: repo:
590 let
591 unit = host.config.systemd.services.valley-signers;
592 publish = unit.serviceConfig.ExecStart;
593 controller = host.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
594 in
595 !(lib.hasInfix "/bin/integrator publish-signers " publish)
596 || flagValue "instance-repo" publish != repo
597 || flagValue "known-signers" publish != flagValue "known-signers" controller
598 || flagValue "name" publish != flagValue "name" controller
599 || flagValue "signer-projects" publish != flagValue "signer-projects" controller
600 || unit.serviceConfig.User or null != "git"
601 || unit.serviceConfig.ReadWritePaths or [ ] != [ repo ]
602 || !(builtins.elem "valley-init.service" unit.after)
603 || !(builtins.elem "multi-user.target" unit.wantedBy)
604 || host.config.systemd.paths.valley-signers.pathConfig.PathChanged or null
605 != flagValue "known-signers" controller
606 || !(builtins.elem "multi-user.target" host.config.systemd.paths.valley-signers.wantedBy);
607
608 signersRenderedWithoutIntegrator =
609 protectedHost.config.systemd.services ? valley-signers
610 || protectedHost.config.systemd.paths ? valley-signers;
611
612 signersUnpublished =
613 signersPublisher integratorHost "/srv/git/sealed.git"
614 || signersPublisher restrictedHost "/srv/git/sealed.git"
615 || signersPublisher integratorSelfHost "/srv/git/guarded.git"
616 || signersPublisher identityHost "/srv/git/sealed.git"
617 || flagValue "name" identityHost.config.systemd.services.valley-signers.serviceConfig.ExecStart
618 != "integrator"
619 || !(builtins.elem "valley-identity.service" identityHost.config.systemd.services.valley-signers.after);
620
621 # A host that restricts no signer gives its controllers no signer
622 # projects file, and one that restricts some gives them one.
623 signerProjectsMisrendered =
624 flagValue "signer-projects" integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
625 != null
626 || flagValue "signer-projects" restrictedHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
627 == null;
628
629 # The declared keys stay authorized beside the compiled ones. A compiled
630 # file that replaced them would make a bad compilation — or a first boot
631 # before the first one — a locked-out git user, which is the one failure
632 # this machinery must never have.
633 identityKeepsDeclaredKeys =
634 identityHost.config.users.users.git.openssh.authorizedKeys.keys == [ ];
635
636 # The check worker and the build slice. Both are off by default, and a
637 # host that never asked for them renders none of either: no unit, no
638 # account, no slice, and a nix daemon where NixOS puts it. integratorHost
639 # is the strongest witness, with the integrator the worker would need
640 # already on.
641 workerRenderedWithoutEnable =
642 integratorHost.config.systemd.services ? valley-worker
643 || integratorHost.config.systemd.timers ? valley-worker
644 || integratorHost.config.systemd.slices ? valley-builds
645 || integratorHost.config.users.users ? valley-worker
646 || integratorHost.config.systemd.services.nix-daemon.serviceConfig ? Slice;
647
648 # The build slice is a host decision of its own. On, it puts the daemon
649 # in the slice at the caps the host names, with no swap, and nothing of
650 # the worker.
651 buildSliceMisrendered =
652 let
653 slice = buildSliceHost.config.systemd.slices.valley-builds.sliceConfig or { };
654 in
655 buildSliceHost.config.systemd.services.nix-daemon.serviceConfig.Slice or null
656 != "valley-builds.slice"
657 || slice.CPUQuota or null != "200%"
658 || slice.MemoryMax or null != "4G"
659 || slice.MemorySwapMax or null != "0"
660 || buildSliceHost.config.systemd.services ? valley-worker
661 || buildSliceHost.config.users.users ? valley-worker;
662
663 # What the VM check runs, held at the operator's values. worker-e2e
664 # boots the worker and lowers the memory cap at runtime to make a build
665 # run past it, so the values themselves are held here. The caps are on
666 # a slice that holds the worker and the nix daemon both, since a build
667 # runs in the daemon's cgroup and never in the worker's; the slice and
668 # the daemon set no device policy, so a NixOS test still gets /dev/kvm.
669 # The worker runs on one CPU, so attest, which runs one nix build per CPU
670 # it may run on, runs one at a time, and each nix build builds one
671 # derivation at a time with 4 cores. The account is not in the git
672 # group, so the repositories on disk are not the worker's to read or
673 # write, and it reaches them only through this host's sshd, checking the
674 # host's key. Its timer starts the first pass five minutes after the
675 # timer starts, not after boot, and each later pass one minute after the
676 # last one ended.
677 workerMisrendered =
678 let
679 unit = workerHost.config.systemd.services.valley-worker;
680 service = unit.serviceConfig;
681 daemon = workerHost.config.systemd.services.nix-daemon.serviceConfig;
682 slice = workerHost.config.systemd.slices.valley-builds.sliceConfig;
683 account = workerHost.config.users.users.valley-worker;
684 timer = workerHost.config.systemd.timers.valley-worker;
685 devicePolicy = c: c ? DevicePolicy || c ? DeviceAllow || c ? PrivateDevices;
686 in
687 service.User or null != "valley-worker"
688 || account.group != "valley-worker"
689 || account.extraGroups != [ ]
690 || service.Type or null != "oneshot"
691 || service.Slice or null != "valley-builds.slice"
692 || daemon.Slice or null != "valley-builds.slice"
693 || slice.CPUQuota or null != "400%"
694 || slice.MemoryMax or null != "8G"
695 || slice.MemorySwapMax or null != "0"
696 || devicePolicy slice
697 || devicePolicy daemon
698 || service.CPUAffinity or null != "0"
699 || !(lib.hasInfix "max-jobs = 1\n" unit.environment.NIX_CONFIG)
700 || !(lib.hasInfix "cores = 4\n" unit.environment.NIX_CONFIG)
701 || !(lib.hasInfix "builders =\n" unit.environment.NIX_CONFIG)
702 || !(lib.hasInfix "-o StrictHostKeyChecking=yes -o HostKeyAlias=localhost" unit.environment.GIT_SSH_COMMAND)
703 || service.ReadWritePaths or [ ] != [
704 "/var/lib/valley-worker"
705 "/nix/var/nix/daemon-socket"
706 ]
707 || !(lib.hasInfix "exec valley-worker --repo guarded --repo sealed --key \"$key\" --name worker.valley.invalid/attestations --state \"$state\"" unit.script)
708 || !(lib.hasInfix "git -C \"$origin\" remote add origin git@localhost:sealed.git\n" unit.script)
709 || timer.timerConfig.OnActiveSec or null != "5m"
710 || timer.timerConfig ? OnBootSec
711 || timer.timerConfig.OnUnitInactiveSec or null != "1m"
712 || !(builtins.elem "timers.target" timer.wantedBy);
713
714 # The restacker. When disabled, it renders no units or user account on
715 # integratorHost. When enabled, each configured project gets a
716 # valley-restacker@<project>.service running valley-restacker run as the
717 # valley-restacker user in its own group, with no access to the bare
718 # repositories on disk. It connects through localhost sshd as its
719 # principal using the key passed via LoadCredential, verifies the host's
720 # own SSH key, has its 0700 state directory as its only persistent
721 # writable path (its private temporary directories are writable too),
722 # and reads the floor from the instance repository over the same SSH
723 # connection.
724 restackerRenderedWithoutEnable =
725 integratorHost.config.systemd.services ? "valley-restacker@"
726 || integratorHost.config.systemd.targets ? valley-restackers
727 || integratorHost.config.users.users ? valley-restacker;
728
729 restackerMisrendered =
730 let
731 unit = restackerHost.config.systemd.services."valley-restacker@";
732 service = unit.serviceConfig;
733 account = restackerHost.config.users.users.valley-restacker;
734 target = restackerHost.config.systemd.targets.valley-restackers;
735 command = service.ExecStart;
736 in
737 service.User or null != "valley-restacker"
738 || service.Group or null != "valley-restacker"
739 || account.group != "valley-restacker"
740 || account.extraGroups != [ ]
741 || service.LoadCredential or [ ] != [ "key:/run/agenix/valley-restacker-key" ]
742 || service.StateDirectory or null != "valley-restacker/%i"
743 || service.StateDirectoryMode or null != "0700"
744 || service.ProtectSystem or null != "strict"
745 || service ? ReadWritePaths
746 || !(lib.hasInfix " -i %d/key " unit.environment.GIT_SSH_COMMAND)
747 || !(lib.hasInfix "-o StrictHostKeyChecking=yes -o HostKeyAlias=localhost" unit.environment.GIT_SSH_COMMAND)
748 || !(lib.hasInfix "-o UserKnownHostsFile=%S/valley-restacker/%i/known_hosts -o GlobalKnownHostsFile=/dev/null" unit.environment.GIT_SSH_COMMAND)
749 || !(lib.hasInfix "/bin/valley-restacker run --repo git@localhost:%i.git --state %S/valley-restacker/%i --principal valley-restacker --interval 15s " command)
750 || !(lib.hasInfix " --instance-repo git@localhost:sealed.git" command)
751 || target.wants != [ "valley-restacker@guarded.service" ]
752 || !(builtins.elem "multi-user.target" target.wantedBy);
753
754 # The web publisher (nix/web-publisher.nix). Off, it renders nothing.
755 # On, one pass is a oneshot on a five-minute timer whose first pass waits
756 # five minutes after the timer starts, and which no switch starts or
757 # restarts. It runs as the dynamic user valley-web-publish, which the
758 # policy path's unit shares, in the git group, with an empty
759 # safe.directory entry that resets the system's and the user's, then an
760 # exact entry for each repository the host serves and no wildcard, the
761 # repositories read-only, nothing writable but its state and cache, and
762 # the mirror, backup and signing keys out of reach. It may connect to the
763 # addresses it is allowed and to no other, and checks before each pass that
764 # this filter is in force. Its Go lookups use the C library. Its memory is
765 # capped with no swap. It reads the instance repository the controllers
766 # read, names the host as the instance, and is given the receiver, the
767 # destination, the display mapping and the bounds on its bundles and its
768 # storage.
769 webPublisherRenderedWithoutEnable =
770 webPublisherOffHost.config.systemd.services ? valley-web-publish
771 || webPublisherOffHost.config.systemd.services ? valley-web-publish-policy
772 || webPublisherOffHost.config.systemd.timers ? valley-web-publish
773 || lib.any (p: (p.name or "") == "valley-web-link") webPublisherOffHost.config.environment.systemPackages;
774
775 # The read links: the policy path's own unit, the same pass with
776 # --policy-only in the same sandbox, as the same dynamic user, so the two
777 # share their state and cache directories, and with the same
778 # credentials; the
779 # credential file read live by both through a static group, root's to
780 # write, under the links lock tmpfiles makes beside it, which only root
781 # can make or replace; the operator's wrapper, with the unit's
782 # safe.directory entries, passing issue alone the flags for reading the
783 # registry, and starting the policy pass after every issue or revoke,
784 # and once more while a link is pending; and the backup holding the
785 # file's directory and the publisher's durable state beside the data
786 # directory.
787 webPublisherLinksMisrendered =
788 let
789 units = webPublisherHost.config.systemd.services;
790 pass = units.valley-web-publish.serviceConfig;
791 policy = units.valley-web-publish-policy.serviceConfig or { };
792 wrapper = lib.findFirst (
793 p: (p.name or "") == "valley-web-link"
794 ) null webPublisherHost.config.environment.systemPackages;
795 in
796 lib.filter (x: x != null) (lib.imap0 (i: c: if c then i else null) [
797 (!(units ? valley-web-publish-policy))
798 (lib.any (u: u.DynamicUser or null != true || u.User or null != "valley-web-publish" || u.Group or null != "valley-web-publish") [
799 pass
800 policy
801 ])
802 (policy.ExecStart or null != "${pass.ExecStart} --policy-only")
803 (removeAttrs policy [ "ExecStart" ] != removeAttrs pass [ "ExecStart" ])
804 (units.valley-web-publish-policy.environment != units.valley-web-publish.environment)
805 (!(lib.hasSuffix " --links /var/lib/valley-web-links/links" pass.ExecStart))
806 (!(lib.all (r: builtins.elem r webPublisherHost.config.systemd.tmpfiles.rules) [
807 "d /var/lib/valley-web-links 0750 root valley-web-links -"
808 "f /var/lib/valley-web-links/links 0640 root valley-web-links -"
809 "f /var/lib/valley-web-links/lock 0640 root valley-web-links -"
810 ]))
811 (!(webPublisherHost.config.users.groups ? valley-web-links))
812 (wrapper == null)
813 (!(lib.hasInfix "valley-web-publish link \"$@\" --links /var/lib/valley-web-links/links \\\n --state /var/lib/valley-web-publish\n}" wrapper.text or ""))
814 (!(lib.hasInfix "issue)\n link \"$@\" --instance-repo /srv/git/sealed.git" wrapper.text or ""))
815 (!(lib.hasInfix "revoke) link \"$@\" ;;" wrapper.text or ""))
816 (!(lib.all (e: lib.hasInfix e wrapper.text or "") [
817 "ulimit -c 0\n"
818 "export GIT_CONFIG_COUNT=5\n"
819 "export GIT_CONFIG_VALUE_0=''\n"
820 "export GIT_CONFIG_VALUE_4=/srv/git/sealed.git\n"
821 "systemctl start valley-web-publish-policy.service"
822 "deliver\nif ! link list --check > /dev/null 2>&1; then\n deliver\nfi\nif link list --check > /dev/null; then"
823 ]))
824 (backupWebPublisherHost.config.services.restic.backups.valley.paths or [ ] != [
825 "/srv/git"
826 "/var/lib/valley-web-links"
827 "/var/lib/private/valley-web-publish"
828 ])
829 ]);
830
831 webPublisherMisrendered =
832 let
833 unit = webPublisherHost.config.systemd.services.valley-web-publish;
834 service = unit.serviceConfig;
835 env = unit.environment;
836 timer = webPublisherHost.config.systemd.timers.valley-web-publish;
837 repos = [
838 "/srv/git/guarded.git"
839 "/srv/git/open.git"
840 "/srv/git/released.git"
841 "/srv/git/sealed.git"
842 ];
843 entries = lib.genList (i: {
844 key = env."GIT_CONFIG_KEY_${toString i}" or null;
845 value = env."GIT_CONFIG_VALUE_${toString i}" or null;
846 }) (lib.length (lib.filter (lib.hasPrefix "GIT_CONFIG_KEY_") (lib.attrNames env)));
847 command = service.ExecStart;
848 in
849 service.Type or null != "oneshot"
850 || service.DynamicUser or null != true
851 || service.User or null != "valley-web-publish"
852 || service.Group or null != "valley-web-publish"
853 || service.SupplementaryGroups or [ ] != [
854 "git"
855 "valley-web-links"
856 ]
857 || env.GIT_CONFIG_COUNT or null != toString (lib.length repos + 1)
858 || entries != map (r: {
859 key = "safe.directory";
860 value = r;
861 }) ([ "" ] ++ repos)
862 || lib.any (e: lib.hasInfix "*" e.value) entries
863 || service.ProtectSystem or null != "strict"
864 || service ? ReadWritePaths
865 || service.ReadOnlyPaths or [ ] != map (r: "-${r}") repos
866 || service.StateDirectory or null != "valley-web-publish"
867 || service.CacheDirectory or null != "valley-web-publish"
868 || env.XDG_CACHE_HOME or null != "/var/cache/valley-web-publish"
869 || !(lib.all (p: builtins.elem p service.InaccessiblePaths) [
870 "-/srv/git/.ssh"
871 "-/run/agenix/valley-integrator-key"
872 "-/run/agenix/valley-git-ssh-key"
873 "-/var/lib/valley-integrator"
874 ])
875 || !(lib.hasInfix "/bin/valley-web-publish --instance-repo /srv/git/sealed.git --instance valley-host --destination example.org --host /nix/store/" command)
876 || !(lib.hasInfix "-valley-config.json --receiver https://example.org --receiver-token %d/receiver-token --principals /nix/store/" command)
877 || service.LoadCredential or [ ] != [ "receiver-token:/run/keys/valley-web-receiver-token" ]
878 || !(lib.hasInfix "-valley-web-principals.json --state /var/lib/valley-web-publish --max-bundles 1G --max-storage 16G --registry-ref refs/heads/main --registry-dir identity" command)
879 || !(lib.hasSuffix "/bin/valley-web-publish-egress-check 198.51.100.1 9" (service.ExecStartPre or ""))
880 || env.GODEBUG or null != "netdns=cgo"
881 || service.IPAddressDeny or null != "any"
882 || service.IPAddressAllow or null != [
883 "192.0.2.10"
884 "2001:db8::10"
885 ]
886 || service.RestrictAddressFamilies or null != [
887 "AF_INET"
888 "AF_INET6"
889 "AF_UNIX"
890 ]
891 || service.MemoryMax or null != "1G"
892 || service.MemorySwapMax or null != "0"
893 || unit.restartIfChanged != false
894 || unit.wantedBy != [ ]
895 || timer.timerConfig.OnActiveSec or null != "5min"
896 || timer.timerConfig.OnUnitInactiveSec or null != "5min"
897 || !(builtins.elem "timers.target" timer.wantedBy);
898
899 # The web receiver module (nix/web-receiver.nix). When disabled, it
900 # renders no units or users. When enabled, it renders separate systemd
901 # units for valley-web-receive and valley-web; both start at boot, restart
902 # on failure, and run independently. The receiver runs as
903 # valley-web-receive in group valley-web, owns the mode-0750 state
904 # directory, and takes the instance, the destination, the pinned verifiers
905 # from the Nix store, the bearer token as a systemd credential from outside
906 # the store, and the body and storage caps. valley-web runs as valley-web
907 # in the same group, orders after the receiver, mounts the state directory
908 # read-only with no persistent writable paths, and takes the state
909 # directory and its listener address. Each unit has its own memory cap and
910 # disables swap.
911 webReceiverRenderedWithoutEnable =
912 webReceiverOffHost.config.systemd.services ? valley-web-receive
913 || webReceiverOffHost.config.systemd.services ? valley-web
914 || webReceiverOffHost.config.users.users ? valley-web-receive;
915
916 webReceiverMisrendered =
917 let
918 cfg = webReceiverHost.config;
919 receive = cfg.systemd.services.valley-web-receive;
920 web = cfg.systemd.services.valley-web;
921 confined =
922 unit:
923 unit.serviceConfig.ProtectSystem or null == "strict"
924 && unit.serviceConfig.NoNewPrivileges or null == true
925 && unit.serviceConfig.CapabilityBoundingSet or null == ""
926 && unit.serviceConfig.MemorySwapMax or null == "0"
927 && unit.serviceConfig.Restart or null == "on-failure"
928 && builtins.elem "multi-user.target" unit.wantedBy
929 && unit.requires == [ ]
930 && unit.bindsTo == [ ];
931 in
932 !(confined receive && confined web)
933 || cfg.users.users.valley-web-receive.group != "valley-web"
934 || cfg.users.users.valley-web.group != "valley-web"
935 || receive.serviceConfig.User or null != "valley-web-receive"
936 || receive.serviceConfig.Group or null != "valley-web"
937 || receive.serviceConfig.StateDirectory or null != "valley-web"
938 || receive.serviceConfig.StateDirectoryMode or null != "0750"
939 || receive.serviceConfig.LoadCredential or [ ] != [ "token:/run/keys/valley-web-receiver-token" ]
940 || receive.serviceConfig.MemoryMax or null != "2G"
941 || !(lib.hasInfix "/bin/valley-web-receive --state /var/lib/valley-web/store --listen 192.0.2.10:8081 --instance valley-host --destination example.org --verifiers /nix/store/" receive.serviceConfig.ExecStart)
942 || !(lib.hasSuffix "-valley-web-verifiers --token-file %d/token --max-body 1G --max-upload 2G --max-storage 8G" receive.serviceConfig.ExecStart)
943 || web.serviceConfig.User or null != "valley-web"
944 || web.serviceConfig.Group or null != "valley-web"
945 || web.serviceConfig ? StateDirectory
946 || web.serviceConfig ? ReadWritePaths
947 || web.serviceConfig ? LoadCredential
948 || web.serviceConfig.ReadOnlyPaths or [ ] != [ "-/var/lib/valley-web" ]
949 || web.serviceConfig.MemoryMax or null != "1G"
950 || !(builtins.elem "valley-web-receive.service" web.after)
951 || !(lib.hasSuffix "/bin/valley-web --state /var/lib/valley-web/store --listen 127.0.0.1:8080" web.serviceConfig.ExecStart)
952 || receive.serviceConfig ? InaccessiblePaths
953 || lib.any (lib.hasInfix "/var/lib/valley-web") cfg.systemd.tmpfiles.rules;
954
955 # With an origin, valley-web signs readers in: it takes --origin, and may
956 # write browser/ in the state directory alone, which tmpfiles makes as
957 # its own, mode 0700, under the state directory made as the receiver's.
958 # Its unit takes no core dump. The receiver's unit cannot reach browser/.
959 webReaderMisrendered =
960 let
961 cfg = webReaderHost.config;
962 receive = cfg.systemd.services.valley-web-receive.serviceConfig;
963 web = cfg.systemd.services.valley-web.serviceConfig;
964 in
965 !(lib.hasSuffix "/bin/valley-web --state /var/lib/valley-web/store --listen 127.0.0.1:8080 --origin https://example.org" web.ExecStart)
966 || web.ReadOnlyPaths or [ ] != [ "-/var/lib/valley-web" ]
967 || web.ReadWritePaths or [ ] != [ "/var/lib/valley-web/browser" ]
968 || web.LimitCORE or null != 0
969 || receive.InaccessiblePaths or [ ] != [ "-/var/lib/valley-web/browser" ]
970 || receive.StateDirectory or null != "valley-web"
971 || !(lib.all (r: builtins.elem r cfg.systemd.tmpfiles.rules) [
972 "d /var/lib/valley-web 0750 valley-web-receive valley-web -"
973 "d /var/lib/valley-web/browser 0700 valley-web valley-web -"
974 ]);
975in
976{
977 module-eval =
978 if failedAssertions != [ ] then
979 throw "valley module-eval: failed assertions: ${
980 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
981 }"
982 else if resticRenderedWithoutDeclaration then
983 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
984 else if busRenderedWithoutEnable then
985 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
986 else if
987 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
988 then
989 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
990 else if unrefusedHosts != [ ] then
991 throw "valley module-eval: ${lib.concatMapStringsSep "; " (r: "a host was not refused although ${r.why}") unrefusedHosts}"
992 else if protectionRenderedWithoutDeclaration then
993 throw "valley module-eval: principal machinery rendered for a declaration with no protection block and no tagged key"
994 else if policyMissingWithoutDeclaration then
995 throw "valley module-eval: a project with no protection block got no pre-receive hook — the push policy is installed on every project a host serves"
996 else if protectedHost.config.services.openssh.settings ? PermitUserEnvironment then
997 throw "valley module-eval: sshd must pass no key's environment on: the git user's shell derives the principal from the authenticated key"
998 else if
999 !(lib.hasInfix "valley-git-shell" protectedHost.config.users.users.git.shell)
1000 then
1001 throw "valley module-eval: the git user's login shell must be the valley's, which derives the principal and pauses pushes until init converges"
1002 else if integratorRenderedWithoutEnable then
1003 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
1004 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
1005 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
1006 else if
1007 controllers != [
1008 "valley-integrator@guarded.service"
1009 "valley-integrator@released.service"
1010 "valley-integrator@sealed.service"
1011 ]
1012 then
1013 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
1014 else if
1015 publishers != [
1016 "valley-publish@guarded.path"
1017 "valley-publish@released.path"
1018 "valley-publish@sealed.path"
1019 ]
1020 then
1021 throw "valley module-eval: each controller's landings are published by a path unit of its own, not ${lib.concatStringsSep ", " publishers}"
1022 else if integratorHost.config.systemd.services."valley-publish@".serviceConfig.User != "git" then
1023 throw "valley module-eval: a landing is published as the git user, which holds the mirror credentials"
1024 else if integratorUser == integratorHost.config.services.valley.user then
1025 throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)"
1026 else if !(integratorHost.config.users.users ? ${integratorUser}) then
1027 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
1028 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
1029 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
1030 else if integratorSafeDirectory then
1031 throw "valley module-eval: a controller must carry safe.directory for the one repository it serves in its own environment — it does not own the repositories, and git refuses what it does not own"
1032 else if integratorFloor then
1033 throw "valley module-eval: a controller must be told which repository carries the floor, and must carry a safe.directory exception for it — the floor is read from that repository's integrated tip, and git refuses a repository its caller does not own"
1034 else if integratorScratch then
1035 throw "valley module-eval: a controller's TMPDIR must be its state directory and that directory must be named in ReadWritePaths — under ProtectSystem=strict a path the unit does not enumerate is one it cannot write, and a verdict needs a worktree on disk"
1036 else if identityRenderedWithoutEnable then
1037 throw "valley module-eval: registry-compilation machinery rendered for a host that never enabled it — nothing may change until a consumer opts in"
1038 else if identityCompiler then
1039 throw "valley module-eval: the compiler must run as the git user, write only its own 0755 state directory, read the registry from the instance repository, write the grants the pre-receive hook reads, and have its authorized_keys added to what sshd reads under the %u token — sshd refuses a keys file owned by anyone but root or the account it authorizes, and a fixed path in that global list would authorize every registry key for every account on the host"
1040 else if identityIntegrator then
1041 throw "valley module-eval: a controller under a compiled registry must read the compiled signers file, start after the compilation, and sign under the name the registry publishes its key under"
1042 else if signersRenderedWithoutIntegrator then
1043 throw "valley module-eval: a signers publication rendered for a host whose integrator is off — there is no transfer statement to verify"
1044 else if signersUnpublished then
1045 throw "valley module-eval: the signers publisher must publish the controllers' own signers file and signing name into the instance repository, as the git user, writing nothing else, after valley-init and the compilation, at boot and whenever the signers file changes"
1046 else if signerProjectsMisrendered then
1047 throw "valley module-eval: a controller is given a signer projects file exactly when the host restricts a signer to projects"
1048 else if identityKeepsDeclaredKeys then
1049 throw "valley module-eval: the declared authorized keys must stay authorized beside the compiled ones — a compiled file that replaced them would make a bad compilation a locked-out git user"
1050 else if workerRenderedWithoutEnable then
1051 throw "valley module-eval: check-worker machinery rendered for a host that never enabled it — nothing, the nix daemon's slice included, may change until a consumer opts in"
1052 else if buildSliceMisrendered then
1053 throw "valley module-eval: the build slice must put the nix daemon in valley-builds.slice at the quota and memory cap the host names, with no swap, and render nothing of the worker"
1054 else if workerMisrendered then
1055 throw "valley module-eval: the check worker must run as its own account outside the git group, on one CPU, with one derivation built at a time on 4 cores, in a slice that holds it and the nix daemon to 4 CPUs and 8 GiB with no swap and no device policy, reaching this host's projects through its sshd under the host's own key, on a timer whose first pass waits five minutes after the timer starts and whose later passes are counted from the end of each pass"
1056 else if restackerRenderedWithoutEnable then
1057 throw "valley module-eval: restacker machinery rendered for a host that never enabled it"
1058 else if restackerMisrendered then
1059 throw "valley module-eval: each restacker controller must run valley-restacker run for its project as its own account outside the git group, with its own 0700 state directory as the only persistent path it may write, pushing over this host's sshd as its principal with the key systemd hands it as a credential, checking the host's own key, and reading the floor from the instance repository"
1060 else if webPublisherRenderedWithoutEnable then
1061 throw "valley module-eval: web publisher machinery rendered for a host that imports its module and never enabled it"
1062 else if webPublisherMisrendered then
1063 throw "valley module-eval: the web publisher must be a oneshot on a five-minute timer whose first pass waits five minutes and which no switch starts, run as a dynamic user in the git group with an empty safe.directory entry and then one exact entry per served repository, the repositories read-only, only its state and cache writable, the host's keys inaccessible and its memory capped with no swap, reading the instance repository and naming the host, the receiver, its token as a credential, the destination, the display mapping, the bounds on its bundles and storage, and the identity registry's directory"
1064 else if webPublisherLinksMisrendered != [ ] then
1065 throw "${builtins.toJSON webPublisherLinksMisrendered} valley module-eval: the web publisher's read links must be read live by each pass through the valley-web-links group from a file only root writes, under a links lock tmpfiles makes beside it, with a policy-only unit in the same sandbox that valley-web-link starts after every issue or revoke and once more while a link is pending, the wrapper trusting the unit's repositories and dumping no core, and the backup must hold the credential file's directory and the publisher's state"
1066 else if webReceiverRenderedWithoutEnable then
1067 throw "valley module-eval: web receiver machinery rendered for a host that imports its module and never enabled it"
1068 else if webReceiverMisrendered then
1069 throw "valley module-eval: the web receiver and valley-web must be two confined units started at boot, restarted on failure and requiring nothing of each other, as two users of the valley-web group, the receiver owning the state directory at 0750 with its token as a credential, its pinned verifiers, its caps and its own memory cap, and valley-web after it with the state directory read-only, no credential and a memory cap of its own"
1070 else if webReaderMisrendered then
1071 throw "valley module-eval: with an origin, valley-web must take --origin, take no core dump, and write the state directory's browser/ alone, which tmpfiles makes as its own at 0700 under the receiver's state directory at 0750, and the receiver's unit must not reach browser/"
1072 else
1073 pkgs.runCommand "valley-module-eval" {
1074 initScript = host.config.systemd.services.valley-init.script;
1075 sshdConfig = host.config.services.openssh.extraConfig;
1076 resticService = host.config.systemd.units."restic-backups-valley.service".text;
1077 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
1078 protectedInit = protectedHost.config.systemd.services.valley-init.script;
1079 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
1080 protectedShell = protectedHost.config.users.users.git.shell;
1081 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
1082 integratorInit = integratorHost.config.systemd.services.valley-init.script;
1083 publishUnit = host.config.systemd.units."valley-publish@.service".text;
1084 identityInit = identityHost.config.systemd.services.valley-init.script;
1085 passAsFile = [
1086 "initScript"
1087 "sshdConfig"
1088 "resticService"
1089 "resticTimer"
1090 "protectedInit"
1091 "protectedKeys"
1092 "integratorUnit"
1093 "integratorInit"
1094 "publishUnit"
1095 "identityInit"
1096 ];
1097 } (builtins.readFile ./module-eval.sh);
1098}