`deploy.sh` + the bare-rebuild guardrail — A small internal developer platform

NixOSInternal Developer PlatformDeployment ToolingClaude CodeOperational Safety

What it is

One deploy.sh (1,155 lines) deploys my 10 NixOS profiles (desktops, laptops, a VPS, a NAS, a Windows/WSL setup) from one inventory file. A PreToolUse hook refuses the most dangerous wrong way to deploy, bare nixos-rebuild switch on a remote, because that mistake broke a node once.

Zoom

Why it exists

Before this work, the deploy story had three problems:

  • Two near-duplicate scripts (deploy-lxc.sh and deploy-others.sh, ~400 lines each) drifted apart and had to be patched twice for every change.
  • A bundled install.sh -q flag meant "skip docker AND skip hardware-config regeneration." On containers that was correct; on laptops it produced an unbootable system because hardware-configuration.nix is per-machine.
  • Bare nixos-rebuild switch on a remote builds with whatever hardware-configuration.nix is in the checkout, not one generated for that machine. This broke a node during a deploy.

Three commits over Feb 13–21, 2026 fixed all three. The deploy path changed again in October 2026, when a build farm and scheduled server deploys came in.

What I built

  • Unified deploy.sh + deploy-servers.conf — one script, one inventory. Grouped TUI with Nerd Font icons, inline editing, multi-IP probing (LAN → Tailscale fallback), --profile / --group / --all / --dry-run flags.
  • One command, the right path per machine — ./deploy.sh --profile P. On servers it runs the same guarded deploy the weekly schedule uses: it refuses a checkout with local edits or unpushed commits, waits until the build farm has built that commit, switches, runs a post-deploy check and rolls back if the check fails. On workstations it runs install.sh, which fetches and fast-forwards only. Neither path throws away local work; --reset is a separate recovery flag that shows what would be lost and asks first.
  • install.sh flag split — -q decomposed into -d (skip docker) and -h (skip hardware-config). -s on every deploy, -d on docker hosts. -u (update flake.lock) is never a default: lock updates go through the build farm, or are tested on my desktop first. Documented in .claude/rules/deployment.md so Claude Code loads it when editing deployment files.
  • hardware-configuration.nix regenerated on the machine — the file is tracked in git, and install.sh regenerates it on the target machine before each build. It refuses -h when the committed file belongs to another machine, and refuses to build when the file picked up runtime mounts (docker overlays, network shares).
  • PreToolUse hook block-nixos-rebuild.sh — 27 lines of bash. Intercepts every Claude Code Bash call, greps for nixos-rebuild\s+switch, and refuses with a message that lists the allowed deploy paths. Declared in the Claude Code Nix module, so every workstation gets the same hook from one source.
  • CLAUDE.md FORBIDDEN box — an explicit zero-exception rule that the model reads at the start of every conversation.

Key decisions & tradeoffs

Decision 1: Enforcement (hook) vs. discipline (rule). The "don't do this" line lived in CLAUDE.md for weeks. The rule was written down, and I ran the command anyway on a tired day. The hook makes the wrong path uncallable: the model has to ask how to deploy, and the redirect message answers in one screen.

Decision 2: One unified deploy.sh vs. two specialized scripts. Two scripts had real benefits (different defaults per target type, different argument parsers). But every change had to be patched twice and they drifted: different SSH user defaults, different set -e behavior. One script with an inventory file keeps the per-target differences in data instead of code.

Decision 3: No git reset --hard in the normal path. The first unified workflow was fetch + git reset --hard origin/main + install.sh on the node. It worked, and it also deleted any local work on the node without asking. Today the normal path never resets: servers refuse to deploy over local changes, workstations only fast-forward, and a reset needs the explicit --reset flag.

What broke / what I learned

The original -q flag was added during a refactor to "speed things up": skip docker and hardware-config in one shorthand. On containers (no real hardware) that was correct. On laptops it produced an unbootable system, because the container defaults overwrote the laptop's disk config. The fix wasn't a warning; I separated the two concerns, so the right flag combination is the one written in deployment.md.

The hook closed one path, not every path. The NAS still failed to boot after deploys on 2026-04-15, twice in August, and on 2026-10-14. In April a stale inventory entry still passed -h, so the build took another machine's disk layout; install.sh now refuses that. The August and October failures came from hardware-configuration.nix being regenerated at the wrong moment: once while docker mounts were active, once while a temporary pool was imported during a disk migration. Docker mounts in that file are now refused before anything is built, by install.sh and by a guard module that every profile imports. For the second case, scheduled updates are now stopped before any storage migration.

Results

  • 1,155-line deploy.sh replaces two diverging ~400-line scripts.
  • 10 NixOS profiles deployable with one command.
  • No reset in the normal path: servers refuse local changes and roll back on a failed check, workstations fast-forward only.
  • One enforced workflow: commit → push → build farm → ./deploy.sh --profile P (or the weekly schedule).

Stack

Bash, NixOS, nixos-rebuild, Claude Code (PreToolUse hook), Nerd Fonts, Tailscale.

Status

  • Repo: private dotfiles flake (same codebase as my-nixos-infrastructure).
  • Running daily: every deploy to desktops, laptops, the VPS and the NAS goes through this path.
  • Hook installation: declarative via the Claude Code Nix module.
  • Related portfolio entries: my-nixos-infrastructure (the codebase), claude-code-mcp-operator (where the hook is declared).

Edited with Claude.