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
|
||||
.mnt
|
||||
.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,
|
||||
"languages": {
|
||||
"QML": {
|
||||
"colorize_brackets": true,
|
||||
"inlay_hints": {
|
||||
"show_background": true,
|
||||
"enabled": true
|
||||
},
|
||||
"show_whitespaces": "all",
|
||||
"show_completions_on_input": true,
|
||||
"always_treat_brackets_as_autoclosed": true,
|
||||
"format_on_save": "on",
|
||||
"language_servers": ["qmljs"]
|
||||
}
|
||||
},
|
||||
"lsp": {
|
||||
"qmljs": {
|
||||
"context_servers": {
|
||||
"linux-tracker": {
|
||||
"url": "http://fwdesk.lab.internal:1984/mcp"
|
||||
}
|
||||
},
|
||||
"languages": {
|
||||
"QML": {
|
||||
"colorize_brackets": true,
|
||||
"inlay_hints": {
|
||||
"show_background": true,
|
||||
"enabled": true
|
||||
},
|
||||
"show_whitespaces": "all",
|
||||
"show_completions_on_input": true,
|
||||
"always_treat_brackets_as_autoclosed": true,
|
||||
"format_on_save": "on",
|
||||
"language_servers": [
|
||||
"qmljs"
|
||||
]
|
||||
}
|
||||
},
|
||||
"lsp": {
|
||||
"qmljs": {
|
||||
"enable_lsp_tasks": true,
|
||||
"binary": {
|
||||
"path": "qmlls",
|
||||
"arguments": ["-E"]
|
||||
},
|
||||
"initialization_options": {
|
||||
"buildDir": "."
|
||||
}
|
||||
}
|
||||
}
|
||||
"binary": {
|
||||
"path": "qmlls",
|
||||
"arguments": [
|
||||
"-E"
|
||||
]
|
||||
},
|
||||
"initialization_options": {
|
||||
"buildDir": "."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Generated
+4
-4
@@ -1088,11 +1088,11 @@
|
||||
"treefmt-nix": "treefmt-nix_3"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1791570912,
|
||||
"narHash": "sha256-qWpBgsVYqmK3vKtoUm7/w1B2mOwHkridUyc1ESAooVg=",
|
||||
"lastModified": 1793031979,
|
||||
"narHash": "sha256-PKMdT/idOFtPwj03PzWzIO4prgPCtMsG1ig1FUlCeTM=",
|
||||
"ref": "refs/heads/main",
|
||||
"rev": "30427e3975637593c995d357815b99aecb512cfa",
|
||||
"revCount": 43,
|
||||
"rev": "ed558a196fcbc65c1af82fb0f54fdd6773c76b66",
|
||||
"revCount": 47,
|
||||
"type": "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-terra1 = ./secrets/linux-tracker-peer-terra1.age;
|
||||
scrypted-envs = ./secrets/scrypted-envs.age;
|
||||
robots-shared = ./secrets/robots-shared.age;
|
||||
robots-sibli = ./secrets/robots-sibli.age;
|
||||
};
|
||||
inherit
|
||||
hyprlauncher
|
||||
|
||||
@@ -2,8 +2,5 @@
|
||||
{
|
||||
services.tailscale.enable = true;
|
||||
|
||||
systemd.network.wait-online.ignoredInterfaces = [
|
||||
"hermes-br"
|
||||
"vm-hermes"
|
||||
];
|
||||
systemd.network.wait-online.enable = false;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
{ config, ... }:
|
||||
{ config, lib, skyg-secrets, ... }:
|
||||
let
|
||||
apps = config.skyg.internalNetworkingMap.apps;
|
||||
robotSecrets = [ "robots-shared" "robots-sibli" ];
|
||||
secret = name: config.age.secrets.${name}.path;
|
||||
ports = {
|
||||
ollama = 11434;
|
||||
ds4 = 8000;
|
||||
@@ -8,6 +10,12 @@ let
|
||||
};
|
||||
in
|
||||
{
|
||||
age.secrets = lib.genAttrs robotSecrets (name: {
|
||||
file = skyg-secrets.${name};
|
||||
owner = "root";
|
||||
mode = "0400";
|
||||
});
|
||||
|
||||
skyg.nixos.server.robotnix = {
|
||||
enable = true;
|
||||
externalInterface = "enp191s0";
|
||||
@@ -17,23 +25,40 @@ in
|
||||
internet = "all";
|
||||
hostAccess = [
|
||||
{ port = ports.ollama; }
|
||||
{ port = ports.tracker; }
|
||||
];
|
||||
lanAccess = [ ];
|
||||
};
|
||||
|
||||
robots.genix = {
|
||||
id = 1;
|
||||
address = apps.genix.ip;
|
||||
# A Hermes agent in every robot.
|
||||
hermes = {
|
||||
enable = true;
|
||||
environmentFiles = [ (secret "robots-shared") ];
|
||||
};
|
||||
|
||||
robots.sibli = {
|
||||
id = 2;
|
||||
address = apps.sibli.ip;
|
||||
cpus = { min = 1; max = 4; };
|
||||
memory = { min = "8G"; max = "16G"; };
|
||||
memory = { min = "2G"; max = "16G"; };
|
||||
disk = { rootSize = "100G"; homeSize = "60G"; };
|
||||
seed.configuration = ./robotnix/genix.nix;
|
||||
access = { };
|
||||
git = {
|
||||
enable = true;
|
||||
remoteUrl = "gitea@gitea.app.internal:Hermenix/genix.git";
|
||||
seed.configuration = ./robotnix/sibli.nix;
|
||||
hermes = {
|
||||
name = "Siblix";
|
||||
environmentFiles = [ (secret "robots-sibli") ];
|
||||
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"; };
|
||||
immich = { ip = "10.0.101.10"; };
|
||||
immich-ml = { ip = "10.0.101.11"; };
|
||||
genix = { ip = "10.0.101.12"; };
|
||||
gilix = { ip = "10.0.101.13"; };
|
||||
sibli = { ip = "10.0.101.14"; };
|
||||
jellyfin = { ip = "10.0.101.5"; };
|
||||
nvr = { ip = "10.0.101.6"; };
|
||||
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
|
||||
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.
|
||||
- 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:
|
||||
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.
|
||||
@@ -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 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
|
||||
|
||||
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 (
|
||||
{ name, ... }:
|
||||
{
|
||||
@@ -165,6 +233,61 @@ let
|
||||
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 {
|
||||
type = tier;
|
||||
default = { };
|
||||
@@ -195,7 +318,7 @@ let
|
||||
remoteUrl = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
example = "git@github.com:ari/genix.git";
|
||||
example = "git@github.com:ari/myrobot.git";
|
||||
description = ''
|
||||
Origin for the flake repo, set on first install. null = no remote:
|
||||
the timer commits locally but never pushes.
|
||||
@@ -292,6 +415,14 @@ in
|
||||
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 {
|
||||
type = types.attrsOf robot;
|
||||
default = { };
|
||||
@@ -343,6 +474,23 @@ in
|
||||
assertion = 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;
|
||||
message = "robotnix: ${r.name} cpus.min must be <= cpus.max.";
|
||||
|
||||
@@ -48,30 +48,68 @@ let
|
||||
hostAccess = both (t: t.hostAccess);
|
||||
};
|
||||
|
||||
mkRobot = name: r: {
|
||||
inherit name;
|
||||
inherit (r)
|
||||
id
|
||||
address
|
||||
autoStart
|
||||
privateUsers
|
||||
cpus
|
||||
memory
|
||||
disk
|
||||
snapshots
|
||||
seed
|
||||
user
|
||||
authorizedKeys
|
||||
git
|
||||
;
|
||||
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;
|
||||
};
|
||||
# Hermes's home inside a robot (the upstream module's default).
|
||||
hermesHome = "/var/lib/hermes/.hermes";
|
||||
|
||||
# Shared Hermes block + the robot's own. Robot settings win.
|
||||
mergeHermes =
|
||||
r:
|
||||
let
|
||||
s = cfg.hermes;
|
||||
pick = a: b: if a != null then a else b;
|
||||
# Robot values that are null follow the shared ones.
|
||||
follow = what: lib.mapAttrs (k: v: pick r.${what}.${k} v) s.${what};
|
||||
in
|
||||
{
|
||||
enable = pick r.enable s.enable;
|
||||
inherit (r) name;
|
||||
settings = lib.recursiveUpdate s.settings r.settings;
|
||||
environmentFiles = lib.unique (s.environmentFiles ++ r.environmentFiles);
|
||||
gateway = follow "gateway";
|
||||
dashboard = follow "dashboard";
|
||||
};
|
||||
|
||||
mkRobot = name: r:
|
||||
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;
|
||||
in
|
||||
@@ -81,6 +119,7 @@ in
|
||||
nftName
|
||||
nixStr
|
||||
robots
|
||||
hermesHome
|
||||
;
|
||||
robotList = lib.attrValues robots;
|
||||
}
|
||||
@@ -103,6 +103,9 @@ let
|
||||
|
||||
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.
|
||||
dnsConf =
|
||||
r:
|
||||
@@ -226,9 +229,14 @@ lib.mkIf cfg.enable {
|
||||
|
||||
chain forward {
|
||||
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}" 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}" drop
|
||||
# Inbound from the LAN to robots: open.
|
||||
@@ -236,6 +244,8 @@ lib.mkIf cfg.enable {
|
||||
|
||||
chain input {
|
||||
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
|
||||
ct state established,related accept
|
||||
meta nfproto ipv6 drop
|
||||
|
||||
@@ -53,6 +53,12 @@ def stop-robot [r: record] {
|
||||
^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.
|
||||
def snapshot-list [r: record] {
|
||||
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] {
|
||||
let id = $"(stamp)-($kind)"
|
||||
let dest = $"($r.base)/snapshots/($id)"
|
||||
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 {
|
||||
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)"
|
||||
}
|
||||
null
|
||||
@@ -135,6 +141,7 @@ def "main list" [] {
|
||||
root: (disk-usage $"($r.run)/root")
|
||||
home: (disk-usage $"($r.run)/home")
|
||||
snapshots: (snapshot-list $r | length)
|
||||
ephemeral: $r.ephemeral
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -204,11 +211,11 @@ def "main restore" [
|
||||
need-root
|
||||
let r = (robot $name)
|
||||
let subs = if $only == null {
|
||||
[root home]
|
||||
} else if $only in [root home] {
|
||||
parts $r
|
||||
} else if $only in (parts $r) {
|
||||
[$only]
|
||||
} 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)"
|
||||
for sub in $subs {
|
||||
@@ -242,6 +249,11 @@ def "main reseed" [
|
||||
stop-robot $r
|
||||
let saved = (take-snapshot $r "pre-reseed")
|
||||
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" }
|
||||
^systemctl start (units $r).robot
|
||||
print $"($name) reseeded. Undo with: robotnix restore ($name) ($saved)"
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
{ config
|
||||
, lib
|
||||
, pkgs
|
||||
, hermes-agent
|
||||
, ...
|
||||
}:
|
||||
let
|
||||
@@ -28,6 +29,29 @@ let
|
||||
if rev != null then builtins.unsafeDiscardStringContext rev else "nixos-unstable";
|
||||
|
||||
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
|
||||
(
|
||||
config.time.timeZone != null
|
||||
@@ -36,6 +60,20 @@ let
|
||||
# The host-owned part of the robot's config -> /etc/nixos/robotnix.nix.
|
||||
robotnixNix =
|
||||
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" ''
|
||||
# Generated by robotnix on the host (first install / reseed, or a host
|
||||
# rebuild if you changed the robot's host-side settings).
|
||||
@@ -70,12 +108,45 @@ let
|
||||
};
|
||||
users.users.${h.nixStr r.user} = {
|
||||
isNormalUser = true;
|
||||
extraGroups = [ "wheel" ];
|
||||
extraGroups = [ "wheel" ${lib.optionalString r.hermes.enable ''"hermes" ''}];
|
||||
openssh.authorizedKeys.keys = [ ${keys r} ];
|
||||
};
|
||||
users.users.root.openssh.authorizedKeys.keys = [ ${keys r} ];
|
||||
${tz}
|
||||
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 ''
|
||||
# git-tracking your flake: auto-commit, and push when a remote is set.
|
||||
environment.systemPackages = [ pkgs.git ];
|
||||
@@ -113,9 +184,14 @@ let
|
||||
# sudo nixos-rebuild switch --flake /etc/nixos
|
||||
{
|
||||
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 {
|
||||
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 = [
|
||||
(robotnixNix r)
|
||||
r.seed.configuration
|
||||
];
|
||||
]
|
||||
++ lib.optional r.hermes.enable hermes-agent.nixosModules.default;
|
||||
}).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 =
|
||||
r:
|
||||
pkgs.writeShellScript "${r.unit}-storage-start" ''
|
||||
set -euo pipefail
|
||||
base=${r.base}
|
||||
mkdir -p ${cfg.baseDir}
|
||||
if [ "$(stat -f -c %T ${cfg.baseDir})" != btrfs ]; then
|
||||
echo "robotnix: ${cfg.baseDir} must be on btrfs (needed for snapshots)" >&2
|
||||
exit 1
|
||||
fi
|
||||
for sub in "$base" "$base/root" "$base/home" "$base/snapshots"; do
|
||||
[ -d "$sub" ] || btrfs subvolume create "$sub"
|
||||
done
|
||||
# No copy-on-write for the images (only affects new files).
|
||||
chattr +C "$base/root" "$base/home"
|
||||
pkgs.writeShellScript "${r.unit}-storage-start" (
|
||||
''
|
||||
set -euo pipefail
|
||||
base=${r.base}
|
||||
mkdir -p ${cfg.baseDir}
|
||||
if [ "$(stat -f -c %T ${cfg.baseDir})" != btrfs ]; then
|
||||
echo "robotnix: ${cfg.baseDir} must be on btrfs (needed for snapshots)" >&2
|
||||
exit 1
|
||||
fi
|
||||
for sub in "$base" "$base/root" "$base/home" "$base/snapshots"; do
|
||||
[ -d "$sub" ] || btrfs subvolume create "$sub"
|
||||
done
|
||||
# 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>
|
||||
img=$1 size=$2 dir=$3
|
||||
@@ -185,7 +291,16 @@ let
|
||||
image "$base/root/root.img" ${r.disk.rootSize} ${r.run}/root
|
||||
image "$base/home/home.img" ${r.disk.homeSize} ${r.run}/home
|
||||
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 =
|
||||
r:
|
||||
@@ -197,37 +312,77 @@ let
|
||||
|
||||
# Installs the seed only when the root image has no system yet (first
|
||||
# start, or after `robotnix reseed`). Never touches an installed robot.
|
||||
# Ephemeral robots also save the fresh root as their clean copy.
|
||||
seedScript =
|
||||
r:
|
||||
pkgs.writeShellScript "${r.unit}-seed" ''
|
||||
set -euo pipefail
|
||||
root=${r.run}/root
|
||||
if [ ! -e "$root/nix/var/nix/profiles/system" ]; then
|
||||
echo "robotnix: installing the seed system into ${r.name}"
|
||||
nixos-install --root "$root" --system ${seedSystem r} \
|
||||
--no-root-passwd --no-bootloader --no-channel-copy
|
||||
install -d -m 0755 "$root/etc/nixos"
|
||||
install -m 0644 ${flakeNix r} "$root/etc/nixos/flake.nix"
|
||||
install -m 0644 ${robotnixNix r} "$root/etc/nixos/robotnix.nix"
|
||||
install -m 0644 ${r.seed.configuration} "$root/etc/nixos/configuration.nix"
|
||||
${lib.optionalString r.git.enable ''
|
||||
echo "robotnix: setting up the git repo for ${r.name}'s flake"
|
||||
git -C "$root/etc/nixos" init -q
|
||||
git -C "$root/etc/nixos" config user.name ${h.nixStr r.git.userName}
|
||||
git -C "$root/etc/nixos" config user.email ${h.nixStr r.git.userEmail}
|
||||
${lib.optionalString (r.git.remoteUrl != null) ''
|
||||
git -C "$root/etc/nixos" remote add origin ${h.nixStr r.git.remoteUrl}
|
||||
pkgs.writeShellScript "${r.unit}-seed" (
|
||||
''
|
||||
set -euo pipefail
|
||||
root=${r.run}/root
|
||||
if [ ! -e "$root/nix/var/nix/profiles/system" ]; then
|
||||
echo "robotnix: installing the seed system into ${r.name}"
|
||||
nixos-install --root "$root" --system ${seedSystem r} \
|
||||
--no-root-passwd --no-bootloader --no-channel-copy
|
||||
install -d -m 0755 "$root/etc/nixos"
|
||||
install -m 0644 ${flakeNix r} "$root/etc/nixos/flake.nix"
|
||||
install -m 0644 ${robotnixNix r} "$root/etc/nixos/robotnix.nix"
|
||||
install -m 0644 ${r.seed.configuration} "$root/etc/nixos/configuration.nix"
|
||||
${lib.optionalString r.git.enable ''
|
||||
echo "robotnix: setting up the git repo for ${r.name}'s flake"
|
||||
git -C "$root/etc/nixos" init -q
|
||||
git -C "$root/etc/nixos" config user.name ${h.nixStr r.git.userName}
|
||||
git -C "$root/etc/nixos" config user.email ${h.nixStr r.git.userEmail}
|
||||
${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
|
||||
git -C "$root/etc/nixos" commit -q -m "robotnix initial seed" || true
|
||||
''}
|
||||
fi
|
||||
# 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
|
||||
# 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"
|
||||
if mountpoint -q "$root"; then umount "$root"; fi
|
||||
# Wait for the loop device to let go of the old image.
|
||||
for _ in $(seq 1 20); do
|
||||
[ -z "$(losetup -j "$base/root/root.img")" ] && break
|
||||
sleep 0.5
|
||||
done
|
||||
btrfs subvolume delete "$base/root"
|
||||
btrfs subvolume snapshot "$base/pristine" "$base/root"
|
||||
mount -o loop,noatime "$base/root/root.img" "$root"
|
||||
'';
|
||||
|
||||
preStart =
|
||||
@@ -250,6 +405,8 @@ let
|
||||
ip neigh del proxy ${r.address} dev ${ext} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
idmap = r: lib.optionalString r.privateUsers ":idmap";
|
||||
|
||||
nspawnArgs =
|
||||
r:
|
||||
lib.escapeShellArgs (
|
||||
@@ -263,12 +420,18 @@ let
|
||||
"--link-journal=try-guest"
|
||||
"--resolv-conf=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 [
|
||||
"--private-users=pick"
|
||||
"--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" ]
|
||||
);
|
||||
|
||||
@@ -300,6 +463,7 @@ let
|
||||
pkgs.coreutils
|
||||
pkgs.util-linux
|
||||
pkgs.git
|
||||
pkgs.btrfs-progs
|
||||
];
|
||||
environment.HOME = "/root";
|
||||
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} =
|
||||
let
|
||||
mem = h.toBytes r.memory.max;
|
||||
@@ -317,7 +506,8 @@ let
|
||||
"${r.unit}-storage.service"
|
||||
"${r.unit}-seed.service"
|
||||
"${r.unit}-dns.service"
|
||||
];
|
||||
]
|
||||
++ lib.optional r.ephemeral "${r.unit}-reset.service";
|
||||
in
|
||||
{
|
||||
description = "robotnix robot ${r.name}";
|
||||
@@ -352,6 +542,7 @@ let
|
||||
KillMode = "mixed";
|
||||
KillSignal = "TERM";
|
||||
DevicePolicy = "closed";
|
||||
DeviceAllow = lib.mkIf r.allowTun [ "/dev/net/tun rwm" ];
|
||||
|
||||
# Resources.
|
||||
CPUQuota = "${toString (r.cpus.max * 100)}%";
|
||||
@@ -381,6 +572,7 @@ let
|
||||
user
|
||||
nft
|
||||
access
|
||||
ephemeral
|
||||
;
|
||||
snapshots = { inherit (r.snapshots) enable interval keep; };
|
||||
})
|
||||
|
||||
+5
-1
@@ -25,9 +25,13 @@ in
|
||||
|
||||
# Hermes envs
|
||||
"secrets/hermes-env.age".publicKeys = [ ari fwdesk ];
|
||||
|
||||
"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)
|
||||
"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