From d4b6616d88845ce7667f9b8be969434534734237 Mon Sep 17 00:00:00 2001 From: Erik Simon Date: Sat, 3 Oct 2026 01:48:29 +0200 Subject: [PATCH] fix(mars): rotated keys reach every Hermes profile's .env Named profiles keep their own .env, so coder kept the old OpenCode key and librarian, with no OPENCODE line at all, was left on an exhausted pool entry. The OpenCode key now goes to every profile; other keys only to profiles that already carry them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011XkDJkdDzneVVdnkZfrvTY --- hosts/mars/hermes-agent.nix | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/hosts/mars/hermes-agent.nix b/hosts/mars/hermes-agent.nix index f7996c4..72066c0 100644 --- a/hosts/mars/hermes-agent.nix +++ b/hosts/mars/hermes-agent.nix @@ -280,23 +280,37 @@ in # DID pass through) but answered every bearer token with # "Invalid gateway API key" — the passthrough list carries the plain # API_SERVER_* vars and drops the secret one. + # + # Each named profile has its own .env, cloned from the root one at + # creation, so a rotation must reach those too (coder and librarian kept + # the old OpenCode key). Profiles only get keys they already carry, unless + # the second argument is "all": librarian had no OPENCODE line and was + # left on an exhausted pool entry for the lapsed key. write_env_key() { setpriv --reuid=${hermesUid} --regid=${hermesGid} --clear-groups \ ${pkgs.runtimeShell} -c ' set -eu - envf=$1 + home=$1 name=$2 + scope=$3 umask 077 key=$(cat) - [ -e "$envf" ] || : > "$envf" - { grep -v "^$name=" "$envf" || true - printf "%s=%s\n" "$name" "$key"; } > "$envf.new" - mv -f "$envf.new" "$envf" - ' _ ${hermesHome}/.env "$1" + [ -e "$home/.env" ] || : > "$home/.env" + for envf in "$home/.env" "$home"/profiles/*/.env; do + [ -f "$envf" ] || continue + if [ "$envf" != "$home/.env" ] && [ "$scope" != all ] \ + && ! grep -q "^$name=" "$envf"; then + continue + fi + { grep -v "^$name=" "$envf" || true + printf "%s=%s\n" "$name" "$key"; } > "$envf.new" + mv -f "$envf.new" "$envf" + done + ' _ ${hermesHome} "$1" "''${2:-}" } # OPENCODE_GO_API_KEY does pass through, but a stale .env line from # `hermes setup` overrides it — that is how a rotated key kept 401ing. - write_env_key OPENCODE_GO_API_KEY < ${config.sops.secrets.opencode_go_api_key.path} + write_env_key OPENCODE_GO_API_KEY all < ${config.sops.secrets.opencode_go_api_key.path} write_env_key ELEVENLABS_API_KEY < ${config.sops.secrets.elevenlabs_api_key.path} # Only these keys are asserted; anything Hermes adds to honcho.json stays.