split bots
This commit is contained in:
1 parent
9217e61675
commit
bfdcd45746
20 files changed
+686
-152
No files matched your search
@@ -42,3 +42,6 @@ docs
|
|||||||
.claude
|
.claude
|
||||||
.mnt
|
.mnt
|
||||||
.pi
|
.pi
|
||||||
|
|
||||||
|
# Zed local settings (machine-local MCP tokens live here)
|
||||||
|
.zed/settings.local.json
|
||||||
+34
-25
@@ -1,29 +1,38 @@
|
|||||||
{
|
{
|
||||||
"show_completions_on_input": true,
|
"show_completions_on_input": true,
|
||||||
"languages": {
|
"context_servers": {
|
||||||
"QML": {
|
"linux-tracker": {
|
||||||
"colorize_brackets": true,
|
"url": "http://fwdesk.lab.internal:1984/mcp"
|
||||||
"inlay_hints": {
|
}
|
||||||
"show_background": true,
|
},
|
||||||
"enabled": true
|
"languages": {
|
||||||
},
|
"QML": {
|
||||||
"show_whitespaces": "all",
|
"colorize_brackets": true,
|
||||||
"show_completions_on_input": true,
|
"inlay_hints": {
|
||||||
"always_treat_brackets_as_autoclosed": true,
|
"show_background": true,
|
||||||
"format_on_save": "on",
|
"enabled": true
|
||||||
"language_servers": ["qmljs"]
|
},
|
||||||
}
|
"show_whitespaces": "all",
|
||||||
},
|
"show_completions_on_input": true,
|
||||||
"lsp": {
|
"always_treat_brackets_as_autoclosed": true,
|
||||||
"qmljs": {
|
"format_on_save": "on",
|
||||||
|
"language_servers": [
|
||||||
|
"qmljs"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"lsp": {
|
||||||
|
"qmljs": {
|
||||||
"enable_lsp_tasks": true,
|
"enable_lsp_tasks": true,
|
||||||
"binary": {
|
"binary": {
|
||||||
"path": "qmlls",
|
"path": "qmlls",
|
||||||
"arguments": ["-E"]
|
"arguments": [
|
||||||
},
|
"-E"
|
||||||
"initialization_options": {
|
]
|
||||||
"buildDir": "."
|
},
|
||||||
}
|
"initialization_options": {
|
||||||
}
|
"buildDir": "."
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Generated
+4
-4
@@ -1088,11 +1088,11 @@
|
|||||||
"treefmt-nix": "treefmt-nix_3"
|
"treefmt-nix": "treefmt-nix_3"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1791570912,
|
"lastModified": 1793031979,
|
||||||
"narHash": "sha256-qWpBgsVYqmK3vKtoUm7/w1B2mOwHkridUyc1ESAooVg=",
|
"narHash": "sha256-PKMdT/idOFtPwj03PzWzIO4prgPCtMsG1ig1FUlCeTM=",
|
||||||
"ref": "refs/heads/main",
|
"ref": "refs/heads/main",
|
||||||
"rev": "30427e3975637593c995d357815b99aecb512cfa",
|
"rev": "ed558a196fcbc65c1af82fb0f54fdd6773c76b66",
|
||||||
"revCount": 43,
|
"revCount": 47,
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "ssh://gitea@gitea.app.internal:22/ari/hermenix.git"
|
"url": "ssh://gitea@gitea.app.internal:22/ari/hermenix.git"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -181,6 +181,8 @@
|
|||||||
linux-tracker-peer-bigbox2 = ./secrets/linux-tracker-peer-bigbox2.age;
|
linux-tracker-peer-bigbox2 = ./secrets/linux-tracker-peer-bigbox2.age;
|
||||||
linux-tracker-peer-terra1 = ./secrets/linux-tracker-peer-terra1.age;
|
linux-tracker-peer-terra1 = ./secrets/linux-tracker-peer-terra1.age;
|
||||||
scrypted-envs = ./secrets/scrypted-envs.age;
|
scrypted-envs = ./secrets/scrypted-envs.age;
|
||||||
|
robots-shared = ./secrets/robots-shared.age;
|
||||||
|
robots-sibli = ./secrets/robots-sibli.age;
|
||||||
};
|
};
|
||||||
inherit
|
inherit
|
||||||
hyprlauncher
|
hyprlauncher
|
||||||
|
|||||||
@@ -2,8 +2,5 @@
|
|||||||
{
|
{
|
||||||
services.tailscale.enable = true;
|
services.tailscale.enable = true;
|
||||||
|
|
||||||
systemd.network.wait-online.ignoredInterfaces = [
|
systemd.network.wait-online.enable = false;
|
||||||
"hermes-br"
|
|
||||||
"vm-hermes"
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
{ config, ... }:
|
{ config, lib, skyg-secrets, ... }:
|
||||||
let
|
let
|
||||||
apps = config.skyg.internalNetworkingMap.apps;
|
apps = config.skyg.internalNetworkingMap.apps;
|
||||||
|
robotSecrets = [ "robots-shared" "robots-sibli" ];
|
||||||
|
secret = name: config.age.secrets.${name}.path;
|
||||||
ports = {
|
ports = {
|
||||||
ollama = 11434;
|
ollama = 11434;
|
||||||
ds4 = 8000;
|
ds4 = 8000;
|
||||||
@@ -8,6 +10,12 @@ let
|
|||||||
};
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
age.secrets = lib.genAttrs robotSecrets (name: {
|
||||||
|
file = skyg-secrets.${name};
|
||||||
|
owner = "root";
|
||||||
|
mode = "0400";
|
||||||
|
});
|
||||||
|
|
||||||
skyg.nixos.server.robotnix = {
|
skyg.nixos.server.robotnix = {
|
||||||
enable = true;
|
enable = true;
|
||||||
externalInterface = "enp191s0";
|
externalInterface = "enp191s0";
|
||||||
@@ -17,23 +25,40 @@ in
|
|||||||
internet = "all";
|
internet = "all";
|
||||||
hostAccess = [
|
hostAccess = [
|
||||||
{ port = ports.ollama; }
|
{ port = ports.ollama; }
|
||||||
{ port = ports.tracker; }
|
|
||||||
];
|
];
|
||||||
lanAccess = [ ];
|
lanAccess = [ ];
|
||||||
};
|
};
|
||||||
|
|
||||||
robots.genix = {
|
# A Hermes agent in every robot.
|
||||||
id = 1;
|
hermes = {
|
||||||
address = apps.genix.ip;
|
enable = true;
|
||||||
|
environmentFiles = [ (secret "robots-shared") ];
|
||||||
|
};
|
||||||
|
|
||||||
|
robots.sibli = {
|
||||||
|
id = 2;
|
||||||
|
address = apps.sibli.ip;
|
||||||
cpus = { min = 1; max = 4; };
|
cpus = { min = 1; max = 4; };
|
||||||
memory = { min = "8G"; max = "16G"; };
|
memory = { min = "2G"; max = "16G"; };
|
||||||
disk = { rootSize = "100G"; homeSize = "60G"; };
|
disk = { rootSize = "100G"; homeSize = "60G"; };
|
||||||
seed.configuration = ./robotnix/genix.nix;
|
seed.configuration = ./robotnix/sibli.nix;
|
||||||
access = { };
|
hermes = {
|
||||||
git = {
|
name = "Siblix";
|
||||||
enable = true;
|
environmentFiles = [ (secret "robots-sibli") ];
|
||||||
remoteUrl = "gitea@gitea.app.internal:Hermenix/genix.git";
|
settings.model = {
|
||||||
|
provider = "anthropic";
|
||||||
|
default = "claude-haiku-5-5";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
allowDocker = true;
|
||||||
|
allowTun = true;
|
||||||
|
ephemeral = true;
|
||||||
|
persist = [
|
||||||
|
"/var/lib/tailscale"
|
||||||
|
"/var/lib/docker"
|
||||||
|
];
|
||||||
|
access = { };
|
||||||
|
git.enable = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
# genix's starting NixOS config (a robotnix seed, not a host module).
|
|
||||||
#
|
|
||||||
# The host copies this into the robot's /etc/nixos on first install and on
|
|
||||||
# `robotnix reseed`. After that the robot owns it. Inside the robot:
|
|
||||||
# sudo nixos-rebuild switch --flake /etc/nixos
|
|
||||||
# Host-managed basics (network, ssh, user) live in /etc/nixos/robotnix.nix.
|
|
||||||
{ pkgs, ... }:
|
|
||||||
{
|
|
||||||
# The user has no password; ssh keys only.
|
|
||||||
security.sudo.wheelNeedsPassword = false;
|
|
||||||
|
|
||||||
environment.systemPackages = with pkgs; [
|
|
||||||
git
|
|
||||||
vim
|
|
||||||
curl
|
|
||||||
htop
|
|
||||||
claude-code
|
|
||||||
goose-cli
|
|
||||||
pi-coding-agent
|
|
||||||
devenv
|
|
||||||
];
|
|
||||||
nixpkgs.config.allowUnfree = true;
|
|
||||||
nix.gc = {
|
|
||||||
automatic = true;
|
|
||||||
dates = "weekly";
|
|
||||||
options = "--delete-older-than 14d";
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# sibli is ephemeral: root resets to this config on every start, so change
|
||||||
|
# things here (then rebuild the host), not inside the robot.
|
||||||
|
{ pkgs, ... }:
|
||||||
|
{
|
||||||
|
# The user has no password; ssh keys only.
|
||||||
|
security.sudo.wheelNeedsPassword = false;
|
||||||
|
|
||||||
|
environment.systemPackages = with pkgs; [
|
||||||
|
git
|
||||||
|
vim
|
||||||
|
curl
|
||||||
|
htop
|
||||||
|
claude-code
|
||||||
|
devenv
|
||||||
|
zellij
|
||||||
|
];
|
||||||
|
nixpkgs.config.allowUnfree = true;
|
||||||
|
|
||||||
|
# Docker + compose. Needs allowDocker = true on the host.
|
||||||
|
virtualisation.docker.enable = true;
|
||||||
|
users.users.ari.extraGroups = [ "docker" ];
|
||||||
|
|
||||||
|
# Own tailnet machine. Needs allowTun = true on the host.
|
||||||
|
# Log in once: sudo tailscale up (state is kept via persist on the host)
|
||||||
|
services.tailscale.enable = true;
|
||||||
|
services.tailscale.openFirewall = true;
|
||||||
|
networking.firewall.trustedInterfaces = [ "tailscale0" ];
|
||||||
|
nix.gc = {
|
||||||
|
automatic = true;
|
||||||
|
dates = "weekly";
|
||||||
|
options = "--delete-older-than 14d";
|
||||||
|
};
|
||||||
|
}
|
||||||
+1
-1
@@ -111,8 +111,8 @@ in
|
|||||||
public-gitea = { ip = "10.0.101.9"; };
|
public-gitea = { ip = "10.0.101.9"; };
|
||||||
immich = { ip = "10.0.101.10"; };
|
immich = { ip = "10.0.101.10"; };
|
||||||
immich-ml = { ip = "10.0.101.11"; };
|
immich-ml = { ip = "10.0.101.11"; };
|
||||||
genix = { ip = "10.0.101.12"; };
|
|
||||||
gilix = { ip = "10.0.101.13"; };
|
gilix = { ip = "10.0.101.13"; };
|
||||||
|
sibli = { ip = "10.0.101.14"; };
|
||||||
jellyfin = { ip = "10.0.101.5"; };
|
jellyfin = { ip = "10.0.101.5"; };
|
||||||
nvr = { ip = "10.0.101.6"; };
|
nvr = { ip = "10.0.101.6"; };
|
||||||
portainer = { ip = "10.0.101.241"; };
|
portainer = { ip = "10.0.101.241"; };
|
||||||
|
|||||||
@@ -56,6 +56,14 @@ One routed virtual switch:
|
|||||||
does the per-robot filtering before `nixos-fw` (priority filter - 10). IPv6
|
does the per-robot filtering before `nixos-fw` (priority filter - 10). IPv6
|
||||||
from robots is dropped (`host0` is IPv4-only). Inbound from the LAN to a
|
from robots is dropped (`host0` is IPv4-only). Inbound from the LAN to a
|
||||||
robot is open on every port — make your own decisions inside the robot.
|
robot is open on every port — make your own decisions inside the robot.
|
||||||
|
- Guards in the same table, so other modules' NAT can't reroute robot
|
||||||
|
traffic (hermenix once DNAT'd any-destination traffic on some ports into
|
||||||
|
its VM):
|
||||||
|
- a connection meant for a robot IP that leaves anywhere but the switch,
|
||||||
|
or lands on the host itself, is dropped (`ct original ip daddr`; replies
|
||||||
|
are untouched via `ct direction original`);
|
||||||
|
- a robot's own new connection that was DNAT'd is dropped. The robot DNS
|
||||||
|
redirect still works: it goes to the host (input), not forward.
|
||||||
- Per-robot dnsmasq on the host (port 5300+id) is the robot's only resolver:
|
- Per-robot dnsmasq on the host (port 5300+id) is the robot's only resolver:
|
||||||
all port 53 is DNAT'd to it, DoT is dropped. The robot cannot change its
|
all port 53 is DNAT'd to it, DoT is dropped. The robot cannot change its
|
||||||
resolv.conf meaningfully; it cannot touch the host's nftables/networkd.
|
resolv.conf meaningfully; it cannot touch the host's nftables/networkd.
|
||||||
@@ -88,6 +96,86 @@ if origin exists) runs on `git.interval` (`daily` default). The robot needs the
|
|||||||
remote reachable (e.g. through `lanAccess`) and its own credentials if the
|
remote reachable (e.g. through `lanAccess`) and its own credentials if the
|
||||||
remote is private.
|
remote is private.
|
||||||
|
|
||||||
|
## Ephemeral robots (opt-in)
|
||||||
|
|
||||||
|
`ephemeral = true` resets root to a clean copy of the seed on every start
|
||||||
|
(host boot, `robotnix restart`, or a reboot inside). Only `/home` survives.
|
||||||
|
|
||||||
|
- The seed step saves the fresh root as a read-only subvolume
|
||||||
|
`<base>/pristine` before the robot first boots, plus `pristine.id` (hash of
|
||||||
|
the seed system and the files it installs).
|
||||||
|
- Before each start, `robotnix-<r>-reset.service` unmounts root, swaps
|
||||||
|
`<base>/root` for a snapshot of `pristine`, and remounts (about a second).
|
||||||
|
It is its own oneshot (no `RemainAfterExit`, so every robot start re-runs
|
||||||
|
it) because the robot unit's `DevicePolicy=closed` blocks the loop devices
|
||||||
|
the mount needs.
|
||||||
|
- When the seed changes, the storage step sees a different id on the next
|
||||||
|
host switch, wipes root, and the seed step reinstalls (a few minutes).
|
||||||
|
- `persist = [ "/var/lib/..." ]` keeps folders: stored in `/home/.persist`
|
||||||
|
inside the home image and bind-mounted at their usual path. SSH host keys
|
||||||
|
always go to `/home/.persist/ssh`, so they don't change each boot.
|
||||||
|
- The robot still has its own writable store, so `nix`/`devenv` work until
|
||||||
|
the next restart. Changes go through `seed.configuration` on the host.
|
||||||
|
- Snapshots and `restore` cover home only; `reseed` drops `pristine` too.
|
||||||
|
- git-sync can't work (fresh repo each start): eval asserts it is off.
|
||||||
|
|
||||||
|
## Hermes in every robot (opt-in)
|
||||||
|
|
||||||
|
`hermes.enable` (shared) runs one Hermes agent per robot with the upstream
|
||||||
|
`services.hermes-agent` module; `robots.<r>.hermes.enable` can opt out.
|
||||||
|
|
||||||
|
- The robot's flake gets a `hermes-agent` input pinned to the host's locked
|
||||||
|
rev, and the host's seed build includes the same module.
|
||||||
|
- Host-managed: `hermes.settings` (shared + robot, deep-merged) goes to
|
||||||
|
config.yaml; `hermes.name` writes SOUL.md ("You are <name>, a Hermes-based
|
||||||
|
agent ...") on every boot; null keeps the stock identity.
|
||||||
|
- Secrets: `hermes.environmentFiles` (shared + robot, KEY=value, e.g. agenix)
|
||||||
|
are passed with `--load-credential` and loaded by the `hermes-agent` unit
|
||||||
|
from `/run/credentials/@system/robotnix-hermes-<n>` (later files win). No
|
||||||
|
file ownership games with the user namespace. Restart the robot after a
|
||||||
|
secret changes.
|
||||||
|
- The `hermes` user/group have a fixed id (870) so kept files stay owned
|
||||||
|
right across reinstalls; the login user is in the `hermes` group and has
|
||||||
|
the `hermes` CLI.
|
||||||
|
- Ephemeral robots keep only `cron/`, `memories/` and `skills/` of
|
||||||
|
`/var/lib/hermes/.hermes` (added to `persist` automatically). Sessions,
|
||||||
|
logs, OAuth logins and pairings reset each start: use API keys and
|
||||||
|
`*_ALLOWED_USERS` env vars instead.
|
||||||
|
- Gateway API (`hermes.gateway`, on by default, port 8642): the host sets
|
||||||
|
`services.hermes-agent.environment` (`API_SERVER_ENABLED`, `_HOST=0.0.0.0`,
|
||||||
|
`_PORT`, `_MODEL_NAME` = `hermes.name` or the robot name). These go to
|
||||||
|
`$HERMES_HOME/.env`, which beats the same names in the secrets. Each
|
||||||
|
robot's secrets need `API_SERVER_KEY`, or the API server won't start.
|
||||||
|
- Dashboard (`hermes.dashboard`, on by default, port 80):
|
||||||
|
`backend.mode = "dashboard"` on 0.0.0.0, run as `hermes-backend.service`
|
||||||
|
with the same secret files, plus `CAP_NET_BIND_SERVICE` when the port is
|
||||||
|
below 1024. A non-loopback dashboard needs a login in the secrets:
|
||||||
|
`HERMES_DASHBOARD_BASIC_AUTH_USERNAME`, `..._PASSWORD_HASH` (or
|
||||||
|
`..._PASSWORD`) and `..._SECRET` (keeps sessions across restarts). Without
|
||||||
|
one it exits and systemd keeps restarting it; the gateway keeps working.
|
||||||
|
Port 80 may clash with the robot's own compose stacks: change the port.
|
||||||
|
- Both: top-level `enable/port/openFirewall`, robots override any of them
|
||||||
|
(null = follow). `openFirewall` opens the TCP port in the robot's own
|
||||||
|
firewall. Eval asserts the two don't share a port.
|
||||||
|
- Existing robots only get Hermes (and gateway/dashboard changes) after
|
||||||
|
`robotnix reseed` (flake.nix and robotnix.nix are written at install).
|
||||||
|
Ephemeral robots pick changes up on the next host switch.
|
||||||
|
|
||||||
|
## Docker and Tailscale (opt-in, host side only)
|
||||||
|
|
||||||
|
These options only change what the host allows. Turn the service on in the
|
||||||
|
robot's own config (e.g. its `seed.configuration`).
|
||||||
|
|
||||||
|
- `allowDocker`: nspawn gets `--system-call-filter=add_key keyctl bpf`
|
||||||
|
(blocked by default, Docker needs them). Docker runs inside the robot's user
|
||||||
|
namespace, so images using UIDs above 65535 won't run.
|
||||||
|
- `allowTun`: `/dev/net/tun` is bound in and allowed on the unit
|
||||||
|
(`DevicePolicy=closed` blocks it otherwise). Used for tailscale: the robot
|
||||||
|
is its own tailnet machine, log in once inside with `sudo tailscale up`.
|
||||||
|
Direct LAN paths to other tailnet peers hit the private-range drop, so it
|
||||||
|
uses public endpoints or DERP.
|
||||||
|
- Both apply on the next robot restart; no reseed needed.
|
||||||
|
|
||||||
## The `robotnix` CLI
|
## The `robotnix` CLI
|
||||||
|
|
||||||
Nushell script (`pkgs.writers.writeNuBin`), part of this module:
|
Nushell script (`pkgs.writers.writeNuBin`), part of this module:
|
||||||
|
|||||||
@@ -95,6 +95,74 @@ let
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Hermes options used by both the shared and the per-robot block.
|
||||||
|
hermesSettings = mkOption {
|
||||||
|
type = types.attrsOf types.anything;
|
||||||
|
default = { };
|
||||||
|
example = {
|
||||||
|
model.provider = "openrouter";
|
||||||
|
};
|
||||||
|
description = "Hermes config.yaml settings. Shared and robot settings are deep-merged (robot wins).";
|
||||||
|
};
|
||||||
|
hermesEnvFiles = mkOption {
|
||||||
|
type = types.listOf types.str;
|
||||||
|
default = [ ];
|
||||||
|
example = [ "/run/agenix/robots-shared" ];
|
||||||
|
description = ''
|
||||||
|
Host paths of KEY=value secret files (e.g. agenix). Passed to the robot
|
||||||
|
as container credentials and loaded into Hermes's environment. Read at
|
||||||
|
robot start: restart the robot after changing one.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
# Gateway API / dashboard options. The top level sets the values; each
|
||||||
|
# robot can override one (null = follow the top level).
|
||||||
|
hermesService =
|
||||||
|
{ what
|
||||||
|
, port
|
||||||
|
, robot
|
||||||
|
, enableText
|
||||||
|
,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
opt =
|
||||||
|
type: default: description:
|
||||||
|
mkOption {
|
||||||
|
type = if robot then types.nullOr type else type;
|
||||||
|
default = if robot then null else default;
|
||||||
|
description = if robot then "${description} null: follow hermes.${what}." else description;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
enable = opt types.bool true enableText;
|
||||||
|
port = opt types.port port "Port on the robot's address.";
|
||||||
|
openFirewall = opt types.bool true "Open the port in the robot's firewall.";
|
||||||
|
};
|
||||||
|
hermesGateway =
|
||||||
|
robot:
|
||||||
|
hermesService {
|
||||||
|
inherit robot;
|
||||||
|
what = "gateway";
|
||||||
|
port = 8642;
|
||||||
|
enableText = ''
|
||||||
|
Turn on the gateway's OpenAI-compatible API on all of the robot's
|
||||||
|
addresses. The robot's secrets need API_SERVER_KEY, or it won't start.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
hermesDashboard =
|
||||||
|
robot:
|
||||||
|
hermesService {
|
||||||
|
inherit robot;
|
||||||
|
what = "dashboard";
|
||||||
|
port = 80;
|
||||||
|
enableText = ''
|
||||||
|
Run the Hermes dashboard on all of the robot's addresses. The robot's
|
||||||
|
secrets need a login (HERMES_DASHBOARD_BASIC_AUTH_USERNAME,
|
||||||
|
HERMES_DASHBOARD_BASIC_AUTH_PASSWORD_HASH and
|
||||||
|
HERMES_DASHBOARD_BASIC_AUTH_SECRET), or the dashboard exits.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
robot = types.submodule (
|
robot = types.submodule (
|
||||||
{ name, ... }:
|
{ name, ... }:
|
||||||
{
|
{
|
||||||
@@ -165,6 +233,61 @@ let
|
|||||||
description = "/home image. Grows on restart if raised.";
|
description = "/home image. Grows on restart if raised.";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
ephemeral = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = ''
|
||||||
|
Reset root to a clean copy of the seed on every start. Only /home
|
||||||
|
(and `persist`) survives. Changes go through seed.configuration on
|
||||||
|
the host. Snapshots then cover /home only.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
persist = mkOption {
|
||||||
|
type = types.listOf (types.strMatching "/[^:[:space:]]+");
|
||||||
|
default = [ ];
|
||||||
|
example = [ "/var/lib/tailscale" ];
|
||||||
|
description = ''
|
||||||
|
Folders kept across resets. Stored in /home/.persist inside the
|
||||||
|
home image and mounted at their usual path.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
hermes = {
|
||||||
|
enable = mkOption {
|
||||||
|
type = types.nullOr types.bool;
|
||||||
|
default = null;
|
||||||
|
description = "Run Hermes in this robot. null: follow hermes.enable.";
|
||||||
|
};
|
||||||
|
name = mkOption {
|
||||||
|
type = types.nullOr types.str;
|
||||||
|
default = null;
|
||||||
|
example = "Siblibot";
|
||||||
|
description = ''
|
||||||
|
Name the agent gives itself ("I am Siblibot, a Hermes-based
|
||||||
|
agent"). Written to SOUL.md by the host on every boot. null keeps
|
||||||
|
the stock Hermes identity.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
settings = hermesSettings;
|
||||||
|
environmentFiles = hermesEnvFiles;
|
||||||
|
gateway = hermesGateway true;
|
||||||
|
dashboard = hermesDashboard true;
|
||||||
|
};
|
||||||
|
allowDocker = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = ''
|
||||||
|
Allow the syscalls Docker needs (add_key, keyctl, bpf). Host side
|
||||||
|
only: enable Docker in the robot's own config.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
allowTun = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = ''
|
||||||
|
Pass /dev/net/tun into the robot (e.g. for tailscale). Host side
|
||||||
|
only: enable tailscale in the robot's own config.
|
||||||
|
'';
|
||||||
|
};
|
||||||
access = mkOption {
|
access = mkOption {
|
||||||
type = tier;
|
type = tier;
|
||||||
default = { };
|
default = { };
|
||||||
@@ -195,7 +318,7 @@ let
|
|||||||
remoteUrl = mkOption {
|
remoteUrl = mkOption {
|
||||||
type = types.nullOr types.str;
|
type = types.nullOr types.str;
|
||||||
default = null;
|
default = null;
|
||||||
example = "git@github.com:ari/genix.git";
|
example = "git@github.com:ari/myrobot.git";
|
||||||
description = ''
|
description = ''
|
||||||
Origin for the flake repo, set on first install. null = no remote:
|
Origin for the flake repo, set on first install. null = no remote:
|
||||||
the timer commits locally but never pushes.
|
the timer commits locally but never pushes.
|
||||||
@@ -292,6 +415,14 @@ in
|
|||||||
description = "Tier applied to every robot.";
|
description = "Tier applied to every robot.";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
hermes = {
|
||||||
|
enable = mkEnableOption "a Hermes agent in every robot (robots can opt out)";
|
||||||
|
settings = hermesSettings;
|
||||||
|
environmentFiles = hermesEnvFiles;
|
||||||
|
gateway = hermesGateway false;
|
||||||
|
dashboard = hermesDashboard false;
|
||||||
|
};
|
||||||
|
|
||||||
robots = mkOption {
|
robots = mkOption {
|
||||||
type = types.attrsOf robot;
|
type = types.attrsOf robot;
|
||||||
default = { };
|
default = { };
|
||||||
@@ -343,6 +474,23 @@ in
|
|||||||
assertion = both "allowedCidrs" "blockedCidrs" == [ ];
|
assertion = both "allowedCidrs" "blockedCidrs" == [ ];
|
||||||
message = "robotnix: ${r.name} both allows and blocks: ${toString (both "allowedCidrs" "blockedCidrs")}.";
|
message = "robotnix: ${r.name} both allows and blocks: ${toString (both "allowedCidrs" "blockedCidrs")}.";
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
assertion = !(r.ephemeral && r.git.enable);
|
||||||
|
message = "robotnix: ${r.name} is ephemeral, so git-sync can't work (fresh repo every start). Set git.enable = false.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = lib.all (p: !(lib.hasPrefix "/home" p || lib.hasPrefix "/nix" p || p == "/")) r.persist;
|
||||||
|
message = "robotnix: ${r.name}'s persist folders can't be /, or under /home or /nix.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion =
|
||||||
|
let
|
||||||
|
g = r.hermes.gateway;
|
||||||
|
d = r.hermes.dashboard;
|
||||||
|
in
|
||||||
|
!(r.hermes.enable && g.enable && d.enable && g.port == d.port);
|
||||||
|
message = "robotnix: ${r.name}'s Hermes gateway and dashboard can't share port ${toString r.hermes.gateway.port}.";
|
||||||
|
}
|
||||||
{
|
{
|
||||||
assertion = r.cpus.min <= r.cpus.max;
|
assertion = r.cpus.min <= r.cpus.max;
|
||||||
message = "robotnix: ${r.name} cpus.min must be <= cpus.max.";
|
message = "robotnix: ${r.name} cpus.min must be <= cpus.max.";
|
||||||
|
|||||||
@@ -48,30 +48,68 @@ let
|
|||||||
hostAccess = both (t: t.hostAccess);
|
hostAccess = both (t: t.hostAccess);
|
||||||
};
|
};
|
||||||
|
|
||||||
mkRobot = name: r: {
|
# Hermes's home inside a robot (the upstream module's default).
|
||||||
inherit name;
|
hermesHome = "/var/lib/hermes/.hermes";
|
||||||
inherit (r)
|
|
||||||
id
|
# Shared Hermes block + the robot's own. Robot settings win.
|
||||||
address
|
mergeHermes =
|
||||||
autoStart
|
r:
|
||||||
privateUsers
|
let
|
||||||
cpus
|
s = cfg.hermes;
|
||||||
memory
|
pick = a: b: if a != null then a else b;
|
||||||
disk
|
# Robot values that are null follow the shared ones.
|
||||||
snapshots
|
follow = what: lib.mapAttrs (k: v: pick r.${what}.${k} v) s.${what};
|
||||||
seed
|
in
|
||||||
user
|
{
|
||||||
authorizedKeys
|
enable = pick r.enable s.enable;
|
||||||
git
|
inherit (r) name;
|
||||||
;
|
settings = lib.recursiveUpdate s.settings r.settings;
|
||||||
nft = nftName name;
|
environmentFiles = lib.unique (s.environmentFiles ++ r.environmentFiles);
|
||||||
port = "vb-${name}";
|
gateway = follow "gateway";
|
||||||
dnsPort = 5300 + r.id;
|
dashboard = follow "dashboard";
|
||||||
base = "${cfg.baseDir}/${name}";
|
};
|
||||||
run = "/run/robotnix/${name}";
|
|
||||||
unit = "robotnix-${name}";
|
mkRobot = name: r:
|
||||||
access = mergeAccess r.access;
|
let
|
||||||
};
|
hermes = mergeHermes r.hermes;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
inherit name hermes;
|
||||||
|
inherit (r)
|
||||||
|
id
|
||||||
|
address
|
||||||
|
autoStart
|
||||||
|
privateUsers
|
||||||
|
cpus
|
||||||
|
memory
|
||||||
|
disk
|
||||||
|
snapshots
|
||||||
|
seed
|
||||||
|
user
|
||||||
|
authorizedKeys
|
||||||
|
git
|
||||||
|
allowDocker
|
||||||
|
allowTun
|
||||||
|
ephemeral
|
||||||
|
;
|
||||||
|
# Ephemeral + Hermes: keep only its cron jobs, memories and skills.
|
||||||
|
persist =
|
||||||
|
r.persist
|
||||||
|
++ lib.optionals (r.ephemeral && hermes.enable) (
|
||||||
|
map (d: "${hermesHome}/${d}") [
|
||||||
|
"cron"
|
||||||
|
"memories"
|
||||||
|
"skills"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
nft = nftName name;
|
||||||
|
port = "vb-${name}";
|
||||||
|
dnsPort = 5300 + r.id;
|
||||||
|
base = "${cfg.baseDir}/${name}";
|
||||||
|
run = "/run/robotnix/${name}";
|
||||||
|
unit = "robotnix-${name}";
|
||||||
|
access = mergeAccess r.access;
|
||||||
|
};
|
||||||
|
|
||||||
robots = lib.mapAttrs mkRobot cfg.robots;
|
robots = lib.mapAttrs mkRobot cfg.robots;
|
||||||
in
|
in
|
||||||
@@ -81,6 +119,7 @@ in
|
|||||||
nftName
|
nftName
|
||||||
nixStr
|
nixStr
|
||||||
robots
|
robots
|
||||||
|
hermesHome
|
||||||
;
|
;
|
||||||
robotList = lib.attrValues robots;
|
robotList = lib.attrValues robots;
|
||||||
}
|
}
|
||||||
@@ -103,6 +103,9 @@ let
|
|||||||
|
|
||||||
vmap = prefix: set (map (r: "${r.address} : jump ${prefix}_${r.nft}") rs);
|
vmap = prefix: set (map (r: "${r.address} : jump ${prefix}_${r.nft}") rs);
|
||||||
|
|
||||||
|
# Every robot's own address.
|
||||||
|
robotIps = set (map (r: r.address) rs);
|
||||||
|
|
||||||
# Host-side dnsmasq per robot. It is the robot's only resolver.
|
# Host-side dnsmasq per robot. It is the robot's only resolver.
|
||||||
dnsConf =
|
dnsConf =
|
||||||
r:
|
r:
|
||||||
@@ -226,9 +229,14 @@ lib.mkIf cfg.enable {
|
|||||||
|
|
||||||
chain forward {
|
chain forward {
|
||||||
type filter hook forward priority filter - 10; policy accept;
|
type filter hook forward priority filter - 10; policy accept;
|
||||||
|
# Traffic meant for a robot goes to that robot, nowhere else (e.g.
|
||||||
|
# another module's DNAT). Replies are not touched.
|
||||||
|
ct direction original ct original ip daddr ${robotIps} oifname != "${br}" drop
|
||||||
iifname "${br}" ct state established,related accept
|
iifname "${br}" ct state established,related accept
|
||||||
iifname "${br}" meta nfproto ipv6 drop
|
iifname "${br}" meta nfproto ipv6 drop
|
||||||
oifname "${br}" meta nfproto ipv6 drop
|
oifname "${br}" meta nfproto ipv6 drop
|
||||||
|
# A robot's own connection that someone redirected elsewhere.
|
||||||
|
iifname "${br}" ct status dnat drop
|
||||||
iifname "${br}" ip saddr vmap ${vmap "out"}
|
iifname "${br}" ip saddr vmap ${vmap "out"}
|
||||||
iifname "${br}" drop
|
iifname "${br}" drop
|
||||||
# Inbound from the LAN to robots: open.
|
# Inbound from the LAN to robots: open.
|
||||||
@@ -236,6 +244,8 @@ lib.mkIf cfg.enable {
|
|||||||
|
|
||||||
chain input {
|
chain input {
|
||||||
type filter hook input priority filter - 10; policy accept;
|
type filter hook input priority filter - 10; policy accept;
|
||||||
|
# Traffic meant for a robot, redirected to the host itself.
|
||||||
|
ct direction original ct original ip daddr ${robotIps} drop
|
||||||
iifname != "${br}" accept
|
iifname != "${br}" accept
|
||||||
ct state established,related accept
|
ct state established,related accept
|
||||||
meta nfproto ipv6 drop
|
meta nfproto ipv6 drop
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ def stop-robot [r: record] {
|
|||||||
^systemctl stop $u.robot $u.seed $u.storage
|
^systemctl stop $u.robot $u.seed $u.storage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# What a snapshot covers. Ephemeral robots reset root on every start, so
|
||||||
|
# only home is worth keeping.
|
||||||
|
def parts [r: record] {
|
||||||
|
if $r.ephemeral { [home] } else { [root home] }
|
||||||
|
}
|
||||||
|
|
||||||
# Snapshot ids are "<stamp>-<kind>", e.g. 2026-10-23T14-00-00-auto.
|
# Snapshot ids are "<stamp>-<kind>", e.g. 2026-10-23T14-00-00-auto.
|
||||||
def snapshot-list [r: record] {
|
def snapshot-list [r: record] {
|
||||||
let dir = $"($r.base)/snapshots"
|
let dir = $"($r.base)/snapshots"
|
||||||
@@ -71,15 +77,15 @@ def snapshot-list [r: record] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Freeze both filesystems (if mounted), snapshot both subvolumes, thaw.
|
# Freeze the filesystems (if mounted), snapshot the subvolumes, thaw.
|
||||||
def take-snapshot [r: record, kind: string] {
|
def take-snapshot [r: record, kind: string] {
|
||||||
let id = $"(stamp)-($kind)"
|
let id = $"(stamp)-($kind)"
|
||||||
let dest = $"($r.base)/snapshots/($id)"
|
let dest = $"($r.base)/snapshots/($id)"
|
||||||
mkdir $dest
|
mkdir $dest
|
||||||
let mounts = ([$"($r.run)/root" $"($r.run)/home"] | where {|m| is-mounted $m })
|
let mounts = (parts $r | each {|p| $"($r.run)/($p)" } | where {|m| is-mounted $m })
|
||||||
let err = (try {
|
let err = (try {
|
||||||
for m in $mounts { ^fsfreeze -f $m }
|
for m in $mounts { ^fsfreeze -f $m }
|
||||||
for sub in [root home] {
|
for sub in (parts $r) {
|
||||||
^btrfs subvolume snapshot -r $"($r.base)/($sub)" $"($dest)/($sub)"
|
^btrfs subvolume snapshot -r $"($r.base)/($sub)" $"($dest)/($sub)"
|
||||||
}
|
}
|
||||||
null
|
null
|
||||||
@@ -135,6 +141,7 @@ def "main list" [] {
|
|||||||
root: (disk-usage $"($r.run)/root")
|
root: (disk-usage $"($r.run)/root")
|
||||||
home: (disk-usage $"($r.run)/home")
|
home: (disk-usage $"($r.run)/home")
|
||||||
snapshots: (snapshot-list $r | length)
|
snapshots: (snapshot-list $r | length)
|
||||||
|
ephemeral: $r.ephemeral
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,11 +211,11 @@ def "main restore" [
|
|||||||
need-root
|
need-root
|
||||||
let r = (robot $name)
|
let r = (robot $name)
|
||||||
let subs = if $only == null {
|
let subs = if $only == null {
|
||||||
[root home]
|
parts $r
|
||||||
} else if $only in [root home] {
|
} else if $only in (parts $r) {
|
||||||
[$only]
|
[$only]
|
||||||
} else {
|
} else {
|
||||||
error make { msg: "--only must be root or home" }
|
error make { msg: $"--only must be one of: (parts $r | str join ', ')" }
|
||||||
}
|
}
|
||||||
let src = $"($r.base)/snapshots/($id)"
|
let src = $"($r.base)/snapshots/($id)"
|
||||||
for sub in $subs {
|
for sub in $subs {
|
||||||
@@ -242,6 +249,11 @@ def "main reseed" [
|
|||||||
stop-robot $r
|
stop-robot $r
|
||||||
let saved = (take-snapshot $r "pre-reseed")
|
let saved = (take-snapshot $r "pre-reseed")
|
||||||
rm -f $"($r.base)/root/root.img"
|
rm -f $"($r.base)/root/root.img"
|
||||||
|
# Ephemeral: drop the clean root too, so the next start reinstalls.
|
||||||
|
if $r.ephemeral {
|
||||||
|
rm -f $"($r.base)/pristine.id"
|
||||||
|
if ($"($r.base)/pristine" | path exists) { ^btrfs subvolume delete $"($r.base)/pristine" }
|
||||||
|
}
|
||||||
if $wipe_home { rm -f $"($r.base)/home/home.img" }
|
if $wipe_home { rm -f $"($r.base)/home/home.img" }
|
||||||
^systemctl start (units $r).robot
|
^systemctl start (units $r).robot
|
||||||
print $"($name) reseeded. Undo with: robotnix restore ($name) ($saved)"
|
print $"($name) reseeded. Undo with: robotnix restore ($name) ($saved)"
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
{ config
|
{ config
|
||||||
, lib
|
, lib
|
||||||
, pkgs
|
, pkgs
|
||||||
|
, hermes-agent
|
||||||
, ...
|
, ...
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
@@ -28,6 +29,29 @@ let
|
|||||||
if rev != null then builtins.unsafeDiscardStringContext rev else "nixos-unstable";
|
if rev != null then builtins.unsafeDiscardStringContext rev else "nixos-unstable";
|
||||||
|
|
||||||
keys = r: lib.concatMapStringsSep " " h.nixStr r.authorizedKeys;
|
keys = r: lib.concatMapStringsSep " " h.nixStr r.authorizedKeys;
|
||||||
|
|
||||||
|
# Ephemeral robots: kept folders live here, inside the home image.
|
||||||
|
persistDir = "/home/.persist";
|
||||||
|
|
||||||
|
# Robots pin the same hermes-agent revision the host has locked.
|
||||||
|
hermesRef = hermes-agent.rev or "main";
|
||||||
|
|
||||||
|
# Fixed ids so kept Hermes files stay owned right across reinstalls.
|
||||||
|
hermesId = 870;
|
||||||
|
|
||||||
|
# Secrets reach Hermes as container credentials, one per file.
|
||||||
|
hermesCreds = r: lib.imap0 (i: f: { id = "robotnix-hermes-${toString i}"; file = f; }) r.hermes.environmentFiles;
|
||||||
|
|
||||||
|
# Stock Hermes identity, with the robot's own name.
|
||||||
|
soul =
|
||||||
|
name:
|
||||||
|
"You are ${name}, a Hermes-based agent. Be direct: match the length of your reply to the weight of "
|
||||||
|
+ "the ask — a one-line question gets a one-line answer, and finished work gets a short report of what "
|
||||||
|
+ "changed, what's verified, and what's left, never a replay of the process. No filler (\"Great question,\" "
|
||||||
|
+ "\"I'd be happy to\"), no restating the request back, no re-summarizing what you already said, no narrating "
|
||||||
|
+ "tool calls the user can see. Plain claims over adjectives; when unsure, say so plainly. Agree because it's "
|
||||||
|
+ "right, not because the user said it. Depth is earned — give it when the user asks for detail, teaches, or "
|
||||||
|
+ "the stakes demand it, not by default.\n";
|
||||||
tz = lib.optionalString
|
tz = lib.optionalString
|
||||||
(
|
(
|
||||||
config.time.timeZone != null
|
config.time.timeZone != null
|
||||||
@@ -36,6 +60,20 @@ let
|
|||||||
# The host-owned part of the robot's config -> /etc/nixos/robotnix.nix.
|
# The host-owned part of the robot's config -> /etc/nixos/robotnix.nix.
|
||||||
robotnixNix =
|
robotnixNix =
|
||||||
r:
|
r:
|
||||||
|
let
|
||||||
|
gw = r.hermes.gateway;
|
||||||
|
db = r.hermes.dashboard;
|
||||||
|
gwOn = r.hermes.enable && gw.enable;
|
||||||
|
dbOn = r.hermes.enable && db.enable;
|
||||||
|
openPorts = lib.optional (gwOn && gw.openFirewall) gw.port ++ lib.optional (dbOn && db.openFirewall) db.port;
|
||||||
|
envFiles = "[ ${lib.concatMapStringsSep " " (c: h.nixStr "/run/credentials/@system/${c.id}") (hermesCreds r)} ]";
|
||||||
|
gwEnv = {
|
||||||
|
API_SERVER_ENABLED = "true";
|
||||||
|
API_SERVER_HOST = "0.0.0.0";
|
||||||
|
API_SERVER_PORT = toString gw.port;
|
||||||
|
API_SERVER_MODEL_NAME = if r.hermes.name != null then r.hermes.name else r.name;
|
||||||
|
};
|
||||||
|
in
|
||||||
builtins.toFile "robotnix-${r.name}.nix" ''
|
builtins.toFile "robotnix-${r.name}.nix" ''
|
||||||
# Generated by robotnix on the host (first install / reseed, or a host
|
# Generated by robotnix on the host (first install / reseed, or a host
|
||||||
# rebuild if you changed the robot's host-side settings).
|
# rebuild if you changed the robot's host-side settings).
|
||||||
@@ -70,12 +108,45 @@ let
|
|||||||
};
|
};
|
||||||
users.users.${h.nixStr r.user} = {
|
users.users.${h.nixStr r.user} = {
|
||||||
isNormalUser = true;
|
isNormalUser = true;
|
||||||
extraGroups = [ "wheel" ];
|
extraGroups = [ "wheel" ${lib.optionalString r.hermes.enable ''"hermes" ''}];
|
||||||
openssh.authorizedKeys.keys = [ ${keys r} ];
|
openssh.authorizedKeys.keys = [ ${keys r} ];
|
||||||
};
|
};
|
||||||
users.users.root.openssh.authorizedKeys.keys = [ ${keys r} ];
|
users.users.root.openssh.authorizedKeys.keys = [ ${keys r} ];
|
||||||
${tz}
|
${tz}
|
||||||
system.stateVersion = ${h.nixStr r.seed.stateVersion};
|
system.stateVersion = ${h.nixStr r.seed.stateVersion};
|
||||||
|
${lib.optionalString r.hermes.enable ''
|
||||||
|
# Hermes agent, managed by the host (robotnix hermes options).
|
||||||
|
services.hermes-agent = {
|
||||||
|
enable = true;
|
||||||
|
addToSystemPackages = true;
|
||||||
|
settings = builtins.fromJSON ${h.nixStr (builtins.toJSON r.hermes.settings)};
|
||||||
|
${lib.optionalString (r.hermes.name != null) ''
|
||||||
|
hermesHomeFiles."SOUL.md" = ${h.nixStr (soul r.hermes.name)};
|
||||||
|
''}
|
||||||
|
${lib.optionalString gwOn ''environment = { ${lib.concatStringsSep " " (lib.mapAttrsToList (k: v: "${k} = ${h.nixStr v};") gwEnv)} }; # gateway API, key from the secrets''}
|
||||||
|
${lib.optionalString dbOn ''backend = { mode = "dashboard"; host = "0.0.0.0"; port = ${toString db.port}; }; # login from the secrets''}
|
||||||
|
};
|
||||||
|
users.users.hermes.uid = ${toString hermesId};
|
||||||
|
users.groups.hermes.gid = ${toString hermesId};
|
||||||
|
# The module doesn't make skills/ (kept on ephemeral robots).
|
||||||
|
systemd.tmpfiles.rules = [ "d ${h.hermesHome}/skills 2770 hermes hermes - -" ];
|
||||||
|
# Secrets: container credentials from the host, read at start.
|
||||||
|
systemd.services.hermes-agent.serviceConfig.EnvironmentFile = ${envFiles};
|
||||||
|
${lib.optionalString dbOn ''systemd.services.hermes-backend.serviceConfig.EnvironmentFile = ${envFiles};''}
|
||||||
|
${lib.optionalString (dbOn && db.port < 1024) ''
|
||||||
|
# Ports below 1024 need this as the hermes user.
|
||||||
|
systemd.services.hermes-backend.serviceConfig.AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
||||||
|
''}
|
||||||
|
${lib.optionalString (openPorts != [ ]) ''networking.firewall.allowedTCPPorts = [ ${lib.concatMapStringsSep " " toString openPorts} ];''}
|
||||||
|
''}
|
||||||
|
${lib.optionalString r.ephemeral ''
|
||||||
|
# Ephemeral robot: root resets on every start. Keep the ssh host
|
||||||
|
# keys in /home so they don't change each boot.
|
||||||
|
services.openssh.hostKeys = [
|
||||||
|
{ type = "ed25519"; path = "${persistDir}/ssh/ssh_host_ed25519_key"; }
|
||||||
|
{ type = "rsa"; bits = 4096; path = "${persistDir}/ssh/ssh_host_rsa_key"; }
|
||||||
|
];
|
||||||
|
''}
|
||||||
${lib.optionalString r.git.enable ''
|
${lib.optionalString r.git.enable ''
|
||||||
# git-tracking your flake: auto-commit, and push when a remote is set.
|
# git-tracking your flake: auto-commit, and push when a remote is set.
|
||||||
environment.systemPackages = [ pkgs.git ];
|
environment.systemPackages = [ pkgs.git ];
|
||||||
@@ -113,9 +184,14 @@ let
|
|||||||
# sudo nixos-rebuild switch --flake /etc/nixos
|
# sudo nixos-rebuild switch --flake /etc/nixos
|
||||||
{
|
{
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/${nixpkgsRef}";
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/${nixpkgsRef}";
|
||||||
outputs = { nixpkgs, ... }: {
|
${lib.optionalString r.hermes.enable ''inputs.hermes-agent.url = "github:NousResearch/hermes-agent/${hermesRef}";''}
|
||||||
|
outputs = { nixpkgs, ... }@inputs: {
|
||||||
nixosConfigurations.${h.nixStr r.name} = nixpkgs.lib.nixosSystem {
|
nixosConfigurations.${h.nixStr r.name} = nixpkgs.lib.nixosSystem {
|
||||||
modules = [ ./robotnix.nix ./configuration.nix ];
|
modules = [
|
||||||
|
./robotnix.nix
|
||||||
|
./configuration.nix
|
||||||
|
${lib.optionalString r.hermes.enable "inputs.hermes-agent.nixosModules.default"}
|
||||||
|
];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -132,24 +208,54 @@ let
|
|||||||
modules = [
|
modules = [
|
||||||
(robotnixNix r)
|
(robotnixNix r)
|
||||||
r.seed.configuration
|
r.seed.configuration
|
||||||
];
|
]
|
||||||
|
++ lib.optional r.hermes.enable hermes-agent.nixosModules.default;
|
||||||
}).config.system.build.toplevel;
|
}).config.system.build.toplevel;
|
||||||
|
|
||||||
|
# Ephemeral robots: fingerprint of everything the seed installs. When it
|
||||||
|
# changes, the clean root is rebuilt.
|
||||||
|
seedId =
|
||||||
|
r:
|
||||||
|
builtins.hashString "sha256" (
|
||||||
|
lib.concatStringsSep " " [
|
||||||
|
"${seedSystem r}"
|
||||||
|
"${flakeNix r}"
|
||||||
|
"${robotnixNix r}"
|
||||||
|
"${r.seed.configuration}"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
storageStart =
|
storageStart =
|
||||||
r:
|
r:
|
||||||
pkgs.writeShellScript "${r.unit}-storage-start" ''
|
pkgs.writeShellScript "${r.unit}-storage-start" (
|
||||||
set -euo pipefail
|
''
|
||||||
base=${r.base}
|
set -euo pipefail
|
||||||
mkdir -p ${cfg.baseDir}
|
base=${r.base}
|
||||||
if [ "$(stat -f -c %T ${cfg.baseDir})" != btrfs ]; then
|
mkdir -p ${cfg.baseDir}
|
||||||
echo "robotnix: ${cfg.baseDir} must be on btrfs (needed for snapshots)" >&2
|
if [ "$(stat -f -c %T ${cfg.baseDir})" != btrfs ]; then
|
||||||
exit 1
|
echo "robotnix: ${cfg.baseDir} must be on btrfs (needed for snapshots)" >&2
|
||||||
fi
|
exit 1
|
||||||
for sub in "$base" "$base/root" "$base/home" "$base/snapshots"; do
|
fi
|
||||||
[ -d "$sub" ] || btrfs subvolume create "$sub"
|
for sub in "$base" "$base/root" "$base/home" "$base/snapshots"; do
|
||||||
done
|
[ -d "$sub" ] || btrfs subvolume create "$sub"
|
||||||
# No copy-on-write for the images (only affects new files).
|
done
|
||||||
chattr +C "$base/root" "$base/home"
|
# No copy-on-write for the images (only affects new files).
|
||||||
|
chattr +C "$base/root" "$base/home"
|
||||||
|
''
|
||||||
|
+ lib.optionalString r.ephemeral ''
|
||||||
|
# Ephemeral: seed changed (or first run) -> wipe root so the seed step
|
||||||
|
# reinstalls and saves a new clean root.
|
||||||
|
if ! mountpoint -q ${r.run}/root \
|
||||||
|
&& [ "$(cat "$base/pristine.id" 2>/dev/null)" != ${seedId r} ]; then
|
||||||
|
echo "robotnix: seed for ${r.name} changed, reinstalling"
|
||||||
|
rm -f "$base/pristine.id"
|
||||||
|
if [ -d "$base/pristine" ]; then btrfs subvolume delete "$base/pristine"; fi
|
||||||
|
btrfs subvolume delete "$base/root"
|
||||||
|
btrfs subvolume create "$base/root"
|
||||||
|
chattr +C "$base/root"
|
||||||
|
fi
|
||||||
|
''
|
||||||
|
+ ''
|
||||||
|
|
||||||
image() { # <img> <size> <mountpoint>
|
image() { # <img> <size> <mountpoint>
|
||||||
img=$1 size=$2 dir=$3
|
img=$1 size=$2 dir=$3
|
||||||
@@ -185,7 +291,16 @@ let
|
|||||||
image "$base/root/root.img" ${r.disk.rootSize} ${r.run}/root
|
image "$base/root/root.img" ${r.disk.rootSize} ${r.run}/root
|
||||||
image "$base/home/home.img" ${r.disk.homeSize} ${r.run}/home
|
image "$base/home/home.img" ${r.disk.homeSize} ${r.run}/home
|
||||||
chmod 0755 ${r.run}/root
|
chmod 0755 ${r.run}/root
|
||||||
'';
|
''
|
||||||
|
+ lib.optionalString (r.ephemeral || r.persist != [ ]) ''
|
||||||
|
install -d -m 0700 ${r.run}${persistDir}
|
||||||
|
''
|
||||||
|
+ lib.concatMapStrings
|
||||||
|
(p: ''
|
||||||
|
mkdir -p ${lib.escapeShellArg "${r.run}${persistDir}${p}"}
|
||||||
|
'')
|
||||||
|
r.persist
|
||||||
|
);
|
||||||
|
|
||||||
storageStop =
|
storageStop =
|
||||||
r:
|
r:
|
||||||
@@ -197,37 +312,77 @@ let
|
|||||||
|
|
||||||
# Installs the seed only when the root image has no system yet (first
|
# Installs the seed only when the root image has no system yet (first
|
||||||
# start, or after `robotnix reseed`). Never touches an installed robot.
|
# start, or after `robotnix reseed`). Never touches an installed robot.
|
||||||
|
# Ephemeral robots also save the fresh root as their clean copy.
|
||||||
seedScript =
|
seedScript =
|
||||||
r:
|
r:
|
||||||
pkgs.writeShellScript "${r.unit}-seed" ''
|
pkgs.writeShellScript "${r.unit}-seed" (
|
||||||
set -euo pipefail
|
''
|
||||||
root=${r.run}/root
|
set -euo pipefail
|
||||||
if [ ! -e "$root/nix/var/nix/profiles/system" ]; then
|
root=${r.run}/root
|
||||||
echo "robotnix: installing the seed system into ${r.name}"
|
if [ ! -e "$root/nix/var/nix/profiles/system" ]; then
|
||||||
nixos-install --root "$root" --system ${seedSystem r} \
|
echo "robotnix: installing the seed system into ${r.name}"
|
||||||
--no-root-passwd --no-bootloader --no-channel-copy
|
nixos-install --root "$root" --system ${seedSystem r} \
|
||||||
install -d -m 0755 "$root/etc/nixos"
|
--no-root-passwd --no-bootloader --no-channel-copy
|
||||||
install -m 0644 ${flakeNix r} "$root/etc/nixos/flake.nix"
|
install -d -m 0755 "$root/etc/nixos"
|
||||||
install -m 0644 ${robotnixNix r} "$root/etc/nixos/robotnix.nix"
|
install -m 0644 ${flakeNix r} "$root/etc/nixos/flake.nix"
|
||||||
install -m 0644 ${r.seed.configuration} "$root/etc/nixos/configuration.nix"
|
install -m 0644 ${robotnixNix r} "$root/etc/nixos/robotnix.nix"
|
||||||
${lib.optionalString r.git.enable ''
|
install -m 0644 ${r.seed.configuration} "$root/etc/nixos/configuration.nix"
|
||||||
echo "robotnix: setting up the git repo for ${r.name}'s flake"
|
${lib.optionalString r.git.enable ''
|
||||||
git -C "$root/etc/nixos" init -q
|
echo "robotnix: setting up the git repo for ${r.name}'s flake"
|
||||||
git -C "$root/etc/nixos" config user.name ${h.nixStr r.git.userName}
|
git -C "$root/etc/nixos" init -q
|
||||||
git -C "$root/etc/nixos" config user.email ${h.nixStr r.git.userEmail}
|
git -C "$root/etc/nixos" config user.name ${h.nixStr r.git.userName}
|
||||||
${lib.optionalString (r.git.remoteUrl != null) ''
|
git -C "$root/etc/nixos" config user.email ${h.nixStr r.git.userEmail}
|
||||||
git -C "$root/etc/nixos" remote add origin ${h.nixStr r.git.remoteUrl}
|
${lib.optionalString (r.git.remoteUrl != null) ''
|
||||||
|
git -C "$root/etc/nixos" remote add origin ${h.nixStr r.git.remoteUrl}
|
||||||
|
''}
|
||||||
|
git -C "$root/etc/nixos" add -A
|
||||||
|
git -C "$root/etc/nixos" commit -q -m "robotnix initial seed" || true
|
||||||
''}
|
''}
|
||||||
git -C "$root/etc/nixos" add -A
|
fi
|
||||||
git -C "$root/etc/nixos" commit -q -m "robotnix initial seed" || true
|
# What nspawn and NixOS's init expect (same as nixos-containers).
|
||||||
''}
|
mkdir -p "$root/etc" "$root/var/lib" "$root/root" "$root/home"
|
||||||
|
mkdir -p -m 0755 "$root/usr/bin"
|
||||||
|
chmod 0700 "$root/root"
|
||||||
|
[ -e "$root/etc/os-release" ] || [ -L "$root/etc/os-release" ] || touch "$root/etc/os-release"
|
||||||
|
[ -e "$root/etc/machine-id" ] || touch "$root/etc/machine-id"
|
||||||
|
''
|
||||||
|
+ lib.optionalString r.ephemeral ''
|
||||||
|
# Ephemeral: save the clean root once, before the robot ever boots.
|
||||||
|
if [ ! -e ${r.base}/pristine.id ]; then
|
||||||
|
echo "robotnix: saving ${r.name}'s clean root"
|
||||||
|
if [ -d ${r.base}/pristine ]; then btrfs subvolume delete ${r.base}/pristine; fi
|
||||||
|
sync -f "$root"
|
||||||
|
fsfreeze -f "$root"
|
||||||
|
rc=0
|
||||||
|
btrfs subvolume snapshot -r ${r.base}/root ${r.base}/pristine || rc=$?
|
||||||
|
fsfreeze -u "$root"
|
||||||
|
[ "$rc" -eq 0 ]
|
||||||
|
echo ${seedId r} > ${r.base}/pristine.id
|
||||||
|
fi
|
||||||
|
''
|
||||||
|
);
|
||||||
|
|
||||||
|
# Ephemeral: swap root for a fresh copy of the clean root. Runs (as its
|
||||||
|
# own unit) before every start, including reboots from inside the robot.
|
||||||
|
resetRoot =
|
||||||
|
r:
|
||||||
|
pkgs.writeShellScript "${r.unit}-reset" ''
|
||||||
|
set -euo pipefail
|
||||||
|
base=${r.base}
|
||||||
|
root=${r.run}/root
|
||||||
|
if [ "$(cat "$base/pristine.id" 2>/dev/null)" != ${seedId r} ] || [ ! -d "$base/pristine" ]; then
|
||||||
|
echo "robotnix: no clean root for ${r.name}; restart its storage to reinstall" >&2
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
# What nspawn and NixOS's init expect (same as nixos-containers).
|
if mountpoint -q "$root"; then umount "$root"; fi
|
||||||
mkdir -p "$root/etc" "$root/var/lib" "$root/root" "$root/home"
|
# Wait for the loop device to let go of the old image.
|
||||||
mkdir -p -m 0755 "$root/usr/bin"
|
for _ in $(seq 1 20); do
|
||||||
chmod 0700 "$root/root"
|
[ -z "$(losetup -j "$base/root/root.img")" ] && break
|
||||||
[ -e "$root/etc/os-release" ] || [ -L "$root/etc/os-release" ] || touch "$root/etc/os-release"
|
sleep 0.5
|
||||||
[ -e "$root/etc/machine-id" ] || touch "$root/etc/machine-id"
|
done
|
||||||
|
btrfs subvolume delete "$base/root"
|
||||||
|
btrfs subvolume snapshot "$base/pristine" "$base/root"
|
||||||
|
mount -o loop,noatime "$base/root/root.img" "$root"
|
||||||
'';
|
'';
|
||||||
|
|
||||||
preStart =
|
preStart =
|
||||||
@@ -250,6 +405,8 @@ let
|
|||||||
ip neigh del proxy ${r.address} dev ${ext} 2>/dev/null || true
|
ip neigh del proxy ${r.address} dev ${ext} 2>/dev/null || true
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
idmap = r: lib.optionalString r.privateUsers ":idmap";
|
||||||
|
|
||||||
nspawnArgs =
|
nspawnArgs =
|
||||||
r:
|
r:
|
||||||
lib.escapeShellArgs (
|
lib.escapeShellArgs (
|
||||||
@@ -263,12 +420,18 @@ let
|
|||||||
"--link-journal=try-guest"
|
"--link-journal=try-guest"
|
||||||
"--resolv-conf=off"
|
"--resolv-conf=off"
|
||||||
"--timezone=off"
|
"--timezone=off"
|
||||||
"--bind=${r.run}/home:/home${lib.optionalString r.privateUsers ":idmap"}"
|
"--bind=${r.run}/home:/home${idmap r}"
|
||||||
]
|
]
|
||||||
|
# Kept folders of an ephemeral robot, stored inside the home image.
|
||||||
|
++ map (p: "--bind=${r.run}${persistDir}${p}:${p}${idmap r}") r.persist
|
||||||
++ lib.optionals r.privateUsers [
|
++ lib.optionals r.privateUsers [
|
||||||
"--private-users=pick"
|
"--private-users=pick"
|
||||||
"--private-users-ownership=auto"
|
"--private-users-ownership=auto"
|
||||||
]
|
]
|
||||||
|
# Docker needs the kernel keyring and bpf, which nspawn blocks by default.
|
||||||
|
++ lib.optional r.allowDocker "--system-call-filter=add_key keyctl bpf"
|
||||||
|
++ lib.optional r.allowTun "--bind=/dev/net/tun"
|
||||||
|
++ map (c: "--load-credential=${c.id}:${c.file}") (hermesCreds r)
|
||||||
++ [ "/nix/var/nix/profiles/system/init" ]
|
++ [ "/nix/var/nix/profiles/system/init" ]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -300,6 +463,7 @@ let
|
|||||||
pkgs.coreutils
|
pkgs.coreutils
|
||||||
pkgs.util-linux
|
pkgs.util-linux
|
||||||
pkgs.git
|
pkgs.git
|
||||||
|
pkgs.btrfs-progs
|
||||||
];
|
];
|
||||||
environment.HOME = "/root";
|
environment.HOME = "/root";
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
@@ -310,6 +474,31 @@ let
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Ephemeral: fresh root before every robot start. Its own unit because
|
||||||
|
# the robot unit blocks devices, and mounting the image needs loop
|
||||||
|
# devices. No RemainAfterExit, so each robot start runs it again.
|
||||||
|
"${r.unit}-reset" = lib.mkIf r.ephemeral {
|
||||||
|
description = "robotnix fresh root for ${r.name}";
|
||||||
|
requires = [
|
||||||
|
"${r.unit}-storage.service"
|
||||||
|
"${r.unit}-seed.service"
|
||||||
|
];
|
||||||
|
bindsTo = [ "${r.unit}-storage.service" ];
|
||||||
|
after = [
|
||||||
|
"${r.unit}-storage.service"
|
||||||
|
"${r.unit}-seed.service"
|
||||||
|
];
|
||||||
|
path = with pkgs; [
|
||||||
|
btrfs-progs
|
||||||
|
coreutils
|
||||||
|
util-linux
|
||||||
|
];
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
ExecStart = resetRoot r;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
${r.unit} =
|
${r.unit} =
|
||||||
let
|
let
|
||||||
mem = h.toBytes r.memory.max;
|
mem = h.toBytes r.memory.max;
|
||||||
@@ -317,7 +506,8 @@ let
|
|||||||
"${r.unit}-storage.service"
|
"${r.unit}-storage.service"
|
||||||
"${r.unit}-seed.service"
|
"${r.unit}-seed.service"
|
||||||
"${r.unit}-dns.service"
|
"${r.unit}-dns.service"
|
||||||
];
|
]
|
||||||
|
++ lib.optional r.ephemeral "${r.unit}-reset.service";
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
description = "robotnix robot ${r.name}";
|
description = "robotnix robot ${r.name}";
|
||||||
@@ -352,6 +542,7 @@ let
|
|||||||
KillMode = "mixed";
|
KillMode = "mixed";
|
||||||
KillSignal = "TERM";
|
KillSignal = "TERM";
|
||||||
DevicePolicy = "closed";
|
DevicePolicy = "closed";
|
||||||
|
DeviceAllow = lib.mkIf r.allowTun [ "/dev/net/tun rwm" ];
|
||||||
|
|
||||||
# Resources.
|
# Resources.
|
||||||
CPUQuota = "${toString (r.cpus.max * 100)}%";
|
CPUQuota = "${toString (r.cpus.max * 100)}%";
|
||||||
@@ -381,6 +572,7 @@ let
|
|||||||
user
|
user
|
||||||
nft
|
nft
|
||||||
access
|
access
|
||||||
|
ephemeral
|
||||||
;
|
;
|
||||||
snapshots = { inherit (r.snapshots) enable interval keep; };
|
snapshots = { inherit (r.snapshots) enable interval keep; };
|
||||||
})
|
})
|
||||||
|
|||||||
+5
-1
@@ -25,9 +25,13 @@ in
|
|||||||
|
|
||||||
# Hermes envs
|
# Hermes envs
|
||||||
"secrets/hermes-env.age".publicKeys = [ ari fwdesk ];
|
"secrets/hermes-env.age".publicKeys = [ ari fwdesk ];
|
||||||
|
|
||||||
"secrets/iu-project.age".publicKeys = [ ari minipc1 ];
|
"secrets/iu-project.age".publicKeys = [ ari minipc1 ];
|
||||||
|
|
||||||
|
# Robots
|
||||||
|
"secrets/robots-shared.age".publicKeys = [ ari fwdesk ];
|
||||||
|
"secrets/robots-genix.age".publicKeys = [ ari fwdesk ];
|
||||||
|
"secrets/robots-sibli.age".publicKeys = [ ari fwdesk ];
|
||||||
|
|
||||||
# Buzz stack env (minipc1)
|
# Buzz stack env (minipc1)
|
||||||
"secrets/buzz-env.age".publicKeys = [ ari minipc1 ];
|
"secrets/buzz-env.age".publicKeys = [ ari minipc1 ];
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user