gateway: fix nginx reload — trigger from host via systemd-run (#889)

The #872 path-unit approach was silently broken: IN_MOVED_TO from an
atomic rename on the host does not propagate across the nspawn
mount-namespace boundary into the container, so the watcher inside
the gateway container never fired.

Fix: after each agents.conf write, c0re calls
`systemd-run --machine=hive-gateway -- nginx -s reload` from the host.
The reload is best-effort (logged on failure, not fatal).

Remove the now-unused `hive-gateway-agents-conf.path` +
`hive-gateway-nginx-reload.service` from the gateway container config.
Update docs/gateway.md + comments to reflect the host-side approach.
This commit is contained in:
atlas 2026-05-31 22:35:06 +02:00 committed by mara
commit 01d7c37af2
3 changed files with 91 additions and 60 deletions

View file

@ -252,10 +252,10 @@ in
# Bind-mount ONLY the gateway-specific subdir of the hyperhive
# state dir. Scoped to /var/lib/hyperhive/gateway/ rather than
# the whole parent so the gateway container can't read forge
# tokens or other files that may live at the parent level (argus
# 🟡 on #872). c0re writes agents.conf under this subdir;
# the systemd path unit inside the container fires nginx -s reload
# on each atomic rename. Pre-created by a tmpfiles rule.
# tokens or other files that may live at the parent level.
# c0re writes agents.conf under this subdir and triggers an nginx
# reload from the host via systemd-run after each write.
# Pre-created by a tmpfiles rule.
bindMounts."/run/hive-state" = {
hostPath = "/var/lib/hyperhive/gateway";
isReadOnly = true;
@ -389,37 +389,14 @@ in
'';
};
# Watch /run/hive-state/agents.conf (bind-mounted from the
# host's /var/lib/hyperhive/agents.conf) for changes and
# trigger an nginx reload when c0re atomically renames a new
# version into place (#869). PathChanged fires on
# IN_CLOSE_WRITE + IN_MOVED_TO, so the atomic rename c0re
# uses (write .conf.tmp → rename) wakes the path unit.
# The reload is a no-op if the new config is identical —
# gateway_nginx::write skips the rename when content is
# unchanged, so the path unit doesn't fire at all on quiet
# ticks.
systemd.paths.hive-gateway-agents-conf = {
wantedBy = [ "nginx.service" ];
after = [ "nginx.service" ];
pathConfig = {
PathChanged = "/run/hive-state/agents.conf";
Unit = "hive-gateway-nginx-reload.service";
};
};
systemd.services.hive-gateway-nginx-reload = {
description = "Reload nginx after agents.conf change";
# Don't block any target — fires only when the path unit
# triggers it.
serviceConfig = {
Type = "oneshot";
# nginx -s reload sends SIGHUP to the master process via
# the pid file. Runs as root inside the container (pid 1
# is the nspawn init; nginx master starts as root).
ExecStart = "/run/current-system/sw/bin/nginx -s reload";
};
};
# nginx reload is triggered from the HOST side by hive-c0re
# via `systemd-run --machine=hive-gateway nginx -s reload`
# after each agents.conf write. A path unit watching the
# bind-mounted file inside the container was tried first
# (in #872) but IN_MOVED_TO from an atomic rename on the host
# does not propagate across the nspawn mount-namespace boundary,
# so the watcher never fired (#889). Host-side trigger is the
# correct approach.
services.nginx = {
enable = true;
@ -546,14 +523,14 @@ in
};
};
# Per-agent location blocks, generated at runtime by
# hive-c0re and written to /var/lib/hyperhive/agents.conf
# hive-c0re and written to /var/lib/hyperhive/gateway/agents.conf
# on the host. The bind-mount at /run/hive-state/ exposes
# that file here. nginx parses `include` at config-load
# time so a reload (triggered by the hive-gateway-nginx-
# reload path unit when agents.conf changes) picks up new
# or removed agents without a nixos-rebuild. nginx's
# longest-prefix-match rule ensures `/agent/<name>/` from
# this file beats the `/agent/` catch-all above (#869).
# time so a reload (triggered by c0re via systemd-run
# after each agents.conf write) picks up new or removed
# agents without a nixos-rebuild. nginx's longest-prefix-
# match rule ensures `/agent/<name>/` from this file beats
# the `/agent/` catch-all above.
extraConfig = ''
include /run/hive-state/agents.conf;
'';