Files
2026-10-09 14:35:12 -04:00

286 lines
9.1 KiB
Nix

{
lib,
pkgs,
modulesPath,
...
}:
let
# Must match services.hermenix.* defaults on the host.
hostAddress = "10.77.0.1";
guestAddress = "10.77.0.2";
prefixLength = 24;
cachePort = 5000;
# Host-only settings outside the flake (--impure). See local.nix.example.
local = if builtins.pathExists /etc/hermenix/local.nix then import /etc/hermenix/local.nix else { };
cachePublicKey = local.cachePublicKey or "";
authorizedKeys = local.authorizedKeys or [ ];
stateDir = "/var/lib/hermes";
useHostCache = cachePublicKey != "";
# systemPackages does not populate systemd unit PATH; list deps once for both.
hermesInstallDeps = with pkgs; [
git
curl
jq
ripgrep
fd
tree
htop
uv
nodejs
ffmpeg
direnv
nix-direnv
nix-output-monitor
gcc
gnumake
pkg-config
python3
xz
bash
coreutils
gnutar
gzip
which
gnugrep
gnused
gawk
findutils
neovim
];
in
{
# virtio drivers needed in bootstrap image too (hardware.nix is not imported there).
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
boot.initrd.availableKernelModules = [
"virtio_pci"
"virtio_blk"
"virtio_scsi"
"ahci"
"sd_mod"
"xhci_pci"
];
networking.hostName = "hermes";
# State disk (identity, workspace, /etc/nixos). neededForBoot: seed runs before activation.
fileSystems."${stateDir}" = {
device = "/dev/disk/by-label/hermes-state";
fsType = "ext4";
neededForBoot = true;
options = [
"defaults"
"noatime"
];
};
# First boot only — re-applying would wipe agent-owned SOUL/memories. Use hermenix ctl reseed.
system.activationScripts.seedHermesState = {
deps = [
"users"
"groups"
];
text = ''
install -d -o hermes -g hermes -m 0750 ${stateDir}
install -d -o hermes -g hermes -m 0750 ${stateDir}/workspace
if [ ! -e ${stateDir}/.seeded ]; then
install -d -o hermes -g hermes -m 0750 ${stateDir}/.hermes
install -d -o hermes -g hermes -m 0755 ${stateDir}/nixos
cp -rTn ${./.} ${stateDir}/nixos
cp -rTn ${./hermes/home} ${stateDir}/.hermes
cp -rTn ${./hermes/workspace} ${stateDir}/workspace
chmod -R u+w ${stateDir}/nixos ${stateDir}/.hermes ${stateDir}/workspace
# Commit while still root-owned (git refuses foreign ownership; activation failures are silent).
if [ ! -d ${stateDir}/nixos/.git ]; then
${pkgs.git}/bin/git -C ${stateDir}/nixos init -q -b main
${pkgs.git}/bin/git -C ${stateDir}/nixos add -A
${pkgs.git}/bin/git -C ${stateDir}/nixos \
-c user.name=seed -c user.email=seed@localhost \
commit -q -m "seed configuration from bootstrap image"
fi
chown -R hermes:hermes ${stateDir}/nixos ${stateDir}/.hermes ${stateDir}/workspace
touch ${stateDir}/.seeded
chown hermes:hermes ${stateDir}/.seeded
fi
# /etc/nixos is a real dir by default; ln -sfn would nest inside it — rm first.
rm -rf /etc/nixos
ln -sfn ${stateDir}/nixos /etc/nixos
'';
};
# Static service-net + DHCP (bridged mode gets LAN lease at metric 100; host route at 200).
networking.useNetworkd = true;
systemd.network.enable = true;
systemd.network.networks."10-eth" = {
matchConfig.Type = "ether";
address = [ "${guestAddress}/${toString prefixLength}" ];
routes = [
{
Gateway = hostAddress;
Metric = 200;
}
];
networkConfig = {
DHCP = "ipv4";
DNS = [
"9.9.9.9"
"149.112.112.112"
];
};
dhcpV4Config = {
UseDNS = false;
RouteMetric = 100;
};
};
networking.nameservers = [
"9.9.9.9"
"149.112.112.112"
];
services.openssh = {
enable = true;
settings.PasswordAuthentication = false;
settings.PermitRootLogin = "no";
};
networking.firewall.allowedTCPPorts = [ 22 ];
services.qemuGuest.enable = true;
# Determinate owns nix.conf — custom settings only via nix.custom.conf (no nix.settings).
environment.etc."nix/nix.custom.conf".text = ''
trusted-users = root hermes
extra-experimental-features = nix-command flakes
extra-substituters = https://cache.flakehub.com${lib.optionalString useHostCache " http://${hostAddress}:${toString cachePort}"}
extra-trusted-public-keys = cache.flakehub.com-3:hJuILl5sVK4iKm86JzgdXW12Y2Hwd5G07qKtHTOcDCM=${lib.optionalString useHostCache " ${cachePublicKey}"}
'';
boot.loader.systemd-boot.configurationLimit = lib.mkDefault 20;
users.users.hermes = {
isNormalUser = true;
home = stateDir;
createHome = false; # the state disk is mounted here and seeded above
linger = true;
group = "hermes";
extraGroups = [
"wheel"
"systemd-journal"
];
openssh.authorizedKeys.keys = authorizedKeys;
};
users.groups.hermes = { };
security.sudo.wheelNeedsPassword = false;
# Upstream installer (not Nix) so hermes update works. Interactive config is upstream docs.
systemd.services.hermes-install = {
description = "Install the Hermes Agent CLI (first boot only)";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
unitConfig.ConditionPathExists = "!${stateDir}/.hermes-installed";
path = hermesInstallDeps;
serviceConfig = {
Type = "oneshot";
User = "hermes";
Restart = "on-failure";
RestartSec = 30;
};
script = ''
set -euo pipefail
curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh \
| ${pkgs.bash}/bin/bash -s -- --non-interactive --skip-setup
touch ${stateDir}/.hermes-installed
'';
};
programs.nix-ld = {
enable = true;
libraries = with pkgs; [
stdenv.cc.cc
zlib
openssl
curl
libxml2
libxslt
glib
nss
nspr
at-spi2-core
cups
dbus
expat
libGL
libdrm
mesa
alsa-lib
];
};
environment.systemPackages = hermesInstallDeps;
# createHome=false so no ~/.bashrc; put ~/.local/bin on PATH via PAM instead.
environment.localBinInPath = true;
programs.direnv = {
enable = true;
nix-direnv.enable = true;
};
environment.etc."hermes/ENVIRONMENT.md".text = ''
You run inside a NixOS virtual machine called "hermes". Facts about your
own environment that are easy to get wrong:
- You have your own /nix/store on your own disk. Changes persist across
reboots. `sudo nixos-rebuild switch --flake /etc/nixos#hermes` is a
real, durable change to this machine.
- /etc/nixos is yours. Edit it, commit it, rebuild from it.
- If a rebuild breaks the system, the previous generation is in the boot
menu. The operator also has snapshots of the state disk underneath that.
- Per-project environments go in a flake.nix in the project directory,
used via `nix develop` or direnv. Do not install things globally.
- This machine runs Determinate Nix. /etc/nix/nix.conf belongs to it and
must not be edited. Custom Nix settings go in /etc/nix/nix.custom.conf,
which is managed from /etc/nixos. Lazy trees are on by default, so flake
evaluation is much cheaper here than on stock Nix.
- Substituters: FlakeHub Cache, then the operator's host cache, then
cache.nixos.org. If FlakeHub Cache starts missing, the login has probably
expired; tell the operator rather than trying to re-auth yourself.
- This machine has two disks. Everything under /var/lib/hermes - your
identity, memories, workspace, and /etc/nixos, which is a symlink into
it - is on the state disk, which the operator snapshots and backs up.
Everything else, including the whole Nix store, is on a disposable
system disk that is never backed up because it can be rebuilt. Do not
keep anything you care about outside /var/lib/hermes.
- Your identity is ~/.hermes/SOUL.md and it is yours to edit. It was
seeded once on first boot and is never overwritten by a rebuild, so
changes you make there survive. Same for ~/.hermes/memories/ and
anything else you keep under ~/.hermes.
- This file is regenerated on every system rebuild. Editing it is
pointless; it will be replaced. Put durable notes in SOUL.md.
- Before your first `nixos-rebuild switch`, commit /etc/nixos. That
commit is the cheapest undo; previous generations are in the boot
menu if a rebuild goes badly.
- The skill at ~/.hermes/skills/nixos-self-config covers how to change
this machine safely. Read it before your first rebuild.
- In NAT mode you have no route to the host or to any local network:
public internet only, plus a read-only Nix binary cache on
${hostAddress}. In bridged mode (network.bridged = true on the host)
you take a LAN address from DHCP instead, LAN devices can reach any
port you open, and lanAccess/allowLan still limit what you can reach.
- Never put a secret in a Nix expression. Everything in the store is
world-readable. Use ${stateDir}/.secrets.env and chmod 0600.
'';
system.stateVersion = "25.11";
}