| 1 | { |
| 2 | description = "the-valley: the CUE domain schema for a valley host, and the NixOS module that installs one"; |
| 3 | |
| 4 | inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11"; |
| 5 | |
| 6 | outputs = |
| 7 | { self, nixpkgs }: |
| 8 | let |
| 9 | lib = nixpkgs.lib; |
| 10 | # Checks use import-from-derivation (the module's cue export), so they |
| 11 | # are only defined for the system that can actually build them here. |
| 12 | systems = [ "x86_64-linux" ]; |
| 13 | |
| 14 | # The integrator's CLI (bin/valley) wrapped for `nix run`. The script |
| 15 | # itself must keep running bare from any checkout — the package is the |
| 16 | # second of its two shipping modes, never a dependency of the first. |
| 17 | valleyScriptFor = |
| 18 | pkgs: |
| 19 | pkgs.writeShellApplication { |
| 20 | name = "valley"; |
| 21 | runtimeInputs = [ pkgs.git ]; |
| 22 | text = builtins.readFile ./bin/valley; |
| 23 | }; |
| 24 | |
| 25 | # The installed package: the wrapped script plus the shell completions, |
| 26 | # at the standard paths home-manager/NixOS auto-link. |
| 27 | valleyCliFor = |
| 28 | pkgs: |
| 29 | pkgs.symlinkJoin { |
| 30 | name = "valley"; |
| 31 | paths = [ (valleyScriptFor pkgs) ]; |
| 32 | nativeBuildInputs = [ pkgs.installShellFiles ]; |
| 33 | postBuild = '' |
| 34 | installShellCompletion --bash --name valley ${./completions/valley.bash} |
| 35 | installShellCompletion --zsh --name _valley ${./completions/_valley} |
| 36 | ''; |
| 37 | }; |
| 38 | |
| 39 | # Markdown prose is filled paragraphs hard-wrapped at 100 columns |
| 40 | # (ida-1ec03b1). One prettier invocation backs both the formatter app |
| 41 | # and the prose-format check, so the two cannot drift. |
| 42 | # Embedded-language formatting is off so fenced code blocks pass |
| 43 | # through byte-for-byte. |
| 44 | proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off"; |
| 45 | |
| 46 | # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns. |
| 47 | proseFmtFor = |
| 48 | pkgs: |
| 49 | pkgs.writeShellApplication { |
| 50 | name = "valley-fmt"; |
| 51 | runtimeInputs = [ |
| 52 | pkgs.git |
| 53 | pkgs.prettier |
| 54 | ]; |
| 55 | text = '' |
| 56 | cd "$(git rev-parse --show-toplevel)" |
| 57 | git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \ |
| 58 | prettier ${proseFmtArgs} --write |
| 59 | ''; |
| 60 | }; |
| 61 | in |
| 62 | { |
| 63 | nixosModules.valley-host = ./nix/valley-host.nix; |
| 64 | nixosModules.default = self.nixosModules.valley-host; |
| 65 | |
| 66 | apps = lib.genAttrs systems (system: { |
| 67 | fmt = { |
| 68 | type = "app"; |
| 69 | program = lib.getExe (proseFmtFor nixpkgs.legacyPackages.${system}); |
| 70 | }; |
| 71 | }); |
| 72 | |
| 73 | packages = lib.genAttrs systems ( |
| 74 | system: |
| 75 | let |
| 76 | valley = valleyCliFor nixpkgs.legacyPackages.${system}; |
| 77 | in |
| 78 | { |
| 79 | inherit valley; |
| 80 | default = valley; |
| 81 | } |
| 82 | ); |
| 83 | |
| 84 | checks = lib.genAttrs systems ( |
| 85 | system: |
| 86 | let |
| 87 | pkgs = nixpkgs.legacyPackages.${system}; |
| 88 | |
| 89 | # Eval-only instantiation of the module — catches option-surface |
| 90 | # and cue-export regressions without building or booting anything. |
| 91 | # Each check host shares the machine baseline and supplies its own |
| 92 | # declaration (and, where the declaration asks for backup, the |
| 93 | # secret paths). |
| 94 | mkHost = |
| 95 | module: |
| 96 | lib.nixosSystem { |
| 97 | inherit system; |
| 98 | modules = [ |
| 99 | self.nixosModules.default |
| 100 | { |
| 101 | fileSystems."/" = { |
| 102 | device = "/dev/disk/by-label/nixos"; |
| 103 | fsType = "ext4"; |
| 104 | }; |
| 105 | boot.loader.grub.enable = false; |
| 106 | system.stateVersion = "25.11"; |
| 107 | services.valley = { |
| 108 | enable = true; |
| 109 | authorizedKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck0 valley-check" ]; |
| 110 | }; |
| 111 | } |
| 112 | module |
| 113 | ]; |
| 114 | }; |
| 115 | |
| 116 | # The example declaration enables backup, so this host supplies |
| 117 | # the four secret paths (eval-only placeholders). |
| 118 | host = mkHost { |
| 119 | services.valley = { |
| 120 | config = ./examples/host.cue; |
| 121 | backup = { |
| 122 | repositoryFile = "/run/agenix/valley-restic-repo"; |
| 123 | passwordFile = "/run/agenix/valley-restic-password"; |
| 124 | sshKeyFile = "/run/agenix/valley-git-ssh-key"; |
| 125 | knownHostsFile = "/var/lib/valley-backup/known_hosts"; |
| 126 | }; |
| 127 | }; |
| 128 | }; |
| 129 | |
| 130 | # A declaration without a backup block predates the field and must |
| 131 | # keep evaluating exactly as before: zero restic machinery. |
| 132 | noBackupHost = mkHost { |
| 133 | services.valley.config = pkgs.writeText "no-backup-host.cue" '' |
| 134 | package valley |
| 135 | projects: "the-valley": {} |
| 136 | ''; |
| 137 | }; |
| 138 | |
| 139 | # The example declaration with the secret paths left unset: the |
| 140 | # module must refuse with assertions naming the missing options. |
| 141 | noSecretsHost = mkHost { |
| 142 | services.valley.config = ./examples/host.cue; |
| 143 | }; |
| 144 | |
| 145 | failedAssertions = builtins.filter (a: !a.assertion) ( |
| 146 | host.config.assertions ++ noBackupHost.config.assertions |
| 147 | ); |
| 148 | |
| 149 | missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions; |
| 150 | |
| 151 | resticRenderedWithoutDeclaration = |
| 152 | noBackupHost.config.systemd.services ? "restic-backups-valley" |
| 153 | || noBackupHost.config.systemd.timers ? "restic-backups-valley"; |
| 154 | in |
| 155 | { |
| 156 | # The CLI must stay a lint-clean script whose help verb answers |
| 157 | # without a repo or a remote — the cheap end of its eviction clause |
| 158 | # (dcr-74c3158); anything needing more graduates instead. The |
| 159 | # completions are held to the same bar, and the installed package |
| 160 | # must carry them at the auto-linked paths. |
| 161 | valley-cli = |
| 162 | let |
| 163 | valley = valleyCliFor pkgs; |
| 164 | in |
| 165 | pkgs.runCommand "valley-cli" |
| 166 | { |
| 167 | nativeBuildInputs = [ |
| 168 | pkgs.shellcheck |
| 169 | pkgs.zsh |
| 170 | valley |
| 171 | ]; |
| 172 | } |
| 173 | '' |
| 174 | shellcheck ${./bin/valley} |
| 175 | shellcheck ${./completions/valley.bash} |
| 176 | # shellcheck cannot lint zsh; a bare parse is the cheap check. |
| 177 | zsh -n ${./completions/_valley} |
| 178 | valley help > help.txt |
| 179 | grep -q '^usage: valley' help.txt |
| 180 | test -f ${valley}/share/bash-completion/completions/valley |
| 181 | test -f ${valley}/share/zsh/site-functions/_valley |
| 182 | touch $out |
| 183 | ''; |
| 184 | |
| 185 | # Markdown prose format (ida-1ec03b1): the check is formatter |
| 186 | # idempotence — formatting the tree must change nothing. On failure, |
| 187 | # fix with `nix run .#fmt`. |
| 188 | prose-format = |
| 189 | pkgs.runCommand "valley-prose-format" |
| 190 | { |
| 191 | nativeBuildInputs = [ pkgs.prettier ]; |
| 192 | } |
| 193 | '' |
| 194 | cd ${self} |
| 195 | find . -name '*.md' -print0 | xargs -0 --no-run-if-empty \ |
| 196 | prettier ${proseFmtArgs} --check |
| 197 | touch $out |
| 198 | ''; |
| 199 | |
| 200 | # The example host declaration must vet against the schema, and the |
| 201 | # schema must reject what it claims to reject: unsafe project names, |
| 202 | # unknown project fields, stray top-level fields (typos, deployment |
| 203 | # concerns), and malformed backup policy (machine concerns, targets |
| 204 | # without an implementation, non-positive retention). CUE closedness |
| 205 | # is easy to regress silently — e.g. embedding #Host instead of |
| 206 | # referencing it opens the schema — so the rejections are pinned |
| 207 | # here. |
| 208 | cue-vet = |
| 209 | let |
| 210 | invalidConfigs = { |
| 211 | unsafe-project-name = '' |
| 212 | package valley |
| 213 | projects: ".hidden": {} |
| 214 | ''; |
| 215 | unknown-project-field = '' |
| 216 | package valley |
| 217 | projects: ok: dataDir: "/srv/git" |
| 218 | ''; |
| 219 | top-level-typo = '' |
| 220 | package valley |
| 221 | project: oops: {} |
| 222 | ''; |
| 223 | deployment-concern-in-cue = '' |
| 224 | package valley |
| 225 | user: "git" |
| 226 | projects: ok: {} |
| 227 | ''; |
| 228 | machine-concern-in-backup = '' |
| 229 | package valley |
| 230 | projects: ok: {} |
| 231 | backup: repositoryFile: "/run/agenix/valley-restic-repo" |
| 232 | ''; |
| 233 | unimplemented-backup-target = '' |
| 234 | package valley |
| 235 | projects: ok: {} |
| 236 | backup: target: "restic-s3" |
| 237 | ''; |
| 238 | negative-retention = '' |
| 239 | package valley |
| 240 | projects: ok: {} |
| 241 | backup: retention: daily: -1 |
| 242 | ''; |
| 243 | }; |
| 244 | in |
| 245 | pkgs.runCommand "valley-cue-vet" |
| 246 | { |
| 247 | nativeBuildInputs = [ pkgs.cue ]; |
| 248 | } |
| 249 | '' |
| 250 | cue vet -c ${./schema/valley.cue} ${./examples/host.cue} |
| 251 | cue export ${./schema/valley.cue} ${./examples/host.cue} > example.json |
| 252 | grep -q 'gunk-dev/the-valley' example.json |
| 253 | grep -q 'restic-sftp' example.json |
| 254 | |
| 255 | # A declaration without a backup block must stay valid — |
| 256 | # consumers written before the field existed keep vetting. |
| 257 | cue vet -c ${./schema/valley.cue} ${pkgs.writeText "no-backup.cue" '' |
| 258 | package valley |
| 259 | projects: "the-valley": {} |
| 260 | ''} |
| 261 | |
| 262 | ${lib.concatStrings ( |
| 263 | lib.mapAttrsToList (name: cfg: '' |
| 264 | if cue vet -c ${./schema/valley.cue} ${pkgs.writeText "${name}.cue" cfg}; then |
| 265 | echo "cue-vet: expected invalid config '${name}' to be rejected" >&2 |
| 266 | exit 1 |
| 267 | fi |
| 268 | '') invalidConfigs |
| 269 | )} |
| 270 | touch $out |
| 271 | ''; |
| 272 | |
| 273 | module-eval = |
| 274 | if failedAssertions != [ ] then |
| 275 | throw "valley module-eval: failed assertions: ${lib.concatMapStringsSep "; " (a: a.message) failedAssertions}" |
| 276 | else if resticRenderedWithoutDeclaration then |
| 277 | throw "valley module-eval: restic machinery rendered for a declaration without a backup block" |
| 278 | else if |
| 279 | !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions) |
| 280 | then |
| 281 | throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them" |
| 282 | else |
| 283 | pkgs.runCommand "valley-module-eval" |
| 284 | { |
| 285 | initScript = host.config.systemd.services.valley-init.script; |
| 286 | sshdConfig = host.config.services.openssh.extraConfig; |
| 287 | resticService = host.config.systemd.units."restic-backups-valley.service".text; |
| 288 | resticTimer = host.config.systemd.units."restic-backups-valley.timer".text; |
| 289 | passAsFile = [ |
| 290 | "initScript" |
| 291 | "sshdConfig" |
| 292 | "resticService" |
| 293 | "resticTimer" |
| 294 | ]; |
| 295 | } |
| 296 | '' |
| 297 | # The repo init list and mirror hook must be generated from |
| 298 | # the CUE export, and the git user's sshd Match block must |
| 299 | # be terminated so it cannot scope later config. |
| 300 | grep -q "the-valley" "$initScriptPath" |
| 301 | grep -q "valley-mirrors" "$initScriptPath" |
| 302 | grep -q "Match All" "$sshdConfigPath" |
| 303 | |
| 304 | # The mirror push must use explicit head/tag refspecs with |
| 305 | # --prune, never --mirror: --mirror also deletes remote-only |
| 306 | # refs, and GitHub's read-only refs/pull/* fails every such |
| 307 | # push. Follow the hook chain from the init script to the |
| 308 | # rendered push script and pin the invocation there. |
| 309 | mirrorHook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-[^ ]*' "$initScriptPath" | head -n1)" |
| 310 | mirrorPush="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-[^ ]*' "$mirrorHook" | head -n1)" |
| 311 | grep -q -- 'push --prune' "$mirrorPush" |
| 312 | grep -qF -- '+refs/heads/*:refs/heads/*' "$mirrorPush" |
| 313 | grep -qF -- '+refs/tags/*:refs/tags/*' "$mirrorPush" |
| 314 | if grep -q -- '--mirror' "$mirrorPush"; then |
| 315 | echo "module-eval: mirror push regressed to --mirror" >&2 |
| 316 | exit 1 |
| 317 | fi |
| 318 | |
| 319 | # The rendered restic units must back up the data directory |
| 320 | # to the consumer-supplied repository with the declared |
| 321 | # retention over a pinned host key, on the declared cadence. |
| 322 | grep -q "RESTIC_REPOSITORY_FILE=/run/agenix/valley-restic-repo" "$resticServicePath" |
| 323 | grep -q -- "--keep-daily 7 --keep-weekly 4 --keep-monthly 6" "$resticServicePath" |
| 324 | grep -q "UserKnownHostsFile=/var/lib/valley-backup/known_hosts" "$resticServicePath" |
| 325 | grep -q "BatchMode=yes" "$resticServicePath" |
| 326 | grep -q "OnCalendar=03:30" "$resticTimerPath" |
| 327 | |
| 328 | # The backup paths render as a --files-from list: follow |
| 329 | # ExecStartPre to that list and pin the data directory. |
| 330 | preStart="$(sed -n 's/^ExecStartPre=//p' "$resticServicePath" | head -n1)" |
| 331 | staticPaths="$(grep -o '/nix/store/[^ ]*-staticPaths' "$preStart" | head -n1)" |
| 332 | grep -qx "/srv/git" "$staticPaths" |
| 333 | touch $out |
| 334 | ''; |
| 335 | } |
| 336 | ); |
| 337 | }; |
| 338 | } |