# Hermes, confined to act only as one Endo guest (of toybox, or of an Endo daemon). # # This module is what ports: the same unit runs on a Linux host and inside # the macOS VM (./vfkit-vm.nix). It reaches the world through exactly two # Unix sockets, and whatever imports it decides what serves them: # # hostSocket the guest's methods (listMessages, request, resolve, # lookup, ...) as an MCP server, bound to one guest agent # llmSocket model inference, through a proxy that holds the API key # # The service has no network of its own (PrivateNetwork), and its root # is empty except for its closure and those two sockets (confinement). { config, lib, pkgs, ... }: let cfg = config.services.hermes-guest; hermes = config.services.hermes-agent; socketDir = "/run/hermes-guest"; # The sandbox every process running as Hermes's user gets, beside # `confinement` (below): an empty root, plus the unit's closure and the # binds each unit lists. See CONTRIBUTING.md#confinement for why it # confines. confine = extra: { enable = true; # the closure of everything the unit mentions, so PATH's packages too: # Hermes's shell tool runs bash, coreutils, git fullUnit = true; packages = extra; }; sandbox = { PrivateNetwork = true; IPAddressDeny = "any"; IPAddressAllow = "localhost"; RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ]; ProtectProc = "invisible"; ProcSubset = "pid"; PrivateDevices = true; PrivateIPC = true; ProtectKernelTunables = true; ProtectKernelModules = true; ProtectKernelLogs = true; ProtectControlGroups = true; ProtectClock = true; ProtectHostname = true; RestrictNamespaces = true; RestrictSUIDSGID = true; LockPersonality = true; CapabilityBoundingSet = ""; SystemCallArchitectures = "native"; }; in { options.services.hermes-guest = { enable = lib.mkEnableOption "Hermes as a confined Endo guest"; hostSocket = lib.mkOption { type = lib.types.str; default = "${socketDir}/host.sock"; description = "The Unix socket where the guest's MCP methods are served."; }; llmSocket = lib.mkOption { type = lib.types.str; default = "${socketDir}/llm.sock"; description = "The Unix socket of the inference proxy, an OpenAI-compatible endpoint."; }; llmPort = lib.mkOption { type = lib.types.port; default = 8080; description = "The loopback port, inside Hermes's own network namespace, that forwards to llmSocket."; }; model = lib.mkOption { type = lib.types.str; description = "The model name, as the proxy's upstream knows it."; }; }; config = lib.mkIf cfg.enable { services.hermes-agent = { enable = true; addToSystemPackages = false; settings = { model = { provider = "custom"; default = cfg.model; base_url = "http://127.0.0.1:${toString cfg.llmPort}/v1"; # the proxy adds the real key; Hermes never holds it api_key = "held-by-proxy"; }; terminal.backend = "local"; # the guest's methods are its whole world: offer them to the model # directly, not behind tool_search/tool_call tools.tool_search.enabled = "off"; }; mcpServers.host = { command = "${pkgs.socat}/bin/socat"; args = [ "STDIO" "UNIX-CONNECT:${cfg.hostSocket}" ]; }; }; # Upstream lingers the hermes uid so cron jobs can `systemd-run --user`. # A user manager starts units outside this sandbox, so: no linger, and # no /run/user (below). Cron jobs then fail closed. users.users.${hermes.user}.linger = lib.mkForce false; systemd.services.hermes-agent = { after = [ "hermes-llm-bridge.service" ]; requires = [ "hermes-llm-bridge.service" ]; # socat: config.yaml's MCP command, which the unit itself doesn't name confinement = confine [ pkgs.socat ]; serviceConfig = sandbox // { BindReadOnlyPaths = [ cfg.hostSocket cfg.llmSocket ]; BindPaths = [ hermes.stateDir ]; ProtectHome = lib.mkForce true; }; }; # Hermes's client wants a TCP base_url. This joins Hermes's network # namespace and forwards its loopback port to llmSocket. It runs as # Hermes's user, so it gets the same sandbox: taking it over (ptrace, # same uid) must gain nothing. systemd.services.hermes-llm-bridge = { description = "Hermes's loopback port to the inference proxy"; unitConfig.JoinsNamespaceOf = "hermes-agent.service"; partOf = [ "hermes-agent.service" ]; confinement = confine [ ]; serviceConfig = sandbox // { User = hermes.user; Group = hermes.group; ExecStart = lib.escapeShellArgs [ "${pkgs.socat}/bin/socat" "TCP-LISTEN:${toString cfg.llmPort},bind=127.0.0.1,fork,reuseaddr" "UNIX-CONNECT:${cfg.llmSocket}" ]; BindReadOnlyPaths = [ cfg.llmSocket ]; ProtectSystem = "strict"; ProtectHome = true; PrivateTmp = true; NoNewPrivileges = true; }; }; systemd.tmpfiles.rules = [ "d ${socketDir} 0755 root root -" ]; }; }