From b7961b6e4ec5f6f49cd1ed6ecaf5e98524a8b031 Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Sun, 11 Oct 2026 15:06:00 +0200 Subject: [PATCH] feat(m3-hermes): herdr sandbox user + fleet runtime service (R2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - System-User herdr (isSystemUser, /var/lib/herdr, homeMode 750) ohne Fleet-Keys - systemd herdr-server: User=herdr, StateDirectory 0750, RuntimeDirectory 0700, herdr aus llm-agents.herdr (gepinnt über Flake), path= statt PATH-Env - moshi-hook-herdr Service mit ConditionPathExists (startet erst nach dem einmaligen imperativen moshi-hook-Install für den herdr-User) - Worker-opencode-Config auf keyless m3ta-gateway (NetBird Agent Network, Budget-Deckel liegt in der NetBird-Policy) - Genau EINE Sudo-Regel abwärts: hermes -> herdr, nur /run/current-system/sw/bin/herdr-run * (Whitelist: agent|pane|workspace|status|integration) Eval verifiziert: serviceConfig.User=herdr, home, sudo-Rule, tmpfiles, ConditionPathExists. Closure-Build vor dem Merge ausfuehren (s. PR-Beschreibung). --- hosts/m3-hermes/services/default.nix | 1 + hosts/m3-hermes/services/herdr-sandbox.nix | 152 +++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 hosts/m3-hermes/services/herdr-sandbox.nix diff --git a/hosts/m3-hermes/services/default.nix b/hosts/m3-hermes/services/default.nix index 0e92b1e..8148ead 100644 --- a/hosts/m3-hermes/services/default.nix +++ b/hosts/m3-hermes/services/default.nix @@ -1,6 +1,7 @@ { imports = [ ./hermes-agent.nix + ./herdr-sandbox.nix ./hermes-dashboard.nix ./netbird.nix ]; diff --git a/hosts/m3-hermes/services/herdr-sandbox.nix b/hosts/m3-hermes/services/herdr-sandbox.nix new file mode 100644 index 0000000..4e30bc1 --- /dev/null +++ b/hosts/m3-hermes/services/herdr-sandbox.nix @@ -0,0 +1,152 @@ +# Agent Fleet Sandbox (R2) — dedicated herdr runtime user without fleet secrets. +# +# Implements the Hub Pattern (agent-fleet-access skill, locked 2026-09-27): +# - Workers run as system user `herdr` (own home, no fleet SSH keys). +# - herdr server as systemd system service, socket in RuntimeDirectory (0700). +# - Orchestrator (hermes) descends via exactly ONE sudo wrapper. +# - Worker LLM access via the NetBird Agent Network endpoint (keyless — +# the proxy injects provider keys server-side; budget caps live in the +# NetBird policy, set UNDER plan quota). +# +# Deliberately NOT in this module (documented follow-ups): +# - moshi-hook binary for the herdr user is installed imperatively once +# (`sudo -u herdr bash -c 'curl -fsSL https://getmoshi.app/install.sh | sh'`) +# until we package moshi-hook for nixpkgs — service unit below is +# declarative, only the binary path is /var/lib/herdr/.local/bin. +# - Pairing is the user's manual phone step (moshi app → token → pair). +# - Gitea push key for the herdr user: follow-up agenix secret (needs the +# placeholder+encrypt dance on this host). +{ + config, + lib, + pkgs, + ... +}: let + herdrPkg = pkgs.llm-agents.herdr; + herdrBin = "${herdrPkg}/bin/herdr"; + + # Keyless LLM endpoint for workers (NetBird Agent Network / m3ta-gateway). + # Same endpoint the orchestrator's opencode already uses — verified working. + workerOpencodeConfig = pkgs.writeText "opencode.jsonc" (builtins.toJSON { + "$schema" = "https://opencode.ai/config.json"; + provider = { + m3ta-gateway = { + npm = "@ai-sdk/openai-compatible"; + name = "m3ta-gateway"; + options = { + baseURL = "https://downy-cardamom.p.m3ta.dev/v1"; + apiKey = "none"; + }; + models = { + claude-sonnet-5 = { + name = "Claude Sonnet 5"; + }; + claude-opus-5 = { + name = "Claude Opus 5"; + }; + }; + }; + }; + model = "m3ta-gateway/claude-sonnet-5"; + }); + + # The single sudo-descending wrapper: orchestrator may run ONLY this, + # ONLY as user herdr, and it whitelists herdr client subcommands. + herdrRun = pkgs.writeShellScriptBin "herdr-run" '' + set -euo pipefail + cmd="''${1:-}" + case "$cmd" in + agent|pane|workspace|status|integration|help|--version) + exec ${herdrBin} "$@" + ;; + *) + echo "herdr-run: subcommand '$cmd' not allowed (agent|pane|workspace|status|integration)" >&2 + exit 64 + ;; + esac + ''; +in { + # ── Sandbox user ────────────────────────────────────────────────────── + users.users.herdr = { + isSystemUser = true; + group = "herdr"; + home = "/var/lib/herdr"; + createHome = true; + homeMode = "750"; + description = "Agent fleet worker sandbox (herdr runtime, no fleet keys)"; + shell = pkgs.bashInteractive; + }; + users.groups.herdr = {}; + + # herdr binary + sudo wrapper reachable system-wide (herdr-run references + # the store path of herdr; wrapper is for interactive admin debugging) + environment.systemPackages = [herdrPkg herdrRun]; + + # ── herdr server (system service, sandbox user) ─────────────────────── + systemd.services.herdr-server = { + description = "herdr server (agent fleet runtime sandbox)"; + after = ["network.target"]; + wantedBy = ["multi-user.target"]; + serviceConfig = { + User = "herdr"; + Group = "herdr"; + StateDirectory = "herdr"; + StateDirectoryMode = "0750"; + RuntimeDirectory = "herdr"; + RuntimeDirectoryMode = "0700"; + ExecStart = "${herdrBin} server"; + Restart = "on-failure"; + RestartSec = "5"; + }; + # Idiomatisch statt PATH-Env (kollidiert sonst mit systemds Default-PATH) + path = with pkgs; [git coreutils gnused gnugrep procps]; + environment = { + # herdr legt den Socket im 0700-RuntimeDirectory ab + XDG_RUNTIME_DIR = "/run/herdr"; + }; + }; + + # ── moshi-hook daemon (sandbox user, system unit — no user-manager + # dependency, avoids the DBUS/XDG headless trap entirely) ─────────── + systemd.services.moshi-hook-herdr = { + description = "moshi-hook daemon (herdr sandbox user)"; + after = ["network-online.target"]; + wants = ["network-online.target"]; + wantedBy = ["multi-user.target"]; + serviceConfig = { + User = "herdr"; + Group = "herdr"; + ExecStart = "/var/lib/herdr/.local/bin/moshi-hook serve"; + WorkingDirectory = "/var/lib/herdr"; + Restart = "on-failure"; + RestartSec = "5"; + # App-push flows outbound (WebSocket to api.getmoshi.app); the socket + # for agent CLI events lands in the herdr home state dir. + Environment = "XDG_RUNTIME_DIR=/run/herdr"; + }; + # Binary is installed imperatively (see header) — do not start the unit + # until that one-time install happened; systemd would just restart-loop. + unitConfig.ConditionPathExists = ["/var/lib/herdr/.local/bin/moshi-hook"]; + }; + + # ── Worker environment: keyless LLM endpoint config ─────────────────── + systemd.tmpfiles.rules = [ + "d /var/lib/herdr/.config 0750 herdr herdr -" + "d /var/lib/herdr/.config/opencode 0750 herdr herdr -" + "L+ /var/lib/herdr/.config/opencode/opencode.jsonc - - - - ${workerOpencodeConfig}" + ]; + + # ── Orchestrator → sandbox: exactly ONE sudo rule, descending only ───── + security.sudo.extraRules = [ + { + users = ["hermes"]; + runAs = "herdr"; + commands = [ + { + command = "/run/current-system/sw/bin/herdr-run *"; + options = ["NOPASSWD"]; + } + ]; + } + ]; +}