| 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 | in |
| 14 | { |
| 15 | nixosModules.valley-host = ./nix/valley-host.nix; |
| 16 | nixosModules.default = self.nixosModules.valley-host; |
| 17 | |
| 18 | checks = lib.genAttrs systems ( |
| 19 | system: |
| 20 | let |
| 21 | pkgs = nixpkgs.legacyPackages.${system}; |
| 22 | |
| 23 | # Eval-only instantiation of the module — catches option-surface |
| 24 | # and cue-export regressions without building or booting anything. |
| 25 | # Each check host shares the machine baseline and supplies its own |
| 26 | # declaration (and, where the declaration asks for backup, the |
| 27 | # secret paths). |
| 28 | mkHost = |
| 29 | module: |
| 30 | lib.nixosSystem { |
| 31 | inherit system; |
| 32 | modules = [ |
| 33 | self.nixosModules.default |
| 34 | { |
| 35 | fileSystems."/" = { |
| 36 | device = "/dev/disk/by-label/nixos"; |
| 37 | fsType = "ext4"; |
| 38 | }; |
| 39 | boot.loader.grub.enable = false; |
| 40 | system.stateVersion = "25.11"; |
| 41 | services.valley = { |
| 42 | enable = true; |
| 43 | authorizedKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck0 valley-check" ]; |
| 44 | }; |
| 45 | } |
| 46 | module |
| 47 | ]; |
| 48 | }; |
| 49 | |
| 50 | # The example declaration enables backup, so this host supplies |
| 51 | # the four secret paths (eval-only placeholders). |
| 52 | host = mkHost { |
| 53 | services.valley = { |
| 54 | config = ./examples/host.cue; |
| 55 | backup = { |
| 56 | repositoryFile = "/run/agenix/valley-restic-repo"; |
| 57 | passwordFile = "/run/agenix/valley-restic-password"; |
| 58 | sshKeyFile = "/run/agenix/valley-git-ssh-key"; |
| 59 | knownHostsFile = "/var/lib/valley-backup/known_hosts"; |
| 60 | }; |
| 61 | }; |
| 62 | }; |
| 63 | |
| 64 | # A declaration without a backup block predates the field and must |
| 65 | # keep evaluating exactly as before: zero restic machinery. |
| 66 | noBackupHost = mkHost { |
| 67 | services.valley.config = pkgs.writeText "no-backup-host.cue" '' |
| 68 | package valley |
| 69 | projects: "the-valley": {} |
| 70 | ''; |
| 71 | }; |
| 72 | |
| 73 | # The example declaration with the secret paths left unset: the |
| 74 | # module must refuse with assertions naming the missing options. |
| 75 | noSecretsHost = mkHost { |
| 76 | services.valley.config = ./examples/host.cue; |
| 77 | }; |
| 78 | |
| 79 | failedAssertions = builtins.filter (a: !a.assertion) ( |
| 80 | host.config.assertions ++ noBackupHost.config.assertions |
| 81 | ); |
| 82 | |
| 83 | missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions; |
| 84 | |
| 85 | resticRenderedWithoutDeclaration = |
| 86 | noBackupHost.config.systemd.services ? "restic-backups-valley" |
| 87 | || noBackupHost.config.systemd.timers ? "restic-backups-valley"; |
| 88 | in |
| 89 | { |
| 90 | # The example host declaration must vet against the schema, and the |
| 91 | # schema must reject what it claims to reject: unsafe project names, |
| 92 | # unknown project fields, stray top-level fields (typos, deployment |
| 93 | # concerns), and malformed backup policy (machine concerns, targets |
| 94 | # without an implementation, non-positive retention). CUE closedness |
| 95 | # is easy to regress silently — e.g. embedding #Host instead of |
| 96 | # referencing it opens the schema — so the rejections are pinned |
| 97 | # here. |
| 98 | cue-vet = |
| 99 | let |
| 100 | invalidConfigs = { |
| 101 | unsafe-project-name = '' |
| 102 | package valley |
| 103 | projects: ".hidden": {} |
| 104 | ''; |
| 105 | unknown-project-field = '' |
| 106 | package valley |
| 107 | projects: ok: dataDir: "/srv/git" |
| 108 | ''; |
| 109 | top-level-typo = '' |
| 110 | package valley |
| 111 | project: oops: {} |
| 112 | ''; |
| 113 | deployment-concern-in-cue = '' |
| 114 | package valley |
| 115 | user: "git" |
| 116 | projects: ok: {} |
| 117 | ''; |
| 118 | machine-concern-in-backup = '' |
| 119 | package valley |
| 120 | projects: ok: {} |
| 121 | backup: repositoryFile: "/run/agenix/valley-restic-repo" |
| 122 | ''; |
| 123 | unimplemented-backup-target = '' |
| 124 | package valley |
| 125 | projects: ok: {} |
| 126 | backup: target: "restic-s3" |
| 127 | ''; |
| 128 | negative-retention = '' |
| 129 | package valley |
| 130 | projects: ok: {} |
| 131 | backup: retention: daily: -1 |
| 132 | ''; |
| 133 | }; |
| 134 | in |
| 135 | pkgs.runCommand "valley-cue-vet" |
| 136 | { |
| 137 | nativeBuildInputs = [ pkgs.cue ]; |
| 138 | } |
| 139 | '' |
| 140 | cue vet -c ${./schema/valley.cue} ${./examples/host.cue} |
| 141 | cue export ${./schema/valley.cue} ${./examples/host.cue} > example.json |
| 142 | grep -q 'gunk-dev/the-valley' example.json |
| 143 | grep -q 'restic-sftp' example.json |
| 144 | |
| 145 | # A declaration without a backup block must stay valid — |
| 146 | # consumers written before the field existed keep vetting. |
| 147 | cue vet -c ${./schema/valley.cue} ${pkgs.writeText "no-backup.cue" '' |
| 148 | package valley |
| 149 | projects: "the-valley": {} |
| 150 | ''} |
| 151 | |
| 152 | ${lib.concatStrings ( |
| 153 | lib.mapAttrsToList (name: cfg: '' |
| 154 | if cue vet -c ${./schema/valley.cue} ${pkgs.writeText "${name}.cue" cfg}; then |
| 155 | echo "cue-vet: expected invalid config '${name}' to be rejected" >&2 |
| 156 | exit 1 |
| 157 | fi |
| 158 | '') invalidConfigs |
| 159 | )} |
| 160 | touch $out |
| 161 | ''; |
| 162 | |
| 163 | module-eval = |
| 164 | if failedAssertions != [ ] then |
| 165 | throw "valley module-eval: failed assertions: ${lib.concatMapStringsSep "; " (a: a.message) failedAssertions}" |
| 166 | else if resticRenderedWithoutDeclaration then |
| 167 | throw "valley module-eval: restic machinery rendered for a declaration without a backup block" |
| 168 | else if |
| 169 | !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions) |
| 170 | then |
| 171 | throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them" |
| 172 | else |
| 173 | pkgs.runCommand "valley-module-eval" |
| 174 | { |
| 175 | initScript = host.config.systemd.services.valley-init.script; |
| 176 | sshdConfig = host.config.services.openssh.extraConfig; |
| 177 | resticService = host.config.systemd.units."restic-backups-valley.service".text; |
| 178 | resticTimer = host.config.systemd.units."restic-backups-valley.timer".text; |
| 179 | passAsFile = [ |
| 180 | "initScript" |
| 181 | "sshdConfig" |
| 182 | "resticService" |
| 183 | "resticTimer" |
| 184 | ]; |
| 185 | } |
| 186 | '' |
| 187 | # The repo init list and mirror hook must be generated from |
| 188 | # the CUE export, and the git user's sshd Match block must |
| 189 | # be terminated so it cannot scope later config. |
| 190 | grep -q "the-valley" "$initScriptPath" |
| 191 | grep -q "valley-mirrors" "$initScriptPath" |
| 192 | grep -q "Match All" "$sshdConfigPath" |
| 193 | |
| 194 | # The mirror push must use explicit head/tag refspecs with |
| 195 | # --prune, never --mirror: --mirror also deletes remote-only |
| 196 | # refs, and GitHub's read-only refs/pull/* fails every such |
| 197 | # push. Follow the hook chain from the init script to the |
| 198 | # rendered push script and pin the invocation there. |
| 199 | mirrorHook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-[^ ]*' "$initScriptPath" | head -n1)" |
| 200 | mirrorPush="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-[^ ]*' "$mirrorHook" | head -n1)" |
| 201 | grep -q -- 'push --prune' "$mirrorPush" |
| 202 | grep -qF -- '+refs/heads/*:refs/heads/*' "$mirrorPush" |
| 203 | grep -qF -- '+refs/tags/*:refs/tags/*' "$mirrorPush" |
| 204 | if grep -q -- '--mirror' "$mirrorPush"; then |
| 205 | echo "module-eval: mirror push regressed to --mirror" >&2 |
| 206 | exit 1 |
| 207 | fi |
| 208 | |
| 209 | # The rendered restic units must back up the data directory |
| 210 | # to the consumer-supplied repository with the declared |
| 211 | # retention over a pinned host key, on the declared cadence. |
| 212 | grep -q "RESTIC_REPOSITORY_FILE=/run/agenix/valley-restic-repo" "$resticServicePath" |
| 213 | grep -q -- "--keep-daily 7 --keep-weekly 4 --keep-monthly 6" "$resticServicePath" |
| 214 | grep -q "UserKnownHostsFile=/var/lib/valley-backup/known_hosts" "$resticServicePath" |
| 215 | grep -q "BatchMode=yes" "$resticServicePath" |
| 216 | grep -q "OnCalendar=03:30" "$resticTimerPath" |
| 217 | |
| 218 | # The backup paths render as a --files-from list: follow |
| 219 | # ExecStartPre to that list and pin the data directory. |
| 220 | preStart="$(sed -n 's/^ExecStartPre=//p' "$resticServicePath" | head -n1)" |
| 221 | staticPaths="$(grep -o '/nix/store/[^ ]*-staticPaths' "$preStart" | head -n1)" |
| 222 | grep -qx "/srv/git" "$staticPaths" |
| 223 | touch $out |
| 224 | ''; |
| 225 | } |
| 226 | ); |
| 227 | }; |
| 228 | } |