From 76dbd753fefd40140656885add42d21412e15d31 Mon Sep 17 00:00:00 2001 From: marshmallow Date: Fri, 24 Oct 2025 21:03:17 +1100 Subject: [PATCH] work on per-cargo-test virtual machine --- flake.nix | 2 +- nix/shells.nix | 2 + nix/tests.nix | 38 ---------------- tests/tests.nix | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 39 deletions(-) delete mode 100644 nix/tests.nix create mode 100644 tests/tests.nix diff --git a/flake.nix b/flake.nix index 8696314..7271f23 100644 --- a/flake.nix +++ b/flake.nix @@ -36,13 +36,13 @@ ./nix/hooks.nix # pre-commit hooks ./nix/utils.nix # utility functions ./nix/shells.nix - ./nix/tests.nix ./wire/cli ./wire/key_agent ./doc ./tests/nix ./runtime ./bench/runner.nix + ./tests/tests.nix ]; systems = import systems; diff --git a/nix/shells.nix b/nix/shells.nix index 752cccf..2fb3f8b 100644 --- a/nix/shells.nix +++ b/nix/shells.nix @@ -5,6 +5,7 @@ lib, craneLib, pkgs, + cargo-testing-vms, ... }: let @@ -32,6 +33,7 @@ cfg.installationScript '' export WIRE_TEST_DIR=$(realpath ./tests/rust) + export WIRE_TEST_VM="${cargo-testing-vms}" '' ]; }; diff --git a/nix/tests.nix b/nix/tests.nix deleted file mode 100644 index 86c6d30..0000000 --- a/nix/tests.nix +++ /dev/null @@ -1,38 +0,0 @@ -{ - perSystem = - { - craneLib, - pkgs, - commonArgs, - ... - }: - let - tests = craneLib.buildPackage ( - { - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - doCheck = false; - - doNotPostBuildInstallCargoBinaries = true; - - buildPhase = '' - cargo test --no-run - ''; - - installPhaseCommand = '' - mkdir -p $out - cp $(ls target/debug/deps/{wire,lib,key_agent}-* | grep -v "\.d") $out - ''; - } - // commonArgs - ); - in - { - packages.cargo-tests = pkgs.writeShellScriptBin "run-tests" '' - set -e - for item in "${tests}"/*; do - echo "running $item" - "$item" - done - ''; - }; -} diff --git a/tests/tests.nix b/tests/tests.nix new file mode 100644 index 0000000..a386c7b --- /dev/null +++ b/tests/tests.nix @@ -0,0 +1,112 @@ +{ inputs, ... }: +{ + perSystem = + { + craneLib, + pkgs, + lib, + commonArgs, + system, + cargo-testing-vms, + ... + }: + let + evalConfig = import (pkgs.path + "/nixos/lib/eval-config.nix"); + tests = craneLib.buildPackage ( + { + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + doCheck = false; + + doNotPostBuildInstallCargoBinaries = true; + + buildPhase = '' + cargo test --no-run + ''; + + installPhaseCommand = '' + mkdir -p $out + cp $(ls target/debug/deps/{wire,lib,key_agent}-* | grep -v "\.d") $out + ''; + } + // commonArgs + ); + in + { + packages.cargo-tests = pkgs.writeShellScriptBin "run-tests" '' + set -e + + export WIRE_TEST_VM="${cargo-testing-vms}" + + for item in "${tests}"/*; do + echo "running $item" + "$item" + done + ''; + + _module.args = { + cargo-testing-vms = + let + mkVM = + index: + evalConfig { + inherit system; + modules = lib.singleton { + imports = [ "${inputs.nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix" ]; + + networking.hostName = "cargo-vm-${builtins.toString index}"; + + boot = { + loader = { + systemd-boot.enable = true; + efi.canTouchEfiVariables = true; + timeout = 0; + }; + + kernelParams = [ "console=ttyS0" ]; + }; + + services = { + openssh = { + enable = true; + settings = { + PermitRootLogin = "without-password"; + }; + }; + + getty.autologinUser = "root"; + }; + + virtualisation = { + graphics = false; + + diskSize = 5024; + diskImage = null; + + forwardPorts = [ + { + from = "host"; + host.port = 2000 + index; + guest.port = 22; + } + ]; + }; + + users.users.root.openssh.authorizedKeys.keys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPSvOZoSGVEpR6eTDK9OJ31MWQPF2s8oLc8J7MBh6nez marsh@maple" + ]; + + users.users.root.initialPassword = "root"; + + system.stateVersion = "23.11"; + }; + }; + in + pkgs.linkFarm "vm-forest" ( + builtins.map (index: { + path = (mkVM index).config.system.build.vm; + name = builtins.toString index; + }) (lib.range 0 1) + ); + }; + }; +} -- 2.51.2