From 5daff0c42198d9c2a4ab24c2a7fd2fac8225058c Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 2 Nov 2026 04:27:33 +0000 Subject: [PATCH] nixos: vendor the oc-password-sync script into the flake (code#3724) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The folded module declared units whose ExecStart target (~/.local/bin/oc-password-sync.sh) the flake did not provide: the script — including the corrected code#3724 drift logic — existed only as an unmanaged operator file on the laptop. Vendor the script beside the module (byte-identical to the verified live file, sha256 fc47b0d0…) and install it at the referenced path via home.file. ExecStart and both units are unchanged, so the next activation swaps the operator file for the store copy with zero behavior change. --- nixos/home/oc-password-sync.nix | 23 +++++++++++++- nixos/home/oc-password-sync.sh | 56 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100755 nixos/home/oc-password-sync.sh diff --git a/nixos/home/oc-password-sync.nix b/nixos/home/oc-password-sync.nix index cdc6a22567..a57d1a4b1f 100644 --- a/nixos/home/oc-password-sync.nix +++ b/nixos/home/oc-password-sync.nix @@ -5,7 +5,9 @@ # oneshot service on change; the .path unit is the one enabled into # default.target. (The service file also carries its own # [Install] WantedBy=default.target, dormant in practice; it is -# replicated as installed.) Script: ~/.local/bin/oc-password-sync.sh. +# replicated as installed.) Script: ./oc-password-sync.sh, vendored +# beside this module and installed by home.file at the path +# ExecStart references (~/.local/bin/oc-password-sync.sh). # # Home-manager module: imported by the amd host config # (nixos/hosts/default.nix) with services.oc-password-sync.enable = true. @@ -13,6 +15,18 @@ # Folded into the flake 2026-11-01 (developing-today/code#3720): # until now the units were plain files in ~/.config/systemd/user. # Semantics are replicated exactly; only provenance changes. +# +# Script vendored 2026-11-01 (code#3724 reconciliation): the units +# were folded, but the script itself stayed an unmanaged operator +# file — and the corrected #3724 logic (drift judged against the +# running OpenChamber process's /proc environ; see the script's +# header) existed only there. The vendored copy is byte-identical +# to the verified live file at the time of the change +# (sha256 fc47b0d0ca4766a65a8a9225c865f59a920176a274e259adaa44d2f57d0a5df3), +# so home-manager taking over the path at the next activation +# changes no behavior: ExecStart and both units are untouched. +# (home-manager.backupFileExtension renames the operator file to +# *.backup on that activation; its bytes equal the store copy.) { config, lib, @@ -45,5 +59,12 @@ in }; Install.WantedBy = [ "default.target" ]; }; + + # Provision the script at the exact path ExecStart references, + # from the copy vendored beside this module (./oc-password-sync.sh). + home.file.".local/bin/oc-password-sync.sh" = { + source = ./oc-password-sync.sh; + executable = true; + }; }; } diff --git a/nixos/home/oc-password-sync.sh b/nixos/home/oc-password-sync.sh new file mode 100755 index 0000000000..22d959f43a --- /dev/null +++ b/nixos/home/oc-password-sync.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# oc-password-sync: keep OpenChamber's upstream OpenCode password in sync +# with the prod server's published service file. The prod server rotates +# its password on every start (~/.local/state/opencode/service-prod.json). +# +# Mechanism (2026-11-01, code#3724): OpenChamber no longer reads the +# password from the old hand-applied systemd drop-in +# (openchamber.service.d/external-opencode.conf). That drop-in was +# retired by design when openchamber#766 gave OpenChamber a start +# wrapper (bin/openchamber-start.sh) that reads service-prod.json +# directly at every start and exports OPENCODE_PASSWORD. This sync's +# remaining job is the mid-run rotation case: when the prod password +# changes while OpenChamber is running, restart OpenChamber so its +# wrapper re-reads the file. Drift is judged against the password the +# running OpenChamber process actually holds (its /proc environ), +# falling back to the last-synced hash in the state file. +# Invoked by oc-password-sync.path on service-prod.json changes, and +# manually. +set -uo pipefail +JSON="${OC_SERVICE_JSON:-$HOME/.local/state/opencode/service-prod.json}" +STATE_DIR="$HOME/.local/state/openchamber" +STATE="$STATE_DIR/password-sync.state" +LOG="$STATE_DIR/password-sync.log" +mkdir -p "$STATE_DIR" +ts() { date -Is; } +log() { echo "$(ts) $*" >>"$LOG"; } +if [ ! -f "$JSON" ]; then log "no service file at $JSON; skipping"; exit 0; fi +NEW=$(OCF="$JSON" python3 -c 'import json,os; print(json.load(open(os.environ["OCF"]))["password"])' 2>/dev/null) +if [ -z "${NEW:-}" ]; then log "could not parse password from $JSON; skipping"; exit 0; fi +NEWHASH=$(printf %s "$NEW" | sha256sum | cut -d" " -f1) +# Current password: what the running OpenChamber process holds. +CUR="" +PID=$(systemctl --user show openchamber.service -p MainPID --value 2>/dev/null || echo 0) +if [ "${PID:-0}" -gt 0 ] && [ -r "/proc/$PID/environ" ]; then + CUR=$(tr "\0" "\n" <"/proc/$PID/environ" | sed -n "s/^OPENCODE_PASSWORD=//p" | head -1) +fi +if [ -n "$CUR" ]; then + if [ "$NEW" = "$CUR" ]; then printf %s "$NEWHASH" >"$STATE"; log "in sync; no action"; exit 0; fi +else + # No live reading (OpenChamber down): compare against last-synced hash. + LAST=$(cat "$STATE" 2>/dev/null || true) + if [ "$NEWHASH" = "$LAST" ]; then log "in sync (state); openchamber not running; no action"; exit 0; fi +fi +if [ "${OC_DRY_RUN:-0}" = "1" ]; then log "DRY RUN: drift detected; would restart openchamber.service"; echo "drift detected (dry run)"; exit 0; fi +if ! systemctl --user restart openchamber.service; then log "restart FAILED"; exit 1; fi +# Verify: the restarted process must hold the new password. +for i in $(seq 1 30); do + PID=$(systemctl --user show openchamber.service -p MainPID --value 2>/dev/null || echo 0) + if [ "${PID:-0}" -gt 0 ] && [ -r "/proc/$PID/environ" ]; then + CUR=$(tr "\0" "\n" <"/proc/$PID/environ" | sed -n "s/^OPENCODE_PASSWORD=//p" | head -1) + if [ "$NEW" = "$CUR" ]; then printf %s "$NEWHASH" >"$STATE"; log "drift corrected; openchamber.service restarted, password verified in new process"; exit 0; fi + fi + sleep 1 +done +log "restart done but password NOT verified in new process within 30s" +exit 1