| 1 | # Everything this flake builds: the CLI and its Go verbs, the |
| 2 | # formatter, the attestation helper, the integrator, the identity |
| 3 | # compiler, the pre-receive hook's policy, the security-key signature |
| 4 | # verifier. The flake wires these into packages, apps, and the checks that |
| 5 | # drive them; nothing here knows about any of those outputs. |
| 6 | { |
| 7 | pkgs, |
| 8 | lib, |
| 9 | # The commit the flake is built from: its full SHA, the SHA with -dirty |
| 10 | # after it for a tree with uncommitted changes, or null when nix knows no |
| 11 | # commit. Only the installed CLI's version stamp reads it. |
| 12 | rev ? null, |
| 13 | }: |
| 14 | rec { |
| 15 | # The CLI's version, which `valley --version` prints and self-update |
| 16 | # reads to tell which commit is installed (selfupdate/): cliVersion and |
| 17 | # the short commit, as 0.1.0-97db597. A dirty tree's has -dirty after |
| 18 | # it, and with no commit known it ends in -unknown, so neither names a |
| 19 | # commit. |
| 20 | cliVersion = "0.1.0"; |
| 21 | version = |
| 22 | if rev == null then |
| 23 | "${cliVersion}-unknown" |
| 24 | else if lib.hasSuffix "-dirty" rev then |
| 25 | "${cliVersion}-${builtins.substring 0 7 rev}-dirty" |
| 26 | else |
| 27 | "${cliVersion}-${builtins.substring 0 7 rev}"; |
| 28 | |
| 29 | # The source of a Go program that uses modules of this repository: its own |
| 30 | # directory and those modules, which its go.mod replaces with their local |
| 31 | # directories. Nothing shared this way is fetched. |
| 32 | withLocal = |
| 33 | dir: modules: |
| 34 | lib.fileset.toSource { |
| 35 | root = ../.; |
| 36 | fileset = lib.fileset.unions ([ dir ] ++ modules); |
| 37 | }; |
| 38 | |
| 39 | # A program that reads signed notes takes the note module (../note). One |
| 40 | # reading of the envelope is shared by the programs that write, verify and |
| 41 | # admit attestations. |
| 42 | withNote = dir: withLocal dir [ ../note ]; |
| 43 | |
| 44 | # vendorHash = null builds in vendor mode, which has no reading of a local |
| 45 | # replace. The replaced module is a directory beside the program and |
| 46 | # nothing is fetched either way, so these builds read go.mod as written. |
| 47 | readsGoMod = '' |
| 48 | export GOFLAGS="''${GOFLAGS//-mod=vendor/-mod=mod}" |
| 49 | ''; |
| 50 | |
| 51 | # Markdown prose is filled paragraphs hard-wrapped at 100 columns |
| 52 | # (ida-1ec03b1). One flag string backs both the formatter app and the |
| 53 | # prose-format check, so the two cannot drift. Embedded-language |
| 54 | # formatting is off so fenced code blocks pass through byte-for-byte. |
| 55 | proseFmtArgs = "--prose-wrap always --print-width 100 --embedded-language-formatting off"; |
| 56 | |
| 57 | # The integrator's CLI (bin/valley) wrapped for `nix run`. The script |
| 58 | # itself must keep running bare from any checkout — the package is the |
| 59 | # second of its two shipping modes, never a dependency of the first. |
| 60 | valley-script-unwrapped = pkgs.writeShellApplication { |
| 61 | name = "valley"; |
| 62 | runtimeInputs = [ |
| 63 | pkgs.git |
| 64 | pkgs.natscli # tail and replay only; every other verb needs just git |
| 65 | pkgs.cue # checks and approving — it composes the policy layers |
| 66 | pkgs.less # review only — its v key is what takes a note |
| 67 | attest # approving only — it runs the checks and signs them |
| 68 | valley-go # status, ready, unready, withdraw and rebase, naming a change by its ID, and pending's drafts |
| 69 | ]; |
| 70 | text = builtins.readFile ../bin/valley; |
| 71 | }; |
| 72 | |
| 73 | # The shipping form. The verification schema travels with the script, as |
| 74 | # it does with the integrator: only the-valley's own tree carries |
| 75 | # schema/, so a project's checkout has none to read. --set-default leaves |
| 76 | # an explicit VALLEY_VERIFICATION_SCHEMA in charge. |
| 77 | valley-script = |
| 78 | pkgs.runCommand "valley" |
| 79 | { |
| 80 | nativeBuildInputs = [ pkgs.makeWrapper ]; |
| 81 | meta.mainProgram = "valley"; |
| 82 | } |
| 83 | '' |
| 84 | mkdir -p $out/bin |
| 85 | makeWrapper ${lib.getExe valley-script-unwrapped} $out/bin/valley \ |
| 86 | --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} |
| 87 | ''; |
| 88 | |
| 89 | # The installed package: the wrapped script stamped with its version, the |
| 90 | # shell completions at the standard paths home-manager/NixOS auto-link, |
| 91 | # and the tiny policy self-update's smoke test derives checks from |
| 92 | # (examples/policy/smoke/). The stamp is set on this outer wrapper alone, |
| 93 | # so the packages the checks and the integrator use do not change with |
| 94 | # every commit. The name carries the version as well: nix profile names |
| 95 | # the element valley from it, and self-update reads a hand-installed |
| 96 | # valley's commit from it. |
| 97 | valley = |
| 98 | pkgs.runCommand "valley-${version}" |
| 99 | { |
| 100 | inherit version; |
| 101 | nativeBuildInputs = [ |
| 102 | pkgs.makeWrapper |
| 103 | pkgs.installShellFiles |
| 104 | ]; |
| 105 | meta.mainProgram = "valley"; |
| 106 | } |
| 107 | '' |
| 108 | mkdir -p $out/bin $out/share/valley |
| 109 | makeWrapper ${lib.getExe valley-script} $out/bin/valley \ |
| 110 | --set VALLEY_VERSION ${version} |
| 111 | installShellCompletion --bash --name valley ${../completions/valley.bash} |
| 112 | installShellCompletion --zsh --name _valley ${../completions/_valley} |
| 113 | cp -r --no-preserve=mode ${../examples/policy/smoke} $out/share/valley/smoke-policy |
| 114 | ''; |
| 115 | |
| 116 | # The verbs of the CLI written in Go (status/). status reads one change's |
| 117 | # request and outcome record from origin, and prints them as sentences or |
| 118 | # as the valley-status/v1 JSON object front ends read. ready, unready and |
| 119 | # withdraw write a change's record, with the one reading of a record the |
| 120 | # push hook shares (../change). rebase rebases a change's whole stack and |
| 121 | # adds each change's rebased head to its record, through the rebase engine |
| 122 | # (../rebase), which also reads how far each version has landed for the |
| 123 | # listings. knot reads and writes the plan graph, with the one reading of |
| 124 | # a graph the push hook shares (../knot), and plan shows that graph as a |
| 125 | # view. compare reads a version's file delta and its approved version's |
| 126 | # with the one reading of valley-delta-v1 (../delta), for review. |
| 127 | # self-update installs the tip of a branch of the-valley into the user's |
| 128 | # nix profile (../selfupdate). Go, |
| 129 | # standard library only — hence vendorHash = null and no module fetch. It |
| 130 | # runs whatever git is on PATH, which inside the CLI's package is the |
| 131 | # CLI's own. Its unit tests hold its shell quoting to the bash it |
| 132 | # replaced, so they need bash, and lay pending out over real repositories, |
| 133 | # so they need git. |
| 134 | valley-go = pkgs.buildGoModule { |
| 135 | pname = "valley-go"; |
| 136 | version = "0"; |
| 137 | src = withLocal ../status [ |
| 138 | ../change |
| 139 | ../delta |
| 140 | ../knot |
| 141 | ../rebase |
| 142 | ../selfupdate |
| 143 | ]; |
| 144 | modRoot = "status"; |
| 145 | vendorHash = null; |
| 146 | preBuild = readsGoMod; |
| 147 | nativeCheckInputs = [ |
| 148 | pkgs.bash |
| 149 | pkgs.git |
| 150 | ]; |
| 151 | meta.mainProgram = "valley-go"; |
| 152 | }; |
| 153 | |
| 154 | # `nix run .#fmt` — rewrap every tracked *.md in the repo to 100 columns. |
| 155 | prose-fmt = pkgs.writeShellApplication { |
| 156 | name = "valley-fmt"; |
| 157 | runtimeInputs = [ |
| 158 | pkgs.git |
| 159 | pkgs.prettier |
| 160 | ]; |
| 161 | text = '' |
| 162 | cd "$(git rev-parse --show-toplevel)" |
| 163 | git ls-files -z -- '*.md' | xargs -0 --no-run-if-empty \ |
| 164 | prettier ${proseFmtArgs} --write |
| 165 | ''; |
| 166 | }; |
| 167 | |
| 168 | # The Phase 2 attestation helper (dcr-0de694f). Go, standard library |
| 169 | # only, note format included — hence vendorHash = null, and hence no |
| 170 | # module fetch at build time. Its unit tests run in the checkPhase and |
| 171 | # need git and ssh-keygen. |
| 172 | attest-unwrapped = pkgs.buildGoModule { |
| 173 | pname = "valley-attest"; |
| 174 | version = "0"; |
| 175 | src = withNote ../attest; |
| 176 | modRoot = "attest"; |
| 177 | vendorHash = null; |
| 178 | preBuild = readsGoMod; |
| 179 | nativeCheckInputs = [ |
| 180 | pkgs.git |
| 181 | pkgs.openssh |
| 182 | ]; |
| 183 | meta.mainProgram = "attest"; |
| 184 | }; |
| 185 | |
| 186 | # The shipping form. git and cue are pinned to this flake's nixpkgs, |
| 187 | # so every host composes and vets a statement with the same tools. |
| 188 | # Signing needs no tool at all: a note is signed on crypto/ed25519 |
| 189 | # inside the binary. `nix` is deliberately NOT pinned: the check being |
| 190 | # attested to must be built by the nix the machine actually runs, and |
| 191 | # a nix carried in here would be a second one. |
| 192 | attest = |
| 193 | pkgs.runCommand "valley-attest" |
| 194 | { |
| 195 | nativeBuildInputs = [ pkgs.makeWrapper ]; |
| 196 | meta.mainProgram = "attest"; |
| 197 | } |
| 198 | '' |
| 199 | mkdir -p $out/bin |
| 200 | makeWrapper ${lib.getExe attest-unwrapped} $out/bin/attest \ |
| 201 | --prefix PATH : ${ |
| 202 | lib.makeBinPath [ |
| 203 | pkgs.git |
| 204 | pkgs.cue |
| 205 | ] |
| 206 | } \ |
| 207 | --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} |
| 208 | ''; |
| 209 | |
| 210 | # The identity registry compiler (dcr-b87f6e8). Go, standard library |
| 211 | # only, same trade as attest and the integrator — hence vendorHash = null |
| 212 | # and no module fetch. Its unit tests pin the note format's key hash |
| 213 | # against the reference vector attest is held to. |
| 214 | identity-unwrapped = pkgs.buildGoModule { |
| 215 | pname = "valley-identity"; |
| 216 | version = "0"; |
| 217 | src = ../identity; |
| 218 | vendorHash = null; |
| 219 | meta.mainProgram = "identity"; |
| 220 | }; |
| 221 | |
| 222 | # The shipping form. git reads the registry out of the instance |
| 223 | # repository's tip and cue vets and exports it, both pinned to this |
| 224 | # flake's nixpkgs so every host compiles a registry the same way. The |
| 225 | # schema travels with the binary: a compiler judging a registry against a |
| 226 | # schema the consumer supplied would be no floor at all. |
| 227 | identity = |
| 228 | pkgs.runCommand "valley-identity" |
| 229 | { |
| 230 | nativeBuildInputs = [ pkgs.makeWrapper ]; |
| 231 | meta.mainProgram = "identity"; |
| 232 | } |
| 233 | '' |
| 234 | mkdir -p $out/bin |
| 235 | makeWrapper ${lib.getExe identity-unwrapped} $out/bin/identity \ |
| 236 | --prefix PATH : ${ |
| 237 | lib.makeBinPath [ |
| 238 | pkgs.git |
| 239 | pkgs.cue |
| 240 | ] |
| 241 | } \ |
| 242 | --set-default VALLEY_IDENTITY_SCHEMA ${../schema/identity.cue} |
| 243 | ''; |
| 244 | |
| 245 | # The security-key signature verifier (sigverify/README.md). Go, |
| 246 | # standard library only — hence vendorHash = null and no module fetch. |
| 247 | # |
| 248 | # The git its git-tag command runs is fixed at build time to this |
| 249 | # flake's git, as an absolute store path compiled into the binary. A |
| 250 | # wrapper that puts git on PATH would leave the binary inside it looking |
| 251 | # git up on PATH, and a caller's environment could change which git that |
| 252 | # is. Compiled in, there is no PATH lookup to change. |
| 253 | # |
| 254 | # Its tests run in the checkPhase against real ssh-keygen and git. The |
| 255 | # security-key cases among them need OpenSSH's sk-dummy authenticator, |
| 256 | # which only the sigverify-unit check supplies; here they skip, so a |
| 257 | # consumer building this package never builds OpenSSH. |
| 258 | sigverify = pkgs.buildGoModule { |
| 259 | pname = "valley-sigverify"; |
| 260 | version = "0"; |
| 261 | src = ../sigverify; |
| 262 | vendorHash = null; |
| 263 | ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ]; |
| 264 | nativeCheckInputs = [ |
| 265 | pkgs.git |
| 266 | pkgs.openssh |
| 267 | ]; |
| 268 | meta.mainProgram = "sigverify"; |
| 269 | }; |
| 270 | |
| 271 | # The pre-receive hook's policy (valleyhook/): what a push may write to a |
| 272 | # project a valley host serves. It validates a push to the plan graph |
| 273 | # with the one reading of a graph the CLI shares (../knot). The same binary is the git user's login |
| 274 | # shell, which derives the pushing principal and pauses pushes until the |
| 275 | # host has converged. Go, standard library only — hence |
| 276 | # vendorHash = null and no module fetch. Its unit tests run in the |
| 277 | # checkPhase. |
| 278 | # |
| 279 | # The git it reads the repository with is fixed at build time, the way |
| 280 | # sigverify's is: an absolute store path compiled into the binary, so no |
| 281 | # PATH a hook runs under can change which git that is. |
| 282 | valleyhook = pkgs.buildGoModule { |
| 283 | pname = "valley-valleyhook"; |
| 284 | version = "0"; |
| 285 | src = withLocal ../valleyhook [ |
| 286 | ../note |
| 287 | ../change |
| 288 | ../knot |
| 289 | ]; |
| 290 | modRoot = "valleyhook"; |
| 291 | vendorHash = null; |
| 292 | preBuild = readsGoMod; |
| 293 | ldflags = [ "-X main.gitProgram=${lib.getExe pkgs.git}" ]; |
| 294 | # The walk's and the change records' tests build hostile objects with |
| 295 | # real git. |
| 296 | nativeCheckInputs = [ pkgs.git ]; |
| 297 | meta.mainProgram = "valleyhook"; |
| 298 | }; |
| 299 | |
| 300 | # The note envelope's own tests, which hold it to refusing every line a |
| 301 | # relayer could append that one of its readers would refuse. |
| 302 | note = pkgs.buildGoModule { |
| 303 | pname = "valley-note"; |
| 304 | version = "0"; |
| 305 | src = ../note; |
| 306 | vendorHash = null; |
| 307 | }; |
| 308 | |
| 309 | # The plan graph (knot/): its canonical form, its layout, cycles and |
| 310 | # transitions, held by its own tests. The package is knotcheck, which |
| 311 | # knot-agree runs to hold the Go validator to the CUE schema and the |
| 312 | # knowledge lint. |
| 313 | knot = pkgs.buildGoModule { |
| 314 | pname = "valley-knot"; |
| 315 | version = "0"; |
| 316 | src = ../knot; |
| 317 | vendorHash = null; |
| 318 | meta.mainProgram = "knotcheck"; |
| 319 | }; |
| 320 | |
| 321 | # The rebase engine (rebase/): a family of stacked changes captured from |
| 322 | # one snapshot of origin, rebased in a throwaway worktree, and published |
| 323 | # as one atomic branch push and one record append per change. Building |
| 324 | # valley-go does not run a module's tests that it only imports, so this |
| 325 | # package runs the engine's: go test, against real repositories, real |
| 326 | # pushes and real git, and go vet. Go, standard library only — hence |
| 327 | # vendorHash = null and no module fetch. |
| 328 | rebase = pkgs.buildGoModule { |
| 329 | pname = "valley-rebase"; |
| 330 | version = "0"; |
| 331 | src = withLocal ../rebase [ ../change ]; |
| 332 | modRoot = "rebase"; |
| 333 | vendorHash = null; |
| 334 | preBuild = readsGoMod; |
| 335 | nativeCheckInputs = [ pkgs.git ]; |
| 336 | postCheck = '' |
| 337 | go vet ./... |
| 338 | ''; |
| 339 | }; |
| 340 | |
| 341 | # Self-update (selfupdate/): the installed CLI brought up to the tip of a |
| 342 | # branch, built with nix and installed into the user's profile. Building |
| 343 | # valley-go does not run the tests of a module it only imports, so this |
| 344 | # package runs them, and go vet: a real bare repository for ls-remote, |
| 345 | # and a stand-in for nix whose builds are small scripts, so nothing is |
| 346 | # built and no profile is touched. Go, standard library only — hence |
| 347 | # vendorHash = null and no module fetch. |
| 348 | selfupdate = pkgs.buildGoModule { |
| 349 | pname = "valley-selfupdate"; |
| 350 | version = "0"; |
| 351 | src = ../selfupdate; |
| 352 | vendorHash = null; |
| 353 | nativeCheckInputs = [ pkgs.git ]; |
| 354 | postCheck = '' |
| 355 | go vet ./... |
| 356 | ''; |
| 357 | }; |
| 358 | |
| 359 | # The change record's own tests: its strict reading, its fuzz target's |
| 360 | # seed corpus, and its branch-name rule, which they hold to real git's |
| 361 | # check-ref-format. |
| 362 | change = pkgs.buildGoModule { |
| 363 | pname = "valley-change"; |
| 364 | version = "0"; |
| 365 | src = ../change; |
| 366 | vendorHash = null; |
| 367 | nativeCheckInputs = [ pkgs.git ]; |
| 368 | }; |
| 369 | |
| 370 | # valley-delta-v1 (delta/): the encoding's strict reading, and the delta |
| 371 | # read from real trees. Its checkPhase runs go vet, the conformance |
| 372 | # vectors' script in its --check mode, which holds the committed expected |
| 373 | # results to what the script computes, and go test, which builds each |
| 374 | # vector's trees with real git and holds the Go code to those results. |
| 375 | delta = pkgs.buildGoModule { |
| 376 | pname = "valley-delta"; |
| 377 | version = "0"; |
| 378 | src = ../delta; |
| 379 | vendorHash = null; |
| 380 | nativeCheckInputs = [ |
| 381 | pkgs.git |
| 382 | pkgs.python3 |
| 383 | ]; |
| 384 | preCheck = '' |
| 385 | go vet ./... |
| 386 | python3 -I testdata/vectors.py --check testdata |
| 387 | ''; |
| 388 | }; |
| 389 | |
| 390 | # The Phase 3 integrator (dcr-439b771). Go, standard library only, |
| 391 | # same trade as attest — hence vendorHash = null and no module fetch. It |
| 392 | # reads change records with the one reading of a record the push hook |
| 393 | # shares (../change). |
| 394 | integrator-unwrapped = pkgs.buildGoModule { |
| 395 | pname = "valley-integrator"; |
| 396 | version = "0"; |
| 397 | # The verification schema is in the source for the policy bridge's |
| 398 | # tests, which compose a policy under it. |
| 399 | src = withLocal ../integrator [ |
| 400 | ../change |
| 401 | ../schema/verification.cue |
| 402 | ]; |
| 403 | modRoot = "integrator"; |
| 404 | vendorHash = null; |
| 405 | preBuild = readsGoMod; |
| 406 | # The policy bridge's tests check a deriver's report against real git, |
| 407 | # and compose a policy with real cue. |
| 408 | nativeCheckInputs = [ |
| 409 | pkgs.git |
| 410 | pkgs.cue |
| 411 | ]; |
| 412 | meta.mainProgram = "integrator"; |
| 413 | }; |
| 414 | |
| 415 | # The shipping form. The integrator drives four programs rather than |
| 416 | # reimplementing what they do: attest judges every statement and |
| 417 | # every note, the valley deriver and cue compose the policy, and git |
| 418 | # holds the requests and the refs. All four are pinned to this |
| 419 | # flake's nixpkgs so an instance's integrators agree; `nix` is |
| 420 | # deliberately absent for the same reason it is absent from attest — |
| 421 | # a closure must be recomputed by the nix the machine actually runs. |
| 422 | integrator = |
| 423 | pkgs.runCommand "valley-integrator" |
| 424 | { |
| 425 | nativeBuildInputs = [ pkgs.makeWrapper ]; |
| 426 | meta.mainProgram = "integrator"; |
| 427 | } |
| 428 | '' |
| 429 | mkdir -p $out/bin |
| 430 | makeWrapper ${lib.getExe integrator-unwrapped} $out/bin/integrator \ |
| 431 | --prefix PATH : ${ |
| 432 | lib.makeBinPath [ |
| 433 | pkgs.git |
| 434 | pkgs.cue |
| 435 | pkgs.natscli |
| 436 | attest |
| 437 | valley-script |
| 438 | ] |
| 439 | } \ |
| 440 | --set-default VALLEY_ATTEST_SCHEMA ${../schema/attestation.cue} \ |
| 441 | --set-default VALLEY_VERIFICATION_SCHEMA ${../schema/verification.cue} \ |
| 442 | --set-default VALLEY_EVENT_SCHEMA ${../schema/events.cue} |
| 443 | ''; |
| 444 | } |