Files
hermenix/nu/hermes-ctl.nu
T
2026-09-16 22:09:42 -04:00

149 lines
5.4 KiB
Nu

#!/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)
}