rehearsal-4: gateway image — generic NixOS + idss-agent as a DO golden snapshot (ADR 0048 made real; first build = Ben's !) #646

Closed
opened 2026-08-22 04:12:29 +00:00 by ben · 1 comment
Owner

Arc: #183 rehearsal phase 4 — rented gateway (S4a). Spec: docs/superpowers/specs/2026-08-21-183-phase-4-rented-gateway-rehearsal-design.md. Plan: docs/superpowers/plans/2026-08-21-183-phase-4-rented-gateway-rehearsal.md (read its Global Constraints first — they apply to this task). This ticket's body is the plan's task verbatim. Phase-3 precedent: #515–#523.

Task 2: Gateway image — generic NixOS + agent, built as a DO golden snapshot

Files:

  • Create: nix/gateway-image/configuration.nix
  • Create: nix/gateway-image/disko.nix (the hybrid BIOS+UEFI layout — byte-equivalent to what internal/clan/enrollment.go renderDisko emits for /dev/vda; read that function first)
  • Modify: flake.nix — add nixosConfigurations.gateway-image (x86_64-linux) importing both files
  • Create: scripts/gateway-image/build.sh
  • Create: scripts/gateway-image/latest.sh
  • Create: scripts/gateway-image/tests/latest-test.sh (offline, fake doctl on PATH)
  • Modify: docs/GOTCHAS.md (append: "image disko must match DiskoLayout or clan update boots a box that never comes back")
  • Modify: STATUS.md

Interfaces:

  • Produces: DO snapshot naming idss-gateway-<12-char-sha>; scripts/gateway-image/latest.sh prints <snapshot-id>\t<name> for the newest; the image's root SSH trusts only keys DO injects at create (ssh_keys), idss-agent.service runs /usr/local/bin/idss-agent join (unit text copied from bootstrap/cmd/bootstrap/installscript.go:53-64), agent binary fetched at build from the Forgejo generic registry https://git.matou.nz/api/packages/Matou/generic/idss-agent/<sha>/agent-x86_64 (the ADR 0185 artifact scripts/bootstrap-reseed/publish.sh publishes).

  • Consumes: nothing from other tasks.

  • Step 1: Write latest-test.sh (failing): puts a fake doctl on PATH that prints two snapshot rows (111 idss-gateway-aaaaaaaaaaaa 2026-08-01T00:00:00Z and 222 idss-gateway-bbbbbbbbbbbb 2026-08-20T00:00:00Z) plus an unrelated clan-lab-golden-x row; asserts latest.sh prints 222\tidss-gateway-bbbbbbbbbbbb; with no matching rows asserts exit 1 and the message no idss-gateway-* snapshot.

  • Step 2: Run it — bash scripts/gateway-image/tests/latest-test.sh → FAIL (script missing).

  • Step 3: Write latest.sh:

    #!/usr/bin/env bash
    # Newest idss-gateway-<sha> DO snapshot: prints "<id>\t<name>". Exit 1 when none.
    set -euo pipefail
    doctl compute snapshot list --resource droplet --format ID,Name,CreatedAt --no-header \
      | awk '$2 ~ /^idss-gateway-[0-9a-f]{12}$/ {print $3 "\t" $1 "\t" $2}' | sort | tail -1 \
      | awk -F'\t' 'NF==3 {print $2 "\t" $3; found=1} END {if (!found) {print "no idss-gateway-* snapshot exists — run scripts/gateway-image/build.sh" > "/dev/stderr"; exit 1}}'
    
  • Step 4: Run the test → PASS.

  • Step 5: Write configuration.nix — imports (modulesPath + "/profiles/qemu-guest.nix"), (modulesPath + "/profiles/headless.nix"), ./disko.nix; boot.loader.grub = { enable = true; efiSupport = true; efiInstallAsRemovable = true; device = "/dev/vda"; }; networking.useDHCP = lib.mkForce true; networking.firewall.allowedTCPPorts = [ 22 ]; services.openssh = { enable = true; settings.PermitRootLogin = "prohibit-password"; }; services.cloud-init = { enable = true; network.enable = true; } (DO injects ssh_keys via metadata — cloud-init lands them in /root/.ssh/authorized_keys); systemd.services.idss-agent = the bootstrap unit verbatim with ExecStart=/usr/local/bin/idss-agent join; system.activationScripts.idssAgent installs ${agentBinary} to /usr/local/bin/idss-agent where agentBinary = pkgs.fetchurl { url = "https://git.matou.nz/api/packages/Matou/generic/idss-agent/${agentSha}/agent-x86_64"; hash = agentHash; } and agentSha/agentHash come from nix/gateway-image/agent-pin.json ({ "sha": "...", "hash": "sha256-..." }, written by build.sh). Header comment: GENERIC — nothing community-specific (ADR 0048); disko MUST equal DiskoLayout (nodeconfig.go).

  • Step 6: Write disko.nix by transcribing renderDisko (enrollment.go ~1430) for device /dev/vda; add a Go test internal/clan/gateway_image_disko_test.go that renders renderDisko("/dev/vda") and asserts the partition labels/types it emits appear verbatim in nix/gateway-image/disko.nix (reads the file relative to the repo root) — the contract test.

  • Step 7: Run go test ./internal/clan -run GatewayImageDisko → PASS; nix eval .#nixosConfigurations.gateway-image.config.system.build.toplevel.drvPath on a Linux host → evaluates (document in the ticket if the worker host lacks nix: the seam test + flake syntax check nix flake check --no-build are the gate).

  • Step 8: Write build.sh (Ben runs it; not CI yet): set -euo pipefail; requires doctl, nix, DIGITALOCEAN_ACCESS_TOKEN; sha=$(git rev-parse --short=12 HEAD); writes agent-pin.json from scripts/bootstrap-reseed registry for $sha (fails loud if the registry lacks it: "publish first"); doctl compute droplet create idss-gateway-build-$sha --region syd1 --size s-2vcpu-4gb --image ubuntu-24-04-x64 --ssh-keys "$(doctl compute ssh-key list --format FingerPrint --no-header | paste -sd,)" --tag-names idss-gateway-build --wait --format PublicIPv4 --no-header; waits for SSH; nix run github:nix-community/nixos-anywhere -- --flake .#gateway-image --target-host root@$ip; waits for SSH again and asserts systemctl is-active idss-agent = active (the image contract, proven at build); doctl compute droplet-action power-off --wait; doctl compute droplet-action snapshot --snapshot-name idss-gateway-$sha --wait; doctl compute droplet delete -f; prints the snapshot id via latest.sh. Trap on EXIT deletes the build droplet.

  • Step 9: Run bash -n scripts/gateway-image/build.sh and shellcheck (if present) → clean. Add the GOTCHAS entry.

  • Step 10: Commit — git commit -m "rehearsal-4: gateway image — generic NixOS + idss-agent as a DO golden snapshot (ADR 0048 made real) (#646)".

**Arc:** #183 rehearsal phase 4 — rented gateway (S4a). Spec: `docs/superpowers/specs/2026-08-21-183-phase-4-rented-gateway-rehearsal-design.md`. Plan: `docs/superpowers/plans/2026-08-21-183-phase-4-rented-gateway-rehearsal.md` (read its **Global Constraints** first — they apply to this task). This ticket's body is the plan's task verbatim. Phase-3 precedent: #515–#523. ### Task 2: Gateway image — generic NixOS + agent, built as a DO golden snapshot **Files:** - Create: `nix/gateway-image/configuration.nix` - Create: `nix/gateway-image/disko.nix` (the hybrid BIOS+UEFI layout — byte-equivalent to what `internal/clan/enrollment.go` `renderDisko` emits for `/dev/vda`; read that function first) - Modify: `flake.nix` — add `nixosConfigurations.gateway-image` (x86_64-linux) importing both files - Create: `scripts/gateway-image/build.sh` - Create: `scripts/gateway-image/latest.sh` - Create: `scripts/gateway-image/tests/latest-test.sh` (offline, fake `doctl` on PATH) - Modify: `docs/GOTCHAS.md` (append: "image disko must match `DiskoLayout` or `clan update` boots a box that never comes back") - Modify: `STATUS.md` **Interfaces:** - Produces: DO snapshot naming `idss-gateway-<12-char-sha>`; `scripts/gateway-image/latest.sh` prints `<snapshot-id>\t<name>` for the newest; the image's root SSH trusts only keys DO injects at create (`ssh_keys`), `idss-agent.service` runs `/usr/local/bin/idss-agent join` (unit text copied from `bootstrap/cmd/bootstrap/installscript.go:53-64`), agent binary fetched at build from the Forgejo generic registry `https://git.matou.nz/api/packages/Matou/generic/idss-agent/<sha>/agent-x86_64` (the ADR 0185 artifact `scripts/bootstrap-reseed/publish.sh` publishes). - Consumes: nothing from other tasks. - [ ] **Step 1: Write `latest-test.sh`** (failing): puts a fake `doctl` on PATH that prints two snapshot rows (`111 idss-gateway-aaaaaaaaaaaa 2026-08-01T00:00:00Z` and `222 idss-gateway-bbbbbbbbbbbb 2026-08-20T00:00:00Z`) plus an unrelated `clan-lab-golden-x` row; asserts `latest.sh` prints `222\tidss-gateway-bbbbbbbbbbbb`; with no matching rows asserts exit 1 and the message `no idss-gateway-* snapshot`. - [ ] **Step 2: Run it** — `bash scripts/gateway-image/tests/latest-test.sh` → FAIL (script missing). - [ ] **Step 3: Write `latest.sh`**: ```bash #!/usr/bin/env bash # Newest idss-gateway-<sha> DO snapshot: prints "<id>\t<name>". Exit 1 when none. set -euo pipefail doctl compute snapshot list --resource droplet --format ID,Name,CreatedAt --no-header \ | awk '$2 ~ /^idss-gateway-[0-9a-f]{12}$/ {print $3 "\t" $1 "\t" $2}' | sort | tail -1 \ | awk -F'\t' 'NF==3 {print $2 "\t" $3; found=1} END {if (!found) {print "no idss-gateway-* snapshot exists — run scripts/gateway-image/build.sh" > "/dev/stderr"; exit 1}}' ``` - [ ] **Step 4: Run the test** → PASS. - [ ] **Step 5: Write `configuration.nix`** — imports `(modulesPath + "/profiles/qemu-guest.nix")`, `(modulesPath + "/profiles/headless.nix")`, `./disko.nix`; `boot.loader.grub = { enable = true; efiSupport = true; efiInstallAsRemovable = true; device = "/dev/vda"; }`; `networking.useDHCP = lib.mkForce true;` `networking.firewall.allowedTCPPorts = [ 22 ];` `services.openssh = { enable = true; settings.PermitRootLogin = "prohibit-password"; };` `services.cloud-init = { enable = true; network.enable = true; }` (DO injects `ssh_keys` via metadata — cloud-init lands them in `/root/.ssh/authorized_keys`); `systemd.services.idss-agent` = the bootstrap unit verbatim with `ExecStart=/usr/local/bin/idss-agent join`; `system.activationScripts.idssAgent` installs `${agentBinary}` to `/usr/local/bin/idss-agent` where `agentBinary = pkgs.fetchurl { url = "https://git.matou.nz/api/packages/Matou/generic/idss-agent/${agentSha}/agent-x86_64"; hash = agentHash; }` and `agentSha`/`agentHash` come from `nix/gateway-image/agent-pin.json` (`{ "sha": "...", "hash": "sha256-..." }`, written by `build.sh`). Header comment: GENERIC — nothing community-specific (ADR 0048); disko MUST equal `DiskoLayout` (nodeconfig.go). - [ ] **Step 6: Write `disko.nix`** by transcribing `renderDisko` (enrollment.go ~1430) for device `/dev/vda`; add a Go test `internal/clan/gateway_image_disko_test.go` that renders `renderDisko("/dev/vda")` and asserts the partition labels/types it emits appear verbatim in `nix/gateway-image/disko.nix` (reads the file relative to the repo root) — the contract test. - [ ] **Step 7: Run** `go test ./internal/clan -run GatewayImageDisko` → PASS; `nix eval .#nixosConfigurations.gateway-image.config.system.build.toplevel.drvPath` on a Linux host → evaluates (document in the ticket if the worker host lacks nix: the seam test + flake syntax check `nix flake check --no-build` are the gate). - [ ] **Step 8: Write `build.sh`** (Ben runs it; not CI yet): `set -euo pipefail`; requires `doctl`, `nix`, `DIGITALOCEAN_ACCESS_TOKEN`; `sha=$(git rev-parse --short=12 HEAD)`; writes `agent-pin.json` from `scripts/bootstrap-reseed` registry for `$sha` (fails loud if the registry lacks it: "publish first"); `doctl compute droplet create idss-gateway-build-$sha --region syd1 --size s-2vcpu-4gb --image ubuntu-24-04-x64 --ssh-keys "$(doctl compute ssh-key list --format FingerPrint --no-header | paste -sd,)" --tag-names idss-gateway-build --wait --format PublicIPv4 --no-header`; waits for SSH; `nix run github:nix-community/nixos-anywhere -- --flake .#gateway-image --target-host root@$ip`; waits for SSH again and asserts `systemctl is-active idss-agent` = `active` (the image contract, proven at build); `doctl compute droplet-action power-off --wait`; `doctl compute droplet-action snapshot --snapshot-name idss-gateway-$sha --wait`; `doctl compute droplet delete -f`; prints the snapshot id via `latest.sh`. Trap on EXIT deletes the build droplet. - [ ] **Step 9: Run** `bash -n scripts/gateway-image/build.sh` and `shellcheck` (if present) → clean. Add the GOTCHAS entry. - [ ] **Step 10: Commit** — `git commit -m "rehearsal-4: gateway image — generic NixOS + idss-agent as a DO golden snapshot (ADR 0048 made real) (#646)"`.

close-report for #646 — status success

✅ close-report gates passed — every claim verified against main; closing.

envelope
{
  "issue": 646,
  "status": "success",
  "commits": [
    "902644b841f84e171f2c4419c83f6aac98016b6c"
  ],
  "changed_files": [
    "STATUS.md",
    "docs/GOTCHAS.md",
    "flake.nix",
    "internal/clan/gateway_image_disko_test.go",
    "nix/gateway-image/agent-pin.json",
    "nix/gateway-image/configuration.nix",
    "nix/gateway-image/disko.nix",
    "scripts/gateway-image/build.sh",
    "scripts/gateway-image/latest.sh",
    "scripts/gateway-image/tests/latest-test.sh"
  ],
  "tests": [
    {
      "command": "bash scripts/gateway-image/tests/latest-test.sh",
      "exit_code": 0
    },
    {
      "command": "nix develop .#go-ci -c go test ./internal/clan -run GatewayImageDisko",
      "exit_code": 0
    },
    {
      "command": "nix develop .#go-ci -c go test ./internal/clan/",
      "exit_code": 0
    },
    {
      "command": "nix develop .#go-ci -c go vet ./internal/clan/",
      "exit_code": 0
    },
    {
      "command": "gofmt -l internal/clan/gateway_image_disko_test.go",
      "exit_code": 0
    },
    {
      "command": "nix eval .#nixosConfigurations.gateway-image.config.system.build.toplevel.drvPath",
      "exit_code": 0
    },
    {
      "command": "nix flake check --no-build",
      "exit_code": 0
    },
    {
      "command": "bash -n scripts/gateway-image/build.sh",
      "exit_code": 0
    },
    {
      "command": "nix run nixpkgs#shellcheck -- scripts/gateway-image/build.sh scripts/gateway-image/latest.sh scripts/gateway-image/tests/latest-test.sh",
      "exit_code": 0
    }
  ],
  "criteria": {
    "latest.sh prints <id>\\t<name> for the newest idss-gateway-<sha> snapshot; exit 1 + 'no idss-gateway-* snapshot' when none": "scripts/gateway-image/latest.sh; proven offline by scripts/gateway-image/tests/latest-test.sh (both cases PASS)",
    "disko.nix byte-equivalent to renderDisko for /dev/vda; contract test asserts it": "nix/gateway-image/disko.nix + internal/clan/gateway_image_disko_test.go (renders renderDisko, asserts each partition line verbatim + device /dev/vda); go test -run GatewayImageDisko PASS",
    "flake nixosConfigurations.gateway-image (x86_64) importing configuration.nix + disko.nix evaluates": "flake.nix gateway-image via clan-core disko input; nix eval …toplevel.drvPath + nix flake check --no-build both green",
    "image generic (ADR 0048) — no secret/slug/brand; root SSH trusts only DO-injected ssh_keys via cloud-init": "nix/gateway-image/configuration.nix (cloud-init network.enable, PermitRootLogin=prohibit-password, no baked authorized key, generic hostname)",
    "idss-agent.service runs /usr/local/bin/idss-agent join, unit verbatim from installscript.go:53-64": "configuration.nix systemd.services.idss-agent (ExecStart=/usr/local/bin/idss-agent join, Restart=always, After/Wants network-online.target, WantedBy multi-user.target)",
    "agent fetched from the ADR-0185 Forgejo generic registry pinned by agent-pin.json": "configuration.nix pkgs.fetchurl url .../idss-agent/${agentSha}/agent-x86_64 from nix/gateway-image/agent-pin.json (placeholder committed so flake evaluates; build.sh writes the real full-sha pin)",
    "build.sh builds the DO golden snapshot idss-gateway-<12-char-sha> via nixos-anywhere, proves idss-agent active, destroys droplet on any exit (ADR 0047)": "scripts/gateway-image/build.sh; bash -n + shellcheck clean (not run live — Ben runs it, the first snapshot is his `!`)",
    "GOTCHAS entry: image disko must match DiskoLayout or clan update boots a box that never comes back": "docs/GOTCHAS.md #47",
    "STATUS.md updated same commit": "STATUS.md header lead #646"
  },
  "summary": "Rented gateway image (#646, phase-4 Task 2): generic NixOS + pinned idss-agent as a DO golden snapshot — flake nixosConfigurations.gateway-image, disko.nix guarded byte-equivalent to renderDisko, plus latest.sh/build.sh and their tests.",
  "reason": "",
  "blockers": [],
  "no_code_change": ""
}
**close-report** for #646 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; closing. <details><summary>envelope</summary> ```json { "issue": 646, "status": "success", "commits": [ "902644b841f84e171f2c4419c83f6aac98016b6c" ], "changed_files": [ "STATUS.md", "docs/GOTCHAS.md", "flake.nix", "internal/clan/gateway_image_disko_test.go", "nix/gateway-image/agent-pin.json", "nix/gateway-image/configuration.nix", "nix/gateway-image/disko.nix", "scripts/gateway-image/build.sh", "scripts/gateway-image/latest.sh", "scripts/gateway-image/tests/latest-test.sh" ], "tests": [ { "command": "bash scripts/gateway-image/tests/latest-test.sh", "exit_code": 0 }, { "command": "nix develop .#go-ci -c go test ./internal/clan -run GatewayImageDisko", "exit_code": 0 }, { "command": "nix develop .#go-ci -c go test ./internal/clan/", "exit_code": 0 }, { "command": "nix develop .#go-ci -c go vet ./internal/clan/", "exit_code": 0 }, { "command": "gofmt -l internal/clan/gateway_image_disko_test.go", "exit_code": 0 }, { "command": "nix eval .#nixosConfigurations.gateway-image.config.system.build.toplevel.drvPath", "exit_code": 0 }, { "command": "nix flake check --no-build", "exit_code": 0 }, { "command": "bash -n scripts/gateway-image/build.sh", "exit_code": 0 }, { "command": "nix run nixpkgs#shellcheck -- scripts/gateway-image/build.sh scripts/gateway-image/latest.sh scripts/gateway-image/tests/latest-test.sh", "exit_code": 0 } ], "criteria": { "latest.sh prints <id>\\t<name> for the newest idss-gateway-<sha> snapshot; exit 1 + 'no idss-gateway-* snapshot' when none": "scripts/gateway-image/latest.sh; proven offline by scripts/gateway-image/tests/latest-test.sh (both cases PASS)", "disko.nix byte-equivalent to renderDisko for /dev/vda; contract test asserts it": "nix/gateway-image/disko.nix + internal/clan/gateway_image_disko_test.go (renders renderDisko, asserts each partition line verbatim + device /dev/vda); go test -run GatewayImageDisko PASS", "flake nixosConfigurations.gateway-image (x86_64) importing configuration.nix + disko.nix evaluates": "flake.nix gateway-image via clan-core disko input; nix eval …toplevel.drvPath + nix flake check --no-build both green", "image generic (ADR 0048) — no secret/slug/brand; root SSH trusts only DO-injected ssh_keys via cloud-init": "nix/gateway-image/configuration.nix (cloud-init network.enable, PermitRootLogin=prohibit-password, no baked authorized key, generic hostname)", "idss-agent.service runs /usr/local/bin/idss-agent join, unit verbatim from installscript.go:53-64": "configuration.nix systemd.services.idss-agent (ExecStart=/usr/local/bin/idss-agent join, Restart=always, After/Wants network-online.target, WantedBy multi-user.target)", "agent fetched from the ADR-0185 Forgejo generic registry pinned by agent-pin.json": "configuration.nix pkgs.fetchurl url .../idss-agent/${agentSha}/agent-x86_64 from nix/gateway-image/agent-pin.json (placeholder committed so flake evaluates; build.sh writes the real full-sha pin)", "build.sh builds the DO golden snapshot idss-gateway-<12-char-sha> via nixos-anywhere, proves idss-agent active, destroys droplet on any exit (ADR 0047)": "scripts/gateway-image/build.sh; bash -n + shellcheck clean (not run live — Ben runs it, the first snapshot is his `!`)", "GOTCHAS entry: image disko must match DiskoLayout or clan update boots a box that never comes back": "docs/GOTCHAS.md #47", "STATUS.md updated same commit": "STATUS.md header lead #646" }, "summary": "Rented gateway image (#646, phase-4 Task 2): generic NixOS + pinned idss-agent as a DO golden snapshot — flake nixosConfigurations.gateway-image, disko.nix guarded byte-equivalent to renderDisko, plus latest.sh/build.sh and their tests.", "reason": "", "blockers": [], "no_code_change": "" } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Matou/idss#646
No description provided.