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.