From 62e73eb4d26f6b9da743ca1fdcf0a8d105a5b4af Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 27 Oct 2026 13:28:27 -0400 Subject: [PATCH] using seperate robotnix --- flake.lock | 30 + flake.nix | 13 + hosts/hl-fwdesk/services/robotnix.nix | 3 +- hosts/hl-fwdesk/services/robotnix/sibli.nix | 59 +- modules/ABOUTME.md | 2 +- modules/nixos/server/ABOUTME.md | 8 +- modules/nixos/server/robotnix/ABOUTME.md | 255 +------ modules/nixos/server/robotnix/default.nix | 517 +-------------- modules/nixos/server/robotnix/helpers.nix | 125 ---- modules/nixos/server/robotnix/network.nix | 304 --------- modules/nixos/server/robotnix/robotnix.nu | 270 -------- modules/nixos/server/robotnix/robots.nix | 622 ------------------ .../server/robotnix/seed/configuration.nix | 24 - pkgs/buzz-cli/default.nix | 2 +- pkgs/buzz-cli/update.sh | 6 + pkgs/delta-editor/default.nix | 4 +- secrets/robots-sibli.age | Bin 668 -> 817 bytes 17 files changed, 158 insertions(+), 2086 deletions(-) delete mode 100644 modules/nixos/server/robotnix/helpers.nix delete mode 100644 modules/nixos/server/robotnix/network.nix delete mode 100644 modules/nixos/server/robotnix/robotnix.nu delete mode 100644 modules/nixos/server/robotnix/robots.nix delete mode 100644 modules/nixos/server/robotnix/seed/configuration.nix diff --git a/flake.lock b/flake.lock index 06717e9..8d4c783 100644 --- a/flake.lock +++ b/flake.lock @@ -3054,6 +3054,35 @@ "type": "github" } }, + "robotnix": { + "inputs": { + "devenv": [], + "git-hooks": [ + "git-hooks" + ], + "hermes-agent": [ + "hermes-agent" + ], + "nixpkgs": [ + "nixpkgs" + ], + "nutest": [], + "treefmt-nix": [] + }, + "locked": { + "lastModified": 1793121438, + "narHash": "sha256-/oPrlfJoMbRNvv9r9M2O9chx74UkxF9y0aV3C6TqSK8=", + "ref": "refs/heads/main", + "rev": "01e1d47856dae496ba7e30d8864ae3228e22612a", + "revCount": 2, + "type": "git", + "url": "ssh://gitea@gitea.app.internal:22/ari/robotnix.git" + }, + "original": { + "type": "git", + "url": "ssh://gitea@gitea.app.internal:22/ari/robotnix.git" + } + }, "root": { "inputs": { "agenix": "agenix", @@ -3080,6 +3109,7 @@ "nixpkgs": "nixpkgs_13", "nixpkgs-unstable": "nixpkgs-unstable", "noctalia": "noctalia", + "robotnix": "robotnix", "skyg-linux-tracker": "skyg-linux-tracker", "skygqts": "skygqts", "stylix": "stylix", diff --git a/flake.nix b/flake.nix index 2c4c9e6..249e392 100644 --- a/flake.nix +++ b/flake.nix @@ -4,6 +4,17 @@ skygqts.url = "git+ssh://gitea@gitea.app.internal:22/ari/skygqts.git"; skygqts.inputs.nixpkgs.follows = "nixpkgs"; hermenix.url = "git+ssh://gitea@gitea.app.internal:22/ari/hermenix.git"; + # Self-managed nspawn robots. Robots pin the same hermes-agent rev. + robotnix = { + url = "git+ssh://gitea@gitea.app.internal:22/ari/robotnix.git"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.hermes-agent.follows = "hermes-agent"; + # Dev/test-only inputs of robotnix: keep them out of this lock. + inputs.git-hooks.follows = "git-hooks"; + inputs.devenv.follows = ""; + inputs.treefmt-nix.follows = ""; + inputs.nutest.follows = ""; + }; # Read-only host monitor (systemd, journal, Docker) over REST + MCP. skyg-linux-tracker = { url = "git+https://gitea.skyg.ca/ari/skyg-linux-tracker.git"; @@ -104,6 +115,7 @@ { self , skygqts , hermenix + , robotnix , skyg-linux-tracker , hhstream , nixpkgs-unstable @@ -197,6 +209,7 @@ hermes-agent skygqts hermenix + robotnix skyg-linux-tracker hhstream flox diff --git a/hosts/hl-fwdesk/services/robotnix.nix b/hosts/hl-fwdesk/services/robotnix.nix index eea08b3..a39a3c8 100644 --- a/hosts/hl-fwdesk/services/robotnix.nix +++ b/hosts/hl-fwdesk/services/robotnix.nix @@ -16,7 +16,8 @@ in mode = "0400"; }); - skyg.nixos.server.robotnix = { + # robotnix flake input; options and docs in its repo. + services.robotnix = { enable = true; externalInterface = "enp191s0"; diff --git a/hosts/hl-fwdesk/services/robotnix/sibli.nix b/hosts/hl-fwdesk/services/robotnix/sibli.nix index 6f9bfe3..9330c2a 100644 --- a/hosts/hl-fwdesk/services/robotnix/sibli.nix +++ b/hosts/hl-fwdesk/services/robotnix/sibli.nix @@ -2,11 +2,8 @@ # things here (then rebuild the host), not inside the robot. { config, lib, pkgs, ... }: let - # The robot's own nixpkgs has no host overlay, so build it from the repo. - # Fine for the seed build; a rebuild inside the robot can't see this path. buzz-cli = pkgs.callPackage ../../../../pkgs/buzz-cli { }; - # Buzz profile name: the Hermes name the host passes to the gateway. - buzzName = config.services.hermes-agent.environment.API_SERVER_MODEL_NAME or "sibli"; + robotoName = config.services.hermes-agent.environment.API_SERVER_MODEL_NAME; in { # The user has no password; ssh keys only. @@ -36,8 +33,10 @@ in ''; }; - # Hermes's Buzz platform runs `buzz`; the service PATH has no system packages. - services.hermes-agent.environment.BUZZ_CLI_PATH = "${buzz-cli}/bin/buzz"; + services.hermes-agent.environment = { + BUZZ_CLI_PATH = "${buzz-cli}/bin/buzz"; + BUZZ_HOME_CHANNEL = "a8969cdc-7ba2-4e25-ae18-b898ab59003d"; + }; # A new Buzz key has no profile, and Hermes won't connect without one. # Publish one at start if missing; the relay keeps it across resets. @@ -52,7 +51,6 @@ in Type = "oneshot"; RemainAfterExit = true; User = "hermes"; - # Same secrets as Hermes (container credentials from the host). EnvironmentFile = config.systemd.services.hermes-agent.serviceConfig.EnvironmentFile; }; script = '' @@ -67,7 +65,7 @@ in n=$(jq 'length' <<<"$out") || n="" fi if [ "$n" = "0" ]; then - if buzz users set-profile --name ${lib.escapeShellArg buzzName}; then + if buzz users set-profile --name ${lib.escapeShellArg robotoName}; then echo "Published Buzz profile." exit 0 fi @@ -84,12 +82,55 @@ in # Docker + compose. Needs allowDocker = true on the host. virtualisation.docker.enable = true; users.users.ari.extraGroups = [ "docker" ]; + users.users.hermes.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) + # Logs in by itself with TS_AUTHKEY from the robots-sibli secret. + # State is kept via persist on the host, so this only matters once. services.tailscale.enable = true; services.tailscale.openFirewall = true; networking.firewall.trustedInterfaces = [ "tailscale0" ]; + + systemd.services.tailscale-login = { + description = "Log in to Tailscale if needed"; + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" "tailscaled.service" ]; + requires = [ "tailscaled.service" ]; + path = [ config.services.tailscale.package pkgs.jq ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + EnvironmentFile = config.systemd.services.hermes-agent.serviceConfig.EnvironmentFile; + }; + script = '' + if [ -z "''${TS_AUTHKEY:-}" ]; then + echo "No TS_AUTHKEY, skipping." + exit 0 + fi + # Retry: the daemon may not be ready yet. Failure is not fatal. + for _ in 1 2 3 4 5 6; do + state="" + if out=$(tailscale status --json); then + state=$(jq -r '.BackendState' <<<"$out") || state="" + fi + case "$state" in + Running) + echo "Already logged in." + exit 0 + ;; + NeedsLogin|Stopped) + if tailscale up --auth-key="$TS_AUTHKEY" --hostname=${robotoName}-bot; then + echo "Logged in." + exit 0 + fi + ;; + esac + sleep 5 + done + echo "Could not log in to Tailscale." >&2 + ''; + }; nix.gc = { automatic = true; dates = "weekly"; diff --git a/modules/ABOUTME.md b/modules/ABOUTME.md index cba97f2..07ba593 100644 --- a/modules/ABOUTME.md +++ b/modules/ABOUTME.md @@ -38,7 +38,7 @@ skyg.rootDir → core/default.nix (set by the flake; repo root stor skyg.nixos.common.* → nixos/common/ skyg.nixos.desktop.* → nixos/desktop/ skyg.nixos.server.* → nixos/server/ -skyg.nixos.server.robotnix.* → nixos/server/robotnix/ (self-managed nspawn robots) +services.robotnix.* → nixos/server/robotnix/ (adapter for the robotnix flake) skyg.core.* → core/ ``` diff --git a/modules/nixos/server/ABOUTME.md b/modules/nixos/server/ABOUTME.md index a138ae6..54e7842 100644 --- a/modules/nixos/server/ABOUTME.md +++ b/modules/nixos/server/ABOUTME.md @@ -18,7 +18,7 @@ server/ ├── k3s/ # Single-binary K3s cluster role ├── k8s/ # Full upstream Kubernetes (master/node) ├── portainer.nix # skyg.nixos.server.portainer — Portainer server or Edge agent -├── robotnix/ # skyg.nixos.server.robotnix — self-managed NixOS nspawn containers +├── robotnix/ # adapter: imports the robotnix flake (services.robotnix) └── services/ # Self-hosted apps (jellyfin, audiobookshelf, comfyui, ds4, colibri, ai…) ``` @@ -36,8 +36,8 @@ server/ | Structure | Namespace | Notes | | ------------ | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `robotnix/` | `skyg.nixos.server.robotnix` | Self-managed NixOS nspawn containers (persistent system, own LAN IP, snapshots, host-controlled web access). The one sanctioned nspawn exception to the oci-containers rule | -| `timers.nix` | `skyg.server.timers` | Generic attrset → timer + oneshot service (robotnix snapshot timers use it) | +| `robotnix/` | `services.robotnix` | Adapter for the robotnix flake (own repo): self-managed NixOS nspawn robots. Sets skyg defaults; the one sanctioned nspawn exception to the oci-containers rule | +| `timers.nix` | `skyg.server.timers` | Generic attrset → timer + oneshot service | ## Option Namespace @@ -50,7 +50,7 @@ skyg.server.arrs.* → arrs/ skyg.server.dns.* → dns/ skyg.nixos.server.k3s.* → k3s/ skyg.nixos.server.k8s.* → k8s/ -skyg.nixos.server.robotnix.* → robotnix/ (self-managed nspawn containers) +services.robotnix.* → robotnix/ (adapter for the robotnix flake) skyg.nixos.server.services.* → services/ skyg.nixos.server.portainer.* → portainer.nix ``` diff --git a/modules/nixos/server/robotnix/ABOUTME.md b/modules/nixos/server/robotnix/ABOUTME.md index b5dee41..a5ffd81 100644 --- a/modules/nixos/server/robotnix/ABOUTME.md +++ b/modules/nixos/server/robotnix/ABOUTME.md @@ -1,234 +1,31 @@ -# robotnix — self-managed NixOS nspawn robots +# robotnix — adapter for the robotnix flake -`skyg.nixos.server.robotnix` runs persistent NixOS `systemd-nspawn` containers -("robots") that fully own their own system. Each robot has a real LAN IP, hard -disk limits, btrfs snapshots, and network access the host controls completely. +robotnix (self-managed NixOS `systemd-nspawn` robots) lives in its own repo: +`git+ssh://gitea@gitea.app.internal:22/ari/robotnix.git`, a flake input of +this repo. Its README and `docs/` describe the design, options, CLI and +tests. Hosts configure it under `services.robotnix` (like `services.hermenix`). -## Model +This folder only holds `default.nix`, which: -- **The robot owns its system.** On first install (`robotnix--seed.service`) - the host builds a starting system with the same entry point the robot's own - flake uses (`lib.nixosSystem` = eval-config with `system = null`), installs it - into `root.img` with `nixos-install --no-bootloader`, and writes - `/etc/nixos/{flake.nix,robotnix.nix,configuration.nix}`. After that the host - never touches the robot's root: it rebuilds with - `nixos-rebuild switch --flake /etc/nixos` against its own pinned nixpkgs and - its own store. Changes survive reboots. -- **`robotnix.nix` is host-owned** (network, user, ssh keys, git-sync). - `configuration.nix` is the robot's own config (`seed.configuration`, default - `./seed/configuration.nix`). Changing a seed config only matters at the next - `robotnix reseed`. -- **No host root.** Robots run with `--private-users=pick` unless - `privateUsers = false`. +- imports `robotnix.nixosModules.default` (passed in via `specialArgs`); +- defaults `user`, `authorizedKeys`, `dnsUpstreams` and `localDomains` from + the `skyg` user, the SSH master keys and the internal networking map; +- asserts no robot address clashes with an app in + `skyg.internalNetworkingMap.apps`. -## Storage +## Decisions -Per robot under `//` (btrfs subvolumes, `nodatacow` images): - -``` -root/root.img 100G (default) sparse ext4, whole OS incl. /nix -home/home.img 60G (default) sparse ext4, /home -snapshots//{root,home} read-only subvolume snapshots -``` - -The host mounts the images at `/run/robotnix//` and passes them in, so a -snapshot freezes both filesystems (`fsfreeze`) for a consistent point in time -while the robot runs. Sizes are hard limits; raise them and the images grow on -the next restart (shrinking is not supported). - -Snapshots: per-robot `snapshots.enable/interval/keep` via `skyg.server.timers` -(`robotnix--snapshot`), `robotnix snapshot|restore|forget|snapshots`. -`restore` saves a `pre-restore` snapshot first. Because images are `nodatacow`, -keep the automatic snapshot count modest — every live snapshot reintroduces -copy-on-write for the referenced blocks. Backups are out of scope for now. - -## Network - -One routed virtual switch: - -- `robotnix-br` bridge (networkd) on 10.78.0.1/24, unmanaged by - NetworkManager; each robot's `vb-` port is `Isolated` (no robot-to- - robot L2). -- Robots hold `/32` with a default route via the switch. The host routes - to them and answers ARP for that IP on `externalInterface` (proxy ARP per IP; - global proxy ARP stays off), so each packet passes the host firewall. -- nftables: a bridge table drops spoofed addresses; an `inet robotnix` table - 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. - -## Access tiers - -`access.shared` + `robots..access` merge per robot (`internet` from the -robot tier wins, all lists are unions). Rule order in the out-chain: - -1. established/related -2. port 53 served by the host / 853 dropped (DoT) -3. `blockedCidrs` and blocked-domain IPs (nftsets fed by dnsmasq `nftset=`) -4. `lanAccess` / `hostAccess` endpoints (host input is matched per-robot by - port-source rules) -5. RFC1918/CGNAT/link-local drop unless `allowLan` -6. internet: `all` (default) or, with `allowlist`, only `allowedCidrs` + - allowed-domain IPs - -Domain rules are IP-based: acceptable for host-controlled filtering, but a -CDN shared with an allowed domain also lets that IP's other sites through (and -dnsmasq learns current IPs). `blockedDomains` resolve to NXDOMAIN; in -allowlist mode everything not allowed is NXDOMAIN + dropped. - -## Git-sync of the robot's flake (default) - -On first install / reseed, the seed creates a git repo at `/etc/nixos` -(`git.enable`, default on) and sets `git.remoteUrl` as `origin` when given. A -timer inside the robot (`robotnix-flake-gitsync` = `git add -A`, commit, push -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 - `/pristine` before the robot first boots, plus `pristine.id` (hash of - the seed system and the files it installs). -- Before each start, `robotnix--reset.service` unmounts root, swaps - `/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..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 , 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-` (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. - -### Buzz identity for a robot's Hermes - -Each robot gets its own Buzz (Nostr) identity in its own `robots-` secret: - -1. `skyg buzz identity ` makes a key and - merges `BUZZ_RELAY_URL` / `BUZZ_PRIVATE_KEY` (and the optional `BUZZ_*` - vars) into `secrets/robots-.age`. Other lines are kept. It needs the - secret to exist and refuses to replace a key without `--rotate`. - The bot answers anyone on the relay by default; `--allowed-users ` - limits it, `--restricted` allows no one. -2. Add the printed npub to the relay: `sudo buzz-manage add-member ` on - hl-minipc1. -3. The robot needs `lanAccess` to the relay (`10.0.101.3` ports 443, 3000, - 8080, 9102; the default relay URL uses 443) and the `buzz` CLI. The robot's - nixpkgs has no host overlay, so its config builds it with - `pkgs.callPackage /pkgs/buzz-cli { }` and points `BUZZ_CLI_PATH` at it - (`services.hermes-agent.environment`), because the service PATH has no - system packages. That relative path only resolves for the host's seed - build, not for a rebuild inside the robot. Restart the robot after the - secret changes. -4. A new key has no profile, and Hermes won't connect without one - (`buzz users get returned no profile`). sibli runs a `buzz-profile` - oneshot at start that calls `buzz users set-profile` when the key has none. - -## 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: -`list`, `status`, `start/stop/restart`, `shell`, `snapshot`, -`snapshots`, `forget`, `restore`, `reseed [--wipe-home]`, `access`. -Runtime state comes from `/etc/robotnix.json`, written by the module. - -## Notes / decisions - -- **nspawn, not `oci-containers`**: the server/GUIDELINES rule about - `oci-containers` targets Docker/Podman. nspawn containers need - `systemd-nspawn` and the NixOS container init; there is no OCI equivalent. -- This is why `systemd-nspawn` is launched by a hand-written unit rather than - the `containers.*` module: that module shares the host store, which would - break the robot's self-rebuilds. The unit mirrors `nixos-containers.nix` - (`Type=notify`, SIGRTMIN+3, exit status 133, `machine.slice`). -- The robot's root gets `/usr/bin` before its first start: `systemd-nspawn` - refuses a tree without `/usr` ("doesn't look like it has an OS tree"), the - same reason `nixos-containers.nix` creates it. The seed's tail block runs on - every start, so no reseed is needed after this landed. -- `systemd-nspawn` runs in `machine.slice` (systemd's nspawn home), not - `system.slice`. -- Timers use `skyg.server.timers`; eval asserts unique robot ids/IPs, name - length ≤ 12 (interface-name limit), no clash with - `skyg.internalNetworkingMap.apps`, and no domain/CIDR both allowed and - blocked. -- `fsfreeze` fails on unfrozen ro/sparse quirks only when the images are not - mounted (robot stopped); the CLI snapshots only mounted filesystems' mounts, - otherwise it snapshots the subvolumes directly. -- flake inputs: only the host nixpkgs is pinned into the robot's flake - (`github:NixOS/nixpkgs/`). Extra inputs belong in the robot's own - `configuration.nix` edits. +- **Own repo** (2026-10): robotnix moved out of `modules/nixos/server` so it + can grow (storage backends, Genix, child robots) with its own tests. +- **Namespace**: `services.robotnix`, not `skyg.*`, same exception as + hermenix: the options belong to an external flake. +- **hermes-agent pin**: the input follows this repo's `hermes-agent`, so + robots pin the same revision the host locked. +- **Buzz stays here**: robotnix knows nothing about Buzz. A robot's seed + config (e.g. `hosts/hl-fwdesk/services/robotnix/sibli.nix`) builds + `pkgs/buzz-cli` from this repo and sets `BUZZ_CLI_PATH` itself. That + relative path works for the seed build only, which is fine for ephemeral + robots; a persistent robot that rebuilds itself would need its own copy. +- **nspawn, not `oci-containers`**: the oci-containers rule targets + Docker/Podman. nspawn robots need their own store for self-rebuilds, which + the `containers.*` module can't give. diff --git a/modules/nixos/server/robotnix/default.nix b/modules/nixos/server/robotnix/default.nix index 28929ea..34ece56 100644 --- a/modules/nixos/server/robotnix/default.nix +++ b/modules/nixos/server/robotnix/default.nix @@ -1,506 +1,35 @@ -# robotnix: self-managed NixOS nspawn containers ("robots"). -# See ABOUTME.md in this folder for the design. -{ config, lib, ... }: +# robotnix lives in its own repo (flake input `robotnix`). This adapter +# imports its host module and fills its defaults from skyg values. +# Hosts configure it under services.robotnix. See ABOUTME.md. +{ config, lib, robotnix, ... }: let - inherit (lib) mkOption mkEnableOption types; - cfg = config.skyg.nixos.server.robotnix; - h = import ./helpers.nix { inherit config lib; }; - - size = types.strMatching "[0-9]+[KMGT]"; - - proto = mkOption { - type = types.enum [ - "tcp" - "udp" - ]; - default = "tcp"; - }; - - # One access tier. Used for `access.shared` and each robot's `access`. - tier = types.submodule { - options = { - internet = mkOption { - type = types.nullOr ( - types.enum [ - "all" - "allowlist" - ] - ); - default = null; - description = '' - "all": the whole public internet (minus blocked entries). - "allowlist": only allowedDomains / allowedCidrs. - null: inherit (robot tier -> shared tier -> "all"). - ''; - }; - allowedDomains = mkOption { - type = types.listOf types.str; - default = [ ]; - example = [ "github.com" ]; - description = "Domains (and their subdomains) reachable in allowlist mode."; - }; - allowedCidrs = mkOption { - type = types.listOf types.str; - default = [ ]; - example = [ "140.82.112.0/20" ]; - description = "Public IPv4 ranges reachable in allowlist mode."; - }; - blockedDomains = mkOption { - type = types.listOf types.str; - default = [ ]; - description = "Domains that never resolve. Applies in both modes."; - }; - blockedCidrs = mkOption { - type = types.listOf types.str; - default = [ ]; - description = "Public IPv4 ranges that are always dropped. Applies in both modes."; - }; - lanAccess = mkOption { - default = [ ]; - example = [ - { - address = "10.0.101.3"; - port = 3000; - } - ]; - description = "LAN endpoints punched through the private-range drop."; - type = types.listOf ( - types.submodule { - options = { - address = mkOption { type = types.str; }; - port = mkOption { type = types.port; }; - inherit proto; - }; - } - ); - }; - hostAccess = mkOption { - default = [ ]; - example = [{ port = 11434; }]; - description = "Ports on this host the robot may reach."; - type = types.listOf ( - types.submodule { - options = { - port = mkOption { type = types.port; }; - inherit proto; - }; - } - ); - }; - allowLan = mkOption { - type = types.bool; - default = false; - description = "Open all private ranges. Almost always the wrong tool; use lanAccess."; - }; - }; - }; - - # 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, ... }: - { - options = { - id = mkOption { - type = types.ints.between 1 250; - description = "Unique number per robot. Picks its DNS port (5300 + id)."; - }; - address = mkOption { - type = types.str; - example = "10.0.101.12"; - description = "The robot's own LAN IPv4 address."; - }; - autoStart = mkOption { - type = types.bool; - default = true; - }; - privateUsers = mkOption { - type = types.bool; - default = true; - description = "Run in a user namespace (root inside != root on the host)."; - }; - user = mkOption { - type = types.str; - default = config.skyg.user.name; - description = "Login user created in the robot."; - }; - authorizedKeys = mkOption { - type = types.listOf types.str; - default = config.skyg.nixos.common.ssh-server.masterPubKeys; - description = "SSH keys for the user and root in the robot."; - }; - cpus = { - min = mkOption { - type = types.ints.positive; - default = 1; - }; - max = mkOption { - type = types.ints.positive; - default = 4; - }; - }; - memory = { - min = mkOption { - type = size; - default = "8G"; - description = "Protected memory (MemoryLow)."; - }; - max = mkOption { - type = size; - default = "16G"; - description = "Hard limit (MemoryMax)."; - }; - swapMax = mkOption { - type = size; - default = "1G"; - }; - }; - disk = { - rootSize = mkOption { - type = size; - default = "100G"; - description = "Root image: the whole OS including /nix. Grows on restart if raised."; - }; - homeSize = mkOption { - type = size; - default = "60G"; - 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 = { }; - description = "This robot's own tier, added on top of access.shared."; - }; - snapshots = { - enable = mkOption { - type = types.bool; - default = true; - }; - interval = mkOption { - type = types.str; - default = "hourly"; - description = "systemd calendar expression."; - }; - keep = mkOption { - type = types.ints.positive; - default = 24; - description = "Automatic snapshots to keep. Manual ones are never pruned."; - }; - }; - git = { - enable = mkOption { - type = types.bool; - default = true; - description = "Track /etc/nixos in git on first install and auto-commit."; - }; - remoteUrl = mkOption { - type = types.nullOr types.str; - default = null; - 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. - ''; - }; - userName = mkOption { - type = types.str; - default = "robotnix"; - }; - userEmail = mkOption { - type = types.str; - default = "robotnix@localhost"; - }; - interval = mkOption { - type = types.str; - default = "daily"; - description = "systemd calendar for the auto-commit (and push, if a remote is set) timer."; - }; - }; - seed = { - configuration = mkOption { - type = types.path; - default = ./seed/configuration.nix; - description = '' - The robot's starting configuration.nix. Copied into /etc/nixos on - first install and on `robotnix reseed`; after that the robot owns it. - ''; - }; - stateVersion = mkOption { - type = types.str; - default = lib.trivial.release; - }; - }; - }; - } - ); + cfg = config.services.robotnix; + apps = config.skyg.internalNetworkingMap.apps; in { - imports = [ - ./network.nix - ./robots.nix - ]; + imports = [ robotnix.nixosModules.default ]; - options.skyg.nixos.server.robotnix = { - enable = mkEnableOption "robotnix self-managed NixOS containers"; - - externalInterface = mkOption { - type = types.nullOr types.str; - default = null; - example = "enp191s0"; - description = "Host LAN uplink. Robots' LAN IPs are proxy-ARPed here."; - }; - - baseDir = mkOption { - type = types.str; - default = "/var/lib/robotnix"; - description = "Must be on btrfs. Each robot gets / as its own subvolume."; - }; - - switch = { - name = mkOption { - type = types.str; - default = "robotnix-br"; - }; - address = mkOption { - type = types.str; - default = "10.78.0.1"; - description = "Host address on the switch: robots' gateway and DNS."; - }; - prefixLength = mkOption { - type = types.int; - default = 24; - }; - }; - - dnsUpstreams = mkOption { - type = types.listOf types.str; - default = [ config.skyg.internalNetworkingMap.appNetwork.gateway ]; - description = "Where the host's per-robot DNS forwards to."; - }; - - localDomains = mkOption { - type = types.listOf types.str; - default = [ + config = lib.mkIf cfg.enable { + services.robotnix = { + user = lib.mkDefault config.skyg.user.name; + authorizedKeys = lib.mkDefault config.skyg.nixos.common.ssh-server.masterPubKeys; + dnsUpstreams = lib.mkDefault [ config.skyg.internalNetworkingMap.appNetwork.gateway ]; + localDomains = lib.mkDefault [ "app.internal" "lab.internal" ]; - description = "Always resolvable, even in allowlist mode (reaching them still needs lanAccess)."; }; - access.shared = mkOption { - type = tier; - default = { }; - 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 = { }; - }; - }; - - config = lib.mkIf cfg.enable { - assertions = - let - rs = h.robotList; - dupes = f: lib.filter (v: lib.count (x: x == v) (map f rs) > 1) (lib.unique (map f rs)); - apps = config.skyg.internalNetworkingMap.apps; - in - [ + # A robot's address must not clash with an app from the network map. + assertions = lib.mapAttrsToList + (name: r: + let + clash = lib.attrNames (lib.filterAttrs (an: a: a.ip == r.address && an != name) apps); + in { - assertion = cfg.externalInterface != null; - message = "skyg.nixos.server.robotnix.externalInterface must be set."; - } - { - assertion = dupes (r: r.id) == [ ]; - message = "robotnix: robot ids must be unique (dupes: ${toString (dupes (r: r.id))})."; - } - { - assertion = dupes (r: r.address) == [ ]; - message = "robotnix: robot addresses must be unique (dupes: ${toString (dupes (r: r.address))})."; - } - ] - ++ lib.concatMap - ( - r: - let - clash = lib.attrNames (lib.filterAttrs (an: a: a.ip == r.address && an != r.name) apps); - both = a: b: lib.intersectLists r.access.${a} r.access.${b}; - in - [ - { - assertion = builtins.match "[a-z][a-z0-9-]{0,11}" r.name != null; - message = "robotnix: robot name '${r.name}' must be lowercase, start with a letter, max 12 chars."; - } - { - assertion = clash == [ ]; - message = "robotnix: ${r.name}'s address ${r.address} is already used by app(s): ${toString clash}."; - } - { - assertion = both "allowedDomains" "blockedDomains" == [ ]; - message = "robotnix: ${r.name} both allows and blocks: ${toString (both "allowedDomains" "blockedDomains")}."; - } - { - 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."; - } - { - assertion = h.toBytes r.memory.min <= h.toBytes r.memory.max; - message = "robotnix: ${r.name} memory.min must be <= memory.max."; - } - ] - ) - rs; + assertion = clash == [ ]; + message = "robotnix: ${name}'s address ${r.address} is already used by app(s): ${toString clash}."; + }) + cfg.robots; }; } diff --git a/modules/nixos/server/robotnix/helpers.nix b/modules/nixos/server/robotnix/helpers.nix deleted file mode 100644 index e2226f0..0000000 --- a/modules/nixos/server/robotnix/helpers.nix +++ /dev/null @@ -1,125 +0,0 @@ -# Shared values for network.nix, robots.nix and the CLI. -{ config, lib }: -let - cfg = config.skyg.nixos.server.robotnix; - - # "16G" -> bytes (IEC, same as systemd and truncate). - toBytes = - s: - let - m = builtins.match "([0-9]+)([KMGT])" s; - mult = { - K = 1024; - M = 1024 * 1024; - G = 1024 * 1024 * 1024; - T = 1024 * 1024 * 1024 * 1024; - }; - in - lib.toInt (builtins.elemAt m 0) * mult.${builtins.elemAt m 1}; - - # nft identifiers can't hold "-". - nftName = name: lib.replaceStrings [ "-" ] [ "_" ] name; - - # Render a string as a Nix string literal (for generated .nix files). - nixStr = s: lib.replaceStrings [ "\${" ] [ "\\\${" ] (builtins.toJSON s); - - # Shared tier + the robot's own tier. Lists are merged, the robot's - # `internet` mode wins when set. - mergeAccess = - r: - let - s = cfg.access.shared; - both = f: lib.unique (f s ++ f r); - in - { - internet = - if r.internet != null then - r.internet - else if s.internet != null then - s.internet - else - "all"; - allowLan = s.allowLan || r.allowLan; - allowedDomains = both (t: t.allowedDomains); - allowedCidrs = both (t: t.allowedCidrs); - blockedDomains = both (t: t.blockedDomains); - blockedCidrs = both (t: t.blockedCidrs); - lanAccess = both (t: t.lanAccess); - hostAccess = both (t: t.hostAccess); - }; - - # 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 -{ - inherit - toBytes - nftName - nixStr - robots - hermesHome - ; - robotList = lib.attrValues robots; -} diff --git a/modules/nixos/server/robotnix/network.nix b/modules/nixos/server/robotnix/network.nix deleted file mode 100644 index 0b634c1..0000000 --- a/modules/nixos/server/robotnix/network.nix +++ /dev/null @@ -1,304 +0,0 @@ -# robotnix network: one routed virtual switch, per-robot firewall and DNS. -# -# Robots plug into the switch bridge and hold their LAN IP as a /32. The host -# routes to them and answers ARP for their IPs on the uplink (proxy ARP), so -# every packet passes the host firewall. Robots never see the host's netns. -{ config -, lib -, pkgs -, ... -}: -let - cfg = config.skyg.nixos.server.robotnix; - h = import ./helpers.nix { inherit config lib; }; - rs = h.robotList; - br = cfg.switch.name; - sw = cfg.switch.address; - - set = xs: "{ ${lib.concatStringsSep ", " xs} }"; - - # Never reachable, whatever the tier says. - neverRanges = [ - "0.0.0.0/8" - "127.0.0.0/8" - "224.0.0.0/4" - "240.0.0.0/4" - ]; - # Private ranges: only via lanAccess / allowLan. - lanRanges = [ - "10.0.0.0/8" - "172.16.0.0/12" - "192.168.0.0/16" - "169.254.0.0/16" - "100.64.0.0/10" - ]; - - # Outbound (forwarded) traffic from one robot. Order matters. - outChain = - r: - let - a = r.access; - in - '' - chain out_${r.nft} { - # DNS only via the host (port 53 is redirected in prerouting). - meta l4proto { tcp, udp } th dport 853 drop - meta l4proto { tcp, udp } th dport 53 drop - # The redirected DNS request (now dport = the robot's dnsmasq port) - # must pass before the private-range drop. - ip daddr ${sw} meta l4proto { tcp, udp } th dport ${toString r.dnsPort} accept - ip daddr ${set neverRanges} drop - '' - + lib.optionalString (a.blockedCidrs != [ ]) '' - ip daddr ${set a.blockedCidrs} drop - '' - + lib.concatMapStrings - (e: '' - ip daddr ${e.address} ${e.proto} dport ${toString e.port} accept - '') - a.lanAccess - + ( - if a.allowLan then - '' - ip daddr ${set lanRanges} accept - '' - else - '' - ip daddr ${set lanRanges} drop - '' - ) - + ( - if a.internet == "all" then - '' - accept - '' - else - lib.optionalString (a.allowedCidrs != [ ]) '' - ip daddr ${set a.allowedCidrs} accept - '' - + '' - ip daddr @allow_${r.nft} accept - drop - '' - ) - + '' - } - ''; - - # Traffic from one robot to the host itself. - inChain = r: '' - chain in_${r.nft} { - meta l4proto { tcp, udp } th dport ${toString r.dnsPort} accept - icmp type echo-request accept - '' - + lib.concatMapStrings - (p: '' - ${p.proto} dport ${toString p.port} accept - '') - r.access.hostAccess - + '' - drop - } - ''; - - 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: - let - a = r.access; - servers = d: lib.concatMapStrings (u: "server=/${d}/${u}\n") cfg.dnsUpstreams; - in - pkgs.writeText "robotnix-${r.name}-dnsmasq.conf" ( - '' - listen-address=${sw} - port=${toString r.dnsPort} - bind-dynamic - no-resolv - no-hosts - no-poll - domain-needed - bogus-priv - user=root - cache-size=1000 - # Short TTLs so allowlist IP sets stay fresh after a host reload. - max-ttl=300 - max-cache-ttl=300 - '' - + lib.concatMapStrings servers cfg.localDomains - + ( - if a.internet == "all" then - lib.concatMapStrings (u: "server=${u}\n") cfg.dnsUpstreams - else - '' - # Allowlist: everything else is NXDOMAIN. - address=/#/ - '' - + lib.concatMapStrings - ( - d: servers d + "nftset=/${d}/4#inet#robotnix#allow_${r.nft}\n" - ) - a.allowedDomains - ) - + lib.concatMapStrings (d: "address=/${d}/\n") a.blockedDomains - ); -in -lib.mkIf cfg.enable { - # Same keys and priority networking.nat (and hermenix) use. - boot.kernel.sysctl = { - "net.ipv4.conf.all.forwarding" = lib.mkOverride 99 true; - "net.ipv4.conf.default.forwarding" = lib.mkOverride 99 true; - }; - - # ---------------------------------------------------------------- switch - systemd.network.enable = true; - systemd.network.netdevs."40-${br}".netdevConfig = { - Kind = "bridge"; - Name = br; - }; - systemd.network.networks."40-${br}" = { - matchConfig.Name = br; - address = [ "${sw}/${toString cfg.switch.prefixLength}" ]; - routes = map - (r: { - Destination = "${r.address}/32"; - Scope = "link"; - }) - rs; - networkConfig = { - ConfigureWithoutCarrier = true; - LinkLocalAddressing = "no"; - IPv6AcceptRA = false; - }; - linkConfig.RequiredForOnline = "no"; - }; - # Robot ports (nspawn names them vb-). Isolated: robots can't talk - # to each other at L2, only through the host's rules. - systemd.network.networks."41-${br}-ports" = lib.mkIf (rs != [ ]) { - matchConfig.Name = lib.concatMapStringsSep " " (r: r.port) rs; - networkConfig = { - Bridge = br; - LinkLocalAddressing = "no"; - }; - bridgeConfig.Isolated = true; - linkConfig.RequiredForOnline = "no"; - }; - networking.networkmanager.unmanaged = [ "interface-name:${br}" ] ++ map (r: "interface-name:${r.port}") rs; - - # -------------------------------------------------------------- firewall - networking.nftables.enable = true; - - # L2: each port may only speak IPv4/ARP from its own address. - networking.nftables.tables.robotnix-bridge = lib.mkIf (rs != [ ]) { - family = "bridge"; - content = '' - chain prerouting { - type filter hook prerouting priority filter; policy accept; - '' - + lib.concatMapStrings - (r: '' - iifname "${r.port}" ether type != { ip, arp } drop - iifname "${r.port}" ip saddr != ${r.address} drop - iifname "${r.port}" arp saddr ip != ${r.address} drop - '') - rs - + '' - } - ''; - }; - - # L3. Runs before nixos-fw, so a drop here is final no matter what other - # modules accept. Source addresses are trusted thanks to the bridge table. - networking.nftables.tables.robotnix = lib.mkIf (rs != [ ]) { - family = "inet"; - content = '' - ${lib.concatMapStrings (r: '' - set allow_${r.nft} { type ipv4_addr; } - '') rs} - - chain prerouting { - type nat hook prerouting priority dstnat; policy accept; - ${lib.concatMapStrings (r: '' - iifname "${br}" ip saddr ${r.address} meta l4proto { tcp, udp } th dport 53 dnat ip to ${sw}:${toString r.dnsPort} - '') rs} - } - - 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. - } - - 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 - ip saddr vmap ${vmap "in"} - drop - } - - ${lib.concatMapStrings outChain rs} - ${lib.concatMapStrings inChain rs} - ''; - }; - - # The robotnix table already filtered; let nixos-fw pass the rest. - networking.firewall.extraInputRules = '' - iifname "${br}" accept - ''; - networking.firewall.extraForwardRules = '' - iifname "${br}" accept - oifname "${br}" accept - ''; - - # ------------------------------------------------------------------- DNS - systemd.services = lib.listToAttrs ( - map - ( - r: - lib.nameValuePair "${r.unit}-dns" { - description = "robotnix DNS for ${r.name}"; - wantedBy = [ "multi-user.target" ]; - after = [ - "network.target" - "nftables.service" - ]; - serviceConfig = { - ExecStart = "${pkgs.dnsmasq}/bin/dnsmasq --keep-in-foreground --pid-file --conf-file=${dnsConf r}"; - Restart = "always"; - RestartSec = 2; - # Root only for nftset; everything else is locked down. - CapabilityBoundingSet = [ - "CAP_NET_ADMIN" - "CAP_NET_BIND_SERVICE" - "CAP_NET_RAW" - "CAP_SETUID" - "CAP_SETGID" - ]; - ProtectSystem = "strict"; - ProtectHome = true; - PrivateTmp = true; - PrivateDevices = true; - NoNewPrivileges = true; - }; - } - ) - rs - ); -} diff --git a/modules/nixos/server/robotnix/robotnix.nu b/modules/nixos/server/robotnix/robotnix.nu deleted file mode 100644 index 6d84722..0000000 --- a/modules/nixos/server/robotnix/robotnix.nu +++ /dev/null @@ -1,270 +0,0 @@ - -# robotnix: manage robotnix robots (self-managed NixOS nspawn containers). -# CONFIG and PATH are set by the Nix wrapper (robots.nix) above this line. - -def cfg [] { open $CONFIG } - -def robot [name: string] { - let robots = (cfg).robots - if $name not-in ($robots | columns) { - error make { msg: $"unknown robot '($name)'. Known: ($robots | columns | str join ', ')" } - } - $robots | get $name -} - -def need-root [] { - if not (is-admin) { error make { msg: "this needs root: run it with sudo" } } -} - -def units [r: record] { - { - robot: $"($r.unit).service" - storage: $"($r.unit)-storage.service" - seed: $"($r.unit)-seed.service" - dns: $"($r.unit)-dns.service" - } -} - -def unit-state [unit: string] { - ^systemctl is-active $unit | complete | get stdout | str trim -} - -def is-mounted [dir: string] { - (^mountpoint -q $dir | complete).exit_code == 0 -} - -def disk-usage [dir: string] { - if not (is-mounted $dir) { return "-" } - let f = (^df -B1 --output=used,size $dir | lines | last | str trim | split row -r '\s+') - $"($f.0 | into filesize) / ($f.1 | into filesize)" -} - -def stamp [] { date now | format date "%Y-%m-%dT%H-%M-%S" } - -def confirm-name [name: string, what: string] { - print $what - let answer = (input $"Type '($name)' to continue: ") - if $answer != $name { error make { msg: "aborted" } } -} - -# Stop the robot and unmount its images (seed and robot are bound to storage). -def stop-robot [r: record] { - let u = (units $r) - ^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 "-", e.g. 2026-10-23T14-00-00-auto. -def snapshot-list [r: record] { - let dir = $"($r.base)/snapshots" - if not ($dir | path exists) { return [] } - ls $dir - | where type == dir - | each {|e| $e.name | path basename } - | sort - | each {|id| - let p = ($id | parse -r '^(?\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2})-(?.+)$') - if ($p | is-empty) { - { id: $id, kind: "?", created: "?" } - } else { - { id: $id, kind: $p.0.kind, created: ($p.0.stamp | str replace 'T' ' ') } - } - } -} - -# 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 = (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 (parts $r) { - ^btrfs subvolume snapshot -r $"($r.base)/($sub)" $"($dest)/($sub)" - } - null - } catch {|e| $e.msg }) - for m in $mounts { do -i { ^fsfreeze -u $m } } - if $err != null { error make { msg: $"snapshot of ($r.name) failed: ($err)" } } - $id -} - -def delete-snapshot [r: record, id: string] { - let d = $"($r.base)/snapshots/($id)" - if not ($d | path exists) { error make { msg: $"no snapshot '($id)' for ($r.name)" } } - for sub in [root home] { - if ($"($d)/($sub)" | path exists) { ^btrfs subvolume delete $"($d)/($sub)" } - } - ^rmdir $d -} - -# Keep only the newest `keep` automatic snapshots. Others are never pruned. -def prune [r: record] { - let autos = (snapshot-list $r | where kind == "auto" | each {|s| $s.id }) - let extra = ($autos | length) - $r.snapshots.keep - if $extra > 0 { - for id in ($autos | first $extra) { delete-snapshot $r $id } - } -} - -# Manage robotnix robots. Run `robotnix --help` for details. -def main [] { - print "robotnix - self-managed NixOS nspawn robots - - list all robots: state, IP, disk use - status unit status - start|stop|restart - shell [--root] log in without ssh (machinectl) - snapshot [--tag t] take a snapshot now - snapshots list snapshots - forget delete a snapshot - restore [--only root|home] - reseed [--wipe-home] reinstall from the seed config - access effective network rules" -} - -# All robots with state, address and disk use. -def "main list" [] { - (cfg).robots | transpose name r | each {|row| - let r = $row.r - { - name: $row.name - state: (unit-state $"($r.unit).service") - address: $r.address - internet: $r.access.internet - root: (disk-usage $"($r.run)/root") - home: (disk-usage $"($r.run)/home") - snapshots: (snapshot-list $r | length) - ephemeral: $r.ephemeral - } - } -} - -# systemd status of a robot and its helper units. -def "main status" [name: string] { - let u = (units (robot $name)) - do -i { ^systemctl status --no-pager --lines=10 $u.robot $u.dns $u.storage } -} - -def "main start" [name: string] { - need-root - ^systemctl start (units (robot $name)).robot -} - -def "main stop" [name: string] { - need-root - ^systemctl stop (units (robot $name)).robot -} - -def "main restart" [name: string] { - need-root - ^systemctl restart (units (robot $name)).robot -} - -# Log into a robot without ssh. Works even if its network is broken. -def "main shell" [name: string, --root] { - let r = (robot $name) - let who = if $root { "root" } else { $r.user } - ^machinectl shell $"($who)@($r.name)" -} - -# Take a consistent snapshot of the robot's root and home. -def "main snapshot" [ - name: string - --tag: string = "manual" # label for the snapshot id - --auto # timer mode: tag "auto" and prune old ones -] { - need-root - let r = (robot $name) - let kind = if $auto { "auto" } else { $tag } - if not ($kind =~ '^[a-z0-9-]+$') { error make { msg: "--tag may only use a-z, 0-9 and -" } } - let id = (take-snapshot $r $kind) - if $auto { prune $r } - print $"snapshot ($id) taken for ($name)" -} - -# List a robot's snapshots. -def "main snapshots" [name: string] { - snapshot-list (robot $name) -} - -# Delete one snapshot. -def "main forget" [name: string, id: string] { - need-root - delete-snapshot (robot $name) $id - print $"deleted snapshot ($id) of ($name)" -} - -# Roll a robot back to a snapshot. The current state is saved first. -def "main restore" [ - name: string - id: string - --only: string # "root" or "home" (default: both) - --yes # skip the confirmation -] { - need-root - let r = (robot $name) - let subs = if $only == null { - parts $r - } else if $only in (parts $r) { - [$only] - } else { - error make { msg: $"--only must be one of: (parts $r | str join ', ')" } - } - let src = $"($r.base)/snapshots/($id)" - for sub in $subs { - if not ($"($src)/($sub)" | path exists) { error make { msg: $"snapshot ($id) has no ($sub)" } } - } - if not $yes { - confirm-name $name $"This replaces ($name)'s ($subs | str join ' and ') with snapshot ($id).\nThe current state is kept as a pre-restore snapshot." - } - stop-robot $r - let saved = (take-snapshot $r "pre-restore") - for sub in $subs { - ^btrfs subvolume delete $"($r.base)/($sub)" - ^btrfs subvolume snapshot $"($src)/($sub)" $"($r.base)/($sub)" - } - ^systemctl start (units $r).robot - print $"($name) restored to ($id). Undo with: robotnix restore ($name) ($saved)" -} - -# Wipe a robot's OS and reinstall it from its seed config. -def "main reseed" [ - name: string - --wipe-home # also start with an empty /home - --yes # skip the confirmation -] { - need-root - let r = (robot $name) - let what = if $wipe_home { "root AND home" } else { "root (home is kept)" } - if not $yes { - confirm-name $name $"This wipes ($name)'s ($what) and reinstalls it from its seed config.\nA pre-reseed snapshot is taken first." - } - 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)" -} - -# Show a robot's effective (shared + own) network rules. -def "main access" [name: string] { - let r = (robot $name) - print ($r.access | table --expand) - if $r.access.internet == "allowlist" { - print "IPs learned from allowed domains:" - do -i { ^nft list set inet robotnix $"allow_($r.nft)" } - } -} diff --git a/modules/nixos/server/robotnix/robots.nix b/modules/nixos/server/robotnix/robots.nix deleted file mode 100644 index 8743c5e..0000000 --- a/modules/nixos/server/robotnix/robots.nix +++ /dev/null @@ -1,622 +0,0 @@ -# robotnix robots: storage, first install, nspawn units, snapshots, CLI. -# -# Layout per robot (all btrfs subvolumes): -# //root/root.img whole OS incl. /nix (hard size limit) -# //home/home.img /home (hard size limit) -# //snapshots//{root,home} -# The host mounts the images under /run/robotnix// so it can freeze -# them for consistent snapshots while the robot runs. -{ config -, lib -, pkgs -, hermes-agent -, ... -}: -let - cfg = config.skyg.nixos.server.robotnix; - h = import ./helpers.nix { inherit config lib; }; - rs = h.robotList; - sw = cfg.switch.address; - br = cfg.switch.name; - ext = cfg.externalInterface; - hostSystem = pkgs.stdenv.hostPlatform.system; - - # The robot's flake pins the same nixpkgs the host used to build its seed. - nixpkgsRef = - let - rev = config.system.nixos.revision; - in - 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 - ) "time.timeZone = ${h.nixStr config.time.timeZone};"; - - # 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). - # The network and git-sync values must match the host's robotnix - # settings; change them and this robot loses its network or its - # auto-commits stop. Everything else is yours. - { lib, pkgs, ... }: - { - boot.isContainer = true; - nixpkgs.hostPlatform = ${h.nixStr hostSystem}; - networking.hostName = ${h.nixStr r.name}; - - # Address and route handed out by the host. IPv4 only. - networking.useDHCP = false; - networking.useNetworkd = true; - networking.enableIPv6 = false; - networking.useHostResolvConf = lib.mkForce false; - networking.nameservers = [ ${h.nixStr sw} ]; - systemd.network.networks."10-host0" = { - matchConfig.Name = "host0"; - address = [ ${h.nixStr "${r.address}/32"} ]; - routes = [ { Gateway = ${h.nixStr sw}; GatewayOnLink = true; } ]; - networkConfig.LinkLocalAddressing = "no"; - }; - - nix.settings.experimental-features = [ "nix-command" "flakes" ]; - - services.openssh = { - enable = true; - settings.PasswordAuthentication = false; - settings.KbdInteractiveAuthentication = false; - }; - users.users.${h.nixStr r.user} = { - isNormalUser = true; - 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 ]; - systemd.services."robotnix-flake-gitsync" = { - path = [ pkgs.git ]; - script = ''' - cd /etc/nixos - git add -A - git diff --cached --quiet || git commit -m "robotnix auto-sync $(date +'%Y-%m-%d %H:%M:%S')" - if git remote get-url origin >/dev/null 2>&1; then - git push -q - fi - '''; - serviceConfig = { - Type = "oneshot"; - User = "root"; - }; - }; - systemd.timers."robotnix-flake-gitsync" = { - wantedBy = [ "timers.target" ]; - timerConfig = { - OnCalendar = ${h.nixStr r.git.interval}; - Persistent = true; - Unit = "robotnix-flake-gitsync.service"; - }; - }; - ''} - } - ''; - - flakeNix = - r: - builtins.toFile "robotnix-${r.name}-flake.nix" '' - # ${r.name}'s own flake. Apply changes with: - # sudo nixos-rebuild switch --flake /etc/nixos - { - inputs.nixpkgs.url = "github:NixOS/nixpkgs/${nixpkgsRef}"; - ${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 - ${lib.optionalString r.hermes.enable "inputs.hermes-agent.nixosModules.default"} - ]; - }; - }; - } - ''; - - # The first system, built on the host exactly as the robot's own flake - # builds it later: lib.nixosSystem is eval-config with system = null and - # nixpkgs' lib, so this mirrors flake.nix's `nixpkgs.lib.nixosSystem` call. - seedSystem = - r: - (import "${pkgs.path}/nixos/lib/eval-config.nix" { - system = null; - inherit lib; - 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" - '' - + 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=$1 size=$2 dir=$3 - if [ ! -e "$img" ]; then - truncate -s "$size" "$img" - mkfs.ext4 -q -F "$img" - fi - mkdir -p -m 0755 "$dir" - if ! mountpoint -q "$dir"; then - rc=0 - e2fsck -p "$img" || rc=$? - if [ "$rc" -ge 4 ]; then - echo "robotnix: $img needs a manual e2fsck" >&2 - exit 1 - fi - mount -o loop,noatime "$img" "$dir" - fi - # Grow when the configured size goes up. Shrinking is not supported. - want=$(numfmt --from=iec "$size") - have=$(stat -c %s "$img") - if [ "$want" -gt "$have" ]; then - truncate -s "$size" "$img" - dev=$(findmnt -n -o SOURCE "$dir") - losetup -c "$dev" - resize2fs "$dev" - elif [ "$want" -lt "$have" ]; then - echo "robotnix: $img is bigger than $size; shrinking is not supported" >&2 - fi - } - - mkdir -p /run/robotnix ${r.run} - chmod 0755 /run/robotnix ${r.run} - 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: - pkgs.writeShellScript "${r.unit}-storage-stop" '' - for d in ${r.run}/home ${r.run}/root; do - if mountpoint -q "$d"; then umount "$d"; fi - done - ''; - - # 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} - ''} - 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 - 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 = - r: - pkgs.writeShellScript "${r.unit}-pre" '' - set -euo pipefail - for _ in $(seq 1 30); do - [ -e /sys/class/net/${br} ] && [ -e /sys/class/net/${ext} ] && break - sleep 1 - done - [ -e /sys/class/net/${br} ] || { echo "robotnix: ${br} is missing" >&2; exit 1; } - # Answer ARP for the robot's LAN IP on the uplink (this IP only). - ip neigh del proxy ${r.address} dev ${ext} 2>/dev/null || true - ip neigh add proxy ${r.address} dev ${ext} - ''; - - postStop = - r: - pkgs.writeShellScript "${r.unit}-post" '' - ip neigh del proxy ${r.address} dev ${ext} 2>/dev/null || true - ''; - - idmap = r: lib.optionalString r.privateUsers ":idmap"; - - nspawnArgs = - r: - lib.escapeShellArgs ( - [ - "--keep-unit" - "--machine=${r.name}" - "--directory=${r.run}/root" - "--notify-ready=yes" - "--kill-signal=SIGRTMIN+3" - "--network-bridge=${br}" - "--link-journal=try-guest" - "--resolv-conf=off" - "--timezone=off" - "--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" ] - ); - - robotUnits = r: { - "${r.unit}-storage" = { - description = "robotnix storage for ${r.name}"; - path = with pkgs; [ - btrfs-progs - coreutils - e2fsprogs - util-linux - ]; - serviceConfig = { - Type = "oneshot"; - RemainAfterExit = true; - ExecStart = storageStart r; - ExecStop = storageStop r; - }; - }; - - "${r.unit}-seed" = { - description = "robotnix first install for ${r.name}"; - requires = [ "${r.unit}-storage.service" ]; - bindsTo = [ "${r.unit}-storage.service" ]; - after = [ "${r.unit}-storage.service" ]; - path = [ - config.system.build.nixos-install - config.nix.package - pkgs.coreutils - pkgs.util-linux - pkgs.git - pkgs.btrfs-progs - ]; - environment.HOME = "/root"; - serviceConfig = { - Type = "oneshot"; - RemainAfterExit = true; - ExecStart = seedScript r; - TimeoutStartSec = "30min"; - }; - }; - - # 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; - deps = [ - "${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}"; - wantedBy = lib.optional r.autoStart "machines.target"; - requires = deps; - bindsTo = [ "${r.unit}-storage.service" ]; - after = deps ++ [ - "network.target" - "systemd-networkd.service" - "nftables.service" - ]; - path = [ - pkgs.iproute2 - pkgs.coreutils - ]; - environment.SYSTEMD_NSPAWN_UNIFIED_HIERARCHY = "1"; - serviceConfig = { - ExecStartPre = preStart r; - ExecStart = "${config.systemd.package}/bin/systemd-nspawn ${nspawnArgs r}"; - # Best effort: refresh LAN ARP caches. - ExecStartPost = "-${pkgs.iputils}/bin/arping -U -c 2 -I ${ext} ${r.address}"; - ExecStopPost = postStop r; - - # Same as nixos-containers. - Type = "notify"; - RestartForceExitStatus = "133"; - SuccessExitStatus = "133"; - Restart = "on-failure"; - TimeoutStartSec = "5min"; - Slice = "machine.slice"; - Delegate = true; - KillMode = "mixed"; - KillSignal = "TERM"; - DevicePolicy = "closed"; - DeviceAllow = lib.mkIf r.allowTun [ "/dev/net/tun rwm" ]; - - # Resources. - CPUQuota = "${toString (r.cpus.max * 100)}%"; - CPUWeight = toString (lib.min 10000 (100 * r.cpus.min)); - MemoryLow = r.memory.min; - MemoryHigh = toString (mem - mem / 16); - MemoryMax = r.memory.max; - MemorySwapMax = r.memory.swapMax; - TasksMax = 16384; - }; - }; - }; - - cliConfig = pkgs.writeText "robotnix.json" ( - builtins.toJSON { - inherit (cfg) baseDir; - robots = lib.mapAttrs - (_: r: { - inherit (r) - name - address - port - dnsPort - base - run - unit - user - nft - access - ephemeral - ; - snapshots = { inherit (r.snapshots) enable interval keep; }; - }) - h.robots; - } - ); - - cliPath = lib.makeBinPath [ - pkgs.btrfs-progs - pkgs.util-linux - pkgs.coreutils - pkgs.nftables - config.systemd.package - ]; - - cli = pkgs.writers.writeNuBin "robotnix" ( - '' - const CONFIG = "${cliConfig}" - $env.PATH = ("${cliPath}" | split row ":" | append ($env.PATH | split row (char esep))) - '' - + builtins.readFile ./robotnix.nu - ); -in -lib.mkIf cfg.enable { - systemd.services = lib.mkMerge (map robotUnits rs); - - systemd.targets.multi-user.wants = [ "machines.target" ]; - - # Memory protection only works if the parent slice is protected too. - systemd.slices.machine.sliceConfig.MemoryLow = lib.mkIf (rs != [ ]) ( - lib.mkDefault (toString (lib.foldl' (acc: r: acc + h.toBytes r.memory.min) 0 rs)) - ); - - skyg.server.timers = lib.listToAttrs ( - map - ( - r: - lib.nameValuePair "${r.unit}-snapshot" { - script = "${cli}/bin/robotnix snapshot ${r.name} --auto"; - OnCalendar = r.snapshots.interval; - } - ) - (lib.filter (r: r.snapshots.enable) rs) - ); - - environment.systemPackages = [ cli ]; -} diff --git a/modules/nixos/server/robotnix/seed/configuration.nix b/modules/nixos/server/robotnix/seed/configuration.nix deleted file mode 100644 index 6056f1d..0000000 --- a/modules/nixos/server/robotnix/seed/configuration.nix +++ /dev/null @@ -1,24 +0,0 @@ -# A robot's own NixOS config (default seed). -# -# 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 - ]; - - nix.gc = { - automatic = true; - dates = "weekly"; - options = "--delete-older-than 14d"; - }; -} diff --git a/pkgs/buzz-cli/default.nix b/pkgs/buzz-cli/default.nix index 28f6b10..ec0a2f5 100644 --- a/pkgs/buzz-cli/default.nix +++ b/pkgs/buzz-cli/default.nix @@ -23,7 +23,7 @@ rustPlatform.buildRustPackage rec { }; # The workspace has git deps in [patch.crates-io]; cargoHash covers them. - cargoHash = lib.fakeHash; + cargoHash = "sha256-UQ8qNqoeDwkwhmVZebi++gb2GP61dr1Zv2QU9mJ6KJ8="; # Only this crate; the workspace has 30+ others (relay, db, ...). cargoBuildFlags = [ "-p" "buzz-cli" ]; diff --git a/pkgs/buzz-cli/update.sh b/pkgs/buzz-cli/update.sh index 193cdf6..ae394d4 100755 --- a/pkgs/buzz-cli/update.sh +++ b/pkgs/buzz-cli/update.sh @@ -24,6 +24,12 @@ else echo "Latest: $VERSION" fi +CURRENT=$(sed -n 's/^ *version = "\(.*\)";/\1/p' default.nix | head -n1) +if [ "$CURRENT" = "$VERSION" ]; then + echo "✅ ${PKG} already at ${VERSION}" + exit 0 +fi + HASH=$(nix flake prefetch "github:${REPO}/desktop-v${VERSION}" --json | jq -r '.hash') if [ -z "$HASH" ] || [ "$HASH" = "null" ]; then echo "Could not prefetch tag desktop-v${VERSION}" diff --git a/pkgs/delta-editor/default.nix b/pkgs/delta-editor/default.nix index 3a7e8b8..764fc11 100644 --- a/pkgs/delta-editor/default.nix +++ b/pkgs/delta-editor/default.nix @@ -29,7 +29,7 @@ let pname = "delta-editor"; - version = "0.21.0"; + version = "0.22.0"; apiUrl = "https://delta.dev/api/releases/nightly/${version}/asset?asset=delta&os=linux&arch=x86_64"; @@ -38,7 +38,7 @@ let inherit version; outputHashMode = "flat"; - outputHash = "sha256-lN5oSycdw1n3AswiGcsB4/jR481noJ5zv3qay5CTXso="; + outputHash = "sha256-eFNXRH+N9CQkTxF2pE9LLVAMmoOuAB+39MJf8CKclEQ="; nativeBuildInputs = [ curl jq cacert ]; SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; diff --git a/secrets/robots-sibli.age b/secrets/robots-sibli.age index faeae86c4eb97fc19f16a8363a9f48264c7daa6f..923e841a93b43e4d20c65c2ef902b65027c53e79 100644 GIT binary patch delta 786 zcmV+t1MU2r1+fN@EPqBvSTu7^Vq|1iD>6|zOm}5wY)&#|M`B@2Pcv*)IdxWMLpge8 zMol(WI0||*c~)~vN^4?ANo8wMK{shfW>9K$Ok`S1b#hZ{ZEIRkQFkzCFKAL~Zwf6w zAaiqQEoEdfH8n9gAZL0^RCjkEaxZZ&VsUyiD|KvAb#i7>MSn6eQBOiER#jzDMKWqR zL`QfxaCt>~dTuap3PMvsW_5aFGGR$GXD?YbQ$=cMQ#EgELv2TCMk{W2N>g@BNN`zM zOgKqV3N0-yAa_tmXJ&F&GE!D?D{gr)cR4{wZBKMLGFDMDLU1`zOL$sYFG*%)c2QwT z3V~YZy;wI+27kFh2ZCgc5?@!oKD-VTdQ}UlQ3Gaj(o_3n@*>6K_*VPv7h}W=5?Zo0 z;xv3A8FLeMiRexu87%?7!0epPsg8KzpqJ{+A@woFDg}7g%gap1uJLMP*A#iFk1LCx zXV*6Pk7L)OsK{SoGA62v?s|R4C5SxfFcUL9i`mnzD}Q|Hp}Oi;%R?Zb+S57kNhO4I zu4rUvcx4Qhk%>PJNv6}QCI72>io#PB42yWqBXf}vhg9fR>>IEp)&s}jZFYX%3!Ny^ zdX!!}=iD0Jeh^G^1c|#0E@$-C{nm!YpyDh{Di<2#Ze|BXYcIX`2jyUMoqrFr-g}~A|6I5h50|3{@b)*jG=~5c z!C6>0G^EaY=2)Dx?EeaBqFB7$GuWb$^4iK5m9n1<|2neDfCoZ=P2!dgcYeJBqpe^G z>+xwpMsC=1$IU60PtyJ9L&5!bT$f>f=?G(0bw`mRHa0)+js2AlK6o3OP(%H=24COzIaO!#dEqTjsvtkV7}GvUuvBHH4PCp+bGQh(SpZrCN9c9b9gJq-`ZsC?TI=cGb05Ezfo zer{+NO>{vLXJ0JAswTsCyLndC0pnH1 zSNPt65!OoQ7NbBgftH}&A*OPjp%Ap>ZEaw3OcS>o6N9EvXTsl(p(OzJ#JT*!vL*KZ Vv`BdgLS+(fj7^T6k?;~Wy`pjC4_yEN