| 1 | # A guide to working in the valley |
| 2 | |
| 3 | This guide follows the jobs people do in a valley. Start with the [README](../README.md) for a short |
| 4 | tour, [Concepts and glossary](./concepts.md) for the terms used here, and |
| 5 | [Reference](./reference.md) for every command flag, environment variable, ref rule, and schema. |
| 6 | |
| 7 | - [Client setup](#client-setup) gets an author or reviewer connected. |
| 8 | - [Authoring a change](#authoring-a-change) takes a local commit to review. |
| 9 | - [Reviewing and approving changes](#reviewing-and-approving-changes) takes a ready change to |
| 10 | `main`. |
| 11 | - [Planning work with knots](#planning-work-with-knots) records and connects work. |
| 12 | - [Recovering from failure](#recovering-from-failure) explains what to do when work stops. |
| 13 | - [Running a valley host](#running-a-valley-host) sets up a NixOS deployment. |
| 14 | - [Contributing to the-valley](#contributing-to-the-valley) runs the project's development checks. |
| 15 | |
| 16 | The commands below describe implemented behavior. Host names, principals, verification policy, and |
| 17 | service settings are deployment choices. Replace the example values with those your administrator |
| 18 | provides. The [README's current status](../README.md#configuration-and-current-status) lists what is |
| 19 | not built. |
| 20 | |
| 21 | ## Client setup |
| 22 | |
| 23 | The client lets you propose work, review changes, and read the live plan. You need git, SSH access |
| 24 | to the valley host, and Nix with flakes enabled. |
| 25 | |
| 26 | 1. Clone the-valley's source from the URL your administrator provides. From that checkout, run: |
| 27 | |
| 28 | ```sh |
| 29 | nix run .#valley -- help |
| 30 | nix shell .#valley |
| 31 | valley --version |
| 32 | ``` |
| 33 | |
| 34 | `nix run` runs one command. `nix shell` puts the packaged `valley` on your path for the rest of |
| 35 | this guide. `valley help` prints a command summary; `valley help <verb>` prints help for one |
| 36 | verb. The package supplies the helpers its commands need. |
| 37 | |
| 38 | 2. Clone the project you will work on and name the principal associated with your SSH key: |
| 39 | |
| 40 | ```sh |
| 41 | git clone git@valley.example:demo |
| 42 | cd demo |
| 43 | git config valley.principal operator |
| 44 | git config user.name "Your name" |
| 45 | git config user.email you@example.com |
| 46 | ``` |
| 47 | |
| 48 | Use your own git author name and email. `valley.principal` names you in change records and knots. |
| 49 | It does not grant permission. The host checks the principal associated with the key you actually |
| 50 | use. |
| 51 | |
| 52 | 3. Check that the client can verify merged landings. You do not need to configure anything. The host |
| 53 | publishes its trusted signers file and the integrator's signing name in the instance repository, |
| 54 | and the client reads them when it needs them. This command shows the publication: |
| 55 | |
| 56 | ```sh |
| 57 | git ls-remote git@valley.example:instance refs/the-valley/signers |
| 58 | ``` |
| 59 | |
| 60 | Use the instance repository your project's `policy/valley` names. If it prints nothing, the host |
| 61 | publishes nothing, and some submitted versions appear `uncertain`; see |
| 62 | [recovery](#an-uncertain-landing). To use other values than the host's, set them yourself. A |
| 63 | setting wins over the publication: |
| 64 | |
| 65 | ```sh |
| 66 | export VALLEY_KNOWN_SIGNERS="$HOME/.config/valley/known-signers" |
| 67 | export VALLEY_INTEGRATOR_SIGNER=integrator |
| 68 | ``` |
| 69 | |
| 70 | 4. If approval runs checks on your machine, configure your Ed25519 attestation key and its |
| 71 | registered signing name: |
| 72 | |
| 73 | ```sh |
| 74 | export VALLEY_ATTEST_KEY="$HOME/.ssh/valley-attest" |
| 75 | export VALLEY_ATTEST_NAME=operator |
| 76 | ``` |
| 77 | |
| 78 | When every required check already has eligible evidence on `origin` (for example, from a check |
| 79 | worker), approval needs no signing key. When a check has to run locally, the host must trust that |
| 80 | signing key. Your SSH principal must also be allowed to approve. These are separate parts of |
| 81 | [identity](./concepts.md#principals-and-grants); see |
| 82 | [Environment variables and configuration](./reference.md#environment-variables-and-configuration) |
| 83 | for every client setting. |
| 84 | |
| 85 | To install updates into your user Nix profile, name the-valley's source repository: |
| 86 | |
| 87 | ```sh |
| 88 | valley self-update --repo git@valley.example:the-valley --branch main |
| 89 | ``` |
| 90 | |
| 91 | It builds the selected commit, tests the new client, and installs it. For repeated use, save the |
| 92 | repository with `git config --global valley.selfUpdate.repo git@valley.example:the-valley`, then run |
| 93 | `valley self-update`. Without a configured repository, self-update is off. Run |
| 94 | `valley self-update --help` for rollback and pinning. |
| 95 | |
| 96 | The installed package includes Bash and Zsh completions. For a shell using the checkout directly, |
| 97 | source `completions/valley.bash` in Bash. In Zsh, add the checkout's `completions` directory to |
| 98 | `fpath` before running `autoload -Uz compinit; compinit`. |
| 99 | |
| 100 | ## Authoring a change |
| 101 | |
| 102 | A change starts with a local commit. This example uses a new git branch called `greeting` to carry |
| 103 | it. Run it in a project with an initial `main` and verification policy already set up. |
| 104 | |
| 105 | ### Create and ready the first version |
| 106 | |
| 107 | ```sh |
| 108 | git fetch origin |
| 109 | git switch -c greeting origin/main |
| 110 | printf 'Hello from the valley.\n' > greeting.txt |
| 111 | git add greeting.txt |
| 112 | git commit -m 'Add a greeting' |
| 113 | valley checks |
| 114 | ``` |
| 115 | |
| 116 | `valley checks` lists what this diff requires. It does not run the checks. Run the commands named by |
| 117 | your project's policy before asking for review. For a Nix check named `prose-format` in this |
| 118 | repository, for example: |
| 119 | |
| 120 | ```sh |
| 121 | nix build --no-link .#checks.x86_64-linux.prose-format -L |
| 122 | ``` |
| 123 | |
| 124 | For this new, unpushed commit, stamp a stable Change ID and publish the first version: |
| 125 | |
| 126 | ```sh |
| 127 | valley ready --stamp |
| 128 | valley status greeting |
| 129 | ``` |
| 130 | |
| 131 | `ready` prints a handle and version, such as `demo kqxm v1 ready`. Use the handle it actually prints |
| 132 | in later commands. The handle identifies the change even when its commit changes. `status` reports |
| 133 | readiness, any request, and the latest integration result. To see the required checks alongside |
| 134 | pending changes, run `valley pending --checks`. |
| 135 | |
| 136 | If the commit already carries a `Change-Id` trailer or a jj `change-id` header, push it first and |
| 137 | run `valley ready` without `--stamp`: |
| 138 | |
| 139 | ```sh |
| 140 | git push -u origin HEAD |
| 141 | valley ready |
| 142 | ``` |
| 143 | |
| 144 | Without `--stamp`, `ready` uses the pushed tip by default. Keep the full Change ID when recording a |
| 145 | new revision. The [change concept](./concepts.md#a-change-ready-approve-submitted) explains how |
| 146 | headers, trailers, and `--change <id>` preserve it. |
| 147 | |
| 148 | ### Revise or pause |
| 149 | |
| 150 | Pause a ready change before editing it: |
| 151 | |
| 152 | ```sh |
| 153 | valley unready kqxm |
| 154 | ``` |
| 155 | |
| 156 | It becomes a draft. If it has an approval, ask a principal allowed to approve to |
| 157 | [delete the outstanding request](#withdraw-or-revoke-an-approval) before you ready a replacement. |
| 158 | Otherwise, readying the edited change can let the old version land: its request still names the old |
| 159 | head. |
| 160 | |
| 161 | Edit the files, then amend the commit while keeping its `Change-Id` trailer: |
| 162 | |
| 163 | ```sh |
| 164 | git add greeting.txt |
| 165 | git commit --amend --no-edit |
| 166 | git push --force-with-lease origin HEAD |
| 167 | valley ready |
| 168 | valley status kqxm |
| 169 | ``` |
| 170 | |
| 171 | The new head becomes the next version of the same change. If you make a new commit without its |
| 172 | trailer or header, use `valley ready --change <full-change-id>` after pushing it. |
| 173 | |
| 174 | ### Withdraw or revoke an approval |
| 175 | |
| 176 | To abandon the change, run `valley withdraw kqxm`. This marks it withdrawn. The integrator removes |
| 177 | outstanding requests on its next pass while the change is still withdrawn. Do not ready a withdrawn |
| 178 | change again until that removal is confirmed. For immediate revocation, a principal allowed to |
| 179 | approve can delete its request: |
| 180 | |
| 181 | ```sh |
| 182 | git push origin :refs/the-valley/integration-requests/main/c-<full-change-id> |
| 183 | ``` |
| 184 | |
| 185 | Replace the whole `<full-change-id>` placeholder before running this command. If an older request |
| 186 | uses a branch name, delete that request too. Use |
| 187 | `git ls-remote origin 'refs/the-valley/integration-requests/main/*'` to list them. Neither pausing |
| 188 | nor withdrawing undoes a completed landing. See the |
| 189 | [integrator's final readiness check](./concepts.md#the-host-the-hook-and-the-integrator) for the |
| 190 | limit when a landing is already in progress. |
| 191 | |
| 192 | ### Restack onto main |
| 193 | |
| 194 | Rebase a ready change when it needs the latest `main`. Before replacing an approved version, |
| 195 | [revoke its outstanding request](#withdraw-or-revoke-an-approval). |
| 196 | |
| 197 | ```sh |
| 198 | valley rebase kqxm |
| 199 | valley status kqxm |
| 200 | ``` |
| 201 | |
| 202 | This rebases the whole stack beneath the named change, pushes the new heads, and records new |
| 203 | versions. It leaves your local checkout in place. Read its output before editing locally again; |
| 204 | fetch the remote updates and reconcile your local branch with them. If a record update fails, finish |
| 205 | with the exact `valley ready` command it prints. |
| 206 | |
| 207 | Get the rebased version reviewed and approved again. A host that runs the restacker rebases ready |
| 208 | changes for you after each landing, and its versions need approval in the same way. Automatic |
| 209 | approval carry is not built. For conflicts or an uncertain landing, use |
| 210 | [recovery](#recovering-from-failure). |
| 211 | |
| 212 | ## Reviewing and approving changes |
| 213 | |
| 214 | A principal allowed to approve can turn a ready change into an integration request. Start in a clone |
| 215 | of the project with the [client signing settings](#client-setup). |
| 216 | |
| 217 | ```sh |
| 218 | valley pending |
| 219 | valley review kqxm |
| 220 | ``` |
| 221 | |
| 222 | Use a handle from `pending`. Review pages the diff and offers actions at the terminal. Choose |
| 223 | `[s]kip` to leave it alone or `[a]pprove` to run its required checks and approve the head you saw. A |
| 224 | draft or withdrawn layer stops review from offering approval for that layer or anything above it. A |
| 225 | stack is shown from the bottom up; approving its top includes the layers below. |
| 226 | |
| 227 | For a revised change, review automatically compares it with an earlier approved version when one can |
| 228 | be identified. A clean restack gets a short summary. Use `[v]iew full diff` to see the whole change. |
| 229 | The [comparison rules](./architecture.md#review-against-the-approved-version) explain what can be |
| 230 | compared. |
| 231 | |
| 232 | You can also approve from the command line: |
| 233 | |
| 234 | ```sh |
| 235 | valley approve kqxm |
| 236 | valley status kqxm |
| 237 | ``` |
| 238 | |
| 239 | On a terminal, `approve` asks you to confirm the head and title. For automation, pass the exact |
| 240 | reviewed commit with `valley approve --head <sha> kqxm`. Approval reuses eligible evidence already |
| 241 | on `origin` for the commit's content tree (see [`valley approve`](./reference.md#valley-approve)), |
| 242 | runs and signs any remaining required checks, and publishes the new evidence and request together. |
| 243 | When every required check is covered, nothing runs and no signing key is needed. It does not wait |
| 244 | for landing. An enabled integrator processes the request; `status` tells you what happened. |
| 245 | |
| 246 | After a successful landing, update your local `main`: |
| 247 | |
| 248 | ```sh |
| 249 | git fetch origin |
| 250 | git switch main |
| 251 | git merge --ff-only origin/main |
| 252 | ``` |
| 253 | |
| 254 | To inspect all recorded changes, including submitted and withdrawn ones, use |
| 255 | `valley changes --json`. For changes across the instance, use `valley pending --all` or |
| 256 | `valley changes --json --all`; see [CLI JSON outputs](./reference.md#cli-json-outputs) for their |
| 257 | output schemas. These need access to the instance and project repositories. |
| 258 | |
| 259 | ### Watch activity and inspect results |
| 260 | |
| 261 | `status` and `changes --json` read the records on the host. Use them to inspect landing results. If |
| 262 | your deployment provides access to its NATS bus, configure a NATS context for that endpoint and run: |
| 263 | |
| 264 | ```sh |
| 265 | valley tail |
| 266 | ``` |
| 267 | |
| 268 | It streams events until you interrupt it. Bus access is a deployment choice; the default host bus |
| 269 | listens on loopback. An administrator can use `journalctl -u valley-integrator@demo` on the host for |
| 270 | service logs. |
| 271 | |
| 272 | An administrator can republish the host's current refs with `replay`. On a host with |
| 273 | `services.valley.bus.enable = true` at its default address, enter `nix shell .#valley` from a |
| 274 | checkout of the-valley, then run: |
| 275 | |
| 276 | ```sh |
| 277 | sudo -u git env NATS_URL=nats://127.0.0.1:4222 "$(command -v valley)" replay /srv/git/demo.git |
| 278 | ``` |
| 279 | |
| 280 | Use the deployment's bus address and credentials if they differ. This writes current-state events to |
| 281 | the bus; it is not a history viewer. See |
| 282 | [events and mirrors](./architecture.md#after-a-push-events-and-mirrors). |
| 283 | |
| 284 | ## Planning work with knots |
| 285 | |
| 286 | The plan graph records work and the relationships between its knots. Read it from a clone of the |
| 287 | primary valley repository: |
| 288 | |
| 289 | ```sh |
| 290 | valley plan |
| 291 | valley plan --all |
| 292 | ``` |
| 293 | |
| 294 | The first command shows this project's plan. The second reads projects published by the serving |
| 295 | host. A source mirror does not carry the plan graph. See |
| 296 | [project storage](./concepts.md#records-project-storage-the-plan-graph-and-knots). |
| 297 | |
| 298 | Create an outcome and inspect the ID it prints: |
| 299 | |
| 300 | ```sh |
| 301 | valley knot new outcome 'Greeting reaches readers' --owner operator |
| 302 | valley knot list --status active |
| 303 | ``` |
| 304 | |
| 305 | Use that ID in place of `oc-1234567` below: |
| 306 | |
| 307 | ```sh |
| 308 | valley knot show oc-1234567 |
| 309 | valley knot set oc-1234567 status=in-progress |
| 310 | valley knot new bug 'Greeting needs an example' --parent oc-1234567 |
| 311 | valley knot link oc-1234567 --change valley:demo/<full-change-id> |
| 312 | ``` |
| 313 | |
| 314 | Replace `<full-change-id>` with the change's full ID. The link connects the plan to the work that |
| 315 | fulfills it. A dependency uses `--blocked-by <knot-id>` instead. When the outcome is complete: |
| 316 | |
| 317 | ```sh |
| 318 | valley knot set oc-1234567 status=done |
| 319 | valley plan |
| 320 | ``` |
| 321 | |
| 322 | Comments record findings on a knot. `valley knot show` prints each comment's ID in brackets. A |
| 323 | comment's author or a holder of the `approve` grant can replace a comment's text with `--edit`: |
| 324 | |
| 325 | ```sh |
| 326 | valley knot comment oc-1234567 -m 'Waiting on the example.' |
| 327 | valley knot show oc-1234567 |
| 328 | valley knot comment oc-1234567 --edit <comment-id> -m 'The example landed.' |
| 329 | ``` |
| 330 | |
| 331 | The plan graph keeps every earlier text in its commit history. `valley knot log` lists the commits |
| 332 | that touched a knot or its comments, and `valley knot show --at <commit>` shows the knot at that |
| 333 | commit: |
| 334 | |
| 335 | ```sh |
| 336 | valley knot log oc-1234567 |
| 337 | valley knot show oc-1234567 --at <commit> |
| 338 | ``` |
| 339 | |
| 340 | Use `valley knot schema` to see each type's fields and allowed statuses. Knot writes validate the |
| 341 | resulting graph and push it directly; they do not go through change approval. See |
| 342 | [`valley knot`](./reference.md#valley-knot) for every subcommand and flag, and |
| 343 | [the plan graph](./architecture.md#the-plan-graph) for validation and concurrent edits. |
| 344 | |
| 345 | ## Recovering from failure |
| 346 | |
| 347 | Start with `valley status <handle>`. It distinguishes work waiting for an integrator from a request |
| 348 | that the integrator has judged. Keep the command's full output when asking your administrator for |
| 349 | help. |
| 350 | |
| 351 | ### A stale or rejected request |
| 352 | |
| 353 | A `stale` result means the change conflicts with `main` or required evidence is missing or does not |
| 354 | transfer across `main` movement. Read the reason in `status`. If the base moved, |
| 355 | [revoke the outstanding request](#withdraw-or-revoke-an-approval), restack the ready change, then |
| 356 | ask for a fresh review and approval: |
| 357 | |
| 358 | ```sh |
| 359 | valley rebase kqxm |
| 360 | valley status kqxm |
| 361 | ``` |
| 362 | |
| 363 | For missing or expired evidence, the approver must rerun approval with trusted signing settings. If |
| 364 | approval reports that evidence from this signer already exists for the tree, follow its recovery |
| 365 | message; retrying the same head does not replace that evidence. |
| 366 | |
| 367 | A `reject` result means the evidence is invalid, such as a signature or statement that does not |
| 368 | match what it claims. Keep the reason and ask the administrator to inspect the evidence and signing |
| 369 | configuration before trying again. See [checks and evidence](./concepts.md#checks-and-evidence). |
| 370 | |
| 371 | If `valley rebase` reports a conflict, it pushes nothing. For a single change, start from its remote |
| 372 | head on a clean local branch, run `git fetch origin` and `git rebase origin/main`, resolve the |
| 373 | reported files, and use `git add <resolved-files>` followed by `git rebase --continue`. Use |
| 374 | `git rebase --abort` to abandon that local attempt. Push with `--force-with-lease`, then run |
| 375 | `valley ready --change <full-change-id>`. For a stack, resolve each layer in order; do not publish |
| 376 | only the top while lower change records still name old heads. |
| 377 | |
| 378 | ### A refused push |
| 379 | |
| 380 | A refusal means the host did not accept that ref update. The message identifies the boundary: |
| 381 | |
| 382 | - **Protected ref:** push the work to a topic branch and use readiness and approval. A deployment |
| 383 | may seal `main` against every SSH principal. |
| 384 | - **Push hold:** ask the administrator to finish maintenance and remove the hold. Client settings |
| 385 | cannot bypass it. |
| 386 | - **Record ownership or non-fast-forward record update:** fetch again and use `valley ready`, |
| 387 | `unready`, or `withdraw` as the owner or a principal allowed to approve. Do not force a |
| 388 | hand-edited record over someone else's update. |
| 389 | - **A ref named like another ref:** push the branch or tag under a name that does not begin with |
| 390 | `refs/`. |
| 391 | - **Malformed or oversized record or knot:** read the named validation error. Use the CLI to make |
| 392 | valid records and `valley knot schema` to inspect the knot model. Reduce oversized content before |
| 393 | retrying. |
| 394 | |
| 395 | The [push rules](./architecture.md#what-a-push-may-write) and |
| 396 | [ref layout table](./reference.md#project-storage-ref-layout-and-bounds) explain each boundary. |
| 397 | |
| 398 | ### An uncertain landing |
| 399 | |
| 400 | An `uncertain` result means the client cannot prove whether the version was submitted. It can follow |
| 401 | a `failed` outcome or incomplete landing evidence. It can also mean the client had no signers to |
| 402 | verify the landing with. In that case the command says on stderr that a transfer statement could not |
| 403 | be verified, and why. Check that the host publishes its signers (step 3 of |
| 404 | [Client setup](#client-setup)). If it does not, ask the administrator for the signers file and the |
| 405 | integrator's signing name, set `VALLEY_KNOWN_SIGNERS` and `VALLEY_INTEGRATOR_SIGNER` to them, and |
| 406 | rerun `valley status <handle>`. |
| 407 | |
| 408 | If it remains uncertain, ask the administrator to inspect `main`, the outcome record, and the |
| 409 | integrator logs together. Do not assume that a consumed request means a successful landing. Resolve |
| 410 | the uncertainty before rebasing or approving again. The |
| 411 | [landing definitions](./concepts.md#a-change-ready-approve-submitted) describe the evidence needed |
| 412 | to resolve this state. |
| 413 | |
| 414 | ## Running a valley host |
| 415 | |
| 416 | A valley host serves repositories over SSH and runs the integrator on NixOS. This recipe creates an |
| 417 | instance repository named `instance` and a project named `demo`. It uses static keys and grants |
| 418 | first. All names, keys, checks, mirrors, and backup destinations are deployment choices; see |
| 419 | [NixOS host options](./reference.md#nixos-host-options) and |
| 420 | [CUE configuration schemas](./reference.md#cue-configuration-schemas) for the full option tables. |
| 421 | |
| 422 | ### Declare the host |
| 423 | |
| 424 | In your NixOS configuration directory, create `valley.cue`: |
| 425 | |
| 426 | ```cue |
| 427 | package valley |
| 428 | projects: { |
| 429 | instance: protection: {refs: ["refs/heads/main"], writers: []} |
| 430 | demo: protection: {refs: ["refs/heads/main"], writers: []} |
| 431 | } |
| 432 | ``` |
| 433 | |
| 434 | Both projects seal `main` against SSH pushes. The integrator writes locally on the host. |
| 435 | [`examples/hosts/protected.cue`](../examples/hosts/protected.cue) exercises four protection modes |
| 436 | for tests; it is not a production recipe. |
| 437 | |
| 438 | Add the-valley as an input named `valley` to your NixOS flake, using your administrator's source |
| 439 | URL. Import `valley.nixosModules.valley-host` in the host's modules. In a module that receives that |
| 440 | input, start with: |
| 441 | |
| 442 | ```nix |
| 443 | { valley, ... }: { |
| 444 | imports = [ valley.nixosModules.valley-host ]; |
| 445 | services.openssh.enable = true; |
| 446 | services.valley = { |
| 447 | enable = true; |
| 448 | config = ./valley.cue; |
| 449 | authorizedKeys = [{ |
| 450 | principal = "operator"; |
| 451 | key = "ssh-ed25519 REPLACE_WITH_OPERATOR_PUBLIC_KEY"; |
| 452 | }]; |
| 453 | grants.approve = [ "operator" ]; |
| 454 | }; |
| 455 | } |
| 456 | ``` |
| 457 | |
| 458 | Pass the input through `specialArgs = { inherit valley; };` in `nixpkgs.lib.nixosSystem` if using |
| 459 | this module's argument. Replace the key with the public SSH key that the operator uses to push. The |
| 460 | `approve` grant is the grant that lets a principal approve. It is also accepted as `request`, and |
| 461 | `grants.request` declares the same grant as `grants.approve`. Keep administrative SSH access |
| 462 | separate from this git account. |
| 463 | |
| 464 | Apply your configuration using your normal NixOS deployment command. For a local flake with a host |
| 465 | named `valley-host`, run this on the host from that configuration directory: |
| 466 | |
| 467 | ```sh |
| 468 | sudo nixos-rebuild switch --flake .#valley-host |
| 469 | sudo systemctl start valley-init |
| 470 | sudo systemctl status valley-init --no-pager |
| 471 | ``` |
| 472 | |
| 473 | `valley-init` creates `/srv/git/instance.git` and `/srv/git/demo.git`, installs hooks, and creates |
| 474 | no initial commit. It publishes the project list once `instanceProject` is configured. Keep the |
| 475 | integrator disabled until both repositories have a `main`. |
| 476 | |
| 477 | ### Seed the initial source |
| 478 | |
| 479 | Create the initial source on the host in `/var/tmp/valley-seed`. Use an ordinary administrator |
| 480 | account with git's author name and email configured: |
| 481 | |
| 482 | ```sh |
| 483 | mkdir -p /var/tmp/valley-seed/instance/policy/instance |
| 484 | mkdir -p /var/tmp/valley-seed/demo/policy |
| 485 | cd /var/tmp/valley-seed/instance |
| 486 | git init --initial-branch=main |
| 487 | printf '# Instance\n' > README.md |
| 488 | cat > policy/instance/floor.cue <<'CUE' |
| 489 | package verification |
| 490 | floor: { |
| 491 | checks: readme: {runner: "command", command: "test -s README.md", validity: "1h"} |
| 492 | classes: source: {paths: {"**": true}, requires: {readme: true}} |
| 493 | unclassified: readme: true |
| 494 | } |
| 495 | CUE |
| 496 | git add README.md policy |
| 497 | git commit -m 'Seed instance policy' |
| 498 | cd /var/tmp/valley-seed/demo |
| 499 | git init --initial-branch=main |
| 500 | printf '# Demo\n' > README.md |
| 501 | printf 'git@valley.example:instance\n' > policy/valley |
| 502 | git add README.md policy |
| 503 | git commit -m 'Seed demo' |
| 504 | ``` |
| 505 | |
| 506 | Replace `valley.example` before committing. `policy/valley` names the instance repository whose |
| 507 | `policy/instance/` supplies this project's floor. The floor here requires only a nonempty README. It |
| 508 | is a smoke check to prove the approval path works. Choose checks that verify your actual projects |
| 509 | before admitting production work. A project's optional `policy/project/*.cue` can add requirements; |
| 510 | it cannot weaken the floor. See [policy](./architecture.md#policy). |
| 511 | |
| 512 | Seed these commits through a local host write. The following commands are for the empty repositories |
| 513 | just created: |
| 514 | |
| 515 | ```sh |
| 516 | sudo chown -R git:git /var/tmp/valley-seed |
| 517 | sudo -u git git -C /srv/git/instance.git fetch /var/tmp/valley-seed/instance main:refs/heads/main |
| 518 | sudo -u git git -C /srv/git/demo.git fetch /var/tmp/valley-seed/demo main:refs/heads/main |
| 519 | ``` |
| 520 | |
| 521 | Local writes bypass `pre-receive`, so this works while SSH writes to `main` are sealed. Keep these |
| 522 | seed directories free of private keys. Another bootstrap choice is to allow a temporary writer and |
| 523 | remove it after seeding; this recipe keeps the sealed configuration throughout. |
| 524 | |
| 525 | ### Provision signing and enable integration |
| 526 | |
| 527 | On the approver's machine, create its attestation key if it does not have one. From the-valley's |
| 528 | checkout, export its public verifier entry: |
| 529 | |
| 530 | ```sh |
| 531 | ssh-keygen -t ed25519 -N '' -f "$HOME/.ssh/valley-attest" |
| 532 | nix run .#attest -- key --key "$HOME/.ssh/valley-attest" --name operator > operator.signer |
| 533 | ``` |
| 534 | |
| 535 | Keep the private key on that machine. Copy only `operator.signer` to the administrator on the host. |
| 536 | On the host, create the integrator's separate key at a runtime path: |
| 537 | |
| 538 | ```sh |
| 539 | sudo install -d -m 0750 -o root -g git /var/lib/valley-secrets |
| 540 | sudo ssh-keygen -t ed25519 -N '' -f /var/lib/valley-secrets/integrator |
| 541 | sudo chgrp git /var/lib/valley-secrets/integrator |
| 542 | sudo chmod 0640 /var/lib/valley-secrets/integrator |
| 543 | ``` |
| 544 | |
| 545 | On the host, clone the-valley's source from the URL your administrator provides. From that checkout, |
| 546 | enter `nix shell .#attest`. Export the integrator entry and combine the two public entries: |
| 547 | |
| 548 | ```sh |
| 549 | sudo "$(command -v attest)" key --key /var/lib/valley-secrets/integrator --name integrator > integrator.signer |
| 550 | cat operator.signer integrator.signer > known-signers |
| 551 | sudo install -m 0644 known-signers /var/lib/valley-secrets/known-signers |
| 552 | ``` |
| 553 | |
| 554 | Add this to `services.valley` and apply the NixOS configuration again: |
| 555 | |
| 556 | ```nix |
| 557 | integrator = { |
| 558 | enable = true; |
| 559 | instanceProject = "instance"; |
| 560 | signingKeyFile = "/var/lib/valley-secrets/integrator"; |
| 561 | signingName = "integrator"; |
| 562 | knownSignersFile = "/var/lib/valley-secrets/known-signers"; |
| 563 | }; |
| 564 | ``` |
| 565 | |
| 566 | The integrator runs as `valley-integrator` in the `git` group. Its key and signers file must be |
| 567 | readable by that account. Private keys belong at runtime paths, never in the Nix store. |
| 568 | |
| 569 | The module requires a nonempty `authorizedKeys`, a signing key, a source for trusted signers, and |
| 570 | exactly one of `instanceProject` or `instancePolicy`. With `instanceProject`, it also requires the |
| 571 | instance's `main` to be protected. `instancePolicy` instead names a directory of floor CUE files on |
| 572 | a host that does not serve the instance repository. Set `signingName` explicitly to the name |
| 573 | published in the signers file. |
| 574 | |
| 575 | The host publishes the `known-signers` file and the integrator's signing name for clients, at |
| 576 | `refs/the-valley/signers` in the instance repository. `valley-signers` writes it at boot, on |
| 577 | activation, and whenever the file changes, so clients need no signer settings. Follow |
| 578 | [Authoring a change](#authoring-a-change) in a clone of `demo`, then approve it as `operator`. |
| 579 | Verify that `valley status <handle>` reports the landing. On the host, inspect: |
| 580 | |
| 581 | ```sh |
| 582 | sudo systemctl status valley-integrator@demo --no-pager |
| 583 | sudo journalctl -u valley-integrator@demo -n 50 --no-pager |
| 584 | sudo journalctl -u valley-signers -n 20 --no-pager |
| 585 | ``` |
| 586 | |
| 587 | ### Use a governed identity registry |
| 588 | |
| 589 | A deployment can maintain principals and keys in the instance source instead of listing all of them |
| 590 | in NixOS. Start with the [registry example](../examples/identity/registry/registry.cue) and |
| 591 | [schema](../schema/identity.cue). Write your own `identity/*.cue` in the instance repository, with |
| 592 | your public keys, signing names, boundaries, and grants. Do not deploy the example's keys. |
| 593 | |
| 594 | Include a registry governor, the operator's push and approval grants, and a signing entry whose |
| 595 | `signs` value matches the integrator's `signingName`. Land the registry through the static setup |
| 596 | above. Then set `services.valley.identity.enable = true` and remove `integrator.knownSignersFile` |
| 597 | from the NixOS configuration. Apply it and inspect the compilation: |
| 598 | |
| 599 | ```sh |
| 600 | sudo systemctl start valley-identity |
| 601 | sudo journalctl -u valley-identity -n 50 --no-pager |
| 602 | ``` |
| 603 | |
| 604 | It reads the integrated registry and writes `/var/lib/valley-identity/known-signers`, authorized |
| 605 | keys, and grants. Distribute that public signers file to clients. Keep a nonempty static |
| 606 | `authorizedKeys` list for bootstrap access. Static grants remain effective alongside compiled |
| 607 | grants; remove a static approval grant if the registry should be its sole source. See |
| 608 | [identity](./architecture.md#identity) for expiry and validation rules. |
| 609 | |
| 610 | ### Publish source mirrors |
| 611 | |
| 612 | A mirror lets readers fetch the source without accessing the primary host. Add a mirror URL to the |
| 613 | project's declaration and apply the host configuration: |
| 614 | |
| 615 | ```cue |
| 616 | projects: demo: mirrors: ["git@mirror.example:team/demo.git"] |
| 617 | ``` |
| 618 | |
| 619 | Provision the `git` Unix user's SSH private key and pinned `known_hosts` under `/srv/git/.ssh`. The |
| 620 | directory should be owned by `git` with mode `0700`; the private key should have mode `0600`. Verify |
| 621 | the mirror's host key through its administrator. The publisher runs as `git` and uses that account's |
| 622 | SSH configuration. |
| 623 | |
| 624 | For an existing mirror, inspect its refs before cutover. Remove unwanted old refs explicitly; the |
| 625 | publisher does not clean up old topic branches or project storage. Start with an empty mirror when |
| 626 | possible. See [mirror cutover](./architecture.md#after-a-push-events-and-mirrors). |
| 627 | |
| 628 | After a test landing, inspect the publisher: |
| 629 | |
| 630 | ```sh |
| 631 | sudo systemctl start valley-publish@demo |
| 632 | sudo journalctl -u valley-publish@demo -n 50 --no-pager |
| 633 | git ls-remote git@mirror.example:team/demo.git |
| 634 | ``` |
| 635 | |
| 636 | Its `main` should match the primary. The publisher sends `main` and tags, not the plan graph or |
| 637 | other project storage. A mirror is not a backup of the valley. |
| 638 | |
| 639 | ### Back up and test a restore |
| 640 | |
| 641 | Backups preserve the whole data directory. Add an offsite policy to `valley.cue`: |
| 642 | |
| 643 | ```cue |
| 644 | backup: { |
| 645 | target: "restic-sftp" |
| 646 | cadence: "nightly" |
| 647 | retention: {daily: 7, weekly: 4, monthly: 6} |
| 648 | } |
| 649 | ``` |
| 650 | |
| 651 | Provision four files outside the Nix store: the restic repository URL, its encryption password, the |
| 652 | SFTP private key, and pinned SSH host keys. Keep a recoverable copy of the password away from this |
| 653 | host. Point the module at those runtime files: |
| 654 | |
| 655 | ```nix |
| 656 | backup = { |
| 657 | repositoryFile = "/var/lib/valley-secrets/restic-repository"; |
| 658 | passwordFile = "/var/lib/valley-secrets/restic-password"; |
| 659 | sshKeyFile = "/var/lib/valley-secrets/restic-ssh"; |
| 660 | knownHostsFile = "/var/lib/valley-secrets/restic-known-hosts"; |
| 661 | }; |
| 662 | ``` |
| 663 | |
| 664 | After applying the configuration, take a backup and inspect its result: |
| 665 | |
| 666 | ```sh |
| 667 | sudo systemctl start restic-backups-valley |
| 668 | sudo journalctl -u restic-backups-valley -n 50 --no-pager |
| 669 | ``` |
| 670 | |
| 671 | The service runs as root and snapshots `services.valley.dataDir`, `/srv/git` by default. Test a |
| 672 | restore into a separate empty directory. With `restic` available, run these in a root shell: |
| 673 | |
| 674 | ```sh |
| 675 | export RESTIC_REPOSITORY_FILE=/var/lib/valley-secrets/restic-repository |
| 676 | export RESTIC_PASSWORD_FILE=/var/lib/valley-secrets/restic-password |
| 677 | restic -o 'sftp.args=-i /var/lib/valley-secrets/restic-ssh -o UserKnownHostsFile=/var/lib/valley-secrets/restic-known-hosts -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=yes' snapshots |
| 678 | restic -o 'sftp.args=-i /var/lib/valley-secrets/restic-ssh -o UserKnownHostsFile=/var/lib/valley-secrets/restic-known-hosts -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=yes' restore latest --target /var/tmp/valley-restore |
| 679 | git -c safe.directory=/var/tmp/valley-restore/srv/git/demo.git -C /var/tmp/valley-restore/srv/git/demo.git fsck --full |
| 680 | git -c safe.directory=/var/tmp/valley-restore/srv/git/instance.git -C /var/tmp/valley-restore/srv/git/instance.git fsck --full |
| 681 | ``` |
| 682 | |
| 683 | Expect a snapshot, a completed restore, and no corrupt or missing objects. Git may report dangling |
| 684 | objects; those alone do not indicate corruption. Inspect the restored `main` and `refs/the-valley/*` |
| 685 | as well. This verifies the restored repository, not a transaction across every ref written while the |
| 686 | backup ran. |
| 687 | |
| 688 | ### Hold pushes for maintenance |
| 689 | |
| 690 | To refuse incoming pushes across every project, create the hold on the host: |
| 691 | |
| 692 | ```sh |
| 693 | sudo touch /srv/git/.valley-hold |
| 694 | ``` |
| 695 | |
| 696 | Remove it after maintenance with `sudo rm /srv/git/.valley-hold`. Use your configured `dataDir` if |
| 697 | it differs. The hold stops SSH pushes; stop the relevant integrator services separately if |
| 698 | maintenance also requires local integration writes to stop. |
| 699 | |
| 700 | ### Find refs named like other refs |
| 701 | |
| 702 | The push hook refuses to create or update a ref named like another ref: `refs/`, `refs/heads/`, |
| 703 | `refs/tags/` or `refs/remotes/` followed by a full ref name. When the ref it is named for is |
| 704 | missing, git reads, fetches and pushes to such a ref in its place. A host may hold some already, |
| 705 | made before the hook refused them. List them once, in every repository the host serves: |
| 706 | |
| 707 | ```sh |
| 708 | for repo in /srv/git/*.git; do |
| 709 | sudo -u git git -C "$repo" for-each-ref --format="$repo %(refname)" \ |
| 710 | refs/refs/ refs/heads/refs/ refs/tags/refs/ refs/remotes/refs/ |
| 711 | done |
| 712 | ``` |
| 713 | |
| 714 | Each line names a repository and a ref. Once any work on a ref has been pushed under another name, |
| 715 | delete the ref on the host: |
| 716 | |
| 717 | ```sh |
| 718 | sudo -u git git -C /srv/git/demo.git update-ref -d refs/heads/refs/heads/main |
| 719 | ``` |
| 720 | |
| 721 | Use your configured `dataDir` if it differs. |
| 722 | |
| 723 | ### Run the web edge |
| 724 | |
| 725 | The web edge is a web server that shows the projects `anyone` may read. The valley host's publisher |
| 726 | pushes signed exports to it, and the edge never connects back. On the edge, `valley-web-receive` |
| 727 | takes the uploads into a state directory, and `valley-web` serves them. They run as two processes, |
| 728 | as two users, over one state directory (see [The receiver](./architecture.md#the-receiver)). |
| 729 | |
| 730 | Make a token, and give the same file to both sides. Keep it out of the Nix store and readable by |
| 731 | root alone: |
| 732 | |
| 733 | ```sh |
| 734 | head -c 32 /dev/urandom | base64 | sudo install -m 0400 /dev/stdin /run/keys/valley-web-receiver-token |
| 735 | ``` |
| 736 | |
| 737 | On the valley host, enable the publisher with the receiver's URL and the token file: |
| 738 | |
| 739 | ```nix |
| 740 | imports = [ the-valley.nixosModules.default the-valley.nixosModules.web-publisher ]; |
| 741 | services.valley.webPublisher = { |
| 742 | enable = true; |
| 743 | destination = "example.org"; |
| 744 | receiver = "https://example.org"; |
| 745 | receiverTokenFile = "/run/keys/valley-web-receiver-token"; |
| 746 | }; |
| 747 | ``` |
| 748 | |
| 749 | The first pass makes the export key and writes its verifier line. Read it, and give it to the |
| 750 | receiver: |
| 751 | |
| 752 | ```sh |
| 753 | sudo systemctl start valley-web-publish |
| 754 | sudo cat /var/lib/valley-web-publish/verifier |
| 755 | ``` |
| 756 | |
| 757 | Until the receiver pins that line, it refuses every upload as `bad-signature`, and the pass fails. |
| 758 | |
| 759 | On an edge that runs NixOS, enable the receiver's module. The receiver listens where the publisher |
| 760 | reaches it, and `valley-web` behind the reverse proxy that serves the edge's name: |
| 761 | |
| 762 | ```nix |
| 763 | imports = [ the-valley.nixosModules.web-receiver ]; |
| 764 | services.valley.webReceiver = { |
| 765 | enable = true; |
| 766 | instance = "valley-host"; |
| 767 | destination = "example.org"; |
| 768 | verifiers = [ "valley-host/web-export+1a2b3c4d+AQ..." ]; |
| 769 | tokenFile = "/run/keys/valley-web-receiver-token"; |
| 770 | listen = "192.0.2.10:8081"; |
| 771 | webListen = "127.0.0.1:8080"; |
| 772 | }; |
| 773 | ``` |
| 774 | |
| 775 | On an edge that does not run NixOS, run the container image instead. Build it with Nix from a |
| 776 | checkout of the-valley, and load it: |
| 777 | |
| 778 | ```sh |
| 779 | nix build .#valley-web-image |
| 780 | docker load < result |
| 781 | ``` |
| 782 | |
| 783 | `docker load` prints the image's name and tag. Put the verifier line and a copy of the token where |
| 784 | the receiver's user, uid 990, can read them, and run two containers over one volume. The receiver |
| 785 | gets the volume writable, and `valley-web` gets it read-only: |
| 786 | |
| 787 | ```sh |
| 788 | sudo install -d -m 0750 -o 990 -g 990 /etc/valley-web |
| 789 | sudo install -m 0400 -o 990 -g 990 /run/keys/valley-web-receiver-token /etc/valley-web/token |
| 790 | echo 'valley-host/web-export+1a2b3c4d+AQ...' | sudo install -m 0444 -o 990 /dev/stdin /etc/valley-web/verifiers |
| 791 | |
| 792 | docker run -d --name valley-web-receive --user valley-web-receive \ |
| 793 | --memory 2g --memory-swap 2g \ |
| 794 | -v valley-web:/var/lib/valley-web -v /etc/valley-web:/etc/valley-web:ro \ |
| 795 | -p 192.0.2.10:8081:8081 valley-web:<tag> \ |
| 796 | valley-web-receive --state /var/lib/valley-web --listen :8081 \ |
| 797 | --instance valley-host --destination example.org \ |
| 798 | --verifiers /etc/valley-web/verifiers --token-file /etc/valley-web/token |
| 799 | |
| 800 | docker run -d --name valley-web --user valley-web \ |
| 801 | --memory 1g --memory-swap 1g \ |
| 802 | -v valley-web:/var/lib/valley-web:ro \ |
| 803 | -p 127.0.0.1:8080:8080 valley-web:<tag> \ |
| 804 | valley-web --state /var/lib/valley-web --listen :8080 |
| 805 | ``` |
| 806 | |
| 807 | A new volume takes the owner and mode of the image's `/var/lib/valley-web`: the receiver's user, |
| 808 | mode `0750`. `valley-web` reads it through the group the two users share. |
| 809 | |
| 810 | Each container gets a memory limit of its own. A container that reaches its limit is held to it: the |
| 811 | kernel reclaims memory, or kills a process, in that container alone. That keeps a spike in one |
| 812 | container from taking the other's memory or the host's only when the host has room for both limits |
| 813 | and its own work, so keep the two limits well within the host's memory. `--memory` is the limit, and |
| 814 | `--memory-swap` set to the same value gives the container no swap, as the NixOS module's units have |
| 815 | none. The sizes above are the module's defaults for `receiverMemoryMax` and `webMemoryMax`. They are |
| 816 | a deployment choice: size them to the edge's uploads and readers. |
| 817 | |
| 818 | The receiver holds the state directory to `--max-storage`, 8 GiB by default. That cap is best |
| 819 | effort, and the hard bound on the disk is the filesystem the state directory lives on (see |
| 820 | [Bounds](./architecture.md#bounds)). So put the state directory on a filesystem of fixed size, or |
| 821 | under a quota, a little larger than `--max-storage`. For the containers, that is the filesystem the |
| 822 | volume lives on. On NixOS, mount such a filesystem at the receiver's state directory, |
| 823 | `/var/lib/valley-web` by default. |
| 824 | |
| 825 | The token travels in every upload request. Reach the receiver over HTTPS, through a reverse proxy |
| 826 | that terminates TLS, or over a private network. On the next pass, the publisher prints |
| 827 | `the receiver activated sequence <n>`, and `valley-web` serves the export. |
| 828 | |
| 829 | ## Contributing to the-valley |
| 830 | |
| 831 | Contributors work in a checkout of this repository. Read [AGENTS.md](../AGENTS.md) before editing. |
| 832 | You can enter the default package's build environment: |
| 833 | |
| 834 | ```sh |
| 835 | nix develop |
| 836 | ``` |
| 837 | |
| 838 | This flake defines no dedicated development shell. `nix develop` does not supply the full toolchain. |
| 839 | For the CLI and Go work, use an explicit tool shell from the repository root: |
| 840 | |
| 841 | ```sh |
| 842 | nix shell .#valley .#attest --inputs-from . nixpkgs#go nixpkgs#cue |
| 843 | ``` |
| 844 | |
| 845 | Each Go component has its own module. Run fast tests from the component you changed, for example: |
| 846 | |
| 847 | ```sh |
| 848 | (cd status && go test ./...) |
| 849 | ``` |
| 850 | |
| 851 | There is no root Go module. To test every Go module from the repository root: |
| 852 | |
| 853 | ```sh |
| 854 | for module in attest change composed delta identity integrator knot note rebase restack selfupdate sigverify status valleyhook; do |
| 855 | (cd "$module" && go test ./...) || exit 1 |
| 856 | done |
| 857 | ``` |
| 858 | |
| 859 | In Nix, building a Go package does not run its unit tests. Those tests run only in the module's |
| 860 | `*-unit` check, such as `valleyhook-unit` for `valleyhook/` or `valley-go-unit` for `status/`: |
| 861 | |
| 862 | ```sh |
| 863 | nix build --no-link .#checks.x86_64-linux.valleyhook-unit -L |
| 864 | ``` |
| 865 | |
| 866 | Run a single Nix check by name. For documentation, format first, then check formatting, links, and |
| 867 | the style rules, then the policy that selects checks: |
| 868 | |
| 869 | ```sh |
| 870 | nix run .#fmt |
| 871 | nix build --no-link .#checks.x86_64-linux.prose-format -L |
| 872 | nix build --no-link .#checks.x86_64-linux.own-policy -L |
| 873 | ``` |
| 874 | |
| 875 | Stage new Markdown files before formatting so the formatter sees them. Review the diff afterward. |
| 876 | Use [nix/checks](../nix/checks/) to find the integration checks for the component you changed. Run |
| 877 | them one at a time; VM checks need the virtualization facilities their definitions specify. The full |
| 878 | flake suite is invoked with `nix flake check` when your task permits it and the machine can run all |
| 879 | its checks. |
| 880 | |
| 881 | Each check is given only the files it reads. [nix/checks.nix](../nix/checks.nix) and |
| 882 | [nix/packages.nix](../nix/packages.nix) narrow each source to those files with `lib.fileset`. An |
| 883 | edit to any other file leaves the check's derivation unchanged, so the check's evidence transfers to |
| 884 | the edited tree. When a check starts to read a new file, give the check that file. The sandbox fails |
| 885 | a check that reads a file it was not given. |
| 886 | |
| 887 | Editing a Go test changes only that module's `*-unit` check, not the other checks that use the |
| 888 | package. A Go package's source leaves out test files, and its `*-unit` check adds them back. |
| 889 | |
| 890 | The NixOS host module uses three of the flake's programs: the integrator, the identity compiler, and |
| 891 | `valleyhook`. Four checks test what the module renders without running those programs: |
| 892 | `module-eval`, `mirror-e2e`, `bus-e2e`, and `init-e2e`. Those checks render the module with stand-in |
| 893 | scripts in place of the three programs ([nix/checks/hosts.nix](../nix/checks/hosts.nix)), so editing |
| 894 | one of the programs leaves those four checks unchanged. Checks that run one of the programs use the |
| 895 | real package. |
| 896 | |
| 897 | To see which checks an edit gives a new derivation, run the measurement script on a commit: |
| 898 | |
| 899 | ```sh |
| 900 | nix/tools/drv-churn.sh --expect nix/tools/drv-churn.expected |
| 901 | ``` |
| 902 | |
| 903 | It applies a fixed list of representative edits to the commit's tree, one at a time, and evaluates |
| 904 | every check's derivation after each. With `--expect`, it fails if any edit changes other checks than |
| 905 | the recorded table says. When a change means to alter the table, write the new one with `--write`. |
| 906 | With `--history origin/main~20..origin/main` instead, it replays the paths each of those commits |
| 907 | touched. Run `nix/tools/drv-churn.sh --help` for every option. |
| 908 | |
| 909 | The [architecture](./architecture.md) explains component invariants and the checks that cover them. |
| 910 | The [reference](./reference.md) documents CLI flags, ref bounds, wire formats, JSON outputs, and |
| 911 | [schema](../schema/) definitions alongside the [concept glossary](./concepts.md#glossary). |