Files
hermes-guest/nix/hermes-guest.nix
T
dckc 3f9ba54477 feat: Endo-compatible host, downloads folder, Nix tools PATH
- the fake host serves a subset of @endo/agent-mcp-stdio's catalog,
  with its names, schemas and answers; evaluate takes only
  E(name).method(arg, ...), and __getMethodNames__
- demo.py --host endo: a real Endo daemon (endo-but-for-bots, llm),
  its guest served by the package's broker
- a downloads folder: the host writes, Hermes reads at ~/Downloads;
  BindReadOnlyPaths on Linux, a virtiofs share on macOS
- .#demo-tools: Hermes and the checks get their whole PATH from the store
- each run saves its MCP messages and a transcript
- the guide: tools as system calls, names as file descriptors;
  no Linux networking, but WebApps
- grants match a named URL before "download"

Generated-by: Claude Opus 5.5
2026-11-29 23:15:06 -06:00

182 lines
6.2 KiB
Nix

# 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 one directory, and whatever imports it decides what
# serves them:
#
# hostSocket the guest's methods (listMessages, request, evaluate,
# ...) as an MCP server, bound to one guest agent
# llmSocket model inference, through a proxy that holds the API key
# downloadsDir the guest's downloads folder, ~/Downloads: the host
# writes it, and Hermes only reads it
#
# The service has no network of its own (PrivateNetwork), and its root is
# empty except for its closure, those two sockets, the downloads folder,
# and its state (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.";
};
downloadsDir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/hermes-downloads";
description = ''
The guest's downloads folder, as the host sees it: the host writes files here (say, a
WebApp's body, by E(dir).write(name, blob)), and Hermes sees it, read-only, as ~/Downloads.
'';
};
model = lib.mkOption {
type = lib.types.str;
description = "The model name, as the proxy's upstream knows it.";
};
reasoningEffort = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "low";
description = "Hermes's agent.reasoning_effort, for models that take only some levels (DeepSeek V4.1: low, high, xhigh, max).";
};
};
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";
}
// lib.optionalAttrs (cfg.reasoningEffort != null) {
agent.reasoning_effort = cfg.reasoningEffort;
};
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
# read-only: Hermes can't plant anything there for the host to trip on
"${cfg.downloadsDir}:${hermes.stateDir}/Downloads"
];
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 -" ];
};
}