gunk-dev/the-valley

host-module.nix

source2af9bd8a8019main's published tip history
32.3 KiB
1# What the NixOS module renders from a declaration, and what it must refuse.
2# The refusals are eval-time: a host that must not evaluate is caught by a
3# throw here, since there is nothing to build once it has. What a host does
4# render is pinned by module-eval.sh over the rendered scripts.
5#
6# Every host here runs the stand-ins for this flake's programs
7# (nix/checks/hosts.nix). Nothing here runs a program: each assertion is
8# about the units, hooks and scripts the module renders, which name a
9# program's path and the flags it is given. So module-eval changes when the
10# module, a host declaration or this check does, and not when a program
11# does.
12{
13 pkgs,
14 lib,
15 stubbedHosts,
16 ...
17}:
18let
19 inherit (stubbedHosts)
20 unprotectedInstanceHost
21 acceptEnvHost
22 matchAcceptEnvHost
23 wildcardAcceptEnvHost
24 quotedAcceptEnvHost
25 tabAcceptEnvHost
26 setEnvHost
27 permitUserEnvironmentHost
28 conflictingKeysHost
29 host
30 noBackupHost
31 noSecretsHost
32 protectedHost
33 busHost
34 mirrorHost
35 integratorHost
36 integratorSelfHost
37 restrictedHost
38 spacedSignerProjectsHost
39 unicodeSpacedSignerProjectsHost
40 identityHost
41 workerHost
42 buildSliceHost
43 noSliceWorkerHost
44 unrestrictedWorkerHost
45 widerWorkerHost
46 integratorNamedWorkerHost
47 ;
48
49 failedAssertions = builtins.filter (a: !a.assertion) (
50 host.config.assertions
51 ++ noBackupHost.config.assertions
52 ++ busHost.config.assertions
53 ++ mirrorHost.config.assertions
54 ++ protectedHost.config.assertions
55 ++ integratorHost.config.assertions
56 ++ integratorSelfHost.config.assertions
57 ++ restrictedHost.config.assertions
58 ++ identityHost.config.assertions
59 ++ workerHost.config.assertions
60 ++ buildSliceHost.config.assertions
61 );
62
63 missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions;
64
65 # The hosts the module must refuse, each with the words its refusal must
66 # say. An assertion that stopped firing would leave the push policy
67 # quietly wrong, so each is held to naming what is wrong.
68 refusedHosts = [
69 {
70 host = unprotectedInstanceHost;
71 says = "whose refs/heads/main is not protected";
72 why = "an instance repository without protection would let any key that can push rewrite the floor and the registry";
73 }
74 {
75 host = acceptEnvHost;
76 says = "AcceptEnv";
77 why = "sshd accepting the principal variable from the client would let any key name any principal";
78 }
79 {
80 host = matchAcceptEnvHost;
81 says = "GIT_CONFIG_*";
82 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";
83 }
84 {
85 host = wildcardAcceptEnvHost;
86 says = "G?T_*";
87 why = "a wildcard that admits GIT_ variables admits them however it is spelled";
88 }
89 {
90 host = quotedAcceptEnvHost;
91 says = "holds a quote or a backslash";
92 why = "a quoted name is one sshd unquotes and admits, so a check that read it differently would pass it";
93 }
94 {
95 host = tabAcceptEnvHost;
96 says = "VALLEY_PRINCIPAL from the client";
97 why = "a name after a tab is a name sshd admits";
98 }
99 {
100 host = setEnvHost;
101 says = "SetEnv";
102 why = "sshd setting the principal itself would make every key push as it";
103 }
104 {
105 host = permitUserEnvironmentHost;
106 says = "PermitUserEnvironment";
107 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";
108 }
109 {
110 host = spacedSignerProjectsHost;
111 says = "services.valley.integrator.signerProjects holds";
112 why = "a project with a space in it would be read back as two projects the declaration never listed";
113 }
114 {
115 host = unicodeSpacedSignerProjectsHost;
116 says = "services.valley.integrator.signerProjects holds";
117 why = "a project holding a no-break space would be read back by the integrator as two projects, or refused at start";
118 }
119 {
120 host = noSliceWorkerHost;
121 says = "services.valley.worker.enable needs services.valley.buildSlice.enable";
122 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";
123 }
124 {
125 host = unrestrictedWorkerHost;
126 says = "must restrict worker.valley.invalid/attestations, the worker's signer, to exactly the worker's projects";
127 why = "a worker whose signer no project list restricts would have its evidence counted in every project by every controller that holds its key";
128 }
129 {
130 host = widerWorkerHost;
131 says = "must restrict worker.valley.invalid/attestations, the worker's signer, to exactly the worker's projects";
132 why = "a worker trusted in a project it does not build would be trusted where it does no work";
133 }
134 {
135 host = integratorNamedWorkerHost;
136 says = "services.valley.worker.signerName must differ from integrator";
137 why = "a worker signing under the controllers' name would make every controller refuse to start";
138 }
139 {
140 host = conflictingKeysHost;
141 says = "more than one principal tag";
142 why = "a key declared under two tags would push as whichever entry sshd reads first";
143 }
144 ];
145
146 unrefusedHosts = builtins.filter (
147 r: !(lib.any (a: !a.assertion && lib.hasInfix r.says a.message) r.host.config.assertions)
148 ) refusedHosts;
149
150 protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys;
151
152 resticRenderedWithoutDeclaration =
153 noBackupHost.config.systemd.services ? "restic-backups-valley"
154 || noBackupHost.config.systemd.timers ? "restic-backups-valley";
155
156 # The bus defaults off; a host that never asked for one must
157 # render zero bus machinery.
158 busRenderedWithoutEnable =
159 noBackupHost.config.systemd.services ? valley-bus
160 || noBackupHost.config.systemd.services ? valley-bus-init;
161
162 # A declaration with no protection block and no tagged key still gets
163 # the push policy on every project — the policy is the host's, and a
164 # protection block only adds protected refs to it — and nothing of the
165 # principal machinery: no tag on a key, no sshd change.
166 protectionRenderedWithoutDeclaration =
167 noBackupHost.config.services.openssh.settings ? PermitUserEnvironment
168 || lib.any (lib.hasInfix "environment=") (
169 noBackupHost.config.users.users.git.openssh.authorizedKeys.keys
170 );
171
172 policyMissingWithoutDeclaration =
173 !(lib.hasInfix "valley-protect-the-valley" noBackupHost.config.systemd.services.valley-init.script);
174
175 # The integrator's units. What a controller *does* is not checked here
176 # and no VM is booted: that is integrator-e2e's job (the real binaries
177 # over real repositories) and, past it, live exercise on a host. These
178 # assertions cover the wiring — that a controller is rendered for what
179 # the declaration protects and for nothing else, under an identity that
180 # is not the git user's, pointed at the key and the signers the consumer
181 # supplied.
182 #
183 # There is a seam between those two halves that neither reaches, and in a
184 # single day it produced five deployment failures. Every assertion below is
185 # an eval of the unit's text; integrator-e2e runs the binaries with no unit
186 # around them at all. What sits between is the unit's sandbox and identity
187 # meeting the filesystem the binaries actually use at runtime:
188 #
189 # - git refusing a repository the service does not own (bd-500adf7)
190 # - a push authenticating as the operator rather than as the run
191 # (bd-353c59d)
192 # - no writable temporary directory, so no worktree could be made
193 # - the worktree metadata root, created by whichever service reached it
194 # first and then unchmodable by the other, which failed valley-init and
195 # took every valley unit down with it
196 # - the state directory not writable under ProtectSystem=strict unless
197 # the unit names it, which the previous fix had made the scratch root
198 #
199 # Each was found by a host, minutes of a live service after a deploy that
200 # every check here passed. They compose: the third's fix set up the fourth,
201 # and the fourth's fix set up the fifth. That is the argument. A seam whose
202 # failures feed each other is not one to pin an assertion at a time, and
203 # each assertion here that marks a piece of it — the git ownership
204 # exception, TMPDIR, the worktrees root, ReadWritePaths — marks a place a
205 # smoke boot would have spoken first.
206 #
207 # A nixosTest that boots one host, starts the units and reconciles one
208 # change end to end is the next check this module needs. Nothing else
209 # closes the seam, and five incidents is enough evidence of what it costs
210 # to leave open. Not built here.
211 #
212 # Protection and integration are separable declarations: protectedHost
213 # declares the same protected projects with the service off, and must
214 # evaluate to no controller at all.
215 integratorRenderedWithoutEnable =
216 protectedHost.config.systemd.services ? "valley-integrator@"
217 || protectedHost.config.systemd.targets ? valley-integrators
218 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
219
220 # One target pulls in each controller, and another the path unit that
221 # publishes each project's queued moves, landings included.
222 integratorWants = integratorHost.config.systemd.targets.valley-integrators.wants;
223 controllers = lib.filter (lib.hasPrefix "valley-integrator@") integratorWants;
224 publishers = integratorHost.config.systemd.targets.valley-publish.wants;
225
226 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
227
228 # The controller is not the repositories' owner, which is the whole point
229 # of running it as itself — so git needs an ownership exception, and
230 # where that exception lives is load-bearing. GIT_CONFIG_* is command
231 # scope, the only scope besides system and global that safe.directory is
232 # read from, and the unit's environment is inherited by every git the
233 # controller drives. It names one repository: a wildcard here would hand
234 # a compromised controller every repository on the host.
235 integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment;
236
237 # The count is what git reads; an entry past it is an entry git ignores,
238 # silently. So it is checked against the entries actually rendered rather
239 # than against a number written here, which would have to be edited every
240 # time a grant is added and would go on passing if it were not.
241 integratorSafeDirectory =
242 integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory"
243 || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git"
244 || integratorGitEnv.GIT_CONFIG_COUNT or null
245 != toString (
246 lib.length (lib.filter (lib.hasPrefix "GIT_CONFIG_KEY_") (lib.attrNames integratorGitEnv))
247 );
248
249 integratorService = integratorHost.config.systemd.services."valley-integrator@".serviceConfig;
250
251 # Scratch goes under the state directory, because the sandbox leaves no
252 # writable temporary directory and a verdict needs a checkout on disk. Two
253 # halves, and each was a live failure on its own. TMPDIR naming a path
254 # outside the state directory is the first; the state directory not being
255 # named in ReadWritePaths is the second, which failed even though
256 # ProtectSystem=strict is documented to leave a declared state directory
257 # writable. Everything the unit may write is enumerated, so what a reader
258 # of the unit sees is the whole of it.
259 # The floor is read from the instance repository's integrated tip
260 # (dcr-f41f718, dcr-b87f6e8), so a controller is told which repository
261 # carries it and reads it there. Three things have to be true of that, and
262 # each was a way for it to be silently wrong.
263 #
264 # The controller has to be told at all — the failure this replaced was a
265 # binary looking for a materialized directory nothing populated.
266 #
267 # It has to work when the repository carrying the floor is not the one the
268 # controller serves, which is the ordinary case: on integratorHost every
269 # controller serves a protected project and reads the floor out of
270 # "open", which no controller serves. git refuses a repository the caller
271 # does not own, so that read needs its own safe.directory entry, named and
272 # not wildcarded — the same failure as the served repository's, one
273 # repository over.
274 #
275 # And it has to keep working when the two coincide, which is what an
276 # instance's own host looks like: integratorSelfHost's controller for
277 # "guarded" reads the floor from "guarded".
278 integratorFloor =
279 let
280 command = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
281 selfCommand = integratorSelfHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
282 env = integratorHost.config.systemd.services."valley-integrator@".environment;
283 in
284 !(lib.hasInfix "--instance-repo /srv/git/sealed.git" command)
285 || !(lib.hasInfix "--instance-repo /srv/git/guarded.git" selfCommand)
286 || env.GIT_CONFIG_COUNT or null != "2"
287 || env.GIT_CONFIG_KEY_1 or null != "safe.directory"
288 || env.GIT_CONFIG_VALUE_1 or null != "/srv/git/sealed.git";
289
290 integratorScratch =
291 integratorGitEnv.TMPDIR or null != "/var/lib/${integratorService.StateDirectory}"
292 || !(builtins.elem "/var/lib/${integratorService.StateDirectory}" integratorService.ReadWritePaths);
293
294 # The registry compiler. It is off by default and must stay inert: a
295 # consumer that has not asked for it renders the system it always did.
296 # integratorHost is the strongest witness available — the integrator on,
297 # the signers file supplied by hand, and every option of the identity
298 # block left alone.
299 identityRenderedWithoutEnable =
300 integratorHost.config.systemd.services ? valley-identity
301 || integratorHost.config.systemd.timers ? valley-identity
302 || integratorHost.config.services.openssh.settings ? PermitUserEnvironment
303 || lib.any (lib.hasInfix "valley-identity") integratorHost.config.services.openssh.authorizedKeysFiles
304 || lib.hasInfix "valley-identity" integratorHost.config.services.openssh.extraConfig
305 || lib.hasInfix "valley-identity" (
306 integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
307 )
308 || lib.hasInfix "--name " (
309 integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
310 );
311
312 identityService = identityHost.config.systemd.services.valley-identity.serviceConfig;
313
314 identityStateDir = "/var/lib/${identityService.StateDirectory or ""}";
315
316 # What the compiled artifacts have to be true of, and each was a way for
317 # the machinery to be quietly wrong.
318 #
319 # The compiler writes into its state directory and nowhere else, so that
320 # directory has to be the one path the unit may write.
321 #
322 # sshd walks every directory above an authorized-keys file and refuses a
323 # group- or world-writable one, so the state directory is 0755 rather than
324 # the 0700 a service's state usually takes — and the unit runs as the git
325 # user, because sshd equally refuses a file owned by anyone but root or
326 # the account it authorizes.
327 #
328 # The compiled file is added to what sshd reads for the git user rather
329 # than replacing it, and for the git user alone: the user's Match block
330 # names the declared keys and the compiled ones, and nothing else — no
331 # ~/.ssh/authorized_keys, no AuthorizedKeysCommand — so every key the git
332 # user accepts carries a tag this module or the compiler wrote. The
333 # compiler is handed the declared keys, so it can refuse a registry key
334 # declared under another tag.
335 identityCompiler =
336 identityService.User or null != identityHost.config.services.valley.user
337 || identityService.StateDirectoryMode or null != "0755"
338 || identityService.ReadWritePaths or [ ] != [ identityStateDir ]
339 || !(lib.hasInfix "--repo /srv/git/sealed.git" identityService.ExecStart)
340 || !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" identityService.ExecStart)
341 || !(lib.hasInfix "--authorized-keys ${identityStateDir}/authorized_keys.git" identityService.ExecStart)
342 || !(lib.hasInfix "--grants ${identityStateDir}/grants" identityService.ExecStart)
343 || !(lib.hasInfix "-valley-declared-keys" identityService.ExecStart)
344 || !(lib.hasInfix "AuthorizedKeysFile /etc/ssh/authorized_keys.d/%u ${identityStateDir}/authorized_keys.git\n" identityHost.config.services.openssh.extraConfig)
345 || !(lib.hasInfix "AuthorizedKeysCommand none" identityHost.config.services.openssh.extraConfig);
346
347 # A controller under a compiled registry reads the compiled signers file
348 # and signs under the name the registry publishes its key under. The key
349 # hash binds name to key, so a controller signing under the host's default
350 # name while the registry publishes it as "integrator" would produce notes
351 # no verifier can find a key for.
352 identityIntegrator =
353 let
354 command = identityHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
355 in
356 !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" command)
357 || !(lib.hasInfix "--name integrator" command)
358 || !(builtins.elem "valley-identity.service"
359 identityHost.config.systemd.services."valley-integrator@".after
360 );
361
362 # The signers publication. A host whose integrator runs and which serves
363 # the instance repository publishes there what a client verifies its
364 # transfer statements with. What it publishes has to be what the
365 # controllers use: the same signers file and the same --name, or none on
366 # either, in which case both take the binary's default. The publisher
367 # writes as the git user, which owns the instance repository, may write
368 # that repository and nothing else, runs after the compilation when there
369 # is one, and runs again whenever the signers file changes. The signer
370 # projects file is the controllers' too, or neither has one.
371 flagValue =
372 flag: command:
373 let
374 m = builtins.match ".*--${flag} ([^ ]+).*" command;
375 in
376 if m == null then null else lib.head m;
377
378 signersPublisher =
379 host: repo:
380 let
381 unit = host.config.systemd.services.valley-signers;
382 publish = unit.serviceConfig.ExecStart;
383 controller = host.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
384 in
385 !(lib.hasInfix "/bin/integrator publish-signers " publish)
386 || flagValue "instance-repo" publish != repo
387 || flagValue "known-signers" publish != flagValue "known-signers" controller
388 || flagValue "name" publish != flagValue "name" controller
389 || flagValue "signer-projects" publish != flagValue "signer-projects" controller
390 || unit.serviceConfig.User or null != "git"
391 || unit.serviceConfig.ReadWritePaths or [ ] != [ repo ]
392 || !(builtins.elem "valley-init.service" unit.after)
393 || !(builtins.elem "multi-user.target" unit.wantedBy)
394 || host.config.systemd.paths.valley-signers.pathConfig.PathChanged or null
395 != flagValue "known-signers" controller
396 || !(builtins.elem "multi-user.target" host.config.systemd.paths.valley-signers.wantedBy);
397
398 signersRenderedWithoutIntegrator =
399 protectedHost.config.systemd.services ? valley-signers
400 || protectedHost.config.systemd.paths ? valley-signers;
401
402 signersUnpublished =
403 signersPublisher integratorHost "/srv/git/sealed.git"
404 || signersPublisher restrictedHost "/srv/git/sealed.git"
405 || signersPublisher integratorSelfHost "/srv/git/guarded.git"
406 || signersPublisher identityHost "/srv/git/sealed.git"
407 || flagValue "name" identityHost.config.systemd.services.valley-signers.serviceConfig.ExecStart
408 != "integrator"
409 || !(builtins.elem "valley-identity.service" identityHost.config.systemd.services.valley-signers.after);
410
411 # A host that restricts no signer gives its controllers no signer
412 # projects file, and one that restricts some gives them one.
413 signerProjectsMisrendered =
414 flagValue "signer-projects" integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
415 != null
416 || flagValue "signer-projects" restrictedHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
417 == null;
418
419 # The declared keys stay authorized beside the compiled ones. A compiled
420 # file that replaced them would make a bad compilation — or a first boot
421 # before the first one — a locked-out git user, which is the one failure
422 # this machinery must never have.
423 identityKeepsDeclaredKeys =
424 identityHost.config.users.users.git.openssh.authorizedKeys.keys == [ ];
425
426 # The check worker and the build slice. Both are off by default, and a
427 # host that never asked for them renders none of either: no unit, no
428 # account, no slice, and a nix daemon where NixOS puts it. integratorHost
429 # is the strongest witness, with the integrator the worker would need
430 # already on.
431 workerRenderedWithoutEnable =
432 integratorHost.config.systemd.services ? valley-worker
433 || integratorHost.config.systemd.timers ? valley-worker
434 || integratorHost.config.systemd.slices ? valley-builds
435 || integratorHost.config.users.users ? valley-worker
436 || integratorHost.config.systemd.services.nix-daemon.serviceConfig ? Slice;
437
438 # The build slice is a host decision of its own. On, it puts the daemon
439 # in the slice at the caps the host names, with no swap, and nothing of
440 # the worker.
441 buildSliceMisrendered =
442 let
443 slice = buildSliceHost.config.systemd.slices.valley-builds.sliceConfig or { };
444 in
445 buildSliceHost.config.systemd.services.nix-daemon.serviceConfig.Slice or null
446 != "valley-builds.slice"
447 || slice.CPUQuota or null != "200%"
448 || slice.MemoryMax or null != "4G"
449 || slice.MemorySwapMax or null != "0"
450 || buildSliceHost.config.systemd.services ? valley-worker
451 || buildSliceHost.config.users.users ? valley-worker;
452
453 # What the VM check runs, held at the operator's values. worker-e2e
454 # boots the worker and lowers the memory cap at runtime to make a build
455 # run past it, so the values themselves are held here. The caps are on
456 # a slice that holds the worker and the nix daemon both, since a build
457 # runs in the daemon's cgroup and never in the worker's; the slice and
458 # the daemon set no device policy, so a NixOS test still gets /dev/kvm.
459 # The worker runs on one CPU, so attest, which runs one nix build per CPU
460 # it may run on, runs one at a time, and each nix build builds one
461 # derivation at a time with 4 cores. The account is not in the git
462 # group, so the repositories on disk are not the worker's to read or
463 # write, and it reaches them only through this host's sshd, checking the
464 # host's key.
465 workerMisrendered =
466 let
467 unit = workerHost.config.systemd.services.valley-worker;
468 service = unit.serviceConfig;
469 daemon = workerHost.config.systemd.services.nix-daemon.serviceConfig;
470 slice = workerHost.config.systemd.slices.valley-builds.sliceConfig;
471 account = workerHost.config.users.users.valley-worker;
472 timer = workerHost.config.systemd.timers.valley-worker;
473 devicePolicy = c: c ? DevicePolicy || c ? DeviceAllow || c ? PrivateDevices;
474 in
475 service.User or null != "valley-worker"
476 || account.group != "valley-worker"
477 || account.extraGroups != [ ]
478 || service.Type or null != "oneshot"
479 || service.Slice or null != "valley-builds.slice"
480 || daemon.Slice or null != "valley-builds.slice"
481 || slice.CPUQuota or null != "400%"
482 || slice.MemoryMax or null != "8G"
483 || slice.MemorySwapMax or null != "0"
484 || devicePolicy slice
485 || devicePolicy daemon
486 || service.CPUAffinity or null != "0"
487 || !(lib.hasInfix "max-jobs = 1\n" unit.environment.NIX_CONFIG)
488 || !(lib.hasInfix "cores = 4\n" unit.environment.NIX_CONFIG)
489 || !(lib.hasInfix "builders =\n" unit.environment.NIX_CONFIG)
490 || !(lib.hasInfix "-o StrictHostKeyChecking=yes -o HostKeyAlias=localhost" unit.environment.GIT_SSH_COMMAND)
491 || service.ReadWritePaths or [ ] != [
492 "/var/lib/valley-worker"
493 "/nix/var/nix/daemon-socket"
494 ]
495 || !(lib.hasInfix "exec valley-worker --repo guarded --repo sealed --key \"$key\" --name worker.valley.invalid/attestations --state \"$state\"" unit.script)
496 || !(lib.hasInfix "git -C \"$origin\" remote add origin git@localhost:sealed.git\n" unit.script)
497 || timer.timerConfig.OnUnitInactiveSec or null != "1m"
498 || !(builtins.elem "timers.target" timer.wantedBy);
499in
500{
501 module-eval =
502 if failedAssertions != [ ] then
503 throw "valley module-eval: failed assertions: ${
504 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
505 }"
506 else if resticRenderedWithoutDeclaration then
507 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
508 else if busRenderedWithoutEnable then
509 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
510 else if
511 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
512 then
513 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
514 else if unrefusedHosts != [ ] then
515 throw "valley module-eval: ${lib.concatMapStringsSep "; " (r: "a host was not refused although ${r.why}") unrefusedHosts}"
516 else if protectionRenderedWithoutDeclaration then
517 throw "valley module-eval: principal machinery rendered for a declaration with no protection block and no tagged key"
518 else if policyMissingWithoutDeclaration then
519 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"
520 else if protectedHost.config.services.openssh.settings ? PermitUserEnvironment then
521 throw "valley module-eval: sshd must pass no key's environment on: the git user's shell derives the principal from the authenticated key"
522 else if
523 !(lib.hasInfix "valley-git-shell" protectedHost.config.users.users.git.shell)
524 then
525 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"
526 else if integratorRenderedWithoutEnable then
527 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
528 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
529 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
530 else if
531 controllers != [
532 "valley-integrator@guarded.service"
533 "valley-integrator@released.service"
534 "valley-integrator@sealed.service"
535 ]
536 then
537 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
538 else if
539 publishers != [
540 "valley-publish@guarded.path"
541 "valley-publish@released.path"
542 "valley-publish@sealed.path"
543 ]
544 then
545 throw "valley module-eval: each controller's landings are published by a path unit of its own, not ${lib.concatStringsSep ", " publishers}"
546 else if integratorHost.config.systemd.services."valley-publish@".serviceConfig.User != "git" then
547 throw "valley module-eval: a landing is published as the git user, which holds the mirror credentials"
548 else if integratorUser == integratorHost.config.services.valley.user then
549 throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)"
550 else if !(integratorHost.config.users.users ? ${integratorUser}) then
551 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
552 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
553 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
554 else if integratorSafeDirectory then
555 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"
556 else if integratorFloor then
557 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"
558 else if integratorScratch then
559 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"
560 else if identityRenderedWithoutEnable then
561 throw "valley module-eval: registry-compilation machinery rendered for a host that never enabled it — nothing may change until a consumer opts in"
562 else if identityCompiler then
563 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"
564 else if identityIntegrator then
565 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"
566 else if signersRenderedWithoutIntegrator then
567 throw "valley module-eval: a signers publication rendered for a host whose integrator is off — there is no transfer statement to verify"
568 else if signersUnpublished then
569 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"
570 else if signerProjectsMisrendered then
571 throw "valley module-eval: a controller is given a signer projects file exactly when the host restricts a signer to projects"
572 else if identityKeepsDeclaredKeys then
573 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"
574 else if workerRenderedWithoutEnable then
575 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"
576 else if buildSliceMisrendered then
577 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"
578 else if workerMisrendered then
579 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 counted from the end of each pass"
580 else
581 pkgs.runCommand "valley-module-eval" {
582 initScript = host.config.systemd.services.valley-init.script;
583 sshdConfig = host.config.services.openssh.extraConfig;
584 resticService = host.config.systemd.units."restic-backups-valley.service".text;
585 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
586 protectedInit = protectedHost.config.systemd.services.valley-init.script;
587 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
588 protectedShell = protectedHost.config.users.users.git.shell;
589 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
590 integratorInit = integratorHost.config.systemd.services.valley-init.script;
591 publishUnit = host.config.systemd.units."valley-publish@.service".text;
592 identityInit = identityHost.config.systemd.services.valley-init.script;
593 passAsFile = [
594 "initScript"
595 "sshdConfig"
596 "resticService"
597 "resticTimer"
598 "protectedInit"
599 "protectedKeys"
600 "integratorUnit"
601 "integratorInit"
602 "publishUnit"
603 "identityInit"
604 ];
605 } (builtins.readFile ./module-eval.sh);
606}