all nuing
This commit is contained in:
1 parent
e99744f628
commit
1e1da2f7a1
7 files changed
+443
-211
No files matched your search
+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)))
|
||||
];
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user