diff --git a/nixos/lib/testing/default.nix b/nixos/lib/testing/default.nix index a89f734b1e645..62e0fea8af6ff 100644 --- a/nixos/lib/testing/default.nix +++ b/nixos/lib/testing/default.nix @@ -14,6 +14,7 @@ let ./legacy.nix ./meta.nix ./name.nix + ./macos-host.nix ./network.nix ./nodes.nix ./pkgs.nix diff --git a/nixos/lib/testing/macos-host.nix b/nixos/lib/testing/macos-host.nix new file mode 100644 index 0000000000000..5cb9169b0bd71 --- /dev/null +++ b/nixos/lib/testing/macos-host.nix @@ -0,0 +1,19 @@ +{ config, lib, hostPkgs, ... }: + +{ + config = lib.mkIf hostPkgs.stdenv.hostPlatform.isDarwin { + defaults = { + virtualisation.host.pkgs = hostPkgs; + + # On non-darwin, mounting the whole nix store from the host is the default because + # this generally makes rebuilds of the VMs faster and the VMs smaller, while reducing + # disk I/O. + # However, on macOS it currently leads to many crashing guest processes. + virtualisation.useNixStoreImage = true; + # Without this, the store image uses "raw" instead of "qcow2" and + # that throws some errors on macOS on startup of the VM + # TODO: remove when `raw` errors are fixed. + virtualisation.writableStore = true; + }; + }; +} diff --git a/nixos/lib/testing/run.nix b/nixos/lib/testing/run.nix index 0cd07d8afd21d..390d36ba27716 100644 --- a/nixos/lib/testing/run.nix +++ b/nixos/lib/testing/run.nix @@ -33,7 +33,9 @@ in derivation = hostPkgs.stdenv.mkDerivation { name = "vm-test-run-${config.name}"; - requiredSystemFeatures = [ "kvm" "nixos-test" ]; + requiredSystemFeatures = [ "nixos-test" ] + ++ lib.optionals hostPkgs.stdenv.hostPlatform.isLinux [ "kvm" ] + ++ lib.optionals hostPkgs.stdenv.hostPlatform.isDarwin [ "apple-virt" ]; buildCommand = '' mkdir -p $out diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index e0004df6f6b2f..d6afccc7fcee5 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -208,6 +208,10 @@ let idx=$((idx + 1)) '')} + ${lib.optionalString (hostPkgs.stdenv.hostPlatform.isDarwin && !config.virtualisation.useNixStoreImage) '' + ulimit -n 12288 + ''} + # Start QEMU. exec ${qemu-common.qemuBinary qemu} \ -name ${config.system.name} \ diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index fc10597e3e120..a3da399c772bc 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -97,9 +97,15 @@ # See doc/builders/testers.chapter.md or # https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest runNixOSTest = - let nixos = import ../../../nixos/lib { - inherit lib; - }; + let + toGuest = builtins.replaceStrings [ "darwin" ] [ "linux" ]; + guestPkgs = + if (!pkgs.stdenv.hostPlatform.isDarwin) + then pkgs + else import pkgs.path { system = toGuest pkgs.system; }; + nixos = import ../../../nixos/lib { + inherit lib; + }; in testModule: nixos.runTest { _file = "pkgs.runNixOSTest implementation"; @@ -107,7 +113,7 @@ (lib.setDefaultModuleLocation "the argument that was passed to pkgs.runNixOSTest" testModule) ]; hostPkgs = pkgs; - node.pkgs = pkgs; + node.pkgs = guestPkgs; }; # See doc/builders/testers.chapter.md or