all nuing

This commit is contained in:
ari committed 2026-09-16 22:09:42 -04:00
1 parent e99744f628
commit 1e1da2f7a1
7 files changed
+443 -211

No files matched your search

+7 -39
View File
@@ -25,33 +25,10 @@
# happen.
# ---------------------------------------------------------------- output ---
def say [msg: string] { print $" ($msg)" }
def note [msg: string] { print $" (ansi dark_gray)($msg)(ansi reset)" }
def ok [msg: string] { print $" (ansi green)✓(ansi reset) ($msg)" }
def warn [msg: string] { print $" (ansi yellow)!(ansi reset) ($msg)" }
def die [msg: string] { print -e $"\n(ansi red)error:(ansi reset) ($msg)"; exit 1 }
def step [n: int, title: string] { print $"\n(ansi attr_bold)== ($n). ($title)(ansi reset)" }
def ask [prompt: string, default: string = ""] {
let suffix = if $default != "" { $" [($default)]" } else { "" }
let reply = (input $" ($prompt)($suffix): ")
if $reply == "" { $default } else { $reply }
}
def confirm [prompt: string] {
let reply = (input $" ($prompt) [y/N] ")
(($reply | str starts-with "y") or ($reply | str starts-with "Y"))
}
def confirm-word [word: string, prompt: string] {
(input $" ($prompt) ") == $word
}
# Single-quote a string for safe embedding in a remote shell command line.
def shquote [s: string] {
"'" + ($s | str replace --all "'" "'\\''") + "'"
}
#
# say/note/ok/warn/die/step/ask/confirm/confirm-word/shquote/vsock-ssh-args/
# run-argv live in nu/lib/common.nu, shared with hermes-vm.nu and
# hermes-ctl.nu - see flake.nix, which concatenates it in front of this file.
# `truncate -s` treats a bare number as bytes, not gigabytes - the size
# prompts' own defaults ("80G", "20G") make gigabytes the obvious intent, so
@@ -448,16 +425,8 @@ def "main migrate" [
die "--rewrite-from and --rewrite-to must have the same length"
}
let ssh_base = [
"ssh"
"-o" $"ProxyCommand=socat - VSOCK-CONNECT:($vsock_cid):22"
"-o" "StrictHostKeyChecking=accept-new"
$"($guest_user)@hermes"
]
def gssh [...args: string] {
let full = $ssh_base ++ $args
^($full | first) ...($full | skip 1)
}
let ssh_base = (vsock-ssh-args $vsock_cid $guest_user "hermes")
def gssh [cmd: string] { run-argv ($ssh_base ++ [$cmd]) }
say $"Source \(read-only\): ($source)"
say $"Destination \(guest\): ($guest_user)@hermes:($dest) \(over vsock, cid ($vsock_cid)\)"
@@ -475,8 +444,7 @@ def "main migrate" [
gssh "true"
step 2 $"streaming ($source) -> guest:($dest) \(this can take a while for 8G+\)"
let stream_argv = $ssh_base ++ [$"mkdir -p '($dest)' && tar -C '($dest)' -xf -"]
^tar -C $source -cf - . | ^($stream_argv | first) ...($stream_argv | skip 1)
^tar -C $source -cf - . | run-argv ($ssh_base ++ [$"mkdir -p '($dest)' && tar -C '($dest)' -xf -"])
step 3 "rewriting stale hostnames in the guest"
let sed_expr = (0..(($rewrite_from | length) - 1)
+148
View File
@@ -0,0 +1,148 @@
#!/usr/bin/env nu
#
# hermes-ctl — drive Hermes itself inside the guest: its identity, config,
# secrets, and the gateway service, plus a passthrough to its own `hermes`
# CLI. For controlling the VM around it (start/stop/snapshots/backups), see
# hermes-vm instead.
#
# Installed by the hermenix NixOS module (modules/host.nix) as part of
# environment.systemPackages under cfg.cliName, wrapped with the per-host
# vsock CID, guest SSH alias, HERMES_HOME location and gateway unit name
# baked in as env vars. The defaults below match the module's own option
# defaults, so `nix run .#hermes-ctl` also works out of the box against a
# stock setup.
#
# Requires nu/lib/common.nu to be concatenated in front of this file - see
# flake.nix and modules/host.nix. Not runnable standalone.
def guest-user [] { "hermes" }
def guest-host [] { $env.HERMES_GUEST_NAME? | default "hermes" }
def vsock-cid [] { ($env.HERMES_VSOCK_CID? | default "77") | into int }
def home-dir [] { $env.HERMES_HOME_DIR? | default "/var/lib/hermes-vm/.hermes" }
def gateway-unit [] { $env.HERMES_GATEWAY_UNIT? | default "hermes-agent" }
def cli-name [] { $env.HERMES_CLI_NAME? | default "hermes-ctl" }
def guest-ssh-args [] { vsock-ssh-args (vsock-cid) (guest-user) (guest-host) }
def gssh [cmd: string] { run-argv ((guest-ssh-args) ++ [$cmd]) }
# Pull a file out of the guest, open it in your editor, push it back.
def edit-remote [remote: string, mode: string = "0644"] {
let tmp = (mktemp)
let fetched = (do -i { gssh $"cat ($remote)" } | complete)
$fetched.stdout | save -f $tmp
let before = (open --raw $tmp | hash sha256)
^($env.EDITOR? | default "vim") $tmp
if (open --raw $tmp | hash sha256) == $before {
rm $tmp
say "unchanged"
return
}
(open --raw $tmp) | gssh $"install -m ($mode) /dev/stdin ($remote)"
rm $tmp
say $"written to ($remote)"
if (confirm "restart the gateway now?") {
gssh $"systemctl --user restart (gateway-unit)"
}
}
def print-usage [] {
print -e $"(cli-name) - drive Hermes inside the guest
soul [edit] show or edit SOUL.md
memory [edit] show or edit memories/USER.md
conf [edit] show or edit config.yaml
secrets [edit|show] manage .env, keys masked by default
status | logs | restart | units
reseed overwrite identity from /etc/nixos/hermes
pull [dir] copy live identity back to the host
Anything else goes straight to the hermes CLI in the guest:
(cli-name) chat
(cli-name) update
(cli-name) sessions list"
}
def "main soul" [action?: string] {
if $action == "edit" {
edit-remote $"(home-dir)/SOUL.md"
} else {
gssh $"cat (home-dir)/SOUL.md"
}
}
def "main memory" [action?: string] {
if $action == "edit" {
edit-remote $"(home-dir)/memories/USER.md"
} else {
gssh $"cat (home-dir)/memories/USER.md"
}
}
def "main conf" [action?: string] {
if $action == "edit" {
edit-remote $"(home-dir)/config.yaml"
} else {
gssh $"cat (home-dir)/config.yaml"
}
}
# API keys. Mode 0600, never enters the Nix store, and not printed unless
# you ask for it by name.
def "main secrets" [action?: string] {
if $action == "edit" {
edit-remote $"(home-dir)/.env" "0600"
} else if $action == "show" {
gssh $"cat (home-dir)/.env"
} else {
gssh $"sed 's/=.*/=<set>/' (home-dir)/.env"
}
}
def "main status" [] { gssh $"systemctl --user status (gateway-unit) --no-pager" }
def "main logs" [] { run-argv ((guest-ssh-args) ++ ["-t" $"journalctl --user -u (gateway-unit) -f"]) }
def "main restart" [] { gssh $"systemctl --user restart (gateway-unit)" }
def "main units" [] { gssh "systemctl --user list-units 'hermes*' --all --no-pager" }
# Overwrite the agent's identity from the seed. Destructive: this is how you
# undo whatever it has decided about itself.
def "main reseed" [] {
warn "This overwrites SOUL.md, memories and config.yaml in the guest"
warn "with the seed from /etc/nixos/hermes. Anything Hermes has"
warn "learned and written there is lost."
if not (confirm-word "reseed" "type 'reseed' to confirm:") { die "aborted" }
gssh $"cp -rT /etc/nixos/hermes/home (home-dir) && cp -rT /etc/nixos/hermes/workspace /var/lib/hermes/workspace && systemctl --user restart (gateway-unit)"
ok "reseeded"
}
# Copy the agent's live identity back here, so you can fold what it has
# learned into the seed in guest/hermes/.
def "main pull" [dest: string = "./hermes-home"] {
mkdir $dest
(gssh $"tar -C (home-dir) --exclude=.env --exclude=sessions --exclude=state.db -cf - .") | ^tar -C $dest -xf -
say $"pulled to ($dest) (secrets and sessions excluded)"
}
def "main help" [] { print-usage; exit 1 }
# Everything not matched above goes straight to the hermes CLI in the guest,
# with a pty for interactive subcommands like `chat`.
#
# A single string, not rest args: nu's parser tries to match any bare
# --flag-shaped token against this command's own flags before it ever
# reaches a rest parameter, so `hermes-ctl chat --model x` would fail to
# parse. Quote the whole thing instead, the same way hermenix.nu's own
# `remote` subcommand has to - e.g. `hermes-ctl "chat --model x"`. ssh
# recognizes -t regardless of where it falls in its own argv, so appending
# it (and the remote words) after the destination, as here, still works.
def main [remainder: string = ""] {
if $remainder == "" {
print-usage
exit 1
}
let words = ($remainder | split row " " | where {|w| $w != "" })
run-argv ((guest-ssh-args) ++ ["-t" "hermes"] ++ $words)
}
+114
View File
@@ -0,0 +1,114 @@
#!/usr/bin/env nu
#
# hermes-vm — host-side control of the Hermes Agent VM itself: start/stop,
# console, config inspection, snapshots and backups. For driving Hermes the
# agent running inside it, see hermes-ctl instead.
#
# Installed by the hermenix NixOS module (modules/host.nix) as part of
# environment.systemPackages, wrapped with the per-host vsock CID, guest SSH
# alias and disk layout baked in as env vars. The defaults below match the
# module's own option defaults, so `nix run .#hermes-vm` also works
# out of the box against a stock setup.
#
# Requires nu/lib/common.nu to be concatenated in front of this file - see
# flake.nix and modules/host.nix. Not runnable standalone.
def guest-user [] { "hermes" }
def guest-host [] { $env.HERMES_GUEST_NAME? | default "hermes" }
def vsock-cid [] { ($env.HERMES_VSOCK_CID? | default "77") | into int }
def disk-base-dir [] { $env.HERMES_DISK_BASE_DIR? | default "/var/lib/hermes-vm" }
def snap-dir [] { $"(disk-base-dir)/snapshots" }
def ssh-args [] { vsock-ssh-args (vsock-cid) (guest-user) (guest-host) }
# Run a single already-fully-formed remote command string, or open an
# interactive shell if none is given. A single string, not rest args: nu's
# parser tries to match any bare --flag-shaped token against this command's
# own flags before it ever reaches a rest parameter, so `hermes-vm ssh sudo
# foo --bar` would fail to parse. Quote the whole remote command instead, the
# same way hermenix.nu's own `remote` subcommand has to.
def gssh [cmd?: string] {
let argv = if $cmd == null { ssh-args } else { (ssh-args) ++ [$cmd] }
run-argv $argv
}
def usage [] {
"usage: hermes-vm {ssh [cmd]|console|config|config-pull [dir]|start|stop|restart|snapshot|snapshots|rollback <snap>|backup|backups}"
}
def main [] {
print (usage)
}
def "main ssh" [cmd?: string] { gssh $cmd }
def "main console" [] { ^journalctl -u hermes-vm -f -o cat }
def "main start" [] { ^systemctl start hermes-vm }
def "main stop" [] { ^systemctl stop hermes-vm }
def "main restart" [] { ^systemctl restart hermes-vm }
# What is the guest actually running right now?
def "main config" [] {
gssh 'echo "== current generation =="; \
readlink -f /run/current-system; \
echo; echo "== generations =="; \
sudo nix-env --list-generations -p /nix/var/nix/profiles/system | tail -n 10; \
echo; echo "== /etc/nixos history =="; \
git -C /etc/nixos log --oneline -n 15; \
echo; echo "== uncommitted =="; \
git -C /etc/nixos status --short'
}
# Copy the guest's live config here for reading or diffing.
def "main config-pull" [dest: string = "./hermes-etc-nixos"] {
mkdir $dest
(gssh "tar -C /etc/nixos -cf - .") | ^tar -C $dest -xf -
say $"pulled to ($dest)"
}
def "main snapshot" [] { ^systemctl start hermes-vm-snapshot }
def "main snapshots" [] {
let entries = (glob $"(snap-dir)/state-*" | where {|p| ($p | path type) == "dir" })
if ($entries | is-empty) {
say "no snapshots"
return
}
$entries
| each {|p|
{
snapshot: ($p | path basename)
size: (du $p | get physical.0)
created: (ls -D $p | get modified.0)
}
}
| sort-by created
| print
}
# Restore the state disk from a snapshot. The system disk is left alone - it
# holds nothing worth restoring.
def "main rollback" [snapshot: string] {
let snap = $"(snap-dir)/($snapshot)"
if not ($snap | path exists) { die $"no such snapshot: ($snapshot)" }
warn "Restores Hermes's identity, memories, workspace and NixOS"
warn $"config to ($snapshot). Everything since is lost."
if not (confirm-word "rollback" "type 'rollback' to confirm:") { die "aborted" }
^systemctl stop hermes-vm
let base = (disk-base-dir)
let stamp = (date now | format date "%s")
^mv $"($base)/state" $"($base)/state.old-($stamp)"
^btrfs subvolume snapshot $snap $"($base)/state"
^systemctl start hermes-vm
ok "restored; previous state kept as state.old-*"
}
def "main backup" [] { ^systemctl start hermes-vm-backup }
def "main backups" [] {
let repo = (^systemctl show hermes-vm-backup -p Environment --value
| str trim | split row " " | where {|e| $e | str starts-with "RESTIC_REPOSITORY=" }
| get 0? | default "" | str replace "RESTIC_REPOSITORY=" "")
^restic -r $repo snapshots --tag hermes
}
+57
View File
@@ -0,0 +1,57 @@
# hermenix — shared helpers for the host-side nushell tools (hermenix,
# hermes-vm, hermes-ctl).
#
# Not a standalone script: nu can't reliably resolve relative `use` imports
# once a script has been copied into the Nix store as a single wrapped
# binary, so the flake concatenates this file with each tool's own script
# instead (see flake.nix / modules/host.nix). Keep this file free of a
# `main` def and of anything specific to one tool.
# ---------------------------------------------------------------- output ---
def say [msg: string] { print $" ($msg)" }
def note [msg: string] { print $" (ansi dark_gray)($msg)(ansi reset)" }
def ok [msg: string] { print $" (ansi green)✓(ansi reset) ($msg)" }
def warn [msg: string] { print $" (ansi yellow)!(ansi reset) ($msg)" }
def die [msg: string] { print -e $"\n(ansi red)error:(ansi reset) ($msg)"; exit 1 }
def step [n: int, title: string] { print $"\n(ansi attr_bold)== ($n). ($title)(ansi reset)" }
def ask [prompt: string, default: string = ""] {
let suffix = if $default != "" { $" [($default)]" } else { "" }
let reply = (input $" ($prompt)($suffix): ")
if $reply == "" { $default } else { $reply }
}
def confirm [prompt: string] {
let reply = (input $" ($prompt) [y/N] ")
(($reply | str starts-with "y") or ($reply | str starts-with "Y"))
}
def confirm-word [word: string, prompt: string] {
(input $" ($prompt) ") == $word
}
# Single-quote a string for safe embedding in a remote shell command line.
def shquote [s: string] {
"'" + ($s | str replace --all "'" "'\\''") + "'"
}
# ------------------------------------------------------------------- ssh ---
# argv for reaching the guest over vsock — no inbound network path needed.
# Shared by every tool that talks to the guest (hermenix migrate, hermes-vm,
# hermes-ctl).
def vsock-ssh-args [vsock_cid: int, guest_user: string, guest_host: string] {
[
"ssh"
"-o" $"ProxyCommand=socat - VSOCK-CONNECT:($vsock_cid):22"
"-o" "StrictHostKeyChecking=accept-new"
$"($guest_user)@($guest_host)"
]
}
# Run an argv list (e.g. from vsock-ssh-args, with trailing command words
# appended) as an external command.
def run-argv [argv: list<string>] {
^($argv | first) ...($argv | skip 1)
}