From b1c6a5250398848885bbb1d3faa0ee8598582411 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 17 Sep 2026 13:18:19 -0400 Subject: [PATCH] hooks + devenv --- .envrc | 3 + .gitignore | 13 + README.md | 203 +++++++++++---- flake.lock | 268 ++++++++++++++++++++ flake.nix | 43 +++- nu/completions.nu | 2 +- nu/ctl/ctl.nu | 154 ++++++------ nu/gen-completions.nu | 107 ++++---- nu/hermenix.nu | 20 +- nu/host/migrate.nu | 92 +++---- nu/host/remote.nu | 28 +-- nu/host/setup.nu | 559 +++++++++++++++++++++++------------------- nu/lib/common.nu | 37 +-- nu/lib/guest.nu | 8 +- nu/vm/vm.nu | 94 +++---- 15 files changed, 1057 insertions(+), 574 deletions(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..87c950d --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +nix develop --no-pure-eval diff --git a/.gitignore b/.gitignore index 8fa49b3..e80416c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,16 @@ # Left behind by `nix build` when run directly instead of through the CLI. /result /result-* +.pre-commit-config.yaml +.devenv + +# Devenv +.devenv* +devenv.local.nix +devenv.local.yaml + +# direnv +.direnv + +# pre-commit +.pre-commit-config.yaml diff --git a/README.md b/README.md index 8c501bd..a1175d4 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,16 @@ The agent can rebuild its own system and update itself, durably. It shares no filesystem with the host, and reaches nothing on your network unless you say so. +One CLI, `hermenix`, with five subcommands: + +| | For | +|---|---| +| `hermenix setup` | one-off host provisioning — disks, cache key, image build | +| `hermenix migrate ` | carry an existing flat `HERMES_HOME` into the guest | +| `hermenix remote ""` | run `setup`/`migrate` on a remote host over SSH | +| `hermenix vm ` | control the VM itself — start/stop/console/snapshots/backups | +| `hermenix ctl ` | drive the agent inside the VM — identity/config/secrets/gateway | + --- ## Installation @@ -60,7 +70,8 @@ sudo mkdir -p /var/lib/hermes-vm/snapshots # BEFORE creating any file inside them sudo chattr +C /var/lib/hermes-vm/system /var/lib/hermes-vm/state -sudo btrfs property set /var/lib/hermes-vm/system state compression none +sudo btrfs property set /var/lib/hermes-vm/system compression none +sudo btrfs property set /var/lib/hermes-vm/state compression none sudo truncate -s 80G /var/lib/hermes-vm/system/system.raw sudo truncate -s 20G /var/lib/hermes-vm/state/state.raw @@ -153,12 +164,12 @@ services.hermenix = { }; ``` -`sudo nixos-rebuild switch`, then `hermes-vm console` to watch it boot. +`sudo nixos-rebuild switch`, then `hermenix vm console` to watch it boot. ### 6. First boot, inside the guest ```bash -hermes-vm ssh +hermenix vm ssh # Confirm the disk labels match guest/hardware.nix BEFORE any rebuild lsblk -f @@ -190,9 +201,9 @@ The identity seed is already in place — `SOUL.md`, a starter `config.yaml`, a the gateway came up and that the unit name matches the default: ```bash -hermes-ctl units -hermes-ctl status -hermes-ctl soul +hermenix ctl units +hermenix ctl status +hermenix ctl soul ``` > **Check the labels in step 6 before the first rebuild.** @@ -239,18 +250,29 @@ interactive prompts in `setup`/`migrate` to work at all over the connection. ## SSH into the guest ```bash -hermes-vm ssh # interactive shell -hermes-vm ssh systemctl status # run a command and exit -hermes-vm console # serial console, for when SSH is down +hermenix vm ssh # interactive shell +hermenix vm ssh "systemctl status" # run a command and exit +hermenix vm console # follow the VM's boot/journal output ``` +`ssh` takes its remote command as a single string, not separate words — quote +anything past a single bare word (nu's own parser otherwise tries to match +`--flag`-shaped tokens against `vm ssh`'s own flags, and errors on extra +positional words either way). + This goes over **vsock**, not the network. There is no listening SSH port exposed anywhere, and the guest needs no inbound network path for you to -reach it. If the guest has broken its own networking entirely, `hermes-vm ssh` -still works — `console` is the fallback for when it's broken its SSH too. +reach it. `console` is just `journalctl -u hermes-vm -f` on the host — a +read-only follow of the VM unit's journal, useful for watching it boot or +diagnosing why SSH won't come up, but it can't take input. There's currently +no interactive fallback for when the guest itself is unresponsive. -Under the hood it's `ssh -o ProxyCommand='socat - VSOCK-CONNECT:77:22'`, if -you'd rather drive it by hand. +Under the hood `ssh` is `ssh -o ProxyCommand='socat - VSOCK-CONNECT::22'` +(cid defaults to 77, `services.hermenix.vsockCid`), if you'd rather drive it +by hand. + +The VM itself is a systemd unit: `hermenix vm start`/`stop`/`restart` control +it directly, same as `systemctl {start,stop,restart} hermes-vm` on the host. --- @@ -282,9 +304,9 @@ never repeats. Two consequences: - Editing `guest/hermes/` after first boot changes nothing by itself. Use - `hermes-ctl reseed` when you actually want to overwrite. + `hermenix ctl reseed` when you actually want to overwrite. - The seed is the *starting* identity, not the current one. To see what - Hermes has made of itself, `hermes-ctl soul`. + Hermes has made of itself, `hermenix ctl soul`. ### Machine facts vs identity @@ -298,7 +320,7 @@ persists. It's always accurate and there's no point editing it. ### Folding what it learned back into the seed ```bash -hermes-ctl pull ./hermes-home # excludes .env, sessions, state.db +hermenix ctl pull ./hermes-home # excludes .env, sessions, state.db diff -ru guest/hermes/home ./hermes-home ``` @@ -310,37 +332,37 @@ scratch. ## Managing Hermes from the host -`hermes-vm` drives the virtual machine. `hermes-ctl` drives the agent inside -it. Rename the latter with `services.hermenix.cliName = "hermes";` if you'd -rather it felt local. +`hermenix vm` drives the virtual machine. `hermenix ctl` drives the agent +inside it. ```bash -hermes-ctl soul # current identity -hermes-ctl soul edit # pull, $EDITOR, push, offer to restart -hermes-ctl memory edit # memories/USER.md -hermes-ctl conf edit # config.yaml +hermenix ctl soul # current identity +hermenix ctl soul edit # pull, $EDITOR, push, offer to restart +hermenix ctl memory edit # memories/USER.md +hermenix ctl conf edit # config.yaml -hermes-ctl secrets # keys, values masked -hermes-ctl secrets edit # edit ~/.hermes/.env, written 0600 -hermes-ctl secrets show # values, when you really mean it +hermenix ctl secrets # keys, values masked +hermenix ctl secrets edit # edit hermesHome's .env, written 0600 +hermenix ctl secrets show # values, when you really mean it -hermes-ctl status # gateway service -hermes-ctl logs # follow it -hermes-ctl restart -hermes-ctl units # find the unit name if the default is wrong +hermenix ctl status # gateway service +hermenix ctl logs # follow it +hermenix ctl restart +hermenix ctl units # find the unit name if the default is wrong -hermes-ctl reseed # overwrite identity from the seed -hermes-ctl pull [dir] # copy live identity to the host +hermenix ctl reseed # overwrite identity from the seed +hermenix ctl pull [dir] # copy live identity to the host ``` Anything unrecognised is passed straight through to the `hermes` CLI in the -guest, with a TTY attached: +guest, with a TTY attached — quote it as one string if it's more than a +single word (same rule as `vm ssh` above): ```bash -hermes-ctl chat -hermes-ctl update -hermes-ctl sessions list -hermes-ctl doctor +hermenix ctl chat +hermenix ctl update +hermenix ctl "sessions list" +hermenix ctl doctor ``` The `edit` variants pull the file out over vsock, open your `$EDITOR` @@ -349,14 +371,14 @@ mode 0600 and never pass through the Nix store — which is the whole reason there's no declarative option for them. If `status` and `logs` come back empty, the gateway unit is probably named -something other than `hermes-agent`. Run `hermes-ctl units` and set +something other than `hermes-agent`. Run `hermenix ctl units` and set `services.hermenix.gatewayUnit` to match; upstream has used more than one name for it. ## Seeing what the guest is running ```bash -hermes-vm config +hermenix vm config ``` Prints the current system store path, the last ten generations, the git log @@ -367,7 +389,7 @@ the running system disagree. To read or diff the config locally: ```bash -hermes-vm config-pull ./guest-live +hermenix vm config-pull ./guest-live diff -ru guest/ guest-live/ ``` @@ -560,14 +582,14 @@ Two things that catch people: `btusb` off it: `boot.blacklistedKernelModules = [ "btusb" ];`, and don't enable `hardware.bluetooth` on the host. - **No hotplug.** QEMU resolves these at start, so a device that's unplugged - when the VM boots won't appear when you plug it in. `hermes-vm restart` + when the VM boots won't appear when you plug it in. `hermenix vm restart` after plugging. Passing a whole USB *controller* through as a PCI device gets you real hotplug for anything in its ports, at the cost of that controller being exclusive to the guest. ### When it doesn't start -`hermes-vm console` first. Then, in rough order of likelihood: the device +`hermenix vm console` first. Then, in rough order of likelihood: the device didn't bind to vfio-pci (`lspci -nnk`), the IOMMU isn't on (`dmesg | grep -i iommu`), or something in the group is still held by the host. The module lifts `LimitMEMLOCK` for you when PCI passthrough is configured, since VFIO @@ -581,18 +603,20 @@ almost every real mistake. ```bash # The agent's own config history -hermes-ctl soul # what it thinks it is now -hermes-vm ssh git -C /etc/nixos log --oneline +hermenix ctl soul # what it thinks it is now +hermenix vm ssh "git -C /etc/nixos log --oneline" # Previous NixOS generations, also in the guest's boot menu -hermes-vm ssh sudo nixos-rebuild switch --rollback +hermenix vm ssh "sudo nixos-rebuild switch --rollback" # Snapshot of the state disk -hermes-vm snapshots -hermes-vm rollback state-20260914-1400 +hermenix vm snapshot # trigger one now, e.g. before a risky change +hermenix vm snapshots +hermenix vm rollback state-20260914-1400 # asks you to type 'rollback' to confirm # Restore from backup -hermes-vm backups +hermenix vm backup # trigger one now +hermenix vm backups ``` ### Snapshots @@ -604,7 +628,7 @@ The host fsfreezes the guest through qemu-guest-agent around each one, so the snapshot captures a consistent filesystem rather than the equivalent of pulling the power. Takes milliseconds; the VM keeps running. -`hermes-vm rollback` stops the VM, moves the current state subvolume aside as +`hermenix vm rollback` stops the VM, moves the current state subvolume aside as `state.old-`, and restores the snapshot in its place. Nothing is deleted, so a mistaken rollback is itself reversible. Clean the `state.old-*` directories up yourself once you're happy. @@ -721,9 +745,14 @@ secrets into your host's store. happens in the initrd. - Snapshots need btrfs. Everything else works on any filesystem; without it you rely on backups alone for recovery. -- `state.old-*` directories left by `hermes-vm rollback` are not cleaned up +- `state.old-*` directories left by `hermenix vm rollback` are not cleaned up automatically, on purpose. Delete them yourself once you're sure. - First boot builds a lot. Give it time. +- `services.hermenix.hermesHome` currently defaults to a path that doesn't + exist in the guest (it's built from the host's disk-image directory, not + the guest's mounted state disk) — set it explicitly on every deployment, + migrated or not. See "Migrating an existing deployment" below for the + value to use. --- @@ -740,8 +769,76 @@ nix run git+ssh://gitea@minipc1.lab.internal:22/ari/hermenix.git#hermenix -- mig Run `... -- migrate --help` for the full set of options (vsock CID, guest user, destination path, hostname rewrites). It copies — never moves or deletes — the source data, so the original stays a safe -fallback until you've verified the new VM. +fallback until you've verified the new VM. To run it from a different +machine than the one hosting the VM, see `hermenix remote` above — the same +`migrate /path/...` argument goes in the quoted command string. -The module's `services.hermenix.hermesHome` option lets `hermes-ctl` (and +The module's `services.hermenix.hermesHome` option lets `hermenix ctl` (and your own `HERMES_HOME` export inside the guest) point at wherever that -existing flat layout lands, instead of the default seeded `~/.hermes`. +existing flat layout lands, instead of the default seeded `.hermes` under the +state disk. + +**Set `hermesHome` explicitly, even if you're not migrating.** The option's +shipped default doesn't currently resolve to a real path inside the guest — +it's built from the host's own disk-image directory rather than the guest's +mounted state disk, so `hermenix ctl soul`/`conf`/`secrets`/etc. will fail +against it unhelpfully until you override it. For the default seeded layout: + +```nix +services.hermenix.hermesHome = "/var/lib/hermes/.hermes"; +``` + +For a migrated flat layout, use whatever `--dest` you passed to `migrate` +(default `/var/lib/hermes`). + +### Don't run it with `sudo` + +`migrate` (like `hermenix vm ssh` and `hermenix ctl`) reaches the guest with +a plain `ssh` call over vsock, authenticating with whatever key or agent the +invoking user already has — the same identity that's in the guest's +`authorizedKeys`. Prefixing the command with `sudo` runs that `ssh` as +**root** instead, which has no matching key, and you'll hit: + +``` +hermes@hermes: Permission denied (publickey,keyboard-interactive). +``` + +If `hermenix vm ssh` works for you as your normal user, `migrate` doesn't +need anything more privileged — run it the same way. + +### If some of the source can't be read + +Old flat `HERMES_HOME` directories, especially ones left behind by a +rootless-Docker deployment, sometimes contain a stray entry owned by a +container-internal uid with no mapping on the host — unreadable by anyone +outside the container. `migrate` tars the source with `--ignore-failed-read`, +so a warning like this is expected and non-fatal: + +``` +tar: ./.hermes: Cannot open: Permission denied +``` + +Everything else still gets copied and the run still completes, including the +hostname rewrite step. If you actually need what's in the unreadable entry, +grab it separately, as root, before or after the main migration — e.g.: + +```bash +sudo tar -C /path/to/HERMES_HOME/.hermes -cf - . \ + | hermenix vm ssh 'mkdir -p /var/lib/hermes/.hermes-old && tar -C /var/lib/hermes/.hermes-old -xf -' +``` + + +## Hacking on this repo + +The devshell provides nushell, nufmt, and pre-commit hooks. Enter it once: + +```bash +direnv allow +``` + +On first entry devenv installs the pre-commit hooks automatically. Two hooks +run on every commit: + +- **nufmt** — formats all `*.nu` files in place +- **gen-completions** — reruns `nu/gen-completions.nu` and stages + `nu/completions.nu` whenever a usage block in the source files changes diff --git a/flake.lock b/flake.lock index 5d34fa5..e41bbef 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,55 @@ { "nodes": { + "cachix": { + "inputs": { + "devenv": [ + "devenv" + ], + "flake-compat": [ + "devenv", + "flake-compat" + ], + "git-hooks": [ + "devenv", + "git-hooks" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1788181969, + "narHash": "sha256-OUB6hPlFBB9FRdZgZXSye4lDOg+fbrqKe8ePv2IM7NY=", + "owner": "cachix", + "repo": "cachix", + "rev": "98093e8eea6c1635ef1337a576fa76f3e8bd4f39", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "latest", + "repo": "cachix", + "type": "github" + } + }, + "crate2nix": { + "flake": false, + "locked": { + "lastModified": 1772186516, + "narHash": "sha256-8s28pzmQ6TOIUzznwFibtW1CMieMUl1rYJIxoQYor58=", + "owner": "rossng", + "repo": "crate2nix", + "rev": "ba5dd398e31ee422fbe021767eb83b0650303a6e", + "type": "github" + }, + "original": { + "owner": "rossng", + "repo": "crate2nix", + "rev": "ba5dd398e31ee422fbe021767eb83b0650303a6e", + "type": "github" + } + }, "determinate": { "inputs": { "determinate-nixd-aarch64-darwin": "determinate-nixd-aarch64-darwin", @@ -57,6 +107,35 @@ "url": "https://install.determinate.systems/determinate-nixd/tag/v3.22.3/x86_64-linux" } }, + "devenv": { + "inputs": { + "cachix": "cachix", + "crate2nix": "crate2nix", + "flake-compat": "flake-compat_2", + "flake-parts": "flake-parts_2", + "ghostty": "ghostty", + "git-hooks": "git-hooks", + "nix": "nix_2", + "nixd": "nixd", + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1789480904, + "narHash": "sha256-anHWHTkxI7423/eUroN9Rc4+H8VIGyIUWq/hV1egBDQ=", + "owner": "cachix", + "repo": "devenv", + "rev": "0fd5e6d3a9b2ae6f10205a3e95e0ca58d526c5a6", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -73,6 +152,22 @@ "type": "github" } }, + "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -94,6 +189,69 @@ "url": "https://flakehub.com/f/hercules-ci/flake-parts/0.1" } }, + "flake-parts_2": { + "inputs": { + "nixpkgs-lib": [ + "devenv", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1788306937, + "narHash": "sha256-lbrI1UYVpFgKSsyrU9oeF7FHfnu8nLD9ja0ou9f2rxk=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f16b25b8c3d2809b87925d0b76652d7821a75c68", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "ghostty": { + "flake": false, + "locked": { + "lastModified": 1786026742, + "narHash": "sha256-tRtgTg/i3w1nFdRHU2IGekfJN8EyP+HARW4MtoUZyqk=", + "owner": "ghostty-org", + "repo": "ghostty", + "rev": "22d13172cde98a0a4dda05d3d6a3fcb0dd8ed018", + "type": "github" + }, + "original": { + "owner": "ghostty-org", + "repo": "ghostty", + "rev": "22d13172cde98a0a4dda05d3d6a3fcb0dd8ed018", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1788267358, + "narHash": "sha256-nt+lUqYVpc9Y6JeMd2WmXzCDojasdadKo0mWcluvY2Y=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "27555e2624241fb116b49095df4caaee85a25691", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "git-hooks-nix": { "inputs": { "flake-compat": "flake-compat", @@ -137,6 +295,72 @@ "url": "https://flakehub.com/f/DeterminateSystems/nix-src/%2A" } }, + "nix_2": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "flake-parts": [ + "devenv", + "flake-parts" + ], + "git-hooks-nix": [ + "devenv", + "git-hooks" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-23-11": [ + "devenv" + ], + "nixpkgs-regression": [ + "devenv" + ] + }, + "locked": { + "lastModified": 1788036898, + "narHash": "sha256-3NT3yTvoRT7+rxLDNovpyeTDIJkZlBoO72rcu2x9Y9o=", + "owner": "cachix", + "repo": "nix", + "rev": "b9b81726b38469c55b9706d80d37d6c73cc7f76c", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "devenv-2.35", + "repo": "nix", + "type": "github" + } + }, + "nixd": { + "inputs": { + "flake-parts": [ + "devenv", + "flake-parts" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "treefmt-nix": "treefmt-nix" + }, + "locked": { + "lastModified": 1788165251, + "narHash": "sha256-ITHt6eU6DuwQqlNV1/wehvKYPE6J8sZIlv75CpDjcHI=", + "owner": "nix-community", + "repo": "nixd", + "rev": "e2ca72c353cfbc3d63e942fe9aeae4ec497863bf", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixd", + "type": "github" + } + }, "nixlib": { "locked": { "lastModified": 1736643958, @@ -250,9 +474,53 @@ "root": { "inputs": { "determinate": "determinate", + "devenv": "devenv", "nixos-generators": "nixos-generators", "nixpkgs": "nixpkgs_3" } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "devenv", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1788332415, + "narHash": "sha256-BTFrmyh0oaVDsvA9NNw0YpbbeppTwU0t70iVx672Ew8=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "860d7c835ab91bfc8972b67092f5f2db8e9390a0", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "devenv", + "nixd", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1786901030, + "narHash": "sha256-WSFCsDSE5ffgD2MqzkM2CYjeFiKhRF/dJUN8uedb6YE=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "27b3b12a8e6375f28ebe122f07d230ca5459bbfa", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 2b9bf73..e115aa7 100644 --- a/flake.nix +++ b/flake.nix @@ -8,10 +8,14 @@ url = "github:nix-community/nixos-generators"; inputs.nixpkgs.follows = "nixpkgs"; }; + devenv = { + url = "github:cachix/devenv"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = - { self, nixpkgs, determinate, nixos-generators, ... }@inputs: + { self, nixpkgs, determinate, nixos-generators, devenv, ... }@inputs: let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; @@ -81,5 +85,42 @@ default = self.packages.${system}.hermenix; }; + + devShells.${system}.default = devenv.lib.mkShell { + inherit inputs pkgs; + modules = [{ + packages = with pkgs; [ + nushell + nufmt + ]; + + enterShell = '' + Welcom to Hermenix DevShell! + ''; + + git-hooks.hooks = { + # Format all .nu files with nufmt. + # nufmt = { + # enable = true; + # name = "nufmt"; + # entry = "${pkgs.nufmt}/bin/nufmt"; + # language = "system"; + # types = [ "file" ]; + # files = "\\.nu$"; + # }; + + # Regenerate nu/completions.nu whenever a usage block changes. + gen-completions = { + enable = true; + name = "gen-completions"; + description = "Regenerate nu/completions.nu from usage blocks"; + entry = "${pkgs.nushell}/bin/nu nu/gen-completions.nu"; + language = "system"; + pass_filenames = false; + files = "^nu/(hermenix\\.nu|vm/vm\\.nu|ctl/ctl\\.nu)$"; + }; + }; + }]; + }; }; } diff --git a/nu/completions.nu b/nu/completions.nu index 8bdd9e2..161199c 100644 --- a/nu/completions.nu +++ b/nu/completions.nu @@ -55,7 +55,7 @@ def "nu-complete hermenix ctl" [] { # Context-aware completer: inspects already-typed words to decide # whether to offer top-level subcommands or a second-level list. def "nu-complete hermenix ctx" [context: string, offset: int] { - let words = ($context | str trim | split row " " | where { $in != "" }) + let words = $context | str trim | split row " " | where { $in != "" } match ($words | length) { 1 => (nu-complete hermenix top) 2 => { diff --git a/nu/ctl/ctl.nu b/nu/ctl/ctl.nu index a92dcc9..151e203 100644 --- a/nu/ctl/ctl.nu +++ b/nu/ctl/ctl.nu @@ -5,33 +5,37 @@ # # Depends on: lib/common.nu, lib/guest.nu (ssh-args). -def home-dir [] { $env.HERMES_HOME_DIR? | default "/var/lib/hermes-vm/.hermes" } -def gateway-unit [] { $env.HERMES_GATEWAY_UNIT? | default "hermes-agent" } +def home-dir [] { + $env.HERMES_HOME_DIR? | default "/var/lib/hermes-vm/.hermes" +} +def gateway-unit [] { + $env.HERMES_GATEWAY_UNIT? | default "hermes-agent" +} def ctl-gssh [cmd: string] { run-argv ((ssh-args) ++ [$cmd]) } # Pull a file out of the guest, open it in $EDITOR, push it back. # Skips the write if nothing changed (hash comparison). def edit-remote [remote: string, mode: string = "0644"] { - let tmp = (mktemp) - let fetched = (do -i { ctl-gssh $"cat ($remote)" } | complete) - $fetched.stdout | save -f $tmp - let before = (open --raw $tmp | hash sha256) + let tmp = (mktemp) + let fetched = do -i { ctl-gssh $"cat ($remote)" } | complete + $fetched.stdout | save -f $tmp + let before = open --raw $tmp | hash sha256 - ^($env.EDITOR? | default "vim") $tmp + ^($env.EDITOR? | default "vim") $tmp - if (open --raw $tmp | hash sha256) == $before { + if (open --raw $tmp | hash sha256) == $before { + rm $tmp + say "unchanged" + return + } + + (open --raw $tmp) | ctl-gssh $"install -m ($mode) /dev/stdin ($remote)" rm $tmp - say "unchanged" - return - } - - (open --raw $tmp) | ctl-gssh $"install -m ($mode) /dev/stdin ($remote)" - rm $tmp - say $"written to ($remote)" - if (confirm "restart the gateway now?") { - ctl-gssh $"systemctl --user restart (gateway-unit)" - } + say $"written to ($remote)" + if (confirm "restart the gateway now?") { + ctl-gssh $"systemctl --user restart (gateway-unit)" + } } # Everything not matched by a more-specific `main ctl *` goes straight to the @@ -42,86 +46,88 @@ def edit-remote [remote: string, mode: string = "0644"] { # reaches a rest parameter, so `hermenix ctl chat --model x` would fail to # parse. Quote the whole thing instead — e.g. `hermenix ctl "chat --model x"`. def "main ctl" [remainder: string = ""] { - if $remainder == "" { - print "usage: hermenix ctl " - print "" - print " soul [edit] show or edit SOUL.md" - print " memory [edit] show or edit memories/USER.md" - print " conf [edit] show or edit config.yaml" - print " secrets [edit|show] manage .env (keys masked by default)" - print " status gateway service status" - print " logs follow gateway journal (-f)" - print " restart restart the gateway" - print " units list all hermes* user units" - print " reseed overwrite identity from /etc/nixos/hermes" - print " pull [dir] copy live identity back to the host" - print "" - print "Anything else is passed straight to the hermes CLI in the guest:" - print " hermenix ctl \"chat\"" - print " hermenix ctl \"update\"" - print " hermenix ctl \"sessions list\"" - exit 1 - } - let words = ($remainder | split row " " | where {|w| $w != "" }) - run-argv ((ssh-args) ++ ["-t" "hermes"] ++ $words) + if $remainder == "" { + print "usage: hermenix ctl " + print "" + print " soul [edit] show or edit SOUL.md" + print " memory [edit] show or edit memories/USER.md" + print " conf [edit] show or edit config.yaml" + print " secrets [edit|show] manage .env (keys masked by default)" + print " status gateway service status" + print " logs follow gateway journal (-f)" + print " restart restart the gateway" + print " units list all hermes* user units" + print " reseed overwrite identity from /etc/nixos/hermes" + print " pull [dir] copy live identity back to the host" + print "" + print "Anything else is passed straight to the hermes CLI in the guest:" + print " hermenix ctl \"chat\"" + print " hermenix ctl \"update\"" + print " hermenix ctl \"sessions list\"" + exit 1 + } + let words = $remainder | split row " " | where {|w| $w != "" } + run-argv ((ssh-args) ++ ["-t" "hermes"] ++ $words) } def "main ctl soul" [action?: string] { - if $action == "edit" { - edit-remote $"(home-dir)/SOUL.md" - } else { - ctl-gssh $"cat (home-dir)/SOUL.md" - } + if $action == "edit" { + edit-remote $"(home-dir)/SOUL.md" + } else { + ctl-gssh $"cat (home-dir)/SOUL.md" + } } def "main ctl memory" [action?: string] { - if $action == "edit" { - edit-remote $"(home-dir)/memories/USER.md" - } else { - ctl-gssh $"cat (home-dir)/memories/USER.md" - } + if $action == "edit" { + edit-remote $"(home-dir)/memories/USER.md" + } else { + ctl-gssh $"cat (home-dir)/memories/USER.md" + } } def "main ctl conf" [action?: string] { - if $action == "edit" { - edit-remote $"(home-dir)/config.yaml" - } else { - ctl-gssh $"cat (home-dir)/config.yaml" - } + if $action == "edit" { + edit-remote $"(home-dir)/config.yaml" + } else { + ctl-gssh $"cat (home-dir)/config.yaml" + } } # API keys. Mode 0600, never enters the Nix store, and not printed unless # you ask for it explicitly. def "main ctl secrets" [action?: string] { - if $action == "edit" { - edit-remote $"(home-dir)/.env" "0600" - } else if $action == "show" { - ctl-gssh $"cat (home-dir)/.env" - } else { - ctl-gssh $"sed 's/=.*/=/' (home-dir)/.env" - } + if $action == "edit" { + edit-remote $"(home-dir)/.env" "0600" + } else if $action == "show" { + ctl-gssh $"cat (home-dir)/.env" + } else { + ctl-gssh $"sed 's/=.*/=/' (home-dir)/.env" + } } -def "main ctl status" [] { ctl-gssh $"systemctl --user status (gateway-unit) --no-pager" } -def "main ctl logs" [] { run-argv ((ssh-args) ++ ["-t" $"journalctl --user -u (gateway-unit) -f"]) } +def "main ctl status" [] { ctl-gssh $"systemctl --user status (gateway-unit) --no-pager" } +def "main ctl logs" [] { run-argv ((ssh-args) ++ ["-t" $"journalctl --user -u (gateway-unit) -f"]) } def "main ctl restart" [] { ctl-gssh $"systemctl --user restart (gateway-unit)" } -def "main ctl units" [] { ctl-gssh "systemctl --user list-units 'hermes*' --all --no-pager" } +def "main ctl units" [] { ctl-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 ctl 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" } - ctl-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" + 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" } + ctl-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 ctl pull" [dest: string = "./hermes-home"] { - mkdir $dest - (ctl-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)" + mkdir $dest + ( + ctl-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)" } diff --git a/nu/gen-completions.nu b/nu/gen-completions.nu index 56f7b1b..a83c439 100644 --- a/nu/gen-completions.nu +++ b/nu/gen-completions.nu @@ -25,10 +25,10 @@ # extracted name contains a quote or starts with "hermenix" (those are # passthrough-example lines, not real subcommands). def parse-usage [file: string, strip_prefix: string = ""] { - open --raw $file - | lines - | where { $in =~ 'print " \S' } - | each {|line| + open --raw $file + | lines + | where { $in =~ 'print " \S' } + | each {|line| # Strip everything up to and including the opening `print "` and the # closing `"` (last one), leaving just the inner usage string. let inner = ($line @@ -44,47 +44,47 @@ def parse-usage [file: string, strip_prefix: string = ""] { } # Split on 2+ spaces — left side is "name [optional args]", right is desc. - let parts = ($body | str trim | split row -r " +" | where { $in != "" }) + let parts = $body | str trim | split row -r " +" | where { $in != "" } if ($parts | length) < 2 { null } else { - let name = ($parts | first | split row " " | first) + let name = $parts | first | split row " " | first # Re-join all remaining parts — descriptions like "systemctl stop hermes-vm" # would otherwise get cut at the double-space inside the description. - let desc = ($parts | skip 1 | str join " " | str trim) + let desc = $parts | skip 1 | str join " " | str trim {value: $name, description: $desc} } } - | where { $in != null } - | where { $in.value != "" } - | where { not ($in.value | str contains '"') } - | where { not ($in.value | str starts-with "hermenix") } - | uniq-by value + | where { $in != null } + | where { $in.value != "" } + | where { not ($in.value | str contains '"') } + | where { not ($in.value | str starts-with "hermenix") } + | uniq-by value } # Render one `nu-complete` function that returns a [value description] table. -def render-complete [fn_name: string, entries: list] { - let rows = ($entries +def render-complete [fn_name: string, entries: list] { + let rows = ($entries | each {|e| $" [($e.value) \"($e.description)\"]" } | str join "\n") - [ - $"def \"nu-complete ($fn_name)\" [] {" - " [" - " [value description];" - $rows - " ]" - "}" - ] | str join "\n" + [ + $"def \"nu-complete ($fn_name)\" [] {" + " [" + " [value description];" + $rows + " ]" + "}" + ] | str join "\n" } # Render the context-aware dispatcher that picks between the three tables. # Written as a single-quoted literal — no interpolation needed. def render-ctx [] { -'# Context-aware completer: inspects already-typed words to decide + '# Context-aware completer: inspects already-typed words to decide # whether to offer top-level subcommands or a second-level list. def "nu-complete hermenix ctx" [context: string, offset: int] { - let words = ($context | str trim | split row " " | where { $in != "" }) + let words = $context | str trim | split row " " | where { $in != "" } match ($words | length) { 1 => (nu-complete hermenix top) 2 => { @@ -100,48 +100,47 @@ def "nu-complete hermenix ctx" [context: string, offset: int] { } def render-extern [] { - 'extern "hermenix" [...args: string@"nu-complete hermenix ctx"]' + 'extern "hermenix" [...args: string@"nu-complete hermenix ctx"]' } def render-header [] { - [ - "# Tab-completion definitions for hermenix." - "#" - "# !! GENERATED — do not edit by hand." - "# !! Regenerate with: nu nu/gen-completions.nu" - "#" - "# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS" - "# picks them up automatically from environment.systemPackages. No manual" - "# sourcing needed." - ] | str join "\n" + [ + "# Tab-completion definitions for hermenix." + "#" + "# !! GENERATED — do not edit by hand." + "# !! Regenerate with: nu nu/gen-completions.nu" + "#" + "# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS" + "# picks them up automatically from environment.systemPackages. No manual" + "# sourcing needed." + ] | str join "\n" } # ── main ──────────────────────────────────────────────────────────────── -let repo_root = ($env.FILE_PWD | path dirname) -let out_file = ($repo_root | path join "nu" "completions.nu") - -let top = (parse-usage ($repo_root | path join "nu" "hermenix.nu") "hermenix") -let vm = (parse-usage ($repo_root | path join "nu" "vm" "vm.nu")) -let ctl = (parse-usage ($repo_root | path join "nu" "ctl" "ctl.nu")) +let repo_root = $env.FILE_PWD | path dirname +let out_file = $repo_root | path join "nu" "completions.nu" +let top = parse-usage ($repo_root | path join "nu" "hermenix.nu") "hermenix" +let vm = parse-usage ($repo_root | path join "nu" "vm" "vm.nu") +let ctl = parse-usage ($repo_root | path join "nu" "ctl" "ctl.nu") if ($top | is-empty) { print -e "warn: no top-level completions found in hermenix.nu" } -if ($vm | is-empty) { print -e "warn: no vm completions found in vm/vm.nu" } +if ($vm | is-empty) { print -e "warn: no vm completions found in vm/vm.nu" } if ($ctl | is-empty) { print -e "warn: no ctl completions found in ctl/ctl.nu" } [ - (render-header) - "" - (render-complete "hermenix top" $top) - "" - (render-complete "hermenix vm" $vm) - "" - (render-complete "hermenix ctl" $ctl) - "" - (render-ctx) - "" - (render-extern) - "" + (render-header) + "" + (render-complete "hermenix top" $top) + "" + (render-complete "hermenix vm" $vm) + "" + (render-complete "hermenix ctl" $ctl) + "" + (render-ctx) + "" + (render-extern) + "" ] | str join "\n" | save -f $out_file print $"wrote ($out_file) [top=($top | length) vm=($vm | length) ctl=($ctl | length)]" diff --git a/nu/hermenix.nu b/nu/hermenix.nu index 968aa88..7e7f85d 100644 --- a/nu/hermenix.nu +++ b/nu/hermenix.nu @@ -26,14 +26,14 @@ # hermenix.nu this file — top-level dispatch (main) def main [] { - print "usage: hermenix ..." - print "" - print " hermenix setup guided host-side setup" - print " hermenix migrate carry an existing flat HERMES_HOME into the guest" - print " hermenix remote \"\" run setup/migrate on a remote host over SSH" - print "" - print " hermenix vm control the VM (start/stop/console/snapshots/backups)" - print " hermenix ctl drive Hermes inside the guest (identity/config/secrets/gateway)" - print "" - print "run `hermenix vm` or `hermenix ctl` for their full subcommand lists." + print "usage: hermenix ..." + print "" + print " hermenix setup guided host-side setup" + print " hermenix migrate carry an existing flat HERMES_HOME into the guest" + print " hermenix remote \"\" run setup/migrate on a remote host over SSH" + print "" + print " hermenix vm control the VM (start/stop/console/snapshots/backups)" + print " hermenix ctl drive Hermes inside the guest (identity/config/secrets/gateway)" + print "" + print "run `hermenix vm` or `hermenix ctl` for their full subcommand lists." } diff --git a/nu/host/migrate.nu b/nu/host/migrate.nu index 1676d96..97fe856 100644 --- a/nu/host/migrate.nu +++ b/nu/host/migrate.nu @@ -25,58 +25,58 @@ def "main migrate" [ --rewrite-to: list = [10.77.0.1 10.77.0.1 10.0.12.1] --rewrite-files: list = [config.yaml auth.json context_length_cache.yaml .env] ] { - if not ($source | path exists) { die $"($source) is not a directory" } - if ($rewrite_from | length) != ($rewrite_to | length) { - die "--rewrite-from and --rewrite-to must have the same length" - } + if not ($source | path exists) { die $"($source) is not a directory" } + if ($rewrite_from | length) != ($rewrite_to | length) { + die "--rewrite-from and --rewrite-to must have the same length" + } - let ssh_base = (vsock-ssh-args $vsock_cid $guest_user "hermes") - def gssh [cmd: string] { run-argv ($ssh_base ++ [$cmd]) } + 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)\)" - print "" - say "This will:" - say $" 1. tar '($source)' \(not modifying it\) and stream it into the guest" - say " 2. rewrite these hostnames in the migrated config, in the guest only:" - for i in 0..(($rewrite_from | length) - 1) { - say $" ($rewrite_from | get $i) -> ($rewrite_to | get $i)" - } - print "" - if not (confirm-word "migrate" "Type 'migrate' to proceed:") { die "aborted" } + say $"Source \(read-only\): ($source)" + say $"Destination \(guest\): ($guest_user)@hermes:($dest) \(over vsock, cid ($vsock_cid)\)" + print "" + say "This will:" + say $" 1. tar '($source)' \(not modifying it\) and stream it into the guest" + say " 2. rewrite these hostnames in the migrated config, in the guest only:" + for i in 0..(($rewrite_from | length) - 1) { + say $" ($rewrite_from | get $i) -> ($rewrite_to | get $i)" + } + print "" + if not (confirm-word "migrate" "Type 'migrate' to proceed:") { die "aborted" } - step 1 "confirming guest is reachable" - gssh "true" + step 1 "confirming guest is reachable" + gssh "true" - step 2 $"streaming ($source) -> guest:($dest) \(this can take a while for 8G+\)" - # --ignore-failed-read: a source entry can be unreadable (e.g. uid-mapped - # leftovers from a prior rootless-container deployment) without that - # aborting an otherwise-successful migration - tar still warns about it, - # it just doesn't fail the whole pipeline over it. - ^tar --ignore-failed-read -C $source -cf - . | run-argv ($ssh_base ++ [$"mkdir -p '($dest)' && tar -C '($dest)' -xf -"]) + step 2 $"streaming ($source) -> guest:($dest) \(this can take a while for 8G+\)" + # --ignore-failed-read: a source entry can be unreadable (e.g. uid-mapped + # leftovers from a prior rootless-container deployment) without that + # aborting an otherwise-successful migration - tar still warns about it, + # it just doesn't fail the whole pipeline over it. + ^tar --ignore-failed-read -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) + step 3 "rewriting stale hostnames in the guest" + let sed_expr = 0..(($rewrite_from | length) - 1 | each {|i| $"s#($rewrite_from | get $i)#($rewrite_to | get $i)#g" } | str join "; ") - for f in $rewrite_files { - let path = $"($dest)/($f)" - gssh $"[ -f '($path)' ] && sed -i '($sed_expr)' '($path)' && echo ' rewrote ($f)' || echo ' ($f) not present, skipping'" - } + for f in $rewrite_files { + let path = $"($dest)/($f)" + gssh $"[ -f '($path)' ] && sed -i '($sed_expr)' '($path)' && echo ' rewrote ($f)' || echo ' ($f) not present, skipping'" + } - print "" - ok $"Done. The original at ($source) is untouched." - print "" - say "Next, inside the guest (hermenix vm ssh):" - say $" export HERMES_HOME=($dest)" - say " # hermes installs itself automatically on first boot - if that's" - say " # already done (check: hermenix ctl units), just:" - say " # determinate-nixd login" - say " # sudo nix flake lock /etc/nixos" - say " # hermes setup" - say " # hermes gateway install" - print "" - say "Then verify with hermenix ctl status / logs before considering the old" - say "deployment retired. Decrypt secrets/hermes-env.age yourself and fold" - say $"anything relevant into ($dest)/.env \(hermenix ctl secrets edit\)." + print "" + ok $"Done. The original at ($source) is untouched." + print "" + say "Next, inside the guest (hermenix vm ssh):" + say $" export HERMES_HOME=($dest)" + say " # hermes installs itself automatically on first boot - if that's" + say " # already done (check: hermenix ctl units), just:" + say " # determinate-nixd login" + say " # sudo nix flake lock /etc/nixos" + say " # hermes setup" + say " # hermes gateway install" + print "" + say "Then verify with hermenix ctl status / logs before considering the old" + say "deployment retired. Decrypt secrets/hermes-env.age yourself and fold" + say $"anything relevant into ($dest)/.env \(hermenix ctl secrets edit\)." } diff --git a/nu/host/remote.nu b/nu/host/remote.nu index 4021c68..d35f6bc 100644 --- a/nu/host/remote.nu +++ b/nu/host/remote.nu @@ -15,20 +15,20 @@ def "main remote" [ cmd: string # subcommand + its own args, quoted as ONE string — e.g. "setup --clean" or "migrate /var/lib/hermes" --flake: string = "git+ssh://gitea@minipc1.lab.internal:22/ari/hermenix.git" ] { - # `cmd` has to be a single string, not a rest param: nu's own CLI parser - # treats any bare --flag-shaped token as an attempt to match a flag on - # THIS command (main remote), even inside a rest parameter and even with - # a `--` separator — so `hermenix remote host setup --clean` fails before - # ever reaching this function. Splitting cmd back into words here (and - # re-quoting each one) sidesteps that entirely. - let words = ($cmd | split row " " | where {|w| $w != "" }) - if ($words | is-empty) { - die $'usage: hermenix remote " [args...]"' - } + # `cmd` has to be a single string, not a rest param: nu's own CLI parser + # treats any bare --flag-shaped token as an attempt to match a flag on + # THIS command (main remote), even inside a rest parameter and even with + # a `--` separator — so `hermenix remote host setup --clean` fails before + # ever reaching this function. Splitting cmd back into words here (and + # re-quoting each one) sidesteps that entirely. + let words = $cmd | split row " " | where {|w| $w != "" } + if ($words | is-empty) { + die $'usage: hermenix remote " [args...]"' + } - let quoted_args = ($words | each {|a| shquote $a } | str join " ") - let remote_cmd = $"nix run --no-write-lock-file --refresh (shquote $flake)#hermenix -- ($quoted_args)" + let quoted_args = $words | each {|a| shquote $a } | str join " " + let remote_cmd = $"nix run --no-write-lock-file --refresh (shquote $flake)#hermenix -- ($quoted_args)" - say $"Running on ($host): ($cmd)" - ^ssh -t -o StrictHostKeyChecking=accept-new $host $remote_cmd + say $"Running on ($host): ($cmd)" + ^ssh -t -o StrictHostKeyChecking=accept-new $host $remote_cmd } diff --git a/nu/host/setup.nu b/nu/host/setup.nu index aa6f8e9..7e75f03 100644 --- a/nu/host/setup.nu +++ b/nu/host/setup.nu @@ -11,28 +11,28 @@ # normalize a bare integer answer to that rather than silently creating a # byte-sized disk. Anything with an existing suffix passes through as-is. def normalize-size [s: string] { - try { - let n = ($s | into int) - $"($n)G" - } catch { - $s - } + try { + let n = $s | into int + $"($n)G" + } catch { + $s + } } # Run a command, sudo-prefixed unless already root (set once via # $env.HERMENIX_NEED_SUDO at the top of `main setup`). Fails loudly, like any # other external command, if the command fails. def priv [...args: string] { - let need_sudo = ($env.HERMENIX_NEED_SUDO? | default false) - let argv = if $need_sudo { ["sudo"] ++ $args } else { $args } - ^($argv | first) ...($argv | skip 1) + let need_sudo = $env.HERMENIX_NEED_SUDO? | default false + let argv = if $need_sudo { ["sudo"] ++ $args } else { $args } + ^($argv | first) ...($argv | skip 1) } # Same as priv, but captures stdout/exit code instead of failing the script. def priv-out [...args: string] { - let need_sudo = ($env.HERMENIX_NEED_SUDO? | default false) - let argv = if $need_sudo { ["sudo"] ++ $args } else { $args } - (^($argv | first) ...($argv | skip 1) | complete) + let need_sudo = $env.HERMENIX_NEED_SUDO? | default false + let argv = if $need_sudo { ["sudo"] ++ $args } else { $args } + (^($argv | first) ...($argv | skip 1) | complete) } # Guided host-side setup: disks, cache signing key, /etc/hermenix/local.nix, @@ -43,215 +43,253 @@ def "main setup" [ --flake: string = "git+ssh://gitea@minipc1.lab.internal:22/ari/hermenix.git" --clean # stop the VM and delete existing disk images + UEFI vars first, for a fully fresh start ] { - $env.HERMENIX_NEED_SUDO = ((^id -u | str trim) != "0") + $env.HERMENIX_NEED_SUDO = ((^id -u | str trim) != "0") - let local_path = "/etc/hermenix/local.nix" - let cache_name = "hermes-cache-1" - let secret_key = "/var/lib/secrets/hermes-cache.secret" - let public_key = "/var/lib/secrets/hermes-cache.pub" - let determinate_sub = "https://install.determinate.systems" - let determinate_key = "cache.flakehub.com-3:hJuILl5sVK4iKm86JzgdXW12Y2Hwd5G07qKtHTOcDCM=" + let local_path = "/etc/hermenix/local.nix" + let cache_name = "hermes-cache-1" + let secret_key = "/var/lib/secrets/hermes-cache.secret" + let public_key = "/var/lib/secrets/hermes-cache.pub" + let determinate_sub = "https://install.determinate.systems" + let determinate_key = "cache.flakehub.com-3:hJuILl5sVK4iKm86JzgdXW12Y2Hwd5G07qKtHTOcDCM=" - print $"(ansi attr_bold) hermenix · guided setup(ansi reset)" - note "This prepares the host. It does not run nixos-rebuild — you'll paste a" - note "module snippet into your own configuration at the end." + print $"(ansi attr_bold) hermenix · guided setup(ansi reset)" + note "This prepares the host. It does not run nixos-rebuild — you'll paste a" + note "module snippet into your own configuration at the end." - step 1 "Checking prerequisites" - if (which nix | is-empty) { die "nix not found on PATH" } - if (do -i { ^nix flake --help } | complete).exit_code != 0 { - die "flakes are not enabled. Add 'experimental-features = nix-command flakes' to /etc/nix/nix.conf" - } - ok "nix with flakes" - - if (which btrfs | is-empty) { die "btrfs-progs not found. Snapshots need it." } - if (which mkfs.ext4 | is-empty) { die "mkfs.ext4 not found (e2fsprogs)" } - ok "btrfs-progs" - - if not ("/dev/kvm" | path exists) { - die "/dev/kvm is missing. KVM is required; check virtualisation is enabled in firmware." - } - ok "kvm" - - if (which nixos-version | is-empty) { - warn "this host does not look like NixOS — the module expects NixOS" - } else { - let ver = ((^nixos-version | complete).stdout | str trim | split row " " | first) - ok $"host is NixOS \(($ver)\)" - } - - step 2 "Guest disks" - note "Two disks, deliberately:" - note " system - NixOS and the Nix store. Big, churny, rebuildable. Not backed up." - note " state - identity, memories, workspace, /etc/nixos. Small. Snapshotted." - - let base = ask "Directory for the disk images" "/var/lib/hermes-vm" - - if $clean { - warn "--clean: this stops the VM and deletes the existing system/state" - warn "disk images and UEFI vars under this directory. The btrfs" - warn "subvolumes and any snapshots are left alone." - if not (confirm-word "clean" "Type 'clean' to confirm:") { die "aborted" } - do -i { priv systemctl stop hermes-vm.service } | complete | ignore - do -i { priv rm "-f" $"($base)/system/system.raw" } | complete | ignore - do -i { priv rm "-f" $"($base)/state/state.raw" } | complete | ignore - do -i { priv rm "-f" $"($base)/OVMF_VARS.fd" } | complete | ignore - # A fresh state disk means a fresh SSH host key on next boot - drop the - # stale known_hosts entry now so the next connect doesn't fail on a - # host-key mismatch. Not run through priv: this is the invoking user's - # own ~/.ssh/known_hosts, not a root-owned path. - do -i { ^ssh-keygen -R hermes } | complete | ignore - ok "cleaned existing disk images and stale known_hosts entry" - } - - let parent = ($base | path dirname) - let fstype = ((do -i { ^stat -f -c "%T" $parent } | complete).stdout | str trim) - let use_btrfs = if $fstype != "btrfs" { - warn $"($parent) is ($fstype), not btrfs" - warn "snapshots need btrfs. Everything else works; you'd rely on backups alone." - if not (confirm "Continue anyway?") { die "aborted" } - false - } else { - true - } - - priv mkdir "-p" $base - - def make-subvol [path: string, use_btrfs: bool] { - let exists = ($use_btrfs and (do -i { priv btrfs subvolume show $path } | complete).exit_code == 0) - if $use_btrfs and not $exists { - do -i { priv rmdir $path } | complete | ignore - priv btrfs subvolume create $path | ignore - priv chattr +C $path - do -i { priv btrfs property set $path compression none } | complete | ignore - priv chown "hermes-vm:hermes-vm" $path - ok $"subvolume ($path) \(nodatacow\)" - } else { - priv mkdir "-p" $path - priv chown "hermes-vm:hermes-vm" $path + step 1 "Checking prerequisites" + if (which nix | is-empty) { die "nix not found on PATH" } + if (do -i { ^nix flake --help } | complete).exit_code != 0 { + die "flakes are not enabled. Add 'experimental-features = nix-command flakes' to /etc/nix/nix.conf" } - } - make-subvol $"($base)/system" $use_btrfs - make-subvol $"($base)/state" $use_btrfs - priv mkdir "-p" $"($base)/snapshots" + ok "nix with flakes" - let system_img = $"($base)/system/system.raw" - let state_img = $"($base)/state/state.raw" + if (which btrfs | is-empty) { die "btrfs-progs not found. Snapshots need it." } + if (which mkfs.ext4 | is-empty) { die "mkfs.ext4 not found (e2fsprogs)" } + ok "btrfs-progs" - let system_existed = ((priv-out test "-f" $system_img).exit_code == 0) - if $system_existed { - let sz = ((priv-out du "-h" "--apparent-size" $system_img).stdout | str trim | split column "\t" | get column0.0) - priv chown "hermes-vm:hermes-vm" $system_img - ok $"system disk exists \(($sz)\)" - } else { - let syssize = (normalize-size (ask "System disk size" "80G")) - priv truncate "-s" $syssize $system_img - priv chown "hermes-vm:hermes-vm" $system_img - if $use_btrfs { - let attrs = (priv-out lsattr "-l" $system_img).stdout - if ($attrs | str contains "No_COW") { - ok "system disk created, nodatacow confirmed" - } else { - warn $"system disk created but nodatacow did NOT apply - check 'lsattr ($system_img)'" - } - } else { - ok "system disk created" + if not ("/dev/kvm" | path exists) { + die "/dev/kvm is missing. KVM is required; check virtualisation is enabled in firmware." } - } + ok "kvm" - if (priv-out test "-f" $state_img).exit_code == 0 { - let sz = ((priv-out du "-h" "--apparent-size" $state_img).stdout | str trim | split column "\t" | get column0.0) - priv chown "hermes-vm:hermes-vm" $state_img - ok $"state disk exists \(($sz)\)" - } else { - let statesize = (normalize-size (ask "State disk size" "20G")) - priv truncate "-s" $statesize $state_img - priv mkfs.ext4 "-q" "-L" hermes-state $state_img - priv chown "hermes-vm:hermes-vm" $state_img - ok "state disk created and formatted (label: hermes-state)" - } - - step 3 "Binary cache signing key" - note "The host serves its store to the guest read-only, so the guest doesn't" - note "re-download from the internet what you already have." - - let key_existed = (priv-out test "-f" $secret_key).exit_code == 0 - if $key_existed { - ok "signing key already exists" - } else { - priv mkdir "-p" /var/lib/secrets - priv nix-store "--generate-binary-cache-key" $cache_name $secret_key $public_key - priv chmod 0400 $secret_key - ok $"generated ($cache_name)" - } - let cache_pub = ((priv-out cat $public_key).stdout | str trim) - say $"Public key: (ansi dark_gray)($cache_pub)(ansi reset)" - - if (do -i { ^id nix-serve } | complete).exit_code == 0 { - priv chown nix-serve $secret_key - ok "key owned by nix-serve" - } else { - warn "the nix-serve user does not exist yet — that's normal on a first run" - note "after your nixos-rebuild, run:" - note $" sudo chown nix-serve ($secret_key)" - } - - step 4 "SSH access to the guest" - let existing = if ($local_path | path exists) { - let evaluated = (do -i { ^nix-instantiate --eval --json --strict -E $"import ($local_path)" } | complete) - if $evaluated.exit_code == 0 { - $evaluated.stdout | from json + if (which nixos-version | is-empty) { + warn "this host does not look like NixOS — the module expects NixOS" } else { - warn $"($local_path) exists but failed to evaluate - treating as empty" - { cachePublicKey: "", authorizedKeys: [] } + let ver = ( + (^nixos-version | complete).stdout + | str trim + | split row " " + | first + ) + ok $"host is NixOS \(($ver)\)" } - } else { - { cachePublicKey: "", authorizedKeys: [] } - } - let existing_keys = ($existing.authorizedKeys? | default []) - let replace_keys = if ($existing_keys | length) > 0 { - ok $"($existing_keys | length) key\(s\) already configured in ($local_path)" - confirm "Replace them?" - } else { - true - } + step 2 "Guest disks" + note "Two disks, deliberately:" + note " system - NixOS and the Nix store. Big, churny, rebuildable. Not backed up." + note " state - identity, memories, workspace, /etc/nixos. Small. Snapshotted." - mut selected_keys = $existing_keys - if $replace_keys { - mut picked = [] - let found = (glob $"($env.HOME)/.ssh/*.pub") - if ($found | length) > 0 { - say "Public keys found in ~/.ssh:" - for pair in ($found | enumerate) { - let comment = (open $pair.item | str trim | split row " " | skip 2 | str join " ") - note $" ($pair.index + 1)) ($pair.item | path basename) — ($comment)" - } - let pick = ask "Numbers to use (space separated, or 'n' to paste one)" "1" - if $pick != "n" { - for n in ($pick | split row " ") { - let idx = ($n | into int) - 1 - if $idx < 0 or $idx >= ($found | length) { - die $"no such option: ($n)" - } - $picked = ($picked | append (open ($found | get $idx) | str trim)) + let base = ask "Directory for the disk images" "/var/lib/hermes-vm" + + if $clean { + warn "--clean: this stops the VM and deletes the existing system/state" + warn "disk images and UEFI vars under this directory. The btrfs" + warn "subvolumes and any snapshots are left alone." + if not (confirm-word "clean" "Type 'clean' to confirm:") { die "aborted" } + do -i { priv systemctl stop hermes-vm.service } | complete | ignore + do -i { priv rm "-f" $"($base)/system/system.raw" } | complete | ignore + do -i { priv rm "-f" $"($base)/state/state.raw" } | complete | ignore + do -i { priv rm "-f" $"($base)/OVMF_VARS.fd" } | complete | ignore + # A fresh state disk means a fresh SSH host key on next boot - drop the + # stale known_hosts entry now so the next connect doesn't fail on a + # host-key mismatch. Not run through priv: this is the invoking user's + # own ~/.ssh/known_hosts, not a root-owned path. + do -i { ^ssh-keygen -R hermes } | complete | ignore + ok "cleaned existing disk images and stale known_hosts entry" + } + + let parent = $base | path dirname + let fstype = (do -i { ^stat -f -c "%T" $parent } | complete).stdout | str trim + let use_btrfs = if $fstype != "btrfs" { + warn $"($parent) is ($fstype), not btrfs" + warn "snapshots need btrfs. Everything else works; you'd rely on backups alone." + if not (confirm "Continue anyway?") { die "aborted" } + false + } else { + true + } + + priv mkdir "-p" $base + + def make-subvol [path: string, use_btrfs: bool] { + let exists = ( + $use_btrfs and (do -i { priv btrfs subvolume show $path } | complete).exit_code == 0 + ) + if $use_btrfs and not $exists { + do -i { priv rmdir $path } | complete | ignore + priv btrfs subvolume create $path | ignore + priv chattr +C $path + do -i { priv btrfs property set $path compression none } | complete | ignore + priv chown "hermes-vm:hermes-vm" $path + ok $"subvolume ($path) \(nodatacow\)" + } else { + priv mkdir "-p" $path + priv chown "hermes-vm:hermes-vm" $path + } + } + make-subvol $"($base)/system" $use_btrfs + make-subvol $"($base)/state" $use_btrfs + priv mkdir "-p" $"($base)/snapshots" + + let system_img = $"($base)/system/system.raw" + let state_img = $"($base)/state/state.raw" + + let system_existed = ((priv-out test "-f" $system_img).exit_code == 0) + if $system_existed { + let sz = ( + (priv-out du "-h" "--apparent-size" $system_img).stdout + | str trim + | split column "\t" + | get column0.0 + ) + priv chown "hermes-vm:hermes-vm" $system_img + ok $"system disk exists \(($sz)\)" + } else { + let syssize = (normalize-size (ask "System disk size" "80G")) + priv truncate "-s" $syssize $system_img + priv chown "hermes-vm:hermes-vm" $system_img + if $use_btrfs { + let attrs = (priv-out lsattr "-l" $system_img).stdout + if ($attrs | str contains "No_COW") { + ok "system disk created, nodatacow confirmed" + } else { + warn $"system disk created but nodatacow did NOT apply - check 'lsattr ($system_img)'" + } + } else { + ok "system disk created" } - } } - if ($picked | is-empty) { - let pasted = ask "Paste a public key (blank to skip)" - if $pasted != "" { $picked = [$pasted] } + if (priv-out test "-f" $state_img).exit_code == 0 { + let sz = ( + (priv-out du "-h" "--apparent-size" $state_img).stdout + | str trim + | split column "\t" + | get column0.0 + ) + priv chown "hermes-vm:hermes-vm" $state_img + ok $"state disk exists \(($sz)\)" + } else { + let statesize = (normalize-size (ask "State disk size" "20G")) + priv truncate "-s" $statesize $state_img + priv mkfs.ext4 "-q" "-L" hermes-state $state_img + priv chown "hermes-vm:hermes-vm" $state_img + ok "state disk created and formatted (label: hermes-state)" } - if ($picked | is-empty) { - warn "no SSH key set — you'll only be able to reach the guest with 'hermenix vm console'" - } - $selected_keys = $picked - } + step 3 "Binary cache signing key" + note "The host serves its store to the guest read-only, so the guest doesn't" + note "re-download from the internet what you already have." - step 5 $"Writing ($local_path)" - let key_lines = ($selected_keys | each {|k| $" \"($k)\"" } | str join "\n") - let content = $"# Written by `hermenix setup`. Lives on the host, not in a repo checkout - + let key_existed = (priv-out test "-f" $secret_key).exit_code == 0 + if $key_existed { + ok "signing key already exists" + } else { + priv mkdir "-p" /var/lib/secrets + (priv + nix-store + "--generate-binary-cache-key" + $cache_name + $secret_key + $public_key + ) + priv chmod 0400 $secret_key + ok $"generated ($cache_name)" + } + let cache_pub = (priv-out cat $public_key).stdout | str trim + say $"Public key: (ansi dark_gray)($cache_pub)(ansi reset)" + + if (do -i { ^id nix-serve } | complete).exit_code == 0 { + priv chown nix-serve $secret_key + ok "key owned by nix-serve" + } else { + warn "the nix-serve user does not exist yet — that's normal on a first run" + note "after your nixos-rebuild, run:" + note $" sudo chown nix-serve ($secret_key)" + } + + step 4 "SSH access to the guest" + let existing = if ($local_path | path exists) { + let evaluated = ( + do -i { ^nix-instantiate --eval --json --strict -E $"import ($local_path)" } + | complete + ) + if $evaluated.exit_code == 0 { + $evaluated.stdout | from json + } else { + warn $"($local_path) exists but failed to evaluate - treating as empty" + { + cachePublicKey: "" + authorizedKeys: [] + } + } + } else { + { + cachePublicKey: "" + authorizedKeys: [] + } + } + let existing_keys = $existing.authorizedKeys? | default [] + + let replace_keys = if ($existing_keys | length) > 0 { + ok $"($existing_keys | length) key\(s\) already configured in ($local_path)" + confirm "Replace them?" + } else { + true + } + + mut selected_keys = $existing_keys + if $replace_keys { + mut picked = [] + let found = (glob $"($env.HOME)/.ssh/*.pub") + if ($found | length) > 0 { + say "Public keys found in ~/.ssh:" + for pair in ($found | enumerate) { + let comment = ( + open $pair.item + | str trim + | split row " " + | skip 2 + | str join " " + ) + note $" ($pair.index + 1)) ($pair.item | path basename) — ($comment)" + } + let pick = ask "Numbers to use (space separated, or 'n' to paste one)" "1" + if $pick != "n" { + for n in ($pick | split row " ") { + let idx = ($n | into int) - 1 + if $idx < 0 or $idx >= ($found | length) { + die $"no such option: ($n)" + } + $picked = ($picked | append (open ($found | get $idx) | str trim)) + } + } + } + + if ($picked | is-empty) { + let pasted = ask "Paste a public key (blank to skip)" + if $pasted != "" { $picked = [$pasted] } + } + + if ($picked | is-empty) { + warn "no SSH key set — you'll only be able to reach the guest with 'hermenix vm console'" + } + $selected_keys = $picked + } + + step 5 $"Writing ($local_path)" + let key_lines = $selected_keys | each {|k| $" \"($k)\"" } | str join "\n" + let content = $"# Written by `hermenix setup`. Lives on the host, not in a repo checkout - # see guest/local.nix.example in the hermenix repo for the reference shape. \{ cachePublicKey = \"($cache_pub)\"; @@ -260,60 +298,67 @@ def "main setup" [ ]; \} " - let tmp = (^mktemp | str trim) - $content | save -f $tmp - priv mkdir "-p" (($local_path | path dirname)) - priv cp $tmp $local_path - priv chmod 0644 $local_path - rm $tmp - ok $"($local_path) written \(($selected_keys | length) key\(s\)\)" + let tmp = (^mktemp | str trim) + $content | save -f $tmp + priv mkdir "-p" (($local_path | path dirname)) + priv cp $tmp $local_path + priv chmod 0644 $local_path + rm $tmp + ok $"($local_path) written \(($selected_keys | length) key\(s\)\)" - step 6 "Building the bootstrap image" - note "The extra substituter flags stop you compiling Determinate Nix yourself." - note "--impure is needed to read /etc/hermenix/local.nix at eval time." - if (confirm "Build now? (first run can take a while)") { - ^nix build --impure --no-write-lock-file --refresh $"($flake)#hermes-guest-image" --option extra-substituters $determinate_sub --option extra-trusted-public-keys $determinate_key - ok "built" - } else { - say "Skipped. Run manually:" - note $" nix build --impure --no-write-lock-file --refresh ($flake)#hermes-guest-image \\" - note $" --option extra-substituters ($determinate_sub) \\" - note $" --option extra-trusted-public-keys ($determinate_key)" - } - - step 7 "Writing the bootstrap image to the system disk" - let images = (glob "result/*.img") - if ($images | is-empty) { - warn "no image found under ./result - skipping" - note "build it, then:" - note $" sudo dd if=result/nixos.img of=($system_img) bs=4M conv=notrunc,fsync status=progress" - } else { - let image = ($images | first) - let img_bytes = (ls $image | get size.0 | into int) - let vol_bytes = ((priv-out stat "-c" "%s" $system_img).stdout | str trim | into int) - say $"image: ($image) \((($img_bytes / 1024 / 1024) | math floor) MiB\)" - say $"target: ($system_img) \((($vol_bytes / 1024 / 1024 / 1024) | math floor) GiB\)" - if $img_bytes > $vol_bytes { die "image is larger than the system disk" } - - if $system_existed { - warn "the system disk already existed. Writing the image replaces the" - warn "guest OS. Hermes's identity and memories live on the STATE disk and" - warn "are untouched, but anything it installed outside /var/lib/hermes is lost." - } - - if (confirm-word "overwrite" "Type 'overwrite' to write the image:") { - priv dd $"if=($image)" $"of=($system_img)" "bs=4M" "conv=notrunc,fsync" "status=progress" - ok "written" + step 6 "Building the bootstrap image" + note "The extra substituter flags stop you compiling Determinate Nix yourself." + note "--impure is needed to read /etc/hermenix/local.nix at eval time." + if (confirm "Build now? (first run can take a while)") { + ^nix build --impure --no-write-lock-file --refresh $"($flake)#hermes-guest-image" --option extra-substituters $determinate_sub --option extra-trusted-public-keys $determinate_key + ok "built" } else { - say "Skipped." + say "Skipped. Run manually:" + note $" nix build --impure --no-write-lock-file --refresh ($flake)#hermes-guest-image \\" + note $" --option extra-substituters ($determinate_sub) \\" + note $" --option extra-trusted-public-keys ($determinate_key)" } - } - step 8 "Next: add the module to your system flake" - let uplink = ((do -i { ^ip route get 1.1.1.1 } | complete).stdout + step 7 "Writing the bootstrap image to the system disk" + let images = (glob "result/*.img") + if ($images | is-empty) { + warn "no image found under ./result - skipping" + note "build it, then:" + note $" sudo dd if=result/nixos.img of=($system_img) bs=4M conv=notrunc,fsync status=progress" + } else { + let image = $images | first + let img_bytes = ls $image | get size.0 | into int + let vol_bytes = (priv-out stat "-c" "%s" $system_img).stdout | str trim | into int + say $"image: ($image) \((($img_bytes / 1024 / 1024) | math floor) MiB\)" + say $"target: ($system_img) \((($vol_bytes / 1024 / 1024 / 1024) | math floor) GiB\)" + if $img_bytes > $vol_bytes { die "image is larger than the system disk" } + + if $system_existed { + warn "the system disk already existed. Writing the image replaces the" + warn "guest OS. Hermes's identity and memories live on the STATE disk and" + warn "are untouched, but anything it installed outside /var/lib/hermes is lost." + } + + if (confirm-word "overwrite" "Type 'overwrite' to write the image:") { + (priv + dd + $"if=($image)" + $"of=($system_img)" + "bs=4M" + "conv=notrunc,fsync" + "status=progress" + ) + ok "written" + } else { + say "Skipped." + } + } + + step 8 "Next: add the module to your system flake" + let uplink = ((do -i { ^ip route get 1.1.1.1 } | complete).stdout | parse -r '.* dev (?P\S+).*' | get dev | get 0? | default "eno1") - print $" + print $" (ansi dark_gray)# flake.nix(ansi reset) inputs.hermenix.url = \"($flake)\"; @@ -329,9 +374,9 @@ def "main setup" [ mem = 8192; \}; " - note $"detected uplink: ($uplink) — check that's right before rebuilding" + note $"detected uplink: ($uplink) — check that's right before rebuilding" - print $" + print $" Then: sudo nixos-rebuild switch @@ -354,9 +399,9 @@ def "main setup" [ hermenix ctl soul " - warn "Check the disk labels before the guest's first nixos-rebuild. If they" - warn "don't match guest/hardware.nix, the first self-rebuild produces a" - warn "system that won't boot." + warn "Check the disk labels before the guest's first nixos-rebuild. If they" + warn "don't match guest/hardware.nix, the first self-rebuild produces a" + warn "system that won't boot." - print $"\n(ansi green)✓ host setup complete(ansi reset)\n" + print $"\n(ansi green)✓ host setup complete(ansi reset)\n" } diff --git a/nu/lib/common.nu b/nu/lib/common.nu index 2069af7..2fd74ed 100644 --- a/nu/lib/common.nu +++ b/nu/lib/common.nu @@ -13,27 +13,30 @@ 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 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 } + 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")) + 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 + (input $" ($prompt) ") == $word } # Single-quote a string for safe embedding in a remote shell command line. def shquote [s: string] { - "'" + ($s | str replace --all "'" "'\\''") + "'" + "'" + ($s | str replace --all "'" "'\\''") + "'" } # ------------------------------------------------------------------- ssh --- @@ -42,18 +45,18 @@ def shquote [s: string] { # 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)" - ] + [ + "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] { - ^($argv | first) ...($argv | skip 1) + ^($argv | first) ...($argv | skip 1) } # --------------------------------------------------------------- version --- @@ -61,5 +64,7 @@ def run-argv [argv: list] { # HERMENIX_VERSION is baked in at build time (flake.nix / modules/host.nix) # as "YYYY.MM.DD-" from this repo's own git metadata. "dev" is # only seen running an unwrapped checkout of the script directly. -def hermenix-version [] { $env.HERMENIX_VERSION? | default "dev" } +def hermenix-version [] { + $env.HERMENIX_VERSION? | default "dev" +} def "main version" [] { print (hermenix-version) } diff --git a/nu/lib/guest.nu b/nu/lib/guest.nu index 4b7b63f..5732cbb 100644 --- a/nu/lib/guest.nu +++ b/nu/lib/guest.nu @@ -3,8 +3,12 @@ # Used by both vm/ and ctl/. 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 guest-host [] { + $env.HERMES_GUEST_NAME? | default "hermes" +} +def vsock-cid [] { + ($env.HERMES_VSOCK_CID? | default "77") | into int +} # Full SSH argv for the guest, shared by `vm` and `ctl` subcommands. def ssh-args [] { vsock-ssh-args (vsock-cid) (guest-user) (guest-host) } diff --git a/nu/vm/vm.nu b/nu/vm/vm.nu index cfad14d..e1ead55 100644 --- a/nu/vm/vm.nu +++ b/nu/vm/vm.nu @@ -5,7 +5,9 @@ # # Depends on: lib/common.nu, lib/guest.nu (ssh-args). -def disk-base-dir [] { $env.HERMES_DISK_BASE_DIR? | default "/var/lib/hermes-vm" } +def disk-base-dir [] { + $env.HERMES_DISK_BASE_DIR? | default "/var/lib/hermes-vm" +} def snap-dir [] { $"(disk-base-dir)/snapshots" } # Run a single already-fully-formed remote command string, or open an @@ -14,38 +16,38 @@ def snap-dir [] { $"(disk-base-dir)/snapshots" } # own flags before it ever reaches a rest parameter, so `hermenix vm ssh sudo # foo --bar` would fail to parse. Quote the whole remote command instead. def vm-gssh [cmd?: string] { - let argv = if $cmd == null { ssh-args } else { (ssh-args) ++ [$cmd] } - run-argv $argv + let argv = if $cmd == null { ssh-args } else { (ssh-args) ++ [$cmd] } + run-argv $argv } def "main vm" [] { - print "usage: hermenix vm " - print "" - print " ssh [cmd] open a shell or run a single command in the guest" - print " console follow the VM's journal output (journalctl -f)" - print " start systemctl start hermes-vm" - print " stop systemctl stop hermes-vm" - print " restart systemctl restart hermes-vm" - print " config show current generation, history and git status" - print " config-pull [dir] copy the guest's /etc/nixos here for reading/diffing" - print " snapshot trigger a state-disk snapshot now" - print " snapshots list existing snapshots" - print " rollback restore the state disk from a snapshot" - print " backup trigger a restic backup now" - print " backups list restic snapshots" + print "usage: hermenix vm " + print "" + print " ssh [cmd] open a shell or run a single command in the guest" + print " console follow the VM's journal output (journalctl -f)" + print " start systemctl start hermes-vm" + print " stop systemctl stop hermes-vm" + print " restart systemctl restart hermes-vm" + print " config show current generation, history and git status" + print " config-pull [dir] copy the guest's /etc/nixos here for reading/diffing" + print " snapshot trigger a state-disk snapshot now" + print " snapshots list existing snapshots" + print " rollback restore the state disk from a snapshot" + print " backup trigger a restic backup now" + print " backups list restic snapshots" } def "main vm ssh" [cmd?: string] { vm-gssh $cmd } def "main vm console" [] { ^journalctl -u hermes-vm -f -o cat } -def "main vm start" [] { ^systemctl start hermes-vm } -def "main vm stop" [] { ^systemctl stop hermes-vm } +def "main vm start" [] { ^systemctl start hermes-vm } +def "main vm stop" [] { ^systemctl stop hermes-vm } def "main vm restart" [] { ^systemctl restart hermes-vm } # What is the guest actually running right now? def "main vm config" [] { - vm-gssh 'echo "== current generation =="; \ + vm-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; \ @@ -57,53 +59,53 @@ def "main vm config" [] { # Copy the guest's live NixOS config here for reading or diffing. def "main vm config-pull" [dest: string = "./hermes-etc-nixos"] { - mkdir $dest - (vm-gssh "tar -C /etc/nixos -cf - .") | ^tar -C $dest -xf - - say $"pulled to ($dest)" + mkdir $dest + (vm-gssh "tar -C /etc/nixos -cf - .") | ^tar -C $dest -xf - + say $"pulled to ($dest)" } def "main vm snapshot" [] { ^systemctl start hermes-vm-snapshot } def "main vm snapshots" [] { - let entries = (glob $"(snap-dir)/state-*" | where {|p| ($p | path type) == "dir" }) - if ($entries | is-empty) { - say "no snapshots" - return - } - $entries - | each {|p| + 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 + | sort-by created + | print } # Restore the state disk from a snapshot. The system disk is left alone — # it holds nothing worth restoring. def "main vm 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-*" + 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 vm backup" [] { ^systemctl start hermes-vm-backup } def "main vm backups" [] { - let repo = (^systemctl show hermes-vm-backup -p Environment --value + 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 + ^restic -r $repo snapshots --tag hermes }