| 1 | # The valley host installer: declarative bare-git hosting over SSH, driven |
| 2 | # by a CUE host declaration (../schema/valley.cue). |
| 3 | # |
| 4 | # Layering: the CUE file owns the domain model — which projects exist, their |
| 5 | # stores, their push mirrors, whether the host's data has offsite backup and |
| 6 | # with what cadence and retention. This module owns machine integration |
| 7 | # only: data directory, unix user, SSH keys, backup credentials. At build |
| 8 | # time the CUE config is vetted against the shipped schema and exported to |
| 9 | # JSON; an invalid config fails the system build with cue's error. Nix |
| 10 | # never redefines the schema. |
| 11 | # |
| 12 | # Repos are only ever created, never deleted or overwritten — removing a |
| 13 | # project (or disabling its git store) leaves the data on disk untouched. |
| 14 | # |
| 15 | # Identity is deliberately thin and host-level: one git user, git-shell, |
| 16 | # key-only, Tailscale ACLs in front. Per-project *access* is not honestly |
| 17 | # enforceable with this mechanism, so it is deliberately not an option |
| 18 | # (.the-valley/decisions/dcr-0f5d9b1-cue-config-host-module.md). Per-project |
| 19 | # *write protection* is, because the pre-receive hook below is a real |
| 20 | # enforcement boundary and each key carries a principal name it can read |
| 21 | # (.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — |
| 22 | # so it is declared, in the project's `protection` block, and this module |
| 23 | # only binds principal names to keys and renders the hook. |
| 24 | # |
| 25 | # Mirror credentials are the consumer's concern: the module assumes the git |
| 26 | # user's SSH identity and known_hosts are provisioned by the host (e.g. |
| 27 | # cosmo, via its secrets). Nothing here plumbs secrets: the backup options |
| 28 | # below take *paths* to consumer-provisioned secret files; the contents |
| 29 | # never pass through this module or the store. |
| 30 | # |
| 31 | # One service runs under its own unix user rather than the git one: the |
| 32 | # integrator. That is the first half of the split bd-500adf7 asks for. |
| 33 | { |
| 34 | config, |
| 35 | pkgs, |
| 36 | lib, |
| 37 | ... |
| 38 | }: |
| 39 | |
| 40 | let |
| 41 | cfg = config.services.valley; |
| 42 | |
| 43 | schema = ../schema/valley.cue; |
| 44 | |
| 45 | # Build-time vet + export of the host declaration. the vet checks the |
| 46 | # declaration against the schema (whose top level applies #Host), so unknown |
| 47 | # fields and unsafe project names fail the build, with cue's error. |
| 48 | configJSON = |
| 49 | pkgs.runCommand "valley-config.json" |
| 50 | { |
| 51 | nativeBuildInputs = [ pkgs.cue ]; |
| 52 | } |
| 53 | '' |
| 54 | cue vet -c ${schema} ${cfg.config} |
| 55 | cue export ${schema} ${cfg.config} > $out |
| 56 | ''; |
| 57 | |
| 58 | host = lib.importJSON configJSON; |
| 59 | |
| 60 | # Projects whose git store is enabled get a bare repository. |
| 61 | gitProjects = lib.filterAttrs (_: p: p.git.enable) host.projects; |
| 62 | repoNames = lib.attrNames gitProjects; |
| 63 | |
| 64 | # The declared durability policy, if any. `backup` is optional in the |
| 65 | # schema: a declaration without it (or with enable = false) renders zero |
| 66 | # backup machinery, exactly as before the field existed. |
| 67 | backupPolicy = host.backup or null; |
| 68 | backupEnabled = backupPolicy != null && backupPolicy.enable; |
| 69 | |
| 70 | # The declared cadence names policy; its wall-clock rendering is this |
| 71 | # installer's choice. A lookup rather than a literal, so a cadence this |
| 72 | # module does not know fails eval loudly instead of mis-scheduling. |
| 73 | backupTimer = { |
| 74 | # Late night with a spread; Persistent runs a missed window at boot. |
| 75 | nightly = { |
| 76 | OnCalendar = "03:30"; |
| 77 | RandomizedDelaySec = "30m"; |
| 78 | Persistent = true; |
| 79 | }; |
| 80 | }; |
| 81 | |
| 82 | # The projects the integrator serves: exactly the ones the declaration |
| 83 | # protects. A protected ref is one only a declared writer may push, and |
| 84 | # the integrator is what lands everyone else's changes onto it. |
| 85 | integratedProjects = lib.filterAttrs (_: p: p ? protection) gitProjects; |
| 86 | |
| 87 | # The integrator this flake builds, in its shipping form: attest, the |
| 88 | # deriver, cue, git and natscli pinned inside the wrapper, so every host |
| 89 | # judges with the same tools (nix/packages.nix). |
| 90 | integratorPackage = (import ./packages.nix { inherit pkgs lib; }).integrator; |
| 91 | |
| 92 | # The whole of a controller's configuration: which repository it serves, |
| 93 | # who it signs as, and whose evidence it accepts. No policy — the |
| 94 | # integrator reads the project layer from the target tip and the instance |
| 95 | # layer from its own side (bd-eaefe82) — and no project name, which the |
| 96 | # binary derives from the bare repo's own directory, the same way the |
| 97 | # post-receive hook does. |
| 98 | integratorCommand = lib.concatStringsSep " " ( |
| 99 | [ |
| 100 | "${integratorPackage}/bin/integrator watch" |
| 101 | "--repo ${cfg.dataDir}/%i.git" |
| 102 | "--key ${toString cfg.integrator.signingKeyFile}" |
| 103 | "--known-signers ${toString cfg.integrator.knownSignersFile}" |
| 104 | "--instance ${toString cfg.integrator.instancePolicy}" |
| 105 | "--interval ${cfg.integrator.interval}" |
| 106 | ] |
| 107 | ++ lib.optional cfg.bus.enable "--bus ${busUrl}" |
| 108 | ); |
| 109 | |
| 110 | # The one filesystem grant the integrator's user gets. It writes refs, |
| 111 | # objects, and the worktree metadata `git worktree add` keeps inside the |
| 112 | # repository, so read access is not enough: the repos it serves become |
| 113 | # group-shared, and its primary group is the git group. Re-applied on |
| 114 | # every activation, because a repo can predate the service. |
| 115 | integratorShareCommands = lib.concatMapStrings (name: '' |
| 116 | repo=${lib.escapeShellArg "${cfg.dataDir}/${name}.git"} |
| 117 | git -C "$repo" config core.sharedRepository group |
| 118 | chmod -R g+rwX "$repo" |
| 119 | find "$repo" -type d -exec chmod g+s {} + |
| 120 | '') (lib.attrNames integratedProjects); |
| 121 | |
| 122 | # The paths the consumer must supply once the integrator is enabled, |
| 123 | # with what each names — for the assertion message. |
| 124 | integratorPathOptions = { |
| 125 | signingKeyFile = "the key its transfer statements are signed with"; |
| 126 | knownSignersFile = "the signers whose evidence it accepts"; |
| 127 | instancePolicy = "the instance policy layer it composes against"; |
| 128 | }; |
| 129 | |
| 130 | # The secret-path options the consumer must supply once the declaration |
| 131 | # enables backup, with what each names — for the assertion message. |
| 132 | backupSecretOptions = { |
| 133 | repositoryFile = "the restic repository URL"; |
| 134 | passwordFile = "the repository encryption password"; |
| 135 | sshKeyFile = "the SSH identity for the sftp target"; |
| 136 | knownHostsFile = "the pinned host key of the sftp target"; |
| 137 | }; |
| 138 | |
| 139 | # Managed post-receive dispatcher. Each repo's post-receive is a symlink to |
| 140 | # this script, which chains every executable dropped into the repo's |
| 141 | # hooks/post-receive.d/ directory. |
| 142 | postReceiveDispatch = pkgs.writeShellScript "valley-post-receive" '' |
| 143 | # Managed by services.valley — do not edit. |
| 144 | # Drop executable hooks into post-receive.d/ next to this symlink. |
| 145 | set -eu |
| 146 | hook_dir="$(dirname "$0")/post-receive.d" |
| 147 | [ -d "$hook_dir" ] || exit 0 |
| 148 | updates="$(cat)" |
| 149 | [ -n "$updates" ] || exit 0 |
| 150 | for hook in "$hook_dir"/*; do |
| 151 | [ -x "$hook" ] || continue |
| 152 | printf '%s\n' "$updates" | "$hook" "$@" |
| 153 | done |
| 154 | ''; |
| 155 | |
| 156 | # Best-effort push replication. Publication mirror: main and tags only. |
| 157 | # Topic branches are review-queue state, not published — the mirror exists |
| 158 | # so consumers can fetch what has been integrated; durability is restic's |
| 159 | # job, not the mirror's. Pushes run detached (setsid) so a dead mirror can |
| 160 | # only ever cost a log line — never block or fail the primary push. |
| 161 | # --prune propagates tag deletions (glob refspec); it is a no-op for heads |
| 162 | # now that the heads refspec names one ref, so a separate best-effort sweep |
| 163 | # deletes every mirror head but main — which also strips anything that |
| 164 | # appears there by other means. The sweep reads only refs/heads (ls-remote |
| 165 | # --heads), never refs/pull/*, and is logged apart from the replication |
| 166 | # push so it cannot change that push's reported outcome. --mirror is |
| 167 | # rejected outright: it also tries to delete remote-only namespaces, and on |
| 168 | # GitHub the read-only refs/pull/* makes that fail every push, masking real |
| 169 | # replication failures. |
| 170 | mirrorPusher = |
| 171 | name: mirrors: |
| 172 | pkgs.writeShellScript "valley-mirror-push-${name}" '' |
| 173 | for url in ${lib.escapeShellArgs mirrors}; do |
| 174 | if ${pkgs.git}/bin/git push --prune "$url" '+refs/heads/main:refs/heads/main' '+refs/tags/*:refs/tags/*' >/dev/null 2>&1; then |
| 175 | ${pkgs.util-linux}/bin/logger -t valley-mirror "${name}: pushed to $url" || true |
| 176 | else |
| 177 | ${pkgs.util-linux}/bin/logger -t valley-mirror "${name}: push to $url FAILED" || true |
| 178 | fi |
| 179 | |
| 180 | # Unpublish anything but main. Refnames cannot contain whitespace, so |
| 181 | # accumulating them space-separated and splitting on expansion is safe. |
| 182 | stale="" |
| 183 | while read -r _ ref; do |
| 184 | [ "$ref" = refs/heads/main ] || stale="$stale $ref" |
| 185 | done < <(${pkgs.git}/bin/git ls-remote --heads "$url" 2>/dev/null) |
| 186 | if [ -n "$stale" ]; then |
| 187 | if ${pkgs.git}/bin/git push "$url" --delete $stale >/dev/null 2>&1; then |
| 188 | ${pkgs.util-linux}/bin/logger -t valley-mirror "${name}: unpublished$stale from $url" || true |
| 189 | else |
| 190 | ${pkgs.util-linux}/bin/logger -t valley-mirror "${name}: unpublish from $url failed (ignored)" || true |
| 191 | fi |
| 192 | fi |
| 193 | done |
| 194 | ''; |
| 195 | |
| 196 | mirrorHook = |
| 197 | name: mirrors: |
| 198 | pkgs.writeShellScript "valley-mirrors-${name}" '' |
| 199 | # Managed by services.valley — best-effort push mirrors for ${name}. |
| 200 | cat >/dev/null # updated refs unused: the push replicates main and tags |
| 201 | ${pkgs.util-linux}/bin/setsid -f ${mirrorPusher name mirrors} </dev/null >/dev/null 2>&1 |
| 202 | exit 0 |
| 203 | ''; |
| 204 | |
| 205 | # The event bus (design/roadmap.md, Phase 1): NATS JetStream, onto which |
| 206 | # the ref-updated hook below projects this host's git activity. The bus is |
| 207 | # never load-bearing for durable state — per-repo events are durable in |
| 208 | # git itself, and `valley replay` rebuilds the stream from a repo's refs — |
| 209 | # so losing its storage costs one replay, nothing more. |
| 210 | busUrl = "nats://${cfg.bus.listen}"; |
| 211 | |
| 212 | busServerConfig = pkgs.writeText "valley-bus.conf" '' |
| 213 | listen: ${cfg.bus.listen} |
| 214 | jetstream { |
| 215 | store_dir: "${cfg.bus.storeDir}" |
| 216 | # The store directory defaults to living on the repo volume; the cap |
| 217 | # keeps a runaway or malicious local publisher from filling it. |
| 218 | max_file_store: 2GiB |
| 219 | } |
| 220 | ''; |
| 221 | |
| 222 | # One ref-updated event per updated ref, read from the post-receive stdin |
| 223 | # this script inherits. Every payload field is derivable from git alone — |
| 224 | # no wall-clock time, no hostname — so replaying a repo's refs reproduces |
| 225 | # the same events (the roadmap's determinism criterion). `valley replay` |
| 226 | # builds the identical payload; change one only with the other. |
| 227 | busPublisher = pkgs.writeShellScript "valley-bus-publish" '' |
| 228 | repo="$(basename "$PWD" .git)" |
| 229 | while read -r old new ref; do |
| 230 | [ -n "$ref" ] || continue |
| 231 | # " is the only JSON-significant character a refname can contain |
| 232 | # (git forbids \ and control characters); names and ids are safe. |
| 233 | event="$(printf '{"event":"ref-updated","repo":"%s","ref":"%s","old":"%s","new":"%s"}' \ |
| 234 | "$repo" "''${ref//\"/\\\"}" "$old" "$new")" |
| 235 | if ${pkgs.natscli}/bin/nats --server ${busUrl} pub \ |
| 236 | "valley.git.$repo.ref-updated" "$event" </dev/null >/dev/null 2>&1; then |
| 237 | ${pkgs.util-linux}/bin/logger -t valley-bus "$repo: published ref-updated $ref" || true |
| 238 | else |
| 239 | ${pkgs.util-linux}/bin/logger -t valley-bus "$repo: publish of ref-updated $ref FAILED" || true |
| 240 | fi |
| 241 | done |
| 242 | ''; |
| 243 | |
| 244 | # The post-receive.d hook, with the mirror hook's failure semantics: the |
| 245 | # publisher runs detached (setsid, inheriting the updates on stdin), so a |
| 246 | # bus problem can only ever cost a log line — never block or fail the |
| 247 | # push. git is the source of truth; the bus is the replaceable component. |
| 248 | busEventHook = pkgs.writeShellScript "valley-bus-events" '' |
| 249 | # Managed by services.valley — project ref updates onto the event bus. |
| 250 | ${pkgs.util-linux}/bin/setsid -f ${busPublisher} >/dev/null 2>&1 |
| 251 | exit 0 |
| 252 | ''; |
| 253 | |
| 254 | # Bus-hook wiring, one symlink per repo. Same rules as the mirror hooks: |
| 255 | # only ever installs, updates, or removes a store symlink — a hand-written |
| 256 | # hook of the same name is left alone. |
| 257 | busHookCommands = lib.concatMapStrings ( |
| 258 | name: |
| 259 | let |
| 260 | bhook = lib.escapeShellArg "${cfg.dataDir}/${name}.git/hooks/post-receive.d/valley-bus"; |
| 261 | in |
| 262 | if cfg.bus.enable then |
| 263 | '' |
| 264 | bhook=${bhook} |
| 265 | if [ -L "$bhook" ]; then |
| 266 | case "$(readlink "$bhook")" in |
| 267 | /nix/store/*) ln -sfn ${busEventHook} "$bhook" ;; |
| 268 | esac |
| 269 | elif [ ! -e "$bhook" ]; then |
| 270 | ln -s ${busEventHook} "$bhook" |
| 271 | fi |
| 272 | '' |
| 273 | else |
| 274 | '' |
| 275 | bhook=${bhook} |
| 276 | if [ -L "$bhook" ]; then |
| 277 | case "$(readlink "$bhook")" in |
| 278 | /nix/store/*) rm -f "$bhook" ;; |
| 279 | esac |
| 280 | fi |
| 281 | '' |
| 282 | ) repoNames; |
| 283 | |
| 284 | # Per-project mirror-hook wiring. Only ever installs, updates, or removes |
| 285 | # a store symlink — a hand-written hook of the same name is left alone. |
| 286 | mirrorHookCommands = lib.concatStrings ( |
| 287 | lib.mapAttrsToList ( |
| 288 | name: p: |
| 289 | let |
| 290 | mhook = lib.escapeShellArg "${cfg.dataDir}/${name}.git/hooks/post-receive.d/valley-mirrors"; |
| 291 | in |
| 292 | if p.mirrors != [ ] then |
| 293 | '' |
| 294 | mhook=${mhook} |
| 295 | if [ -L "$mhook" ]; then |
| 296 | case "$(readlink "$mhook")" in |
| 297 | /nix/store/*) ln -sfn ${mirrorHook name p.mirrors} "$mhook" ;; |
| 298 | esac |
| 299 | elif [ ! -e "$mhook" ]; then |
| 300 | ln -s ${mirrorHook name p.mirrors} "$mhook" |
| 301 | fi |
| 302 | '' |
| 303 | else |
| 304 | '' |
| 305 | mhook=${mhook} |
| 306 | if [ -L "$mhook" ]; then |
| 307 | case "$(readlink "$mhook")" in |
| 308 | /nix/store/*) rm -f "$mhook" ;; |
| 309 | esac |
| 310 | fi |
| 311 | '' |
| 312 | ) gitProjects |
| 313 | ); |
| 314 | |
| 315 | # The environment variable carrying the pushing key's principal name. |
| 316 | # sshd sets it from the key's own authorized_keys entry; the pre-receive |
| 317 | # hook reads it. One name, honoured only because PermitUserEnvironment |
| 318 | # below names it. |
| 319 | principalEnv = "VALLEY_PRINCIPAL"; |
| 320 | |
| 321 | # Keys that name a principal. An entry written as a bare string names |
| 322 | # none, and renders exactly as it always did. |
| 323 | taggedKeys = lib.filter (k: !lib.isString k) cfg.authorizedKeys; |
| 324 | |
| 325 | authorizedKeyLine = |
| 326 | k: if lib.isString k then k else ''environment="${principalEnv}=${k.principal}" ${k.key}''; |
| 327 | |
| 328 | # Where a contributor's attestations live (design/contribute.md, step 5). |
| 329 | attestationNamespace = "refs/the-valley/attestations/"; |
| 330 | |
| 331 | # The pre-receive hook: the one structural git invariant |
| 332 | # (design/architecture.md, design/contribute.md). Two clauses and nothing |
| 333 | # else — only a declared writer may update a protected ref, and an |
| 334 | # attestation ref may only be created. Every other ref is open to anyone |
| 335 | # with push access, and all policy beyond this lives in the integrator. |
| 336 | # |
| 337 | # What is protected and who may write it comes from the declaration's |
| 338 | # `protection` block, like every other domain fact here. This renders it. |
| 339 | # |
| 340 | # Pushes arrive over SSH as one shared git user, so the unix account |
| 341 | # behind a push says nothing about who pushed. The principal comes from |
| 342 | # the key instead: services.valley.authorizedKeys tags each key's |
| 343 | # authorized_keys entry, sshd puts that tag in the environment of the |
| 344 | # receive-pack this hook runs under, and an untagged key has no principal |
| 345 | # at all. The client cannot supply the tag itself — sshd passes on no |
| 346 | # client environment (no AcceptEnv). |
| 347 | # |
| 348 | # This governs pushes, which is every write that crosses the host |
| 349 | # boundary. It does not govern writes made on the host: the integrator |
| 350 | # updates refs locally, as could anyone holding the git user's shell. |
| 351 | # Local access is the host's own boundary, not this hook's. |
| 352 | protectHook = |
| 353 | name: p: |
| 354 | pkgs.writeShellScript "valley-protect-${name}" '' |
| 355 | # Managed by services.valley — do not edit. |
| 356 | set -eu |
| 357 | |
| 358 | principal="''${${principalEnv}:-}" |
| 359 | [ -n "$principal" ] || principal="<untagged key>" |
| 360 | writers=( ${lib.escapeShellArgs p.writers} ) |
| 361 | protected=( ${lib.escapeShellArgs p.refs} ) |
| 362 | |
| 363 | is_writer() { |
| 364 | local w |
| 365 | for w in ''${writers[@]+"''${writers[@]}"}; do |
| 366 | if [ "$w" = "$principal" ]; then return 0; fi |
| 367 | done |
| 368 | return 1 |
| 369 | } |
| 370 | |
| 371 | rejected=0 |
| 372 | while read -r old new ref; do |
| 373 | [ -n "$ref" ] || continue |
| 374 | |
| 375 | # An attestation is a record of what was checked; a record that can |
| 376 | # be rewritten or dropped is not one. So the namespace is |
| 377 | # create-only, for everyone — an all-zero old id is a creation, at |
| 378 | # any hash length. |
| 379 | case "$ref" in |
| 380 | ${attestationNamespace}*) |
| 381 | case "$old" in |
| 382 | *[!0]*) |
| 383 | echo "valley: $principal may not update $ref — attestation refs are create-only" >&2 |
| 384 | rejected=1 |
| 385 | ;; |
| 386 | esac |
| 387 | continue |
| 388 | ;; |
| 389 | esac |
| 390 | |
| 391 | # Protected refs, matched as shell globs (so `*` crosses path |
| 392 | # separators). Unprotected refs are not the hook's business. |
| 393 | for pattern in ''${protected[@]+"''${protected[@]}"}; do |
| 394 | case "$ref" in |
| 395 | $pattern) |
| 396 | if ! is_writer; then |
| 397 | echo "valley: $principal may not write $ref — a protected ref of ${name} (writers: ${ |
| 398 | lib.concatStringsSep ", " p.writers |
| 399 | })" >&2 |
| 400 | rejected=1 |
| 401 | fi |
| 402 | break |
| 403 | ;; |
| 404 | esac |
| 405 | done |
| 406 | done |
| 407 | exit "$rejected" |
| 408 | ''; |
| 409 | |
| 410 | # Per-project pre-receive wiring, from the declaration's protection |
| 411 | # blocks. Same rules as the other managed hooks: only ever installs, |
| 412 | # updates, or removes a store symlink — a hand-written hook of the same |
| 413 | # name is left alone. A project that declares no protection gets no hook, |
| 414 | # so a declaration written before the field installs nothing new. |
| 415 | protectHookCommands = lib.concatStrings ( |
| 416 | lib.mapAttrsToList ( |
| 417 | name: p: |
| 418 | let |
| 419 | phook = lib.escapeShellArg "${cfg.dataDir}/${name}.git/hooks/pre-receive"; |
| 420 | in |
| 421 | if p ? protection then |
| 422 | '' |
| 423 | phook=${phook} |
| 424 | if [ -L "$phook" ]; then |
| 425 | case "$(readlink "$phook")" in |
| 426 | /nix/store/*) ln -sfn ${protectHook name p.protection} "$phook" ;; |
| 427 | esac |
| 428 | elif [ ! -e "$phook" ]; then |
| 429 | ln -s ${protectHook name p.protection} "$phook" |
| 430 | fi |
| 431 | '' |
| 432 | else |
| 433 | '' |
| 434 | phook=${phook} |
| 435 | if [ -L "$phook" ]; then |
| 436 | case "$(readlink "$phook")" in |
| 437 | /nix/store/*) rm -f "$phook" ;; |
| 438 | esac |
| 439 | fi |
| 440 | '' |
| 441 | ) gitProjects |
| 442 | ); |
| 443 | in |
| 444 | { |
| 445 | options.services.valley = { |
| 446 | enable = lib.mkEnableOption "the valley host: declarative bare-git hosting driven by a CUE declaration"; |
| 447 | |
| 448 | user = lib.mkOption { |
| 449 | type = lib.types.str; |
| 450 | default = "git"; |
| 451 | description = "System user that owns the repositories and accepts SSH pushes."; |
| 452 | }; |
| 453 | |
| 454 | group = lib.mkOption { |
| 455 | type = lib.types.str; |
| 456 | default = "git"; |
| 457 | description = "Primary group of the valley git user."; |
| 458 | }; |
| 459 | |
| 460 | dataDir = lib.mkOption { |
| 461 | type = lib.types.path; |
| 462 | default = "/srv/git"; |
| 463 | example = "/mnt/git"; |
| 464 | description = '' |
| 465 | Directory holding the bare repositories, one `<name>.git` per |
| 466 | project declared in {option}`services.valley.config`. Also the git |
| 467 | user's home, so clone URLs are relative to it (`git@host:name.git`). |
| 468 | Consumers typically point it at a dedicated dataset. |
| 469 | ''; |
| 470 | }; |
| 471 | |
| 472 | authorizedKeys = lib.mkOption { |
| 473 | type = lib.types.listOf ( |
| 474 | lib.types.either lib.types.str ( |
| 475 | lib.types.submodule { |
| 476 | options = { |
| 477 | key = lib.mkOption { |
| 478 | type = lib.types.str; |
| 479 | description = "The SSH public key, as it would appear in `authorized_keys`."; |
| 480 | }; |
| 481 | |
| 482 | principal = lib.mkOption { |
| 483 | type = lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]*"; |
| 484 | example = "integrator"; |
| 485 | description = '' |
| 486 | Name of the principal this key acts as. Every push made |
| 487 | with the key carries the name, and |
| 488 | {option}`services.valley.protect` decides what a name may |
| 489 | write. Nothing else uses it. |
| 490 | ''; |
| 491 | }; |
| 492 | }; |
| 493 | } |
| 494 | ) |
| 495 | ); |
| 496 | default = [ ]; |
| 497 | example = lib.literalExpression '' |
| 498 | [ |
| 499 | "ssh-ed25519 AAAA… someone@somewhere" |
| 500 | { |
| 501 | principal = "integrator"; |
| 502 | key = "ssh-ed25519 AAAA… integrator"; |
| 503 | } |
| 504 | ] |
| 505 | ''; |
| 506 | description = '' |
| 507 | SSH public keys allowed to push/fetch as the git user. Access is |
| 508 | host-level by design: every key can reach every project. |
| 509 | |
| 510 | A key written as a plain string is anonymous — it can push, and it |
| 511 | can write nothing protected. A key written as an attribute set |
| 512 | names the principal it acts as, which is the only thing that can |
| 513 | tell one pusher from another: pushes arrive as the shared git user, |
| 514 | so identity has to ride on the key. |
| 515 | |
| 516 | Which refs a principal may write is declared, not an option here: |
| 517 | it is a statement about what the host serves, and lives in the |
| 518 | project's `protection` block in |
| 519 | {option}`services.valley.config`. Binding a name to keys is the |
| 520 | machine half — the interim form of the identity registry's |
| 521 | compilation (dcr-b87f6e8). |
| 522 | ''; |
| 523 | }; |
| 524 | |
| 525 | config = lib.mkOption { |
| 526 | type = lib.types.path; |
| 527 | example = lib.literalExpression "./valley.cue"; |
| 528 | description = '' |
| 529 | Path to the host's CUE declaration (`package valley`) — the |
| 530 | canonical statement of what this valley host serves, validated |
| 531 | against the shipped schema at build time. This is the single domain |
| 532 | input: projects, their push mirrors, and the backup policy are |
| 533 | declared here, never as Nix options. |
| 534 | ''; |
| 535 | }; |
| 536 | |
| 537 | # The event bus. Machine options, not domain: whether this machine runs |
| 538 | # a bus, where it listens, and where its storage lives. What gets |
| 539 | # published onto it is defined portably in ../schema/events.cue. |
| 540 | bus = { |
| 541 | enable = lib.mkEnableOption "the valley event bus: NATS JetStream, onto which every push's ref updates are projected as events"; |
| 542 | |
| 543 | listen = lib.mkOption { |
| 544 | type = lib.types.str; |
| 545 | default = "127.0.0.1:4222"; |
| 546 | example = "100.100.1.7:4222"; |
| 547 | description = '' |
| 548 | Address the bus listens on, `host:port`. Localhost by default: |
| 549 | nothing off the host can reach the bus until the consumer widens |
| 550 | this deliberately — e.g. to the host's tailnet address once a |
| 551 | remote `valley tail` wants in. Local publishers (the ref-updated |
| 552 | hook) connect to this same address, so it must be reachable from |
| 553 | the host itself. |
| 554 | ''; |
| 555 | }; |
| 556 | |
| 557 | storeDir = lib.mkOption { |
| 558 | type = lib.types.path; |
| 559 | default = "${cfg.dataDir}/.bus"; |
| 560 | defaultText = lib.literalExpression ''"''${config.services.valley.dataDir}/.bus"''; |
| 561 | description = '' |
| 562 | Directory holding the JetStream file storage. The default lives |
| 563 | under {option}`services.valley.dataDir`; it can never collide |
| 564 | with a repository because project names must start with an |
| 565 | alphanumeric. The stream is a projection of git, rebuildable |
| 566 | with `valley replay` — losing this directory costs one replay. |
| 567 | ''; |
| 568 | }; |
| 569 | }; |
| 570 | |
| 571 | # The integrator (design/roadmap.md, Phase 3): the controller that |
| 572 | # lands a change once its evidence still transfers to the current tip. |
| 573 | # Machine options only — whether this host runs controllers, who they |
| 574 | # are, and how often they look. Which projects get one is not an option |
| 575 | # either: it follows from which projects the declaration protects, |
| 576 | # because a protected ref is exactly what a controller exists to write. |
| 577 | integrator = { |
| 578 | enable = lib.mkEnableOption "the valley integrator: one controller per protected project, landing changes whose evidence transfers"; |
| 579 | |
| 580 | user = lib.mkOption { |
| 581 | type = lib.types.str; |
| 582 | default = "valley-integrator"; |
| 583 | description = '' |
| 584 | System user the controllers run as. Deliberately not |
| 585 | {option}`services.valley.user`: one unix identity per service is |
| 586 | the split bd-500adf7 asks for, and this service begins it. |
| 587 | |
| 588 | The account exists only to write the repositories it serves. It |
| 589 | gets no shell and no SSH key, and its primary group is |
| 590 | {option}`services.valley.group`, which is the whole of its |
| 591 | permission model: {option}`services.valley.dataDir` is already |
| 592 | group-readable, and each served repository is made group-shared |
| 593 | (group write, setgid directories) so a controller can write |
| 594 | refs, objects, and git's worktree metadata. Nothing else on the |
| 595 | host belongs to that group, and the bus's store directory (mode |
| 596 | 0700) stays out of reach. |
| 597 | |
| 598 | The key and signers files are the consumer's to provision; both |
| 599 | must be readable by this user. |
| 600 | ''; |
| 601 | }; |
| 602 | |
| 603 | signingKeyFile = lib.mkOption { |
| 604 | type = lib.types.nullOr lib.types.path; |
| 605 | default = null; |
| 606 | example = "/run/agenix/valley-integrator-key"; |
| 607 | description = '' |
| 608 | ed25519 private key the controllers sign their transfer |
| 609 | statements with. A runtime path the consumer provisions (e.g. |
| 610 | agenix), never a store path — the store is world-readable. |
| 611 | ''; |
| 612 | }; |
| 613 | |
| 614 | knownSignersFile = lib.mkOption { |
| 615 | type = lib.types.nullOr lib.types.path; |
| 616 | default = null; |
| 617 | example = "/var/lib/valley-instance/known_signers"; |
| 618 | description = '' |
| 619 | File of verifier keys whose attestations count, one per line, in |
| 620 | `attest key` format. This is the identity registry's interim |
| 621 | compilation (dcr-b87f6e8): no compiler exists yet, so the file |
| 622 | is supplied directly. Evidence signed by nobody in it does not |
| 623 | transfer, which a controller reports per check rather than |
| 624 | treating as a forgery. |
| 625 | ''; |
| 626 | }; |
| 627 | |
| 628 | instancePolicy = lib.mkOption { |
| 629 | type = lib.types.nullOr lib.types.path; |
| 630 | default = null; |
| 631 | example = "/var/lib/valley-instance/policy"; |
| 632 | description = '' |
| 633 | Directory of `*.cue` files holding the instance policy layer, |
| 634 | from a checkout of the instance repository on this host. Both |
| 635 | layers are resolved from the controller's own side — this one, |
| 636 | and the project's `policy/` at the target tip — so a change can |
| 637 | never supply the policy that gates it (bd-eaefe82). |
| 638 | ''; |
| 639 | }; |
| 640 | |
| 641 | interval = lib.mkOption { |
| 642 | type = lib.types.str; |
| 643 | default = "15s"; |
| 644 | example = "1m"; |
| 645 | description = '' |
| 646 | How long a controller waits between passes. The integrator polls |
| 647 | deliberately: integration requests are durable in git, and the |
| 648 | controller level-triggers over those refs, so a lost event costs |
| 649 | one interval and nothing more. |
| 650 | ''; |
| 651 | }; |
| 652 | }; |
| 653 | |
| 654 | # Machine integration for the declared backup policy. The declaration |
| 655 | # states WHAT must hold (offsite backup, cadence, retention); these |
| 656 | # options supply HOW on this machine — where the repository is and how |
| 657 | # to authenticate. All are paths to files the consumer provisions |
| 658 | # (e.g. agenix), required when the declaration enables backup. Point |
| 659 | # them at runtime paths, never at files in the Nix store. |
| 660 | backup = { |
| 661 | repositoryFile = lib.mkOption { |
| 662 | type = lib.types.nullOr lib.types.path; |
| 663 | default = null; |
| 664 | example = "/run/agenix/valley-restic-repo"; |
| 665 | description = '' |
| 666 | File containing the restic repository URL, for the sftp target |
| 667 | e.g. `sftp://u123456@u123456.your-storagebox.de:23//./backups/valley`. |
| 668 | ''; |
| 669 | }; |
| 670 | |
| 671 | passwordFile = lib.mkOption { |
| 672 | type = lib.types.nullOr lib.types.path; |
| 673 | default = null; |
| 674 | example = "/run/agenix/valley-restic-password"; |
| 675 | description = '' |
| 676 | File containing the restic repository encryption password. |
| 677 | Losing it loses the backups — keep a copy somewhere that |
| 678 | survives this host. |
| 679 | ''; |
| 680 | }; |
| 681 | |
| 682 | sshKeyFile = lib.mkOption { |
| 683 | type = lib.types.nullOr lib.types.path; |
| 684 | default = null; |
| 685 | example = "/run/agenix/valley-git-ssh-key"; |
| 686 | description = '' |
| 687 | SSH private key that authenticates to the sftp target. The |
| 688 | backup service runs as root, so any identity the target |
| 689 | authorizes works — reusing the mirror key is fine. |
| 690 | ''; |
| 691 | }; |
| 692 | |
| 693 | knownHostsFile = lib.mkOption { |
| 694 | type = lib.types.nullOr lib.types.path; |
| 695 | default = null; |
| 696 | example = "/var/lib/valley-backup/known_hosts"; |
| 697 | description = '' |
| 698 | known_hosts file pinning the sftp target's host key. Pin from |
| 699 | the provider's published fingerprints, not a blind ssh-keyscan. |
| 700 | ''; |
| 701 | }; |
| 702 | }; |
| 703 | }; |
| 704 | |
| 705 | config = lib.mkIf cfg.enable { |
| 706 | assertions = [ |
| 707 | { |
| 708 | assertion = cfg.authorizedKeys != [ ]; |
| 709 | message = "services.valley.authorizedKeys must not be empty — the git user would be unreachable."; |
| 710 | } |
| 711 | ] |
| 712 | ++ lib.optionals backupEnabled ( |
| 713 | lib.mapAttrsToList (name: what: { |
| 714 | assertion = cfg.backup.${name} != null; |
| 715 | message = "services.valley.backup.${name} must be set: the host declaration enables backup, and ${what} is machine integration the consumer supplies (e.g. from its secrets)."; |
| 716 | }) backupSecretOptions |
| 717 | ) |
| 718 | ++ lib.optionals cfg.integrator.enable ( |
| 719 | lib.mapAttrsToList (name: what: { |
| 720 | assertion = cfg.integrator.${name} != null; |
| 721 | message = "services.valley.integrator.${name} must be set: the integrator is enabled, and ${what} is machine integration the consumer supplies."; |
| 722 | }) integratorPathOptions |
| 723 | ++ [ |
| 724 | { |
| 725 | assertion = cfg.integrator.user != cfg.user; |
| 726 | message = "services.valley.integrator.user must differ from services.valley.user: the integrator runs under its own unix identity (bd-500adf7)."; |
| 727 | } |
| 728 | { |
| 729 | assertion = integratedProjects != { }; |
| 730 | message = "services.valley.integrator.enable is on and the host declaration protects no project — a controller only ever serves a protected target, so this would render no service at all."; |
| 731 | } |
| 732 | ] |
| 733 | ); |
| 734 | |
| 735 | users.groups.${cfg.group} = { }; |
| 736 | |
| 737 | users.users.${cfg.user} = { |
| 738 | isSystemUser = true; |
| 739 | group = cfg.group; |
| 740 | home = cfg.dataDir; |
| 741 | # git-shell only allows git-upload-pack/git-receive-pack/git-upload-archive; |
| 742 | # interactive logins are rejected (no ~/git-shell-commands). |
| 743 | shell = "${pkgs.git}/bin/git-shell"; |
| 744 | openssh.authorizedKeys.keys = map authorizedKeyLine cfg.authorizedKeys; |
| 745 | }; |
| 746 | |
| 747 | # The integrator's own account. No shell, no keys, no home of its own |
| 748 | # beyond the state directory its units get; the git group is the only |
| 749 | # thing it holds. |
| 750 | users.users.${cfg.integrator.user} = lib.mkIf cfg.integrator.enable { |
| 751 | isSystemUser = true; |
| 752 | group = cfg.group; |
| 753 | home = "/var/lib/valley-integrator"; |
| 754 | description = "The valley integrator"; |
| 755 | }; |
| 756 | |
| 757 | services.openssh.enable = lib.mkDefault true; |
| 758 | |
| 759 | # Honour the principal tag on a key's entry, and nothing else: the |
| 760 | # pattern-list form admits that one variable name. Rendered only once a |
| 761 | # key names a principal, so a host with none keeps the sshd config it |
| 762 | # had. |
| 763 | services.openssh.settings = lib.optionalAttrs (taggedKeys != [ ]) { |
| 764 | PermitUserEnvironment = principalEnv; |
| 765 | }; |
| 766 | |
| 767 | # Belt-and-braces hardening for the git user. The trailing `Match All` |
| 768 | # closes the block so it can't scope directives appended to sshd_config |
| 769 | # after this snippet. |
| 770 | services.openssh.extraConfig = '' |
| 771 | Match User ${cfg.user} |
| 772 | AllowTcpForwarding no |
| 773 | AllowAgentForwarding no |
| 774 | X11Forwarding no |
| 775 | PermitTunnel no |
| 776 | Match All |
| 777 | ''; |
| 778 | |
| 779 | # git-shell spawns git-receive-pack/git-upload-pack from PATH |
| 780 | environment.systemPackages = [ pkgs.git ]; |
| 781 | |
| 782 | systemd.tmpfiles.rules = [ |
| 783 | "d ${cfg.dataDir} 0750 ${cfg.user} ${cfg.group} - -" |
| 784 | ] |
| 785 | ++ lib.optionals cfg.bus.enable [ |
| 786 | "d ${cfg.bus.storeDir} 0700 ${cfg.user} ${cfg.group} - -" |
| 787 | ]; |
| 788 | |
| 789 | # The bus itself. It runs as the git user — the store directory lives in |
| 790 | # that user's data — but the sandbox is what confines it: the unit can |
| 791 | # write nowhere except its store directory. |
| 792 | systemd.services.valley-bus = lib.mkIf cfg.bus.enable { |
| 793 | description = "The valley event bus (NATS JetStream)"; |
| 794 | wantedBy = [ "multi-user.target" ]; |
| 795 | after = [ |
| 796 | "network.target" |
| 797 | "systemd-tmpfiles-setup.service" |
| 798 | ]; |
| 799 | unitConfig.RequiresMountsFor = cfg.bus.storeDir; |
| 800 | serviceConfig = { |
| 801 | ExecStart = "${pkgs.nats-server}/bin/nats-server -c ${busServerConfig}"; |
| 802 | Restart = "on-failure"; |
| 803 | User = cfg.user; |
| 804 | Group = cfg.group; |
| 805 | LimitNOFILE = 65536; |
| 806 | NoNewPrivileges = true; |
| 807 | ProtectSystem = "strict"; |
| 808 | ReadWritePaths = [ cfg.bus.storeDir ]; |
| 809 | ProtectHome = true; |
| 810 | PrivateTmp = true; |
| 811 | PrivateDevices = true; |
| 812 | ProtectClock = true; |
| 813 | ProtectHostname = true; |
| 814 | ProtectKernelLogs = true; |
| 815 | ProtectKernelModules = true; |
| 816 | ProtectKernelTunables = true; |
| 817 | ProtectControlGroups = true; |
| 818 | ProtectProc = "invisible"; |
| 819 | RestrictAddressFamilies = [ |
| 820 | "AF_INET" |
| 821 | "AF_INET6" |
| 822 | "AF_UNIX" |
| 823 | ]; |
| 824 | RestrictNamespaces = true; |
| 825 | RestrictRealtime = true; |
| 826 | RestrictSUIDSGID = true; |
| 827 | LockPersonality = true; |
| 828 | CapabilityBoundingSet = ""; |
| 829 | SystemCallArchitectures = "native"; |
| 830 | SystemCallFilter = [ "@system-service" ]; |
| 831 | UMask = "0077"; |
| 832 | }; |
| 833 | }; |
| 834 | |
| 835 | # The stream is config, not state: (re)created idempotently on every |
| 836 | # boot, capturing everything under valley.>. An existing stream — and |
| 837 | # the events in it — is left untouched. |
| 838 | systemd.services.valley-bus-init = lib.mkIf cfg.bus.enable { |
| 839 | description = "Create the valley event stream"; |
| 840 | wantedBy = [ "multi-user.target" ]; |
| 841 | requires = [ "valley-bus.service" ]; |
| 842 | after = [ "valley-bus.service" ]; |
| 843 | serviceConfig = { |
| 844 | Type = "oneshot"; |
| 845 | User = cfg.user; |
| 846 | Group = cfg.group; |
| 847 | }; |
| 848 | path = [ pkgs.natscli ]; |
| 849 | script = '' |
| 850 | # The server needs a moment to accept connections after startup. |
| 851 | for _ in $(seq 1 50); do |
| 852 | nats --server ${busUrl} stream ls >/dev/null 2>&1 && break |
| 853 | sleep 0.2 |
| 854 | done |
| 855 | if ! nats --server ${busUrl} stream info valley >/dev/null 2>&1; then |
| 856 | nats --server ${busUrl} stream add valley \ |
| 857 | --subjects 'valley.>' --storage file --defaults |
| 858 | fi |
| 859 | ''; |
| 860 | }; |
| 861 | |
| 862 | # Create missing bare repos and (re)wire the managed hooks on every |
| 863 | # activation where the declaration changed. |
| 864 | systemd.services.valley-init = { |
| 865 | description = "Initialize the valley host's bare git repositories"; |
| 866 | wantedBy = [ "multi-user.target" ]; |
| 867 | after = [ "systemd-tmpfiles-setup.service" ]; |
| 868 | unitConfig.RequiresMountsFor = cfg.dataDir; |
| 869 | serviceConfig = { |
| 870 | Type = "oneshot"; |
| 871 | User = cfg.user; |
| 872 | Group = cfg.group; |
| 873 | }; |
| 874 | path = [ pkgs.git ]; |
| 875 | script = '' |
| 876 | repos=( ${lib.escapeShellArgs repoNames} ) |
| 877 | for name in "''${repos[@]}"; do |
| 878 | repo="${cfg.dataDir}/$name.git" |
| 879 | # Check HEAD rather than the directory itself so a pre-existing |
| 880 | # empty directory still gets initialized. |
| 881 | if [ ! -e "$repo/HEAD" ]; then |
| 882 | git init --bare --initial-branch=main "$repo" |
| 883 | fi |
| 884 | |
| 885 | # Hook scaffolding: post-receive dispatches to post-receive.d/. |
| 886 | # Only manage the hook if it is absent or already ours (a store |
| 887 | # symlink) — a hand-written hook is left alone. |
| 888 | mkdir -p "$repo/hooks/post-receive.d" |
| 889 | hook="$repo/hooks/post-receive" |
| 890 | if [ -L "$hook" ]; then |
| 891 | case "$(readlink "$hook")" in |
| 892 | /nix/store/*) ln -sfn ${postReceiveDispatch} "$hook" ;; |
| 893 | esac |
| 894 | elif [ ! -e "$hook" ]; then |
| 895 | ln -s ${postReceiveDispatch} "$hook" |
| 896 | fi |
| 897 | done |
| 898 | |
| 899 | # Per-project push-mirror hooks. |
| 900 | ${mirrorHookCommands} |
| 901 | |
| 902 | # The one structural invariant, on every project declared protected. |
| 903 | ${protectHookCommands} |
| 904 | |
| 905 | # The ref-updated publisher hook, on every repo when the bus is on. |
| 906 | ${busHookCommands} |
| 907 | |
| 908 | # Group-shared repositories, on what the integrator serves. |
| 909 | ${lib.optionalString cfg.integrator.enable integratorShareCommands} |
| 910 | ''; |
| 911 | }; |
| 912 | |
| 913 | # One controller per served project, as instances of a single template: |
| 914 | # the units differ only in the repository they serve, which is the |
| 915 | # instance name. `watch` rather than a timer firing `reconcile`: the |
| 916 | # binary owns its poll loop and its interval, and a timer would be a |
| 917 | # second cadence over the same level-triggered refs. |
| 918 | systemd.services."valley-integrator@" = lib.mkIf cfg.integrator.enable { |
| 919 | description = "The valley integrator for %i"; |
| 920 | after = [ |
| 921 | "network.target" |
| 922 | "valley-init.service" |
| 923 | ] |
| 924 | ++ lib.optional cfg.bus.enable "valley-bus-init.service"; |
| 925 | requires = [ "valley-init.service" ]; |
| 926 | unitConfig.RequiresMountsFor = cfg.dataDir; |
| 927 | # nix is not pinned, for the reason the integrator package does not |
| 928 | # pin it: a check's input closure must be recomputed by the nix this |
| 929 | # machine actually runs. Everything else a controller drives is |
| 930 | # pinned inside the wrapper. |
| 931 | path = [ config.nix.package ]; |
| 932 | # The repositories belong to the git user and the controller does |
| 933 | # not, so git refuses to touch them as dubiously owned. The exception |
| 934 | # rides in the unit's environment, for two reasons. GIT_CONFIG_* is |
| 935 | # command-scope config, which is protected config, so safe.directory |
| 936 | # is honoured from it — a repository-scoped setting would be ignored, |
| 937 | # deliberately. And every git the controller drives is a child of |
| 938 | # this unit, whether run by the integrator itself or by attest or the |
| 939 | # deriver, so one environment covers all of them. |
| 940 | # |
| 941 | # One repository per instance: never a global gitconfig, never a |
| 942 | # wildcard. Every other repository on the host stays refused. Linked |
| 943 | # worktrees need no entry of their own — the controller creates them, |
| 944 | # so it owns them, and git checks the worktree and its gitdir rather |
| 945 | # than the repository they point at. |
| 946 | environment = { |
| 947 | GIT_CONFIG_COUNT = "1"; |
| 948 | GIT_CONFIG_KEY_0 = "safe.directory"; |
| 949 | GIT_CONFIG_VALUE_0 = "${cfg.dataDir}/%i.git"; |
| 950 | }; |
| 951 | serviceConfig = { |
| 952 | ExecStart = integratorCommand; |
| 953 | Restart = "on-failure"; |
| 954 | RestartSec = "10s"; |
| 955 | User = cfg.integrator.user; |
| 956 | Group = cfg.group; |
| 957 | StateDirectory = "valley-integrator"; |
| 958 | StateDirectoryMode = "0700"; |
| 959 | WorkingDirectory = "/var/lib/valley-integrator"; |
| 960 | NoNewPrivileges = true; |
| 961 | ProtectSystem = "strict"; |
| 962 | # The one repository it serves, and the nix daemon's socket — |
| 963 | # recomputing a closure means asking the daemon, and connecting to |
| 964 | # a unix socket needs write access to it. |
| 965 | ReadWritePaths = [ |
| 966 | "${cfg.dataDir}/%i.git" |
| 967 | "/nix/var/nix/daemon-socket" |
| 968 | ]; |
| 969 | ProtectHome = true; |
| 970 | PrivateTmp = true; |
| 971 | PrivateDevices = true; |
| 972 | ProtectClock = true; |
| 973 | ProtectHostname = true; |
| 974 | ProtectKernelLogs = true; |
| 975 | ProtectKernelModules = true; |
| 976 | ProtectKernelTunables = true; |
| 977 | ProtectControlGroups = true; |
| 978 | ProtectProc = "invisible"; |
| 979 | RestrictAddressFamilies = [ |
| 980 | "AF_INET" |
| 981 | "AF_INET6" |
| 982 | "AF_UNIX" |
| 983 | ]; |
| 984 | RestrictNamespaces = true; |
| 985 | RestrictRealtime = true; |
| 986 | RestrictSUIDSGID = true; |
| 987 | LockPersonality = true; |
| 988 | CapabilityBoundingSet = ""; |
| 989 | SystemCallArchitectures = "native"; |
| 990 | SystemCallFilter = [ "@system-service" ]; |
| 991 | # Group write, not the bus unit's 0077: the git user owns these |
| 992 | # repositories and has to stay able to write what a controller |
| 993 | # leaves in them. |
| 994 | UMask = "0002"; |
| 995 | }; |
| 996 | }; |
| 997 | |
| 998 | # wantedBy on a template unit enables nothing, so the instances the |
| 999 | # declaration asks for are pulled in by name. |
| 1000 | systemd.targets.valley-integrators = lib.mkIf cfg.integrator.enable { |
| 1001 | description = "The valley integrator's controllers"; |
| 1002 | wantedBy = [ "multi-user.target" ]; |
| 1003 | wants = map (name: "valley-integrator@${name}.service") (lib.attrNames integratedProjects); |
| 1004 | }; |
| 1005 | |
| 1006 | # Offsite backup, rendered only when the declaration asks for it. The |
| 1007 | # declaration states the policy — that backup exists, its cadence and |
| 1008 | # retention; the services.valley.backup.* options supply the machine |
| 1009 | # half. Declaration absent or disabled ⇒ no restic config at all. |
| 1010 | services.restic.backups = lib.mkIf backupEnabled { |
| 1011 | valley = { |
| 1012 | initialize = true; |
| 1013 | repositoryFile = cfg.backup.repositoryFile; |
| 1014 | passwordFile = cfg.backup.passwordFile; |
| 1015 | paths = [ cfg.dataDir ]; |
| 1016 | # The schema admits only the restic-sftp target today; a second |
| 1017 | # target would grow a dispatch here. The service runs as root: |
| 1018 | # authenticate with the supplied identity and only the pinned |
| 1019 | # host key. No prompt can be answered inside a systemd unit, so |
| 1020 | # BatchMode + strict host key checking turn a would-be hang into |
| 1021 | # an immediate error. |
| 1022 | extraOptions = [ |
| 1023 | "sftp.args='-i ${cfg.backup.sshKeyFile} -o UserKnownHostsFile=${cfg.backup.knownHostsFile} -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=yes'" |
| 1024 | ]; |
| 1025 | timerConfig = backupTimer.${backupPolicy.cadence}; |
| 1026 | pruneOpts = [ |
| 1027 | "--keep-daily ${toString backupPolicy.retention.daily}" |
| 1028 | "--keep-weekly ${toString backupPolicy.retention.weekly}" |
| 1029 | "--keep-monthly ${toString backupPolicy.retention.monthly}" |
| 1030 | ]; |
| 1031 | }; |
| 1032 | }; |
| 1033 | }; |
| 1034 | } |