| 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 with the example config โ |
| 24 | # catches option-surface and cue-export regressions without |
| 25 | # building or booting anything. |
| 26 | host = lib.nixosSystem { |
| 27 | inherit system; |
| 28 | modules = [ |
| 29 | self.nixosModules.default |
| 30 | { |
| 31 | fileSystems."/" = { |
| 32 | device = "/dev/disk/by-label/nixos"; |
| 33 | fsType = "ext4"; |
| 34 | }; |
| 35 | boot.loader.grub.enable = false; |
| 36 | system.stateVersion = "25.11"; |
| 37 | services.valley = { |
| 38 | enable = true; |
| 39 | authorizedKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck0 valley-check" ]; |
| 40 | config = ./examples/host.cue; |
| 41 | }; |
| 42 | } |
| 43 | ]; |
| 44 | }; |
| 45 | failedAssertions = builtins.filter (a: !a.assertion) host.config.assertions; |
| 46 | in |
| 47 | { |
| 48 | # The example host declaration must vet against the schema, and the |
| 49 | # schema must reject what it claims to reject: unsafe project names, |
| 50 | # unknown project fields, and stray top-level fields (typos, |
| 51 | # deployment concerns). CUE closedness is easy to regress silently โ |
| 52 | # e.g. embedding #Host instead of referencing it opens the schema โ |
| 53 | # so the rejections are pinned here. |
| 54 | cue-vet = |
| 55 | let |
| 56 | invalidConfigs = { |
| 57 | unsafe-project-name = '' |
| 58 | package valley |
| 59 | projects: ".hidden": {} |
| 60 | ''; |
| 61 | unknown-project-field = '' |
| 62 | package valley |
| 63 | projects: ok: dataDir: "/srv/git" |
| 64 | ''; |
| 65 | top-level-typo = '' |
| 66 | package valley |
| 67 | project: oops: {} |
| 68 | ''; |
| 69 | deployment-concern-in-cue = '' |
| 70 | package valley |
| 71 | user: "git" |
| 72 | projects: ok: {} |
| 73 | ''; |
| 74 | }; |
| 75 | in |
| 76 | pkgs.runCommand "valley-cue-vet" |
| 77 | { |
| 78 | nativeBuildInputs = [ pkgs.cue ]; |
| 79 | } |
| 80 | '' |
| 81 | cue vet -c ${./schema/valley.cue} ${./examples/host.cue} |
| 82 | cue export ${./schema/valley.cue} ${./examples/host.cue} > example.json |
| 83 | grep -q 'gunk-dev/the-valley' example.json |
| 84 | |
| 85 | ${lib.concatStrings ( |
| 86 | lib.mapAttrsToList (name: cfg: '' |
| 87 | if cue vet -c ${./schema/valley.cue} ${pkgs.writeText "${name}.cue" cfg}; then |
| 88 | echo "cue-vet: expected invalid config '${name}' to be rejected" >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | '') invalidConfigs |
| 92 | )} |
| 93 | touch $out |
| 94 | ''; |
| 95 | |
| 96 | module-eval = |
| 97 | if failedAssertions != [ ] then |
| 98 | throw "valley module-eval: failed assertions: ${lib.concatMapStringsSep "; " (a: a.message) failedAssertions}" |
| 99 | else |
| 100 | pkgs.runCommand "valley-module-eval" |
| 101 | { |
| 102 | initScript = host.config.systemd.services.valley-init.script; |
| 103 | sshdConfig = host.config.services.openssh.extraConfig; |
| 104 | passAsFile = [ |
| 105 | "initScript" |
| 106 | "sshdConfig" |
| 107 | ]; |
| 108 | } |
| 109 | '' |
| 110 | # The repo init list and mirror hook must be generated from |
| 111 | # the CUE export, and the git user's sshd Match block must |
| 112 | # be terminated so it cannot scope later config. |
| 113 | grep -q "the-valley" "$initScriptPath" |
| 114 | grep -q "valley-mirrors" "$initScriptPath" |
| 115 | grep -q "Match All" "$sshdConfigPath" |
| 116 | touch $out |
| 117 | ''; |
| 118 | } |
| 119 | ); |
| 120 | }; |
| 121 | } |