all nuing
This commit is contained in:
1 parent
e99744f628
commit
1e1da2f7a1
7 files changed
+443
-211
No files matched your search
@@ -15,6 +15,14 @@
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
|
||||
# nu/lib/common.nu holds helpers shared by every host-side tool below
|
||||
# (say/note/ok/warn/die/step/ask/confirm/confirm-word/shquote,
|
||||
# vsock-ssh-args, run-argv). It's never `use`d at runtime - once a
|
||||
# script is wrapped into a single file in the Nix store, a relative
|
||||
# `use` isn't reliably resolvable - so it's concatenated in here
|
||||
# instead, in front of each tool's own script.
|
||||
nuScript = path: (builtins.readFile ./nu/lib/common.nu) + "\n" + (builtins.readFile path);
|
||||
in
|
||||
{
|
||||
nixosModules.default = ./modules/host.nix;
|
||||
@@ -55,7 +63,52 @@
|
||||
]))
|
||||
];
|
||||
}
|
||||
(builtins.readFile ./nu/hermenix.nu);
|
||||
(nuScript ./nu/hermenix.nu);
|
||||
|
||||
# Host-side control of the VM itself (start/stop/console/snapshots/
|
||||
# backups) - see nu/hermes-vm.nu. Defaults match
|
||||
# services.hermenix's own option defaults; modules/host.nix wraps
|
||||
# this same script with the per-host values baked in via env vars
|
||||
# instead (see its own writeNuBin calls).
|
||||
hermes-vm = pkgs.writers.writeNuBin "hermes-vm"
|
||||
{
|
||||
makeWrapperArgs = [
|
||||
"--prefix"
|
||||
"PATH"
|
||||
":"
|
||||
(pkgs.lib.makeBinPath (with pkgs; [
|
||||
openssh
|
||||
socat
|
||||
coreutils
|
||||
gnutar
|
||||
btrfs-progs
|
||||
util-linux
|
||||
restic
|
||||
]))
|
||||
];
|
||||
}
|
||||
(nuScript ./nu/hermes-vm.nu);
|
||||
|
||||
# Host-side control of Hermes itself, inside the guest (identity,
|
||||
# config, secrets, gateway service, passthrough to its own `hermes`
|
||||
# CLI) - see nu/hermes-ctl.nu. Same per-host-defaults/module-override
|
||||
# relationship as hermes-vm above.
|
||||
hermes-ctl = pkgs.writers.writeNuBin "hermes-ctl"
|
||||
{
|
||||
makeWrapperArgs = [
|
||||
"--prefix"
|
||||
"PATH"
|
||||
":"
|
||||
(pkgs.lib.makeBinPath (with pkgs; [
|
||||
openssh
|
||||
socat
|
||||
coreutils
|
||||
gnutar
|
||||
vim
|
||||
]))
|
||||
];
|
||||
}
|
||||
(nuScript ./nu/hermes-ctl.nu);
|
||||
|
||||
default = self.packages.${system}.hermenix;
|
||||
};
|
||||
|
||||
+62
-171
@@ -808,182 +808,73 @@ in
|
||||
};
|
||||
|
||||
environment.systemPackages = [
|
||||
(pkgs.writeShellScriptBin "hermes-vm" ''
|
||||
set -euo pipefail
|
||||
SSH=(${pkgs.openssh}/bin/ssh
|
||||
-o ProxyCommand="${pkgs.socat}/bin/socat - VSOCK-CONNECT:${toString cfg.vsockCid}:22"
|
||||
-o StrictHostKeyChecking=accept-new
|
||||
hermes@${cfg.name})
|
||||
cmd="''${1-}"; shift || true
|
||||
case "$cmd" in
|
||||
ssh) exec "''${SSH[@]}" "$@" ;;
|
||||
console) exec journalctl -u hermes-vm -f -o cat ;;
|
||||
start) exec systemctl start hermes-vm ;;
|
||||
stop) exec systemctl stop hermes-vm ;;
|
||||
restart) exec systemctl restart hermes-vm ;;
|
||||
|
||||
# What is the guest actually running right now?
|
||||
config)
|
||||
"''${SSH[@]}" '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.
|
||||
config-pull)
|
||||
dest="''${1:-./hermes-etc-nixos}"
|
||||
mkdir -p "$dest"
|
||||
"''${SSH[@]}" 'tar -C /etc/nixos -cf - .' | tar -C "$dest" -xf -
|
||||
echo "pulled to $dest"
|
||||
;;
|
||||
|
||||
snapshot) exec systemctl start hermes-vm-snapshot ;;
|
||||
snapshots)
|
||||
${pkgs.util-linux}/bin/column -t <<<"$(
|
||||
echo "SNAPSHOT SIZE CREATED"
|
||||
for d in ${snapDir}/state-*; do
|
||||
[ -e "$d" ] || continue
|
||||
echo "$(basename "$d") $(du -sh "$d" 2>/dev/null | cut -f1) $(stat -c %y "$d" | cut -d. -f1)"
|
||||
done
|
||||
)" ;;
|
||||
|
||||
# Restore the state disk from a snapshot. The system disk is left
|
||||
# alone - it holds nothing worth restoring.
|
||||
rollback)
|
||||
[ $# -ge 1 ] || { echo "usage: hermes-vm rollback <snapshot>" >&2; exit 1; }
|
||||
snap=${snapDir}/"$1"
|
||||
[ -d "$snap" ] || { echo "no such snapshot: $1" >&2; exit 1; }
|
||||
echo "Restores Hermes's identity, memories, workspace and NixOS" >&2
|
||||
echo "config to $1. Everything since is lost." >&2
|
||||
read -r -p "type 'rollback' to confirm: " c
|
||||
[ "$c" = rollback ] || { echo "aborted" >&2; exit 1; }
|
||||
systemctl stop hermes-vm
|
||||
mv ${cfg.disk.baseDir}/state ${cfg.disk.baseDir}/state.old-$(date +%s)
|
||||
${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot "$snap" ${cfg.disk.baseDir}/state
|
||||
systemctl start hermes-vm
|
||||
echo "restored; previous state kept as state.old-*" ;;
|
||||
|
||||
backup) exec systemctl start hermes-vm-backup ;;
|
||||
backups)
|
||||
exec ${pkgs.restic}/bin/restic \
|
||||
-r "$(systemctl show hermes-vm-backup -p Environment --value | tr ' ' '\n' | sed -n 's/^RESTIC_REPOSITORY=//p')" \
|
||||
snapshots --tag hermes ;;
|
||||
*)
|
||||
echo "usage: hermes-vm {ssh [cmd]|console|config|config-pull [dir]|start|stop|restart|snapshot|snapshots|rollback <snap>|backup|backups}" >&2
|
||||
exit 1 ;;
|
||||
esac
|
||||
'')
|
||||
# Nu scripts, shared with the standalone flake packages of the same
|
||||
# name (packages.hermes-vm / packages.hermes-ctl) - see flake.nix. The
|
||||
# per-host values that used to be spliced into the bash strings here
|
||||
# via Nix interpolation are passed in as env vars instead, via
|
||||
# makeWrapperArgs' --set, same mechanism already used for PATH below.
|
||||
(pkgs.writers.writeNuBin "hermes-vm"
|
||||
{
|
||||
makeWrapperArgs = [
|
||||
"--prefix"
|
||||
"PATH"
|
||||
":"
|
||||
(lib.makeBinPath (with pkgs; [
|
||||
openssh
|
||||
socat
|
||||
coreutils
|
||||
gnutar
|
||||
btrfs-progs
|
||||
util-linux
|
||||
restic
|
||||
]))
|
||||
"--set"
|
||||
"HERMES_VSOCK_CID"
|
||||
(toString cfg.vsockCid)
|
||||
"--set"
|
||||
"HERMES_GUEST_NAME"
|
||||
cfg.name
|
||||
"--set"
|
||||
"HERMES_DISK_BASE_DIR"
|
||||
cfg.disk.baseDir
|
||||
];
|
||||
}
|
||||
((builtins.readFile ../nu/lib/common.nu) + "\n" + (builtins.readFile ../nu/hermes-vm.nu)))
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Driving Hermes itself, as opposed to the VM around it.
|
||||
# ---------------------------------------------------------------
|
||||
(pkgs.writeShellScriptBin cfg.cliName ''
|
||||
set -euo pipefail
|
||||
SSH=(${pkgs.openssh}/bin/ssh
|
||||
-o ProxyCommand="${pkgs.socat}/bin/socat - VSOCK-CONNECT:${toString cfg.vsockCid}:22"
|
||||
-o StrictHostKeyChecking=accept-new
|
||||
hermes@${cfg.name})
|
||||
HOME_DIR=${cfg.hermesHome}
|
||||
UNIT=${cfg.gatewayUnit}
|
||||
|
||||
# Pull a file out of the guest, open it in your editor, push it back.
|
||||
edit_remote() {
|
||||
remote="$1"; mode="''${2:-0644}"
|
||||
tmp=$(mktemp); trap 'rm -f "$tmp"' EXIT
|
||||
"''${SSH[@]}" "cat $remote" > "$tmp" 2>/dev/null || true
|
||||
before=$(${pkgs.coreutils}/bin/sha256sum < "$tmp")
|
||||
"''${EDITOR:-${pkgs.vim}/bin/vim}" "$tmp"
|
||||
if [ "$(${pkgs.coreutils}/bin/sha256sum < "$tmp")" = "$before" ]; then
|
||||
echo "unchanged"; return
|
||||
fi
|
||||
"''${SSH[@]}" "install -m $mode /dev/stdin $remote" < "$tmp"
|
||||
echo "written to $remote"
|
||||
read -r -p "restart the gateway now? [y/N] " yn
|
||||
case "$yn" in [yY]*) "''${SSH[@]}" "systemctl --user restart $UNIT" ;; esac
|
||||
(pkgs.writers.writeNuBin cfg.cliName
|
||||
{
|
||||
makeWrapperArgs = [
|
||||
"--prefix"
|
||||
"PATH"
|
||||
":"
|
||||
(lib.makeBinPath (with pkgs; [
|
||||
openssh
|
||||
socat
|
||||
coreutils
|
||||
gnutar
|
||||
vim
|
||||
]))
|
||||
"--set"
|
||||
"HERMES_VSOCK_CID"
|
||||
(toString cfg.vsockCid)
|
||||
"--set"
|
||||
"HERMES_GUEST_NAME"
|
||||
cfg.name
|
||||
"--set"
|
||||
"HERMES_HOME_DIR"
|
||||
cfg.hermesHome
|
||||
"--set"
|
||||
"HERMES_GATEWAY_UNIT"
|
||||
cfg.gatewayUnit
|
||||
"--set"
|
||||
"HERMES_CLI_NAME"
|
||||
cfg.cliName
|
||||
];
|
||||
}
|
||||
|
||||
cmd="''${1-}"; shift || true
|
||||
case "$cmd" in
|
||||
soul)
|
||||
case "''${1-}" in
|
||||
edit) edit_remote "$HOME_DIR/SOUL.md" ;;
|
||||
*) "''${SSH[@]}" "cat $HOME_DIR/SOUL.md" ;;
|
||||
esac ;;
|
||||
|
||||
memory)
|
||||
case "''${1-}" in
|
||||
edit) edit_remote "$HOME_DIR/memories/USER.md" ;;
|
||||
*) "''${SSH[@]}" "cat $HOME_DIR/memories/USER.md" ;;
|
||||
esac ;;
|
||||
|
||||
conf)
|
||||
case "''${1-}" in
|
||||
edit) edit_remote "$HOME_DIR/config.yaml" ;;
|
||||
*) "''${SSH[@]}" "cat $HOME_DIR/config.yaml" ;;
|
||||
esac ;;
|
||||
|
||||
# API keys. Mode 0600, never enters the Nix store, and not printed
|
||||
# unless you ask for it by name.
|
||||
secrets)
|
||||
case "''${1-}" in
|
||||
edit) edit_remote "$HOME_DIR/.env" 0600 ;;
|
||||
show) "''${SSH[@]}" "cat $HOME_DIR/.env" ;;
|
||||
*) "''${SSH[@]}" "sed 's/=.*/=<set>/' $HOME_DIR/.env" ;;
|
||||
esac ;;
|
||||
|
||||
status) "''${SSH[@]}" "systemctl --user status $UNIT --no-pager" ;;
|
||||
logs) "''${SSH[@]}" -t "journalctl --user -u $UNIT -f" ;;
|
||||
restart) "''${SSH[@]}" "systemctl --user restart $UNIT" ;;
|
||||
units) "''${SSH[@]}" "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.
|
||||
reseed)
|
||||
echo "This overwrites SOUL.md, memories and config.yaml in the guest" >&2
|
||||
echo "with the seed from /etc/nixos/hermes. Anything Hermes has" >&2
|
||||
echo "learned and written there is lost." >&2
|
||||
read -r -p "type 'reseed' to confirm: " confirm
|
||||
[ "$confirm" = "reseed" ] || { echo "aborted" >&2; exit 1; }
|
||||
"''${SSH[@]}" "cp -rT /etc/nixos/hermes/home $HOME_DIR && \
|
||||
cp -rT /etc/nixos/hermes/workspace /var/lib/hermes/workspace && \
|
||||
systemctl --user restart $UNIT"
|
||||
echo "reseeded" ;;
|
||||
|
||||
# Copy the agent's live identity back here, so you can fold what it
|
||||
# has learned into the seed in guest/hermes/.
|
||||
pull)
|
||||
dest="''${1:-./hermes-home}"
|
||||
mkdir -p "$dest"
|
||||
"''${SSH[@]}" "tar -C $HOME_DIR --exclude=.env --exclude=sessions --exclude=state.db -cf - ." \
|
||||
| tar -C "$dest" -xf -
|
||||
echo "pulled to $dest (secrets and sessions excluded)" ;;
|
||||
|
||||
""|help|-h|--help)
|
||||
cat >&2 <<'USAGE'
|
||||
${cfg.cliName} - 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:
|
||||
${cfg.cliName} chat
|
||||
${cfg.cliName} update
|
||||
${cfg.cliName} sessions list
|
||||
USAGE
|
||||
exit 1 ;;
|
||||
|
||||
*) exec "''${SSH[@]}" -t hermes "$cmd" "$@" ;;
|
||||
esac
|
||||
'')
|
||||
((builtins.readFile ../nu/lib/common.nu) + "\n" + (builtins.readFile ../nu/hermes-ctl.nu)))
|
||||
];
|
||||
};
|
||||
}
|
||||
+7
-39
@@ -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)
|
||||
|
||||
@@ -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
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user