diff --git a/.envrc b/.envrc index 87c950d..bed9d77 100644 --- a/.envrc +++ b/.envrc @@ -1,3 +1,6 @@ -#!/usr/bin/env bash +watch_file devenv.nix +watch_file flake.nix +watch_file flake.lock -nix develop --no-pure-eval +# devenv needs impure eval (git metadata, DEVENV_ROOT, git-hooks install). +use flake . --no-pure-eval diff --git a/.gitignore b/.gitignore index e80416c..42edd75 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,14 @@ devenv.local.yaml # pre-commit .pre-commit-config.yaml + +# Devenv +.devenv* +devenv.local.nix +devenv.local.yaml + +# direnv +.direnv + +# pre-commit +.pre-commit-config.yaml diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..10bf230 --- /dev/null +++ b/bin/test @@ -0,0 +1,14 @@ +#!/usr/bin/env nu +# +# bin/test — run the full hermenix CLI test suite (nu/tests/). +# +# Requires the nutest module on NU_LIB_DIRS, which `nix develop`'s devShell +# sets up. For filtering to specific suites/tests, run +# `nutest run-tests --path nu/tests --match-tests ` directly. + +use nutest + +def main [] { + let repo_root = ($env.FILE_PWD | path dirname) + nutest run-tests --path ($repo_root | path join "nu" "tests") --fail +} diff --git a/devenv.nix b/devenv.nix new file mode 100644 index 0000000..9ff5ca7 --- /dev/null +++ b/devenv.nix @@ -0,0 +1,84 @@ +{ pkgs, inputs, ... }: + +let + nu = "${pkgs.nushell}/bin/nu"; + # nu-check based linter: fails on any file that does not parse. + nuCheck = pkgs.writeShellScript "nu-check-all" '' + status=0 + for f in "$@"; do + if ! ${nu} --no-config-file -c "exit (if (nu-check --debug '$f') { 0 } else { 1 })"; then + echo "nu-check failed: $f" >&2 + status=1 + fi + done + exit $status + ''; +in +{ + packages = with pkgs; [ + nushell + nufmt + nixfmt + deadnix + statix + ]; + + env.NU_LIB_DIRS = "${inputs.nutest}"; + + enterShell = '' + echo "Welcome to the Hermenix devshell!" + ''; + + # ── Formatters / linters ──────────────────────────────────────────────── + treefmt = { + enable = true; + config = { + programs = { + nixfmt.enable = true; # nix formatter + deadnix.enable = true; # nix: dead code removal + statix.enable = true; # nix: anti-pattern lints + }; + # No upstream treefmt-nix module for nushell, so wire nufmt manually. + settings.formatter.nufmt = { + command = "${pkgs.nufmt}/bin/nufmt"; + includes = [ "*.nu" ]; + }; + }; + }; + + # ── Git hooks ─────────────────────────────────────────────────────────── + git-hooks.hooks = { + # Formats nix + nushell; package is wired up by the treefmt integration. + treefmt.enable = true; + + nu-check = { + enable = true; + name = "nu-check"; + description = "Parse-check nushell sources"; + entry = "${nuCheck}"; + language = "system"; + pass_filenames = true; + files = "\\.nu$"; + }; + + gen-completions = { + enable = true; + name = "gen-completions"; + description = "Regenerate nu/completions.nu from usage blocks"; + entry = "${nu} nu/gen-completions.nu"; + language = "system"; + pass_filenames = false; + files = "^nu/(hermenix\\.nu|vm/vm\\.nu|ctl/ctl\\.nu)$"; + }; + + nu-tests = { + enable = true; + name = "nu-tests"; + description = "Run the hermenix CLI test suite"; + entry = "${nu} -I ${inputs.nutest} bin/test"; + language = "system"; + pass_filenames = false; + files = "^nu/"; + }; + }; +} diff --git a/flake.lock b/flake.lock index e41bbef..e205ef2 100644 --- a/flake.lock +++ b/flake.lock @@ -168,6 +168,22 @@ "type": "github" } }, + "flake-compat_3": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "NixOS", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "flake-compat", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -274,6 +290,27 @@ "url": "https://flakehub.com/f/cachix/git-hooks.nix/0.1.941" } }, + "git-hooks_2": { + "inputs": { + "flake-compat": "flake-compat_3", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1789514889, + "narHash": "sha256-PFD1nxptCXJyJoiYO1gf/5Vv43yqGpHtMC6LUhl9/pQ=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "59f4ca0d063a1a3ec722c88b51a33004862e5379", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "nix": { "inputs": { "flake-parts": "flake-parts", @@ -471,12 +508,32 @@ "url": "https://flakehub.com/f/NixOS/nixpkgs/0.1" } }, + "nutest": { + "flake": false, + "locked": { + "lastModified": 1783281957, + "narHash": "sha256-IkDGL0yGIapatoaJiqjk+6CALIRe7FF1Pq2/5zHkIqw=", + "owner": "vyadh", + "repo": "nutest", + "rev": "c46af12bc6e3819b95d69f54f35526ca3e5810d6", + "type": "github" + }, + "original": { + "owner": "vyadh", + "ref": "v1.2.0", + "repo": "nutest", + "type": "github" + } + }, "root": { "inputs": { "determinate": "determinate", "devenv": "devenv", + "git-hooks": "git-hooks_2", "nixos-generators": "nixos-generators", - "nixpkgs": "nixpkgs_3" + "nixpkgs": "nixpkgs_3", + "nutest": "nutest", + "treefmt-nix": "treefmt-nix_2" } }, "rust-overlay": { @@ -521,6 +578,26 @@ "repo": "treefmt-nix", "type": "github" } + }, + "treefmt-nix_2": { + "inputs": { + "nixpkgs": [ + "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 953edd1..967371f 100644 --- a/flake.nix +++ b/flake.nix @@ -12,23 +12,46 @@ url = "github:cachix/devenv"; inputs.nixpkgs.follows = "nixpkgs"; }; + git-hooks = { + url = "github:cachix/git-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + # A nushell test framework, used by nu/tests/. No nixpkgs package exists + # for it, so it's pulled in as a plain source input instead. + nutest = { + url = "github:vyadh/nutest/v1.2.0"; + flake = false; + }; }; outputs = - { self, nixpkgs, determinate, nixos-generators, devenv, ... }@inputs: + { + self, + nixpkgs, + determinate, + nixos-generators, + devenv, + ... + }@inputs: let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; - nuScript = builtins.concatStringsSep "\n" (map builtins.readFile [ - ./nu/lib/common.nu - ./nu/lib/guest.nu - ./nu/host/setup.nu - ./nu/host/migrate.nu - ./nu/host/remote.nu - ./nu/vm/vm.nu - ./nu/ctl/ctl.nu - ./nu/hermenix.nu - ]); + nuScript = builtins.concatStringsSep "\n" ( + map builtins.readFile [ + ./nu/lib/common.nu + ./nu/lib/guest.nu + ./nu/host/setup.nu + ./nu/host/migrate.nu + ./nu/host/remote.nu + ./nu/vm/vm.nu + ./nu/ctl/ctl.nu + ./nu/hermenix.nu + ] + ); gitRev = self.rev or self.dirtyRev or "unknown"; shortRev = if gitRev == "unknown" then "unknown" else builtins.substring 0 7 gitRev; version = @@ -51,13 +74,14 @@ ./guest/common.nix ]; }; - hermenix = pkgs.writers.writeNuBin "hermenix" - { - makeWrapperArgs = [ - "--prefix" - "PATH" - ":" - (pkgs.lib.makeBinPath (with pkgs; [ + hermenix = pkgs.writers.writeNuBin "hermenix" { + makeWrapperArgs = [ + "--prefix" + "PATH" + ":" + (pkgs.lib.makeBinPath ( + with pkgs; + [ openssh socat gnutar @@ -69,13 +93,13 @@ coreutils vim nix - ])) - "--set" - "HERMENIX_VERSION" - version - ]; - } - nuScript; + ] + )) + "--set" + "HERMENIX_VERSION" + version + ]; + } nuScript; hermenix-completions = pkgs.writeTextFile { name = "hermenix-completions"; @@ -88,39 +112,7 @@ 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)$"; - }; - }; - }]; + modules = [ ./devenv.nix ]; }; }; } diff --git a/guest/common.nix b/guest/common.nix index 853acdd..b8571e6 100644 --- a/guest/common.nix +++ b/guest/common.nix @@ -1,4 +1,9 @@ -{ config, lib, pkgs, modulesPath, ... }: +{ + lib, + pkgs, + modulesPath, + ... +}: let # These must match services.hermenix.* on the host. If you changed the @@ -17,10 +22,7 @@ let # either need a checkout to hold it or get baked into the immutable store # copy Nix fetches. Falls back to safe defaults (no host cache, no SSH # access) if it hasn't been created yet. - local = - if builtins.pathExists /etc/hermenix/local.nix - then import /etc/hermenix/local.nix - else { }; + local = if builtins.pathExists /etc/hermenix/local.nix then import /etc/hermenix/local.nix else { }; cachePublicKey = local.cachePublicKey or ""; authorizedKeys = local.authorizedKeys or [ ]; @@ -41,11 +43,33 @@ let # third-party install script that shells out to some common Unix tool by # bare name doesn't trigger another one of these round-trips. hermesInstallDeps = with pkgs; [ - git curl jq ripgrep fd tree htop - uv nodejs ffmpeg - direnv nix-direnv nix-output-monitor - gcc gnumake pkg-config python3 xz - bash coreutils gnutar gzip which gnugrep gnused gawk findutils + git + curl + jq + ripgrep + fd + tree + htop + uv + nodejs + ffmpeg + direnv + nix-direnv + nix-output-monitor + gcc + gnumake + pkg-config + python3 + xz + bash + coreutils + gnutar + gzip + which + gnugrep + gnused + gawk + findutils ]; in { @@ -56,7 +80,12 @@ in # driver support for the VM environment itself is needed either way. imports = [ (modulesPath + "/profiles/qemu-guest.nix") ]; boot.initrd.availableKernelModules = [ - "virtio_pci" "virtio_blk" "virtio_scsi" "ahci" "sd_mod" "xhci_pci" + "virtio_pci" + "virtio_blk" + "virtio_scsi" + "ahci" + "sd_mod" + "xhci_pci" ]; networking.hostName = "hermes"; @@ -76,7 +105,10 @@ in device = "/dev/disk/by-label/hermes-state"; fsType = "ext4"; neededForBoot = true; - options = [ "defaults" "noatime" ]; + options = [ + "defaults" + "noatime" + ]; }; # ===================================================================== @@ -88,7 +120,10 @@ in # worked out. Use `hermes-ctl reseed` when you actually mean to overwrite. # ===================================================================== system.activationScripts.seedHermesState = { - deps = [ "users" "groups" ]; + deps = [ + "users" + "groups" + ]; text = '' install -d -o hermes -g hermes -m 0750 ${stateDir} install -d -o hermes -g hermes -m 0750 ${stateDir}/workspace @@ -141,10 +176,16 @@ in systemd.network.networks."10-eth" = { matchConfig.Type = "ether"; address = [ "${guestAddress}/${toString prefixLength}" ]; - routes = [{ Gateway = hostAddress; }]; - networkConfig.DNS = [ "9.9.9.9" "149.112.112.112" ]; + routes = [ { Gateway = hostAddress; } ]; + networkConfig.DNS = [ + "9.9.9.9" + "149.112.112.112" + ]; }; - networking.nameservers = [ "9.9.9.9" "149.112.112.112" ]; + networking.nameservers = [ + "9.9.9.9" + "149.112.112.112" + ]; services.openssh = { enable = true; @@ -173,12 +214,8 @@ in trusted-users = root hermes extra-experimental-features = nix-command flakes - extra-substituters = https://cache.flakehub.com${ - lib.optionalString useHostCache " http://${hostAddress}:${toString cachePort}" - } - extra-trusted-public-keys = cache.flakehub.com-3:hJuILl5sVK4iKm86JzgdXW12Y2Hwd5G07qKtHTOcDCM=${ - lib.optionalString useHostCache " ${cachePublicKey}" - } + extra-substituters = https://cache.flakehub.com${lib.optionalString useHostCache " http://${hostAddress}:${toString cachePort}"} + extra-trusted-public-keys = cache.flakehub.com-3:hJuILl5sVK4iKm86JzgdXW12Y2Hwd5G07qKtHTOcDCM=${lib.optionalString useHostCache " ${cachePublicKey}"} ''; # Keep enough generations that a bad self-rebuild is one boot-menu entry @@ -198,7 +235,10 @@ in createHome = false; # the state disk is mounted here and seeded above linger = true; group = "hermes"; - extraGroups = [ "wheel" "systemd-journal" ]; + extraGroups = [ + "wheel" + "systemd-journal" + ]; openssh.authorizedKeys.keys = authorizedKeys; }; users.groups.hermes = { }; @@ -243,9 +283,23 @@ in programs.nix-ld = { enable = true; libraries = with pkgs; [ - stdenv.cc.cc zlib openssl curl libxml2 libxslt - glib nss nspr at-spi2-core cups dbus expat - libGL libdrm mesa alsa-lib + stdenv.cc.cc + zlib + openssl + curl + libxml2 + libxslt + glib + nss + nspr + at-spi2-core + cups + dbus + expat + libGL + libdrm + mesa + alsa-lib ]; }; diff --git a/guest/hardware.nix b/guest/hardware.nix index e2b1c73..efa4b85 100644 --- a/guest/hardware.nix +++ b/guest/hardware.nix @@ -21,7 +21,10 @@ fileSystems."/boot" = { device = "/dev/disk/by-label/ESP"; fsType = "vfat"; - options = [ "fmask=0077" "dmask=0077" ]; + options = [ + "fmask=0077" + "dmask=0077" + ]; }; boot.loader.systemd-boot.enable = true; diff --git a/modules/host.nix b/modules/host.nix index 7fa17d6..4c42b3d 100644 --- a/modules/host.nix +++ b/modules/host.nix @@ -1,7 +1,18 @@ -{ config, lib, pkgs, ... }: +{ + config, + lib, + pkgs, + ... +}: let - inherit (lib) mkOption mkEnableOption mkIf types optionalString; + inherit (lib) + mkOption + mkEnableOption + mkIf + types + optionalString + ; cfg = config.services.hermenix; net = cfg.network; @@ -34,11 +45,13 @@ let # Freeze the guest filesystems around a snapshot so it captures a consistent # state rather than the equivalent of pulling the power. - qga = verb: pkgs.writeShellScript "hermes-vm-fs${verb}" '' - set -eu - printf '%s' '{"execute":"guest-fs${verb}"}' \ - | ${pkgs.socat}/bin/socat -t2 - UNIX-CONNECT:${runDir}/qga.sock >/dev/null 2>&1 || true - ''; + qga = + verb: + pkgs.writeShellScript "hermes-vm-fs${verb}" '' + set -eu + printf '%s' '{"execute":"guest-fs${verb}"}' \ + | ${pkgs.socat}/bin/socat -t2 - UNIX-CONNECT:${runDir}/qga.sock >/dev/null 2>&1 || true + ''; in { options.services.hermenix = { @@ -156,7 +169,14 @@ in keep = mkOption { type = types.listOf types.str; - default = [ "--keep-daily" "14" "--keep-weekly" "8" "--keep-monthly" "12" ]; + default = [ + "--keep-daily" + "14" + "--keep-weekly" + "8" + "--keep-monthly" + "12" + ]; description = "Arguments passed to restic forget."; }; }; @@ -206,21 +226,31 @@ in lanAccess = mkOption { default = [ ]; - example = [ { address = "192.168.1.50"; port = 11434; } ]; + example = [ + { + address = "192.168.1.50"; + port = 11434; + } + ]; description = '' Specific endpoints elsewhere on your network that the guest may reach, punched through the default RFC1918 drop. One entry per service. ''; - type = types.listOf (types.submodule { - options = { - address = mkOption { type = types.str; }; - port = mkOption { type = types.port; }; - proto = mkOption { - type = types.enum [ "tcp" "udp" ]; - default = "tcp"; + type = types.listOf ( + types.submodule { + options = { + address = mkOption { type = types.str; }; + port = mkOption { type = types.port; }; + proto = mkOption { + type = types.enum [ + "tcp" + "udp" + ]; + default = "tcp"; + }; }; - }; - }); + } + ); }; hostAccess = mkOption { @@ -230,43 +260,76 @@ in Ports on this host itself that the guest may reach. The service must also be bound to an address the guest can see - see the README. ''; - type = types.listOf (types.submodule { - options = { - port = mkOption { type = types.port; }; - proto = mkOption { - type = types.enum [ "tcp" "udp" ]; - default = "tcp"; + type = types.listOf ( + types.submodule { + options = { + port = mkOption { type = types.port; }; + proto = mkOption { + type = types.enum [ + "tcp" + "udp" + ]; + default = "tcp"; + }; }; - }; - }); + } + ); }; publish = mkOption { default = [ ]; - example = [ { hostPort = 8080; guestPort = 8080; } ]; + example = [ + { + hostPort = 8080; + guestPort = 8080; + } + ]; description = '' Guest ports to publish on the host's uplink, reachable from the wider network. You do not need this to reach the guest from the host itself. ''; - type = types.listOf (types.submodule { - options = { - hostPort = mkOption { type = types.port; }; - guestPort = mkOption { type = types.port; }; - proto = mkOption { - type = types.enum [ "tcp" "udp" ]; - default = "tcp"; + type = types.listOf ( + types.submodule { + options = { + hostPort = mkOption { type = types.port; }; + guestPort = mkOption { type = types.port; }; + proto = mkOption { + type = types.enum [ + "tcp" + "udp" + ]; + default = "tcp"; + }; }; - }; - }); + } + ); }; network = { - bridge = mkOption { type = types.str; default = "hermes-br"; }; - tapId = mkOption { type = types.str; default = "vm-hermes"; }; - mac = mkOption { type = types.str; default = "02:00:00:7e:00:01"; }; - hostAddress = mkOption { type = types.str; default = "10.77.0.1"; }; - guestAddress = mkOption { type = types.str; default = "10.77.0.2"; }; - prefixLength = mkOption { type = types.int; default = 24; }; + bridge = mkOption { + type = types.str; + default = "hermes-br"; + }; + tapId = mkOption { + type = types.str; + default = "vm-hermes"; + }; + mac = mkOption { + type = types.str; + default = "02:00:00:7e:00:01"; + }; + hostAddress = mkOption { + type = types.str; + default = "10.77.0.1"; + }; + guestAddress = mkOption { + type = types.str; + default = "10.77.0.2"; + }; + prefixLength = mkOption { + type = types.int; + default = 24; + }; externalInterface = mkOption { type = types.nullOr types.str; default = null; @@ -277,7 +340,12 @@ in passthrough = { iommu = mkOption { - type = types.nullOr (types.enum [ "intel" "amd" ]); + type = types.nullOr ( + types.enum [ + "intel" + "amd" + ] + ); default = null; description = '' Enable IOMMU kernel parameters for your CPU vendor. Required for any @@ -289,8 +357,14 @@ in pci = mkOption { default = [ ]; example = [ - { address = "0000:01:00.0"; id = "10de:2204"; } - { address = "0000:01:00.1"; id = "10de:1aef"; } + { + address = "0000:01:00.0"; + id = "10de:2204"; + } + { + address = "0000:01:00.1"; + id = "10de:1aef"; + } ]; description = '' PCI devices handed to the guest via VFIO. The host loses them @@ -304,26 +378,33 @@ in initrd, before a host driver can. Strongly recommended for GPUs; unbinding an active amdgpu or nvidia driver later is unreliable. ''; - type = types.listOf (types.submodule { - options = { - address = mkOption { - type = types.str; - example = "0000:01:00.0"; - description = "Full PCI address, as shown by lspci -D."; + type = types.listOf ( + types.submodule { + options = { + address = mkOption { + type = types.str; + example = "0000:01:00.0"; + description = "Full PCI address, as shown by lspci -D."; + }; + id = mkOption { + type = types.nullOr types.str; + default = null; + example = "10de:2204"; + description = "vendor:device, from lspci -nn. Enables early binding."; + }; }; - id = mkOption { - type = types.nullOr types.str; - default = null; - example = "10de:2204"; - description = "vendor:device, from lspci -nn. Enables early binding."; - }; - }; - }); + } + ); }; usb = mkOption { default = [ ]; - example = [ { vendorId = "8087"; productId = "0026"; } ]; + example = [ + { + vendorId = "8087"; + productId = "0026"; + } + ]; description = '' USB devices forwarded to the guest, matched by vendor and product ID from lsusb. Integrated Bluetooth radios are USB devices and go @@ -332,12 +413,20 @@ in QEMU resolves these at start, so a device that is unplugged when the VM boots will not appear later without a restart. ''; - type = types.listOf (types.submodule { - options = { - vendorId = mkOption { type = types.str; example = "046d"; }; - productId = mkOption { type = types.str; example = "c52b"; }; - }; - }); + type = types.listOf ( + types.submodule { + options = { + vendorId = mkOption { + type = types.str; + example = "046d"; + }; + productId = mkOption { + type = types.str; + example = "c52b"; + }; + }; + } + ); }; }; @@ -423,7 +512,10 @@ in systemd.services.hermes-vm = { description = "Hermes Agent VM"; wantedBy = [ "multi-user.target" ]; - after = [ "network-online.target" "hermes-vm-tap.service" ]; + after = [ + "network-online.target" + "hermes-vm-tap.service" + ]; requires = [ "hermes-vm-tap.service" ]; wants = [ "network-online.target" ]; @@ -451,50 +543,52 @@ in StateDirectory = "hermes-vm"; RuntimeDirectoryMode = "0750"; - ExecStart = lib.concatStringsSep " " ([ - "${pkgs.qemu_kvm}/bin/qemu-system-x86_64" - "-name ${cfg.name}" - "-machine q35,accel=kvm,smm=on" - "-cpu host" - "-smp ${toString cfg.vcpu}" - "-m ${toString cfg.mem}" - "-nodefaults" - "-no-user-config" - "-display none" - "-serial stdio" - # QEMU's own seccomp sandbox, everything denied that we don't need. - "-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny" - # UEFI: firmware read-only, variables per-VM and writable. - "-drive if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware}" - "-drive if=pflash,format=raw,unit=1,file=${stateDir}/OVMF_VARS.fd" - # System disk: NixOS and the Nix store. Rebuildable, never snapshotted. - # aio=threads and a writeback cache because these are files on a - # filesystem, not block devices; aio=native wants the latter. - "-drive if=none,id=system,format=raw,file=${systemImage},cache=writeback,aio=threads,discard=unmap" - "-device virtio-blk-pci,drive=system" - # State disk: everything irreplaceable. Mounted at /var/lib/hermes. - "-drive if=none,id=state,format=raw,file=${stateImage},cache=writeback,aio=threads,discard=unmap" - "-device virtio-blk-pci,drive=state" - # Pre-created tap. vhost=off on purpose: vhost-net would move packet - # handling into the host kernel, which is exactly the surface we are - # trying not to expose. Costs throughput we do not need. - "-netdev tap,id=net0,ifname=${net.tapId},script=no,downscript=no,vhost=off" - "-device virtio-net-pci,netdev=net0,mac=${net.mac}" - # Host -> guest management, no network involved. - "-device vhost-vsock-pci,guest-cid=${toString cfg.vsockCid}" - "-device virtio-rng-pci" - "-qmp unix:${runDir}/qmp.sock,server=on,wait=off" - ] ++ lib.optionals cfg.disk.quiesce [ - "-chardev socket,path=${runDir}/qga.sock,server=on,wait=off,id=qga0" - "-device virtio-serial-pci" - "-device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0" - ] - # Passed-through PCI devices. The host cannot use these while the VM - # is running. - ++ map (d: "-device vfio-pci,host=${d.address}") pt.pci - # A USB controller, only if something needs plugging into it. - ++ lib.optional anyUsb "-device qemu-xhci,id=xhci" - ++ map (u: "-device usb-host,vendorid=0x${u.vendorId},productid=0x${u.productId}") pt.usb + ExecStart = lib.concatStringsSep " " ( + [ + "${pkgs.qemu_kvm}/bin/qemu-system-x86_64" + "-name ${cfg.name}" + "-machine q35,accel=kvm,smm=on" + "-cpu host" + "-smp ${toString cfg.vcpu}" + "-m ${toString cfg.mem}" + "-nodefaults" + "-no-user-config" + "-display none" + "-serial stdio" + # QEMU's own seccomp sandbox, everything denied that we don't need. + "-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny" + # UEFI: firmware read-only, variables per-VM and writable. + "-drive if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware}" + "-drive if=pflash,format=raw,unit=1,file=${stateDir}/OVMF_VARS.fd" + # System disk: NixOS and the Nix store. Rebuildable, never snapshotted. + # aio=threads and a writeback cache because these are files on a + # filesystem, not block devices; aio=native wants the latter. + "-drive if=none,id=system,format=raw,file=${systemImage},cache=writeback,aio=threads,discard=unmap" + "-device virtio-blk-pci,drive=system" + # State disk: everything irreplaceable. Mounted at /var/lib/hermes. + "-drive if=none,id=state,format=raw,file=${stateImage},cache=writeback,aio=threads,discard=unmap" + "-device virtio-blk-pci,drive=state" + # Pre-created tap. vhost=off on purpose: vhost-net would move packet + # handling into the host kernel, which is exactly the surface we are + # trying not to expose. Costs throughput we do not need. + "-netdev tap,id=net0,ifname=${net.tapId},script=no,downscript=no,vhost=off" + "-device virtio-net-pci,netdev=net0,mac=${net.mac}" + # Host -> guest management, no network involved. + "-device vhost-vsock-pci,guest-cid=${toString cfg.vsockCid}" + "-device virtio-rng-pci" + "-qmp unix:${runDir}/qmp.sock,server=on,wait=off" + ] + ++ lib.optionals cfg.disk.quiesce [ + "-chardev socket,path=${runDir}/qga.sock,server=on,wait=off,id=qga0" + "-device virtio-serial-pci" + "-device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0" + ] + # Passed-through PCI devices. The host cannot use these while the VM + # is running. + ++ map (d: "-device vfio-pci,host=${d.address}") pt.pci + # A USB controller, only if something needs plugging into it. + ++ lib.optional anyUsb "-device qemu-xhci,id=xhci" + ++ map (u: "-device usb-host,vendorid=0x${u.vendorId},productid=0x${u.productId}") pt.usb ); ExecStop = stopScript; @@ -530,10 +624,18 @@ in RestrictNamespaces = true; RestrictRealtime = true; RestrictSUIDSGID = true; - RestrictAddressFamilies = [ "AF_UNIX" "AF_NETLINK" "AF_VSOCK" ]; + RestrictAddressFamilies = [ + "AF_UNIX" + "AF_NETLINK" + "AF_VSOCK" + ]; LockPersonality = true; SystemCallArchitectures = "native"; - SystemCallFilter = [ "@system-service" "~@resources" "~@obsolete" ]; + SystemCallFilter = [ + "@system-service" + "~@resources" + "~@obsolete" + ]; # KVM means no JIT, so W^X is safe. If QEMU refuses to start, this is # the first thing to flip. MemoryDenyWriteExecute = true; @@ -595,7 +697,8 @@ in path = [ pkgs.kmod ]; script = '' modprobe vfio-pci - '' + lib.concatMapStrings (d: '' + '' + + lib.concatMapStrings (d: '' dev=/sys/bus/pci/devices/${d.address} if [ ! -e "$dev" ]; then echo "${d.address} is not present on this machine" >&2 @@ -646,7 +749,7 @@ in networking.nat = { enable = true; internalInterfaces = [ net.bridge ]; - externalInterface = net.externalInterface; + inherit (net) externalInterface; forwardPorts = map (s: { sourcePort = s.hostPort; destination = "${net.guestAddress}:${toString s.guestPort}"; @@ -675,33 +778,37 @@ in '') cfg.hostAccess; # Order matters here. Allowlists must precede the RFC1918 drop. - networking.firewall.extraForwardRules = - '' - iifname != "${net.bridge}" accept - ct state established,related accept - '' - # Guest reaching named services elsewhere on your network. - + lib.concatMapStrings (s: '' - iifname "${net.bridge}" ip daddr ${s.address} ${s.proto} dport ${toString s.port} accept - '') cfg.lanAccess - # Inbound to guest services published on the uplink. - + lib.concatMapStrings (s: '' - oifname "${net.bridge}" ip daddr ${net.guestAddress} ${s.proto} dport ${toString s.guestPort} accept - '') cfg.publish - + (if cfg.allowLan then '' - iifname "${net.bridge}" accept - '' else '' - iifname "${net.bridge}" ip daddr { - 10.0.0.0/8, - 172.16.0.0/12, - 192.168.0.0/16, - 169.254.0.0/16, - 127.0.0.0/8, - 100.64.0.0/10 - } drop - iifname "${net.bridge}" ip6 daddr { fc00::/7, fe80::/10, ::1/128 } drop - iifname "${net.bridge}" accept - ''); + networking.firewall.extraForwardRules = '' + iifname != "${net.bridge}" accept + ct state established,related accept + '' + # Guest reaching named services elsewhere on your network. + + lib.concatMapStrings (s: '' + iifname "${net.bridge}" ip daddr ${s.address} ${s.proto} dport ${toString s.port} accept + '') cfg.lanAccess + # Inbound to guest services published on the uplink. + + lib.concatMapStrings (s: '' + oifname "${net.bridge}" ip daddr ${net.guestAddress} ${s.proto} dport ${toString s.guestPort} accept + '') cfg.publish + + ( + if cfg.allowLan then + '' + iifname "${net.bridge}" accept + '' + else + '' + iifname "${net.bridge}" ip daddr { + 10.0.0.0/8, + 172.16.0.0/12, + 192.168.0.0/16, + 169.254.0.0/16, + 127.0.0.0/8, + 100.64.0.0/10 + } drop + iifname "${net.bridge}" ip6 daddr { fc00::/7, fe80::/10, ::1/128 } drop + iifname "${net.bridge}" accept + '' + ); # ===================================================================== # Host store as a read-only cache for the guest @@ -723,7 +830,11 @@ in # ===================================================================== systemd.services.hermes-vm-snapshot = mkIf cfg.snapshots.enable { description = "Snapshot the Hermes VM state disk"; - path = with pkgs; [ btrfs-progs coreutils socat ]; + path = with pkgs; [ + btrfs-progs + coreutils + socat + ]; serviceConfig.Type = "oneshot"; script = '' set -euo pipefail @@ -772,7 +883,12 @@ in # ===================================================================== systemd.services.hermes-vm-backup = mkIf cfg.backup.enable { description = "Back up Hermes state"; - path = with pkgs; [ restic openssh socat gnutar ]; + path = with pkgs; [ + restic + openssh + socat + gnutar + ]; serviceConfig = { Type = "oneshot"; EnvironmentFile = lib.mkIf (cfg.backup.environmentFile != null) cfg.backup.environmentFile; @@ -826,18 +942,21 @@ in "--prefix" "PATH" ":" - (lib.makeBinPath (with pkgs; [ - openssh - socat - coreutils - gnutar - gnused - btrfs-progs - util-linux - restic - vim - nix - ])) + (lib.makeBinPath ( + with pkgs; + [ + openssh + socat + coreutils + gnutar + gnused + btrfs-progs + util-linux + restic + vim + nix + ] + )) "--set" "HERMES_VSOCK_CID" (toString cfg.vsockCid) @@ -858,16 +977,21 @@ in cfg.version ]; } - (builtins.concatStringsSep "\n" (map builtins.readFile [ - ../nu/lib/common.nu - ../nu/lib/guest.nu - ../nu/host/setup.nu - ../nu/host/migrate.nu - ../nu/host/remote.nu - ../nu/vm/vm.nu - ../nu/ctl/ctl.nu - ../nu/hermenix.nu - ]))) + ( + builtins.concatStringsSep "\n" ( + map builtins.readFile [ + ../nu/lib/common.nu + ../nu/lib/guest.nu + ../nu/host/setup.nu + ../nu/host/migrate.nu + ../nu/host/remote.nu + ../nu/vm/vm.nu + ../nu/ctl/ctl.nu + ../nu/hermenix.nu + ] + ) + ) + ) ]; }; } diff --git a/nu/gen-completions.nu b/nu/gen-completions.nu index e69417d..1d584fc 100644 --- a/nu/gen-completions.nu +++ b/nu/gen-completions.nu @@ -117,32 +117,37 @@ def render-header [] { } # ── main ──────────────────────────────────────────────────────────────── +# +# Guarded so nu/tests/completions_test.nu can `source` this file to reach +# the pure parse-usage/render-* functions above without regenerating +# nu/completions.nu as a side effect of the test. +if not ($env.HERMENIX_TEST_SOURCING? | default false) { + 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 ($ctl | is-empty) { print -e "warn: no ctl completions found in 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 ($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) + "" + ] | str join "\n" | save -f $out_file -[ - (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 + nufmt $out_file -nufmt $out_file - -print $"wrote ($out_file) [top=($top | length) vm=($vm | length) ctl=($ctl | length)]" + print $"wrote ($out_file) [top=($top | length) vm=($vm | length) ctl=($ctl | length)]" +} diff --git a/nu/host/migrate.nu b/nu/host/migrate.nu index 97fe856..85ab66a 100644 --- a/nu/host/migrate.nu +++ b/nu/host/migrate.nu @@ -56,7 +56,8 @@ def "main migrate" [ ^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 + let last_idx = ($rewrite_from | length) - 1 + let sed_expr = (0..$last_idx | each {|i| $"s#($rewrite_from | get $i)#($rewrite_to | get $i)#g" } | str join "; ") for f in $rewrite_files { diff --git a/nu/tests/_bundle.nu b/nu/tests/_bundle.nu new file mode 100644 index 0000000..1b4beba --- /dev/null +++ b/nu/tests/_bundle.nu @@ -0,0 +1,17 @@ +# Reproduces flake.nix's `nuScript` concatenation (flake.nix:22-31) so +# cli_smoke_test.nu sees the exact same command tree the real `hermenix` +# binary has. Other test files only need a subset of these files (see each +# source file's own "Depends on:" header) and source those directly instead +# — `source` is parse-time and order-dependent, so pulling in more than a +# test needs just adds fragility. +# +# Keep this list in sync with flake.nix's `nuScript` — if one changes, the +# other must too. +source ../lib/common.nu +source ../lib/guest.nu +source ../host/setup.nu +source ../host/migrate.nu +source ../host/remote.nu +source ../vm/vm.nu +source ../ctl/ctl.nu +source ../hermenix.nu diff --git a/nu/tests/cli_smoke_test.nu b/nu/tests/cli_smoke_test.nu new file mode 100644 index 0000000..2c06cee --- /dev/null +++ b/nu/tests/cli_smoke_test.nu @@ -0,0 +1,57 @@ +use std/assert +use std/testing * +source _bundle.nu + +# `source` is a parse-time keyword, so if any subcommand's argument/flag +# signature anywhere in the bundle were malformed, sourcing _bundle.nu +# above would already have failed before any test here could run. These +# tests additionally assert the full dispatch tree ends up registered +# under the names the real CLI is invoked with (`hermenix vm ssh` etc. +# dispatches to `main vm ssh`), without invoking any subcommand's body — +# same "parse only" check GUIDELINES.md recommends doing by hand. + +def command-names [] { + scope commands | where name =~ '^main( |$)' | get name +} + +@test +def "top-level subcommands are registered" [] { + let names = (command-names) + for n in [ + "main" "main setup" "main migrate" "main remote" + "main vm" "main ctl" "main version" + ] { + assert ($n in $names) $"missing top-level command: ($n)" + } +} + +@test +def "vm subcommands are registered" [] { + let names = (command-names) + for n in [ + "main vm ssh" "main vm console" "main vm start" "main vm stop" + "main vm restart" "main vm config" "main vm config-pull" + "main vm snapshot" "main vm snapshots" "main vm rollback" + "main vm backup" "main vm backups" + ] { + assert ($n in $names) $"missing vm command: ($n)" + } +} + +@test +def "ctl subcommands are registered" [] { + let names = (command-names) + for n in [ + "main ctl" "main ctl soul" "main ctl memory" "main ctl conf" + "main ctl secrets" "main ctl status" "main ctl logs" "main ctl restart" + "main ctl units" "main ctl reseed" "main ctl pull" + ] { + assert ($n in $names) $"missing ctl command: ($n)" + } +} + +@test +def "vm rollback requires a snapshot argument" [] { + let sig = (scope commands | where name == "main vm rollback" | get 0.signatures) + assert (($sig | values | first | any {|p| $p.parameter_name == "snapshot"})) +} diff --git a/nu/tests/common_test.nu b/nu/tests/common_test.nu new file mode 100644 index 0000000..d76678a --- /dev/null +++ b/nu/tests/common_test.nu @@ -0,0 +1,38 @@ +use std/assert +use std/testing * +source ../lib/common.nu + +@test +def "shquote passes through a plain string" [] { + assert equal (shquote "hello") "'hello'" +} + +@test +def "shquote escapes embedded single quotes" [] { + assert equal (shquote "it's here") "'it'\\''s here'" +} + +@test +def "vsock-ssh-args builds the expected argv" [] { + let argv = vsock-ssh-args 77 "hermes" "hermes" + assert equal $argv [ + "ssh" + "-o" "ProxyCommand=socat - VSOCK-CONNECT:77:22" + "-o" "StrictHostKeyChecking=accept-new" + "hermes@hermes" + ] +} + +@test +def "hermenix-version defaults to dev when unset" [] { + with-env { HERMENIX_VERSION: null } { + assert equal (hermenix-version) "dev" + } +} + +@test +def "hermenix-version passes through when set" [] { + with-env { HERMENIX_VERSION: "2026.01.01-abc1234" } { + assert equal (hermenix-version) "2026.01.01-abc1234" + } +} diff --git a/nu/tests/completions_test.nu b/nu/tests/completions_test.nu new file mode 100644 index 0000000..364c77b --- /dev/null +++ b/nu/tests/completions_test.nu @@ -0,0 +1,69 @@ +use std/assert +use std/testing * + +# gen-completions.nu is not part of the CLI bundle (nu/tests/_bundle.nu) — +# it's a standalone driver script with its own file-writing tail. That tail +# is guarded behind HERMENIX_TEST_SOURCING (see gen-completions.nu) so +# sourcing it here to reach the pure parse-usage/render-* functions doesn't +# regenerate nu/completions.nu as a side effect. +$env.HERMENIX_TEST_SOURCING = true +source ../gen-completions.nu + +@test +def "parse-usage extracts value and description pairs" [] { + let tmp = (mktemp) + [ + "def main [] {" + ' print " setup guided host-side setup"' + ' print " migrate carry over existing data"' + "}" + ] | str join "\n" | save -f $tmp + let result = (parse-usage $tmp) + rm $tmp + assert equal $result [ + { value: "setup", description: "guided host-side setup" } + { value: "migrate", description: "carry over existing data" } + ] +} + +@test +def "parse-usage strips a prefix and skips passthrough/quoted lines" [] { + let tmp = (mktemp) + [ + "def main [] {" + ' print " hermenix setup guided host-side setup"' + ' print " hermenix \"example\" skipped, quoted"' + "}" + ] | str join "\n" | save -f $tmp + let result = (parse-usage $tmp "hermenix") + rm $tmp + assert equal $result [ + { value: "setup", description: "guided host-side setup" } + ] +} + +@test +def "render-complete renders a nu-complete def with the given entries" [] { + let out = (render-complete "hermenix top" [{ value: "setup", description: "guided host-side setup" }]) + assert ($out | str contains "nu-complete hermenix top") + assert ($out | str contains "setup") + assert ($out | str contains "guided host-side setup") +} + +@test +def "render-header documents that the file is generated" [] { + let out = (render-header) + assert ($out | str contains "GENERATED") +} + +@test +def "render-extern references the context completer" [] { + assert equal (render-extern) 'extern "hermenix" [...args: string@"nu-complete hermenix ctx"]' +} + +@test +def "render-ctx dispatches on the second word" [] { + let out = (render-ctx) + assert ($out | str contains "nu-complete hermenix vm") + assert ($out | str contains "nu-complete hermenix ctl") +} diff --git a/nu/tests/ctl_test.nu b/nu/tests/ctl_test.nu new file mode 100644 index 0000000..b251590 --- /dev/null +++ b/nu/tests/ctl_test.nu @@ -0,0 +1,33 @@ +use std/assert +use std/testing * +source ../lib/common.nu +source ../lib/guest.nu +source ../ctl/ctl.nu + +@test +def "home-dir defaults to the vm state disk path" [] { + with-env { HERMES_HOME_DIR: null } { + assert equal (home-dir) "/var/lib/hermes-vm/.hermes" + } +} + +@test +def "home-dir honors HERMES_HOME_DIR" [] { + with-env { HERMES_HOME_DIR: "/tmp/.hermes" } { + assert equal (home-dir) "/tmp/.hermes" + } +} + +@test +def "gateway-unit defaults to hermes-agent" [] { + with-env { HERMES_GATEWAY_UNIT: null } { + assert equal (gateway-unit) "hermes-agent" + } +} + +@test +def "gateway-unit honors HERMES_GATEWAY_UNIT" [] { + with-env { HERMES_GATEWAY_UNIT: "hermes-agent-2" } { + assert equal (gateway-unit) "hermes-agent-2" + } +} diff --git a/nu/tests/guest_test.nu b/nu/tests/guest_test.nu new file mode 100644 index 0000000..0b1cc42 --- /dev/null +++ b/nu/tests/guest_test.nu @@ -0,0 +1,44 @@ +use std/assert +use std/testing * +source ../lib/common.nu +source ../lib/guest.nu + +@test +def "guest-user is always hermes" [] { + assert equal (guest-user) "hermes" +} + +@test +def "guest-host defaults to hermes" [] { + with-env { HERMES_GUEST_NAME: null } { + assert equal (guest-host) "hermes" + } +} + +@test +def "guest-host honors HERMES_GUEST_NAME" [] { + with-env { HERMES_GUEST_NAME: "other-guest" } { + assert equal (guest-host) "other-guest" + } +} + +@test +def "vsock-cid defaults to 77" [] { + with-env { HERMES_VSOCK_CID: null } { + assert equal (vsock-cid) 77 + } +} + +@test +def "vsock-cid honors HERMES_VSOCK_CID" [] { + with-env { HERMES_VSOCK_CID: "99" } { + assert equal (vsock-cid) 99 + } +} + +@test +def "ssh-args composes vsock-ssh-args from guest defaults" [] { + with-env { HERMES_GUEST_NAME: null, HERMES_VSOCK_CID: null } { + assert equal (ssh-args) (vsock-ssh-args 77 "hermes" "hermes") + } +} diff --git a/nu/tests/setup_test.nu b/nu/tests/setup_test.nu new file mode 100644 index 0000000..84116df --- /dev/null +++ b/nu/tests/setup_test.nu @@ -0,0 +1,24 @@ +use std/assert +use std/testing * +source ../lib/common.nu +source ../host/setup.nu + +@test +def "normalize-size adds G to a bare number" [] { + assert equal (normalize-size "80") "80G" +} + +@test +def "normalize-size leaves an existing suffix alone" [] { + assert equal (normalize-size "80G") "80G" +} + +@test +def "normalize-size passes non-numeric input through unchanged" [] { + assert equal (normalize-size "lots") "lots" +} + +@test +def "normalize-size passes an empty string through unchanged" [] { + assert equal (normalize-size "") "" +} diff --git a/nu/tests/vm_test.nu b/nu/tests/vm_test.nu new file mode 100644 index 0000000..e9d7dba --- /dev/null +++ b/nu/tests/vm_test.nu @@ -0,0 +1,26 @@ +use std/assert +use std/testing * +source ../lib/common.nu +source ../lib/guest.nu +source ../vm/vm.nu + +@test +def "disk-base-dir defaults to var-lib path" [] { + with-env { HERMES_DISK_BASE_DIR: null } { + assert equal (disk-base-dir) "/var/lib/hermes-vm" + } +} + +@test +def "disk-base-dir honors HERMES_DISK_BASE_DIR" [] { + with-env { HERMES_DISK_BASE_DIR: "/tmp/hermes-vm" } { + assert equal (disk-base-dir) "/tmp/hermes-vm" + } +} + +@test +def "snap-dir is a snapshots subdir of disk-base-dir" [] { + with-env { HERMES_DISK_BASE_DIR: "/tmp/hermes-vm" } { + assert equal (snap-dir) "/tmp/hermes-vm/snapshots" + } +}