feat(mars): expose luna over the OpenAI-compatible API server

API_SERVER_ENABLED/HOST/PORT in the env template, bound 0.0.0.0 for the
same reason the dashboard is: the container is --network=host and nothing
opens 8642 in the firewall, so it is reachable over tailscale0 only.

API_SERVER_KEY has to go through .env like the ElevenLabs key did
(461d6cc). Confirmed the same way: with the key set only in the container
env the server came up — so API_SERVER_ENABLED/HOST *did* pass through —
but answered every bearer token with "Invalid gateway API key". The s6
passthrough list carries the plain API_SERVER_* vars and drops the secret
one. The .env write is now a write_env_key helper so both keys share it.

restartUnits gains prepare-dirs, which is what writes the .env line; the
container then has to come back up to read it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-20 22:45:05 +02:00
co-authored by Claude Opus 5
parent 9566624686
commit 8dc3affdda
2 changed files with 38 additions and 11 deletions
+14 -3
View File
@@ -272,17 +272,28 @@ in
# setpriv because root must not write into hermesHome — luna could plant a # setpriv because root must not write into hermesHome — luna could plant a
# symlink there. The key arrives on an fd opened while still root, so it # symlink there. The key arrives on an fd opened while still root, so it
# never lands in argv or the journal and her uid needs no /run/secrets access. # never lands in argv or the journal and her uid needs no /run/secrets access.
#
# API_SERVER_KEY is in the same boat, confirmed the same way: with it set
# only in the container env the server came up (so API_SERVER_ENABLED/HOST
# 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.
write_env_key() {
setpriv --reuid=${hermesUid} --regid=${hermesGid} --clear-groups \ setpriv --reuid=${hermesUid} --regid=${hermesGid} --clear-groups \
${pkgs.runtimeShell} -c ' ${pkgs.runtimeShell} -c '
set -eu set -eu
envf=$1 envf=$1
name=$2
umask 077 umask 077
key=$(cat) key=$(cat)
[ -e "$envf" ] || : > "$envf" [ -e "$envf" ] || : > "$envf"
{ grep -v "^ELEVENLABS_API_KEY=" "$envf" || true { grep -v "^$name=" "$envf" || true
printf "ELEVENLABS_API_KEY=%s\n" "$key"; } > "$envf.new" printf "%s=%s\n" "$name" "$key"; } > "$envf.new"
mv -f "$envf.new" "$envf" mv -f "$envf.new" "$envf"
' _ ${hermesHome}/.env < ${config.sops.secrets.elevenlabs_api_key.path} ' _ ${hermesHome}/.env "$1"
}
write_env_key ELEVENLABS_API_KEY < ${config.sops.secrets.elevenlabs_api_key.path}
write_env_key API_SERVER_KEY < ${config.sops.secrets.hermes_api_server_key.path}
if [ -d ${hermesHome}/.config ]; then if [ -d ${hermesHome}/.config ]; then
chown ${hermesUid}:${hermesGid} ${hermesHome}/.config chown ${hermesUid}:${hermesGid} ${hermesHome}/.config
+16
View File
@@ -40,6 +40,18 @@
sops.secrets.gitea_hermes_webhook_secret = { sops.secrets.gitea_hermes_webhook_secret = {
restartUnits = [ "hermes-agent-webhook-routes.service" ]; restartUnits = [ "hermes-agent-webhook-routes.service" ];
}; };
# Bearer token for the OpenAI-compatible API server (see the env template
# below). Same value in secrets/terra.yaml, where luna-voiced reads it.
# API_SERVER_HOST=0.0.0.0 for the same reason the dashboard binds it (see
# hermes-agent.nix): the container is --network=host and nothing opens 8642
# in the firewall, so it is reachable over tailscale0 only — LAN-closed.
# prepare-dirs writes it into luna's .env (the container env alone does not
# reach the gateway — see hermes-agent.nix); the container then has to come
# back up to read it.
sops.secrets.hermes_api_server_key.restartUnits = [
"hermes-agent-prepare-dirs.service"
"podman-hermes-agent.service"
];
sops.templates."hermes-agent.env".content = '' sops.templates."hermes-agent.env".content = ''
OPENCODE_GO_API_KEY=${config.sops.placeholder.opencode_go_api_key} OPENCODE_GO_API_KEY=${config.sops.placeholder.opencode_go_api_key}
TELEGRAM_BOT_TOKEN=${config.sops.placeholder.telegram_bot_token} TELEGRAM_BOT_TOKEN=${config.sops.placeholder.telegram_bot_token}
@@ -49,6 +61,10 @@
WEBHOOK_PORT=8644 WEBHOOK_PORT=8644
HERMES_DASHBOARD_OIDC_CLIENT_SECRET=${config.sops.placeholder.hermes_dashboard_oidc_client_secret} HERMES_DASHBOARD_OIDC_CLIENT_SECRET=${config.sops.placeholder.hermes_dashboard_oidc_client_secret}
ELEVENLABS_API_KEY=${config.sops.placeholder.elevenlabs_api_key} ELEVENLABS_API_KEY=${config.sops.placeholder.elevenlabs_api_key}
API_SERVER_ENABLED=true
API_SERVER_HOST=0.0.0.0
API_SERVER_PORT=8642
API_SERVER_KEY=${config.sops.placeholder.hermes_api_server_key}
''; '';
# luna's gitea push token (services/dev/gitea.nix provisions the account + # luna's gitea push token (services/dev/gitea.nix provisions the account +