From 4d1426cab0c9e17e67671e843fe9189ba75846ef Mon Sep 17 00:00:00 2001 From: oqyude Date: Thu, 15 Oct 2026 04:35:06 +0300 Subject: [PATCH] feat(home): add herdr user service + herdr.zeroq.su vhost MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per-UID design of herdr (Unix sockets, no HTTP) drove the home-manager choice over a NixOS module — mirrors the existing opencode-web pattern. - home/modules/herdr.nix: new systemd user service running herdr 0.9.3 on sapphira, --session herdr-prod, EnvironmentFile from sops opencode_server. 2G/4G cgroup caps, Restart=always (mitigates herdr#4920). - modules/server/nginx.nix: herdr.zeroq.su vhost with Authelia forward-auth (wildcard *.zeroq.su policy already covers it). Upstream 127.0.0.1:7317 is the Bun server from the devswha/herdr-web-ui plugin (970star, MIT) — vhost 502s until 'herdr plugin install devswha/herdr-web-ui' runs. - modules/server/coredns.nix: 192.168.1.20 herdr.zeroq.su A record. - home/server.nix: import the new module. --- home/modules/herdr.nix | 114 +++++++++++++++++++++++++++++++++++++ home/server.nix | 1 + modules/server/coredns.nix | 1 + modules/server/nginx.nix | 52 +++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 home/modules/herdr.nix diff --git a/home/modules/herdr.nix b/home/modules/herdr.nix new file mode 100644 index 0000000..c759fdd --- /dev/null +++ b/home/modules/herdr.nix @@ -0,0 +1,114 @@ +# Unattended herdr (https://herdr.dev) agent orchestrator running as a +# systemd USER service on sapphira. Matches the orchestrator's per-UID +# design: Unix sockets live in $XDG_CONFIG_HOME/herdr/ with mode 0600, +# owner-only. See .agent/context/research-herdr-2026-10.md for the full +# architecture assessment. +# +# Why user service, not system: +# - herdr stores Unix sockets (API + client) in $XDG_CONFIG_HOME/herdr/ +# with mode 0600 and owner-only access. A system service would run +# under a different uid, so the human user could not reach the API +# without sudo and there is no benefit to running it as a dedicated +# uid (herdr itself holds no secrets — see "Credentials" below). +# - herdr's design is per-UID: one server per user, configurable session +# namespaces via --session. A system service would fight the tool. +# - The existing opencode-web service on sapphira (home/modules/opencode.nix) +# is already a user service; this module mirrors that pattern exactly. +# +# Why --session herdr-prod: +# - herdr's default session name is fixed at $XDG_CONFIG_HOME/herdr/; +# using --session namespaced it under sessions/herdr-prod/. This lets +# a TUI `herdr` client run interactively on sapphira (e.g. for ad-hoc +# debugging) without colliding with the unattended server's sockets. +# (Background: herdr issue #4544 — default socket path is fixed, so +# parallel/automated herdr workloads land on the user's live server.) +# +# Children OpenCode instances (integration-managed, see integrations docs): +# - herdr spawns `opencode serve` per worktree on demand via the built-in +# opencode integration. Those children inherit the parent env, so +# EnvironmentFile below covers both herdr and its opencode children. +# - This deliberately bypasses the shared background `opencode serve` +# service that the existing opencode-web systemd user unit uses. Per +# opencode issue #55076 (severity:high, reproduced, ~$24 runaway), +# the shared background service resumes killed sessions without their +# original permissions or step limits. herdr-managed opencode children +# are scoped to herdr's lifetime, so a herdr restart cleanly drops them. +# +# Credentials: +# - herdr stores NO secrets. Per herdr docs/integrations/, each agent +# (OpenCode, Claude, Codex, etc.) reads its own config from its own +# XDG paths (~/.config/opencode/, ~/.claude/, ~/.codex/, ...). herdr +# only propagates the parent process env to child panes. So the +# EnvironmentFile below is the same one used by opencode-web +# (modules/users.nix:117-122), and the sops-managed OpenCode auth/ +# account JSON files (modules/users.nix:128-141) are read by the +# spawned OpenCode children on their own — no extra wiring here. +{ + config, + lib, + pkgs, + xlib, + ... +}: +{ + # Install herdr binary. nixpkgs already installs bash/fish/zsh shell + # completions via installShellCompletion in the package's postInstall + # (verified: pkgs/by-name/he/herdr/package.nix). + home.packages = [ pkgs.herdr ]; + + # systemd user service: headless `herdr server` running in the + # dedicated `herdr-prod` session namespace. + # + # Why Type=simple (not notify): upstream herdr does not implement + # sd_notify (no such call in src/platform/unix_common.rs:338-365). + # Readiness is implied by socket creation in + # $XDG_CONFIG_HOME/herdr/sessions/herdr-prod/ (mode 0600, owner oqyude). + # + # Why Restart=always (not on-failure): herdr issue #4920 — the server + # can shut down after the last client detaches on some paths. + # Restart=always guarantees the unattended daemon stays up; systemd + # de-bounces spurious exits via RestartSec. + systemd.user.services.herdr = { + Unit = { + Description = "herdr agent orchestrator (production session, sapphira)"; + Documentation = [ + "https://herdr.dev/docs/" + "https://herdr.dev/docs/socket-api/" + "https://herdr.dev/docs/session-state/" + ]; + # Spawned children may open Tailscale / LAN sockets (e.g. for the + # built-in opencode integration that needs to reach other hosts). + # Wait for routable network so they don't race NetworkManager-wait-online. + After = [ "network-online.target" ]; + Wants = [ "network-online.target" ]; + }; + Service = { + Type = "simple"; + ExecStart = "${lib.getExe pkgs.herdr} server --session herdr-prod"; + Restart = "always"; + RestartSec = "5s"; + # EnvironmentFile is decrypted by sops-nix (modules/users.nix:117-122) + # to xlib.dirs.opencode-server-env. Contains OPENCODE_SERVER_PASSWORD + # and any other env vars the spawned `opencode serve` children need. + # herdr propagates parent process env to child panes (verified: + # src/session.rs:171-187), so this single EnvironmentFile covers + # both the orchestrator and its integration-managed OpenCode + # instances. herdr itself stores NO secrets. + EnvironmentFile = xlib.dirs.opencode-server-env; + # Memory protection, same pattern as opencode-web + # (home/modules/opencode.nix:407-412). sapphira has 5.6 GiB RAM + # with ~1 GiB baseline; herdr's cgroup covers itself AND its + # spawned opencode subprocesses, so the budget is available + # headroom minus a 1.5 GiB cushion for the rest of the host. + # Sizes are conservative; bump together if a workload legitimately + # needs more. + MemoryHigh = "2G"; + MemoryMax = "4G"; + OOMScoreAdjust = -900; + OOMPolicy = "continue"; + }; + Install = { + WantedBy = [ "default.target" ]; + }; + }; +} \ No newline at end of file diff --git a/home/server.nix b/home/server.nix index 55a85ff..348bb7c 100644 --- a/home/server.nix +++ b/home/server.nix @@ -8,6 +8,7 @@ { imports = [ ./minimal.nix + ./modules/herdr.nix ./modules/opencode.nix ]; home.file = xlib.helpers.mkSymlinks config { diff --git a/modules/server/coredns.nix b/modules/server/coredns.nix index db950ce..a443397 100644 --- a/modules/server/coredns.nix +++ b/modules/server/coredns.nix @@ -16,6 +16,7 @@ 192.168.1.20 flux.zeroq.su 192.168.1.20 git.zeroq.su 192.168.1.20 glances.zeroq.su + 192.168.1.20 herdr.zeroq.su 192.168.1.20 homebox.zeroq.su 192.168.1.20 immich.zeroq.su 192.168.1.20 kuma.zeroq.su diff --git a/modules/server/nginx.nix b/modules/server/nginx.nix index 3a6a578..72d29cb 100644 --- a/modules/server/nginx.nix +++ b/modules/server/nginx.nix @@ -228,6 +228,58 @@ in }; }; }; + # herdr.zeroq.su — web UI for the herdr orchestrator (devswha/herdr-web-ui, + # MIT, 970★). herdr itself has no HTTP surface (only JSON-RPC over a + # per-UID Unix socket; see home/modules/herdr.nix for the design + # context). The webui is provided by the `devswha/herdr-web-ui` herdr + # plugin, which spawns a Bun-based HTTP+WebSocket server on + # 127.0.0.1:7317 as a herdr startup hook. Without the plugin installed + # this vhost 502s; the plugin install is `herdr plugin install + # devswha/herdr-web-ui` (one shot, then the plugin's update mechanism + # takes over; the user is expected to handle it — see + # https://herdrweb.dev/ for the install guide and Tailscale pairing). + # Same auth_request / 401→302 wiring as vtimeline/tty below — the + # wildcard rule `*.zeroq.su` in modules/server/authelia.nix already + # covers this subdomain under `one_factor`, so no policy change is + # needed. + "herdr.zeroq.su" = { + forceSSL = true; + enableACME = true; + locations = { + "/" = { + proxyPass = "http://127.0.0.1:7317"; + proxyWebsockets = true; + extraConfig = '' + auth_request /authelia; + auth_request_set $authelia_user $upstream_http_remote_user; + # Same 401→302 trick as vtimeline/tty: a bare 302 from + # Authelia surfaces to the client as a 500 ("auth request + # unexpected status"), so we rewrite the response status to + # a 302 pointing at the authelia login UI with an absolute + # `$scheme://$host$request_uri` so the post-login `rd` + # lands the user back on herdr.zeroq.su, not on + # authelia.zeroq.su/. + error_page 401 =302 https://authelia.zeroq.su/?rd=$scheme://$host$request_uri; + ''; + }; + "= /authelia" = { + extraConfig = '' + internal; + proxy_pass http://127.0.0.1:9091/api/authz/forward-auth; + proxy_set_header X-Original-URL $request_uri; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Host $host; + proxy_set_header X-Forwarded-Method $request_method; + proxy_set_header X-Forwarded-Uri $request_uri; + proxy_set_header X-Forwarded-For $remote_addr; + # Same Accept-forces-401 trick as vtimeline.zeroq.su — see + # the comment there for why Authelia's default 302 is + # harmful here. + proxy_set_header Accept "application/json"; + ''; + }; + }; + }; # Authelia login UI — same podman container on 127.0.0.1:9091 as the # forward-auth endpoint above, just exposed on a separate vhost so # Authelia has a stable absolute URL to redirect users to. Authelia