feat: Hermes confined to act only as an Endo guest
A NixOS module (nix/hermes-guest.nix) layers confinement on upstream's services.hermes-agent: no network (PrivateNetwork), an empty /run with two Unix sockets bound back in, and no user manager. Hermes reaches the world through host.sock, the guest's Endo methods (listMessages, request, lookup, reply, ...) as MCP, and llm.sock, an inference proxy that holds the API key. The same unit runs on a Linux host (nix/linux-host.nix) and in a vfkit microVM on macOS (nix/vfkit-vm.nix), where systemd relays the two sockets over vsock to launchd on the host (nix/darwin-host.nix). test/demo.py runs unmodified Hermes against a fake host and a scripted model, and checks what the host received. Generated-by: Claude Opus 5.5
This commit is contained in:
commit
e38d681a9e
13 files changed
+1379
No files matched your search
@@ -0,0 +1,47 @@
|
||||
# On macOS: launchd agents for the host side of the VM, with no nix-darwin.
|
||||
# The result has the plists in share/LaunchAgents; see the README.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
runner, # the VM's microvm runner (config.microvm.declaredRunner)
|
||||
hostDir,
|
||||
upstream,
|
||||
keyFile,
|
||||
}:
|
||||
let
|
||||
proxy = pkgs.writers.writePython3 "llm-proxy" { flakeIgnore = [ "E501" ]; } (
|
||||
builtins.readFile ../llm-proxy.py
|
||||
);
|
||||
plist = name: attrs: pkgs.writeText "${name}.plist" (lib.generators.toPlist { escape = true; } attrs);
|
||||
|
||||
# the inference proxy: launchd listens, and hands each connection to a
|
||||
# fresh proxy as its stdin (inetd style), as systemd's Accept=yes does
|
||||
llmProxy = plist "net.madmode.hermes-llm-proxy" {
|
||||
Label = "net.madmode.hermes-llm-proxy";
|
||||
ProgramArguments = [
|
||||
"${proxy}"
|
||||
upstream
|
||||
keyFile
|
||||
];
|
||||
inetdCompatibility.Wait = false;
|
||||
Sockets.Listener = {
|
||||
SockPathName = "${hostDir}/llm.sock";
|
||||
SockPathMode = 384; # 0600
|
||||
};
|
||||
StandardErrorPath = "${hostDir}/llm-proxy.log";
|
||||
};
|
||||
|
||||
vm = plist "net.madmode.hermes-vm" {
|
||||
Label = "net.madmode.hermes-vm";
|
||||
ProgramArguments = [ "${runner}/bin/microvm-run" ];
|
||||
WorkingDirectory = hostDir;
|
||||
KeepAlive = true;
|
||||
StandardOutPath = "${hostDir}/vm.log";
|
||||
StandardErrorPath = "${hostDir}/vm.log";
|
||||
};
|
||||
in
|
||||
pkgs.runCommand "hermes-guest-darwin-host" { } ''
|
||||
mkdir -p $out/share/LaunchAgents
|
||||
cp ${llmProxy} $out/share/LaunchAgents/net.madmode.hermes-llm-proxy.plist
|
||||
cp ${vm} $out/share/LaunchAgents/net.madmode.hermes-vm.plist
|
||||
''
|
||||
@@ -0,0 +1,141 @@
|
||||
# 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 sees no
|
||||
# other Unix sockets on the machine.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.hermes-guest;
|
||||
hermes = config.services.hermes-agent;
|
||||
socketDir = "/run/hermes-guest";
|
||||
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" ];
|
||||
serviceConfig = {
|
||||
PrivateNetwork = true;
|
||||
IPAddressDeny = "any";
|
||||
IPAddressAllow = "localhost";
|
||||
RestrictAddressFamilies = [
|
||||
"AF_UNIX"
|
||||
"AF_INET"
|
||||
"AF_INET6"
|
||||
];
|
||||
# Path-named Unix sockets ignore network namespaces: hide them all,
|
||||
# then put back the two this service may use. (/var/run is a
|
||||
# symlink to /run, so it's covered too.)
|
||||
TemporaryFileSystem = [ "/run" ];
|
||||
BindReadOnlyPaths = [
|
||||
cfg.hostSocket
|
||||
cfg.llmSocket
|
||||
];
|
||||
ProtectHome = lib.mkForce true;
|
||||
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";
|
||||
};
|
||||
};
|
||||
|
||||
# Hermes's client wants a TCP base_url. This joins Hermes's network
|
||||
# namespace and forwards its loopback port to llmSocket.
|
||||
systemd.services.hermes-llm-bridge = {
|
||||
description = "Hermes's loopback port to the inference proxy";
|
||||
unitConfig.JoinsNamespaceOf = "hermes-agent.service";
|
||||
partOf = [ "hermes-agent.service" ];
|
||||
serviceConfig = {
|
||||
User = hermes.user;
|
||||
Group = hermes.group;
|
||||
PrivateNetwork = true;
|
||||
ExecStart = lib.escapeShellArgs [
|
||||
"${pkgs.socat}/bin/socat"
|
||||
"TCP-LISTEN:${toString cfg.llmPort},bind=127.0.0.1,fork,reuseaddr"
|
||||
"UNIX-CONNECT:${cfg.llmSocket}"
|
||||
];
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
NoNewPrivileges = true;
|
||||
CapabilityBoundingSet = "";
|
||||
};
|
||||
};
|
||||
|
||||
systemd.tmpfiles.rules = [ "d ${socketDir} 0755 root root -" ];
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
# On a Linux host: Hermes's unit runs here, confined by systemd, and the
|
||||
# host's systemd serves both of its sockets.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.hermes-guest;
|
||||
in
|
||||
{
|
||||
imports = [
|
||||
./hermes-guest.nix
|
||||
./llm-proxy.nix
|
||||
];
|
||||
|
||||
options.services.hermes-guest.hostSocketUnit = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
example = "endo-guest-hermes.socket";
|
||||
description = ''
|
||||
The socket unit that serves hostSocket: the guest's host (toybox, say), bound to Hermes's
|
||||
guest agent, with SocketUser set to Hermes's user and SocketMode 0600.
|
||||
'';
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
services.hermes-llm-proxy = {
|
||||
enable = true;
|
||||
socket = cfg.llmSocket;
|
||||
socketUser = config.services.hermes-agent.user;
|
||||
};
|
||||
systemd.services.hermes-agent = {
|
||||
after = [
|
||||
"hermes-llm.socket"
|
||||
cfg.hostSocketUnit
|
||||
];
|
||||
requires = [
|
||||
"hermes-llm.socket"
|
||||
cfg.hostSocketUnit
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
# The inference proxy, socket-activated: one service instance per
|
||||
# connection on `socket`, holding the API key as a systemd credential.
|
||||
# Used on a Linux host, and (with launchd in place of systemd) on macOS.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.hermes-llm-proxy;
|
||||
proxy = pkgs.writers.writePython3 "llm-proxy" { flakeIgnore = [ "E501" ]; } (
|
||||
builtins.readFile ../llm-proxy.py
|
||||
);
|
||||
in
|
||||
{
|
||||
options.services.hermes-llm-proxy = {
|
||||
enable = lib.mkEnableOption "the inference proxy for a confined Hermes";
|
||||
socket = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Where to listen.";
|
||||
};
|
||||
socketUser = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "The one user who may connect.";
|
||||
};
|
||||
upstream = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
example = "https://openrouter.ai/api/v1";
|
||||
description = "The OpenAI-compatible base URL to forward to.";
|
||||
};
|
||||
keyFile = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "The API key, as a runtime path (not a nix path, which would copy it to the store).";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
systemd.sockets.hermes-llm = {
|
||||
wantedBy = [ "sockets.target" ];
|
||||
listenStreams = [ cfg.socket ];
|
||||
socketConfig = {
|
||||
Accept = true;
|
||||
SocketUser = cfg.socketUser;
|
||||
SocketMode = "0600";
|
||||
};
|
||||
};
|
||||
systemd.services."hermes-llm@" = {
|
||||
description = "Hermes inference proxy, one connection";
|
||||
serviceConfig = {
|
||||
ExecStart = "${proxy} ${lib.escapeShellArg cfg.upstream} %d/llm-key";
|
||||
StandardInput = "socket";
|
||||
StandardOutput = "journal";
|
||||
LoadCredential = [ "llm-key:${cfg.keyFile}" ];
|
||||
DynamicUser = true;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
PrivateDevices = true;
|
||||
NoNewPrivileges = true;
|
||||
CapabilityBoundingSet = "";
|
||||
RestrictAddressFamilies = [
|
||||
"AF_UNIX"
|
||||
"AF_INET"
|
||||
"AF_INET6"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
# On macOS: a NixOS microVM (vfkit) that runs the same confined unit.
|
||||
# Inside the VM, systemd serves Hermes's two sockets too; each connection
|
||||
# is relayed over vsock to the host (CID 2), where vfkit connects it to a
|
||||
# Unix socket in hostDir. So the host's listeners on those two paths
|
||||
# decide what Hermes can reach.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.hermes-guest;
|
||||
vm = cfg.vm;
|
||||
|
||||
# a socket unit at `path` in the VM, relaying each connection to vsock `port`
|
||||
relay = name: path: port: {
|
||||
sockets."hermes-${name}-relay" = {
|
||||
wantedBy = [ "sockets.target" ];
|
||||
listenStreams = [ path ];
|
||||
socketConfig = {
|
||||
Accept = true;
|
||||
SocketUser = config.services.hermes-agent.user;
|
||||
SocketMode = "0600";
|
||||
};
|
||||
};
|
||||
services."hermes-${name}-relay@" = {
|
||||
serviceConfig = {
|
||||
ExecStart = "${pkgs.socat}/bin/socat STDIO VSOCK-CONNECT:2:${toString port}";
|
||||
StandardInput = "socket";
|
||||
StandardOutput = "socket";
|
||||
DynamicUser = true;
|
||||
RestrictAddressFamilies = [ "AF_VSOCK" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
imports = [ ./hermes-guest.nix ];
|
||||
|
||||
options.services.hermes-guest.vm = {
|
||||
hostDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
example = "/Users/me/.local/state/hermes-guest";
|
||||
description = "The host directory with the VM's state image, and the two Unix sockets vfkit connects to.";
|
||||
};
|
||||
hostPort = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 5000;
|
||||
};
|
||||
llmPort = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 5001;
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
system.stateVersion = "25.11";
|
||||
networking.hostName = "hermes-guest";
|
||||
|
||||
microvm = {
|
||||
hypervisor = "vfkit";
|
||||
vcpu = 2;
|
||||
mem = 4096;
|
||||
# no network interface at all
|
||||
interfaces = [ ];
|
||||
# the host's /nix/store, read-only, as on a Linux host (where
|
||||
# ProtectSystem=strict leaves the store readable): no store image to
|
||||
# rebuild on each change. The store holds no secrets, as long as
|
||||
# none is ever written as a nix path.
|
||||
shares = [
|
||||
{
|
||||
tag = "ro-store";
|
||||
source = "/nix/store";
|
||||
mountPoint = "/nix/.ro-store";
|
||||
proto = "virtiofs";
|
||||
}
|
||||
];
|
||||
volumes = [
|
||||
{
|
||||
image = "${vm.hostDir}/hermes-state.img";
|
||||
mountPoint = "/var/lib/hermes";
|
||||
size = 8192;
|
||||
}
|
||||
];
|
||||
# microvm.vsock.cid is not implemented for vfkit, so the devices go
|
||||
# straight to vfkit: a guest connect to (2, port) makes vfkit connect
|
||||
# to socketURL on the host.
|
||||
vfkit.extraArgs = [
|
||||
"--device"
|
||||
"virtio-vsock,port=${toString vm.hostPort},socketURL=${vm.hostDir}/host.sock"
|
||||
"--device"
|
||||
"virtio-vsock,port=${toString vm.llmPort},socketURL=${vm.hostDir}/llm.sock"
|
||||
];
|
||||
};
|
||||
|
||||
systemd = lib.mkMerge [
|
||||
(relay "host" cfg.hostSocket vm.hostPort)
|
||||
(relay "llm" cfg.llmSocket vm.llmPort)
|
||||
{
|
||||
services.hermes-agent = {
|
||||
after = [
|
||||
"hermes-host-relay.socket"
|
||||
"hermes-llm-relay.socket"
|
||||
];
|
||||
requires = [
|
||||
"hermes-host-relay.socket"
|
||||
"hermes-llm-relay.socket"
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
|
||||
# the console is the operator's way in, for debugging
|
||||
services.getty.autologinUser = "root";
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user