# 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"]; } ]; } ]; }