From b0ac7f2507265a5bf175e158bda109865466810f Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Sat, 20 Jan 2024 11:02:58 -0800 Subject: [PATCH 01/20] macOS support for NixOS tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #193336 Closes #261694 Related to 108984 This pull request is a composite of the changes from #193336 and also #261694, with some changes of my own that I made. The goal here was to get the following flake to build and run on `aarch64-darwin`: ```nix { inputs.nixpkgs.url = ; outputs = { nixpkgs, ... }: { checks.aarch64-darwin.default = nixpkgs.legacyPackages.aarch64-darwin.nixosTest { name = "test"; nodes.machine = { }; testScript = ""; }; }; } ``` … and after this change it does. There's no longer a need for the user to set `nodes.*.nixpkgs.pkgs` or `nodes.*.virtualisation.host.pkgs` as the correct values are inferred from the host system. This change is spiritually much closer to the approach in #261694 than the approach in #193336. However, I still made a few changes compared to #261694: - I didn't include the change to increase `ulimit` I think this change was questionable because I feel like a script provided by Nixpkgs shouldn't be tinkering with the user's ambient `ulimit` settings. - I named the required system feature `hvf` instead of `apple-virt` I preferred the `hvf` system feature name chosen by #193336. - I didn't use the `node.pkgs` setting to set `nodes.*.nixpkgs.pkgs` That does not work, based on my testing, but setting the same thing in `nixos/lib/testing/nodes.nix` does work. - I created a `pkgsLocal` helper This is based on the feedback from @roberth here: https://github.com/NixOS/nixpkgs/pull/261694/files#r1365443955 … and is similar in spirit to `pkgsCross`. --- nixos/lib/testing/default.nix | 1 + nixos/lib/testing/macos-host.nix | 18 ++++++++++++++++++ nixos/lib/testing/nodes.nix | 25 +++++++++++++++++++++++-- nixos/lib/testing/run.nix | 4 +++- pkgs/top-level/stage.nix | 15 +++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 nixos/lib/testing/macos-host.nix diff --git a/nixos/lib/testing/default.nix b/nixos/lib/testing/default.nix index a89f734b1e64509..62e0fea8af6ff18 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 000000000000000..14aafa4f61ee878 --- /dev/null +++ b/nixos/lib/testing/macos-host.nix @@ -0,0 +1,18 @@ +{ config, lib, hostPkgs, ... }: + +{ + config = lib.mkIf hostPkgs.stdenv.hostPlatform.isDarwin { + defaults = { + # 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/nodes.nix b/nixos/lib/testing/nodes.nix index 73e6d386fd1da3f..605577666235eca 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -14,6 +14,25 @@ let types ; + inherit (hostPkgs) hostPlatform pkgsLocal; + + guestPkgs = + if hostPlatform.isLinux + then hostPkgs + else + let + hostToGuest = { + "x86_64-darwin" = pkgsLocal.gnu64; + "aarch64-darwin" = pkgsLocal.aarch64-multiplatform; + }; + + supportedHosts = lib.concatStringsSep ", " (lib.attrNames hostToGuest); + + message = + "Unsupported non-Linux host system: ${hostPlatform.system}, supported: ${supportedHosts}"; + in + hostToGuest.${hostPlatform.system} or (throw message); + baseOS = import ../eval-config.nix { inherit lib; @@ -24,16 +43,18 @@ let [ ./nixos-test-base.nix { key = "nodes"; _module.args.nodes = config.nodesCompat; } - ({ config, ... }: + ({ config, pkgs, ... }: { virtualisation.qemu.package = testModuleArgs.config.qemu.package; + virtualisation.host.pkgs = hostPkgs; + nixpkgs.pkgs = guestPkgs; }) ({ options, ... }: { key = "nodes.nix-pkgs"; config = optionalAttrs (!config.node.pkgsReadOnly) ( mkIf (!options.nixpkgs.pkgs.isDefined) { # TODO: switch to nixpkgs.hostPlatform and make sure containers-imperative test still evaluates. - nixpkgs.system = hostPkgs.stdenv.hostPlatform.system; + nixpkgs.system = hostPlatform.system; } ); }) diff --git a/nixos/lib/testing/run.nix b/nixos/lib/testing/run.nix index 9440c1acdfd816f..de5a9b97e61d513 100644 --- a/nixos/lib/testing/run.nix +++ b/nixos/lib/testing/run.nix @@ -41,7 +41,9 @@ in rawTestDerivation = 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/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix index 1cc05167cee8371..c7cc18ce37b3797 100644 --- a/pkgs/top-level/stage.nix +++ b/pkgs/top-level/stage.nix @@ -185,10 +185,25 @@ let # that target system. For instance, pkgsCross.raspberryPi.hello, # will refer to the "hello" package built for the ARM6-based # Raspberry Pi. + # + # This is essentially the same thing as pkgsLocal except for crossSystem + # instead of localSystem. pkgsCross = lib.mapAttrs (n: crossSystem: nixpkgsFun { inherit crossSystem; }) lib.systems.examples; + # This maps each entry in lib.systems.examples to its own package set where + # packages are built and run on that system. For example, + # pkgsLocal.x86_64-linux.hello will refer to the "hello" package bult for + # x86_64-linux. + # + # This is essentially the same thing as pkgsCross except for localSystem + # instead of crossSystem. + pkgsLocal = + lib.mapAttrs + (n: localSystem: nixpkgsFun { inherit localSystem; }) + lib.systems.examples; + pkgsLLVM = nixpkgsFun { overlays = [ (self': super': { From 6c013e3051f6206890a8f5af672a39642ec9e32d Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Fri, 16 Feb 2024 13:55:56 -0800 Subject: [PATCH 02/20] Improve error message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as suggested by @roberth Co-authored-by: Robert Hensing --- nixos/lib/testing/nodes.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index 605577666235eca..aaf996c325d56e5 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -29,7 +29,7 @@ let supportedHosts = lib.concatStringsSep ", " (lib.attrNames hostToGuest); message = - "Unsupported non-Linux host system: ${hostPlatform.system}, supported: ${supportedHosts}"; + "NixOS Test: don't know which VM guest system to pair with VM host system: ${hostPlatform.system}. Perhaps you intended to run the tests on a Linux host, or one of the following systems that may run NixOS tests: ${supportedHosts}"; in hostToGuest.${hostPlatform.system} or (throw message); From 9aa89945e1c5e58859e3b0aa8f987ae8c025316e Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Fri, 16 Feb 2024 13:56:32 -0800 Subject: [PATCH 03/20] Remove unused `pkgs` parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as suggested by @roberth Co-authored-by: Robert Hensing --- nixos/lib/testing/nodes.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index aaf996c325d56e5..2a3f77bb5e0b9f0 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -43,7 +43,7 @@ let [ ./nixos-test-base.nix { key = "nodes"; _module.args.nodes = config.nodesCompat; } - ({ config, pkgs, ... }: + ({ config, ... }: { virtualisation.qemu.package = testModuleArgs.config.qemu.package; virtualisation.host.pkgs = hostPkgs; From ccbfcca197297c4a0fb947641d1cb9ef5e8e1ce7 Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Fri, 16 Feb 2024 13:58:23 -0800 Subject: [PATCH 04/20] s/pkgsLocal/pkgsNative/ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as suggsted by @roberth --- nixos/lib/testing/nodes.nix | 6 +++--- pkgs/top-level/stage.nix | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index 2a3f77bb5e0b9f0..5df9aafc37fa0ed 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -14,7 +14,7 @@ let types ; - inherit (hostPkgs) hostPlatform pkgsLocal; + inherit (hostPkgs) hostPlatform pkgsNative; guestPkgs = if hostPlatform.isLinux @@ -22,8 +22,8 @@ let else let hostToGuest = { - "x86_64-darwin" = pkgsLocal.gnu64; - "aarch64-darwin" = pkgsLocal.aarch64-multiplatform; + "x86_64-darwin" = pkgsNative.gnu64; + "aarch64-darwin" = pkgsNative.aarch64-multiplatform; }; supportedHosts = lib.concatStringsSep ", " (lib.attrNames hostToGuest); diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix index c7cc18ce37b3797..013bfc6129cb765 100644 --- a/pkgs/top-level/stage.nix +++ b/pkgs/top-level/stage.nix @@ -186,7 +186,7 @@ let # will refer to the "hello" package built for the ARM6-based # Raspberry Pi. # - # This is essentially the same thing as pkgsLocal except for crossSystem + # This is essentially the same thing as pkgsNative except for crossSystem # instead of localSystem. pkgsCross = lib.mapAttrs (n: crossSystem: nixpkgsFun { inherit crossSystem; }) @@ -194,12 +194,12 @@ let # This maps each entry in lib.systems.examples to its own package set where # packages are built and run on that system. For example, - # pkgsLocal.x86_64-linux.hello will refer to the "hello" package bult for + # pkgsNative.x86_64-linux.hello will refer to the "hello" package bult for # x86_64-linux. # # This is essentially the same thing as pkgsCross except for localSystem # instead of crossSystem. - pkgsLocal = + pkgsNative = lib.mapAttrs (n: localSystem: nixpkgsFun { inherit localSystem; }) lib.systems.examples; From 2bc4a7959477a91d14d6524403bb7c4b653bf4e4 Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Fri, 16 Feb 2024 14:00:15 -0800 Subject: [PATCH 05/20] Exclude `pkgsNative` from build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as caught by @cole-h --- pkgs/top-level/release-attrpaths-superset.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/top-level/release-attrpaths-superset.nix b/pkgs/top-level/release-attrpaths-superset.nix index 673b63a5ac34f35..14847a86333e367 100644 --- a/pkgs/top-level/release-attrpaths-superset.nix +++ b/pkgs/top-level/release-attrpaths-superset.nix @@ -52,6 +52,7 @@ let pkgsMusl = true; pkgsStatic = true; pkgsCross = true; + pkgsNative = true; pkgsi686Linux = true; }; From 21f534a7ec630f54e3edc65533ce72b67f4af35f Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Fri, 16 Feb 2024 15:11:19 -0800 Subject: [PATCH 06/20] Fix typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as caught by @robert Co-authored-by: Robert Hensing --- pkgs/top-level/stage.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix index 013bfc6129cb765..2af7f31f1192f5b 100644 --- a/pkgs/top-level/stage.nix +++ b/pkgs/top-level/stage.nix @@ -194,7 +194,7 @@ let # This maps each entry in lib.systems.examples to its own package set where # packages are built and run on that system. For example, - # pkgsNative.x86_64-linux.hello will refer to the "hello" package bult for + # pkgsNative.x86_64-linux.hello will refer to the "hello" package built for # x86_64-linux. # # This is essentially the same thing as pkgsCross except for localSystem From 90679ea06c7f5ad3538d8c3940aba576865609bf Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Wed, 21 Feb 2024 15:12:03 -0800 Subject: [PATCH 07/20] Specify guest *system* instead of guest *packages* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … based on feedback from @roberth This plays nicer with other changes the user might want to make and also works in conjunction with the `pkgsReadOnly`. Note that this required deleting the `nixpkgs.pkgs` default because that's not compatible with setting `nixpkgs.system`. If `nixpkgs.pkgs` is defined (even as a default-valued option) then `nixpkgs.system` is ignored. --- nixos/lib/testing/nodes.nix | 11 +++++------ pkgs/build-support/testers/default.nix | 5 ----- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index 5df9aafc37fa0ed..eb9bf27cd8bacad 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -16,14 +16,14 @@ let inherit (hostPkgs) hostPlatform pkgsNative; - guestPkgs = + guestSystem = if hostPlatform.isLinux - then hostPkgs + then hostPkgs.system else let hostToGuest = { - "x86_64-darwin" = pkgsNative.gnu64; - "aarch64-darwin" = pkgsNative.aarch64-multiplatform; + "x86_64-darwin" = "x86_64-linux"; + "aarch64-darwin" = "aarch64-linux"; }; supportedHosts = lib.concatStringsSep ", " (lib.attrNames hostToGuest); @@ -47,14 +47,13 @@ let { virtualisation.qemu.package = testModuleArgs.config.qemu.package; virtualisation.host.pkgs = hostPkgs; - nixpkgs.pkgs = guestPkgs; }) ({ options, ... }: { key = "nodes.nix-pkgs"; config = optionalAttrs (!config.node.pkgsReadOnly) ( mkIf (!options.nixpkgs.pkgs.isDefined) { # TODO: switch to nixpkgs.hostPlatform and make sure containers-imperative test still evaluates. - nixpkgs.system = hostPlatform.system; + nixpkgs.system = guestSystem; } ); }) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index fc10597e3e120ed..73cd1899a743151 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -121,11 +121,6 @@ (import ../../../nixos/lib/testing-python.nix { inherit (stdenv.hostPlatform) system; inherit pkgs; - extraConfigurations = [( - { lib, ... }: { - config.nixpkgs.pkgs = lib.mkDefault pkgs; - } - )]; }); in test: From 03f2f87b543a5ab76e20253247a922134382bd5c Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Wed, 21 Feb 2024 15:18:04 -0800 Subject: [PATCH 08/20] Revert the addition of `pkgsNative` This is no longer necessary now that we're overriding `nixpkgs.system` instead of `nixpkgs.pkgs` --- pkgs/top-level/release-attrpaths-superset.nix | 1 - pkgs/top-level/stage.nix | 15 --------------- 2 files changed, 16 deletions(-) diff --git a/pkgs/top-level/release-attrpaths-superset.nix b/pkgs/top-level/release-attrpaths-superset.nix index 14847a86333e367..673b63a5ac34f35 100644 --- a/pkgs/top-level/release-attrpaths-superset.nix +++ b/pkgs/top-level/release-attrpaths-superset.nix @@ -52,7 +52,6 @@ let pkgsMusl = true; pkgsStatic = true; pkgsCross = true; - pkgsNative = true; pkgsi686Linux = true; }; diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix index 2af7f31f1192f5b..1cc05167cee8371 100644 --- a/pkgs/top-level/stage.nix +++ b/pkgs/top-level/stage.nix @@ -185,25 +185,10 @@ let # that target system. For instance, pkgsCross.raspberryPi.hello, # will refer to the "hello" package built for the ARM6-based # Raspberry Pi. - # - # This is essentially the same thing as pkgsNative except for crossSystem - # instead of localSystem. pkgsCross = lib.mapAttrs (n: crossSystem: nixpkgsFun { inherit crossSystem; }) lib.systems.examples; - # This maps each entry in lib.systems.examples to its own package set where - # packages are built and run on that system. For example, - # pkgsNative.x86_64-linux.hello will refer to the "hello" package built for - # x86_64-linux. - # - # This is essentially the same thing as pkgsCross except for localSystem - # instead of crossSystem. - pkgsNative = - lib.mapAttrs - (n: localSystem: nixpkgsFun { inherit localSystem; }) - lib.systems.examples; - pkgsLLVM = nixpkgsFun { overlays = [ (self': super': { From 08702014a400b230c3a21303afd730174e77d306 Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Wed, 21 Feb 2024 15:52:48 -0800 Subject: [PATCH 09/20] Remove `macos-host.nix` Based on my local testing these options seem unnecessary and the VM runs fine without them being set. I originally cargo-culted these from #261694 but they don't seem necessary. --- nixos/lib/testing/default.nix | 1 - nixos/lib/testing/macos-host.nix | 18 ------------------ 2 files changed, 19 deletions(-) delete mode 100644 nixos/lib/testing/macos-host.nix diff --git a/nixos/lib/testing/default.nix b/nixos/lib/testing/default.nix index 62e0fea8af6ff18..a89f734b1e64509 100644 --- a/nixos/lib/testing/default.nix +++ b/nixos/lib/testing/default.nix @@ -14,7 +14,6 @@ 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 deleted file mode 100644 index 14aafa4f61ee878..000000000000000 --- a/nixos/lib/testing/macos-host.nix +++ /dev/null @@ -1,18 +0,0 @@ -{ config, lib, hostPkgs, ... }: - -{ - config = lib.mkIf hostPkgs.stdenv.hostPlatform.isDarwin { - defaults = { - # 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; - }; - }; -} From ba4e0dd6a1056e0139fa849ba5068716868c80a2 Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Wed, 21 Feb 2024 16:02:13 -0800 Subject: [PATCH 10/20] Remove unused `pkgsNative` --- nixos/lib/testing/nodes.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index eb9bf27cd8bacad..b2ffeeaa9e0045c 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -14,7 +14,7 @@ let types ; - inherit (hostPkgs) hostPlatform pkgsNative; + inherit (hostPkgs) hostPlatform; guestSystem = if hostPlatform.isLinux From 0474c0aff4d98597516290a451a236bd95860c67 Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Wed, 21 Feb 2024 16:55:05 -0800 Subject: [PATCH 11/20] Fix evaluation error on Linux --- nixos/lib/testing/nodes.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index b2ffeeaa9e0045c..7941d69e38d2bd3 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -18,7 +18,7 @@ let guestSystem = if hostPlatform.isLinux - then hostPkgs.system + then hostPlatform.system else let hostToGuest = { From ab45d3118634ad3cd0c319e5bdb42e31524ecb0d Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Wed, 28 Feb 2024 15:34:06 -0800 Subject: [PATCH 12/20] Fix test failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Specifically, the `nixosTest-example` test correctly flagged that the change from 90679ea06c7f5ad3538d8c3940aba576865609bf broke the user's expectation that the `pkgs` in `pkgs.nixosTest` would be reused as the package set for the NixOS test system. However, if we restore the original code removed by that commit: ```nix nixpkgs.pkgs = lib.mkDefault pkgs; ``` … then that won't work because then we can't override `nixpkgs.system` and reinstantiate that package set. The whole *point* of this set of changes is that we DON'T want to use the user's `pkgs` set as is because then the wrong system is used for the NixOS test (on macOS). Specifically, on Apple Silicon the `pkgs` in `pkgs.nixosTest` is going to be instantiated with `system = aarch64-darwin;` but we want the `nixpkgs.pkgs` that the NixOS machine uses to be that same package set reinstantiated with `system = aarch64-linux;`. Then I realized we can strike a balance by doing this instead: ```nix nixpkgs.overlays = lib.mkDefault pkgs.overlays; nixpkgs.config = lib.mkDefault pkgs.config; ``` … then we can get something which splits the difference: we preserve the user's original package overrides but now we can reinstantiate `nixpkgs.pkgs` with the new (correct) `nixpkgs.system`. This isn't a foolproof solution, though, because `pkgs.overlays` and `pkgs.config` do not necessarily contain all the information to recreate the original `pkgs` that the user supplied. As a concrete example, if the user were to extend the package set "artificially" like this: ```nix (pkgs // { foo = …; }).nixosTest … ``` … then that `pkgs.foo` attribute would no longer be passed along to `nixpgs.pkgs`. However, in practice I believe most users are going to be overriding `pkgs` using either the `config.packageOverrides` or `overlays` arguments to the Nixpkgs function, so I think this is a reasonable compromise. Moreover, the benefit of this compromise is that the NixOS test framework is much more "fire and forget" (we don't need to instruct the users to instantiate Nixpkgs themselves twice for the host and guest architecture). --- pkgs/build-support/testers/default.nix | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index 73cd1899a743151..9471d6a083aaa54 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -121,6 +121,22 @@ (import ../../../nixos/lib/testing-python.nix { inherit (stdenv.hostPlatform) system; inherit pkgs; + extraConfigurations = [( + { lib, ... }: { + # Carefully note that we intentionally don't do this: + # + # config.nixpkgs.pkgs = lib.mkDefault pkgs; + # + # … because we want to to be able to reinstantiate `nixpkgs.pkgs` + # with the (possibly different) `nixpkgs.system` specified in + # `./nixos/lib/testing/nodes.nix`. This means that only package + # overrides specified in the `overlays` or + # `config.packageOverrides` arguments to Nixpkgs will be passed + # through to `nixpkgs.pkgs`. + config.nixpkgs.overlays = lib.mkDefault pkgs.overlays; + config.nixpkgs.config = lib.mkDefault pkgs.config; + } + )]; }); in test: From 5da6236a6db86f35f6cfc877732611040f9b64de Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 16:14:12 +0100 Subject: [PATCH 13/20] nixosTest: Make similar to previous implementation again I'd considered suggesting to add a conditional here, but that would create VM-host-dependent portability issues for no good reason. --- pkgs/build-support/testers/default.nix | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index 9471d6a083aaa54..da537a0f915b473 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -123,18 +123,12 @@ inherit pkgs; extraConfigurations = [( { lib, ... }: { - # Carefully note that we intentionally don't do this: - # - # config.nixpkgs.pkgs = lib.mkDefault pkgs; - # - # … because we want to to be able to reinstantiate `nixpkgs.pkgs` - # with the (possibly different) `nixpkgs.system` specified in - # `./nixos/lib/testing/nodes.nix`. This means that only package - # overrides specified in the `overlays` or - # `config.packageOverrides` arguments to Nixpkgs will be passed - # through to `nixpkgs.pkgs`. - config.nixpkgs.overlays = lib.mkDefault pkgs.overlays; - config.nixpkgs.config = lib.mkDefault pkgs.config; + config.nixpkgs.pkgs = lib.mkDefault ( + import pkgs.path { + inherit (pkgs) config overlays; + system = stdenv.hostPlatform.parsed.cpu.name + "-linux"; + } + ); } )]; }); From 724e0212b5606d517c82b2a378aa94e2e85c2c2c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 16:23:10 +0100 Subject: [PATCH 14/20] pkgs.pkgsLinux: init --- pkgs/top-level/stage.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix index 1cc05167cee8371..54251d33bb59fc8 100644 --- a/pkgs/top-level/stage.nix +++ b/pkgs/top-level/stage.nix @@ -243,6 +243,16 @@ let }; } else throw "x86_64 Darwin package set can only be used on Darwin systems."; + # If already linux: the same package set unaltered + # Otherwise, return a natively built linux package set for the current cpu architecture string. + # (ABI and other details will be set to the default for the cpu/os pair) + pkgsLinux = + if stdenv.hostPlatform.isLinux + then self + else nixpkgsFun { + localSystem = lib.systems.elaborate "${stdenv.hostPlatform.parsed.cpu.name}-linux"; + }; + # Extend the package set with zero or more overlays. This preserves # preexisting overlays. Prefer to initialize with the right overlays # in one go when calling Nixpkgs, for performance and simplicity. From 2ef8ac2416f3013bb3837f7f7a3ae03e2013c102 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 16:23:39 +0100 Subject: [PATCH 15/20] nixosTest: Use memoized pkgsLinux This solves the previously introduced evaluation performance regression when evaluating multiple tests at once. --- pkgs/build-support/testers/default.nix | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index da537a0f915b473..992acdc6fb95d87 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -1,4 +1,4 @@ -{ pkgs, buildPackages, lib, callPackage, runCommand, stdenv, substituteAll, testers }: +{ pkgs, pkgsLinux, buildPackages, lib, callPackage, runCommand, stdenv, substituteAll, testers }: # Documentation is in doc/builders/testers.chapter.md { # See https://nixos.org/manual/nixpkgs/unstable/#tester-testBuildFailure @@ -123,12 +123,7 @@ inherit pkgs; extraConfigurations = [( { lib, ... }: { - config.nixpkgs.pkgs = lib.mkDefault ( - import pkgs.path { - inherit (pkgs) config overlays; - system = stdenv.hostPlatform.parsed.cpu.name + "-linux"; - } - ); + config.nixpkgs.pkgs = lib.mkDefault pkgsLinux; } )]; }); From 315229bcd718c6e776c7c144900791dc8d94c05c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 16:25:34 +0100 Subject: [PATCH 16/20] testers.runNixOSTest: Make it work on non-linux VM hosts --- pkgs/build-support/testers/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index 992acdc6fb95d87..362622d9b7ee837 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -107,7 +107,7 @@ (lib.setDefaultModuleLocation "the argument that was passed to pkgs.runNixOSTest" testModule) ]; hostPkgs = pkgs; - node.pkgs = pkgs; + node.pkgs = pkgsLinux; }; # See doc/builders/testers.chapter.md or From b9159142c50ade7111457aac47344e1065a15bf3 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 17:29:13 +0100 Subject: [PATCH 17/20] treewide: Make some NixOS tests darwin-host-proof --- nixos/tests/acme.nix | 5 ++++- pkgs/build-support/testers/test/default.nix | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/nixos/tests/acme.nix b/nixos/tests/acme.nix index 272782dc2f621da..d63a77fcdd23cff 100644 --- a/nixos/tests/acme.nix +++ b/nixos/tests/acme.nix @@ -1,4 +1,7 @@ -{ pkgs, lib, ... }: let +{ config, lib, ... }: let + + pkgs = config.node.pkgs; + commonConfig = ./common/acme/client; dnsServerIP = nodes: nodes.dnsserver.networking.primaryIPAddress; diff --git a/pkgs/build-support/testers/test/default.nix b/pkgs/build-support/testers/test/default.nix index c48c9f299ebf070..da67711156bede6 100644 --- a/pkgs/build-support/testers/test/default.nix +++ b/pkgs/build-support/testers/test/default.nix @@ -27,11 +27,11 @@ lib.recurseIntoAttrs { # Check that the wiring of nixosTest is correct. # Correct operation of the NixOS test driver should be asserted elsewhere. - nixosTest-example = pkgs-with-overlay.testers.nixosTest ({ lib, pkgs, figlet, ... }: { + nixosTest-example = pkgs-with-overlay.testers.nixosTest ({ lib, ... }: { name = "nixosTest-test"; nodes.machine = { pkgs, ... }: { system.nixos = dummyVersioning; - environment.systemPackages = [ pkgs.proof-of-overlay-hello figlet ]; + environment.systemPackages = [ pkgs.proof-of-overlay-hello pkgs.figlet ]; }; testScript = '' machine.succeed("hello | figlet >/dev/console") From ddab3591954178e204d92308f7c7ca1c106809ee Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 17:29:48 +0100 Subject: [PATCH 18/20] nixos/all-tests: Make runTest work on darwin --- nixos/tests/all-tests.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 9e27969190f7574..0e4f0c3482dd5d1 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -78,8 +78,9 @@ let # it with `allowAliases = false`? # warnIf pkgs.config.allowAliases "nixosTests: pkgs includes aliases." { + _file = "${__curPos.file} readOnlyPkgs"; _class = "nixosTest"; - node.pkgs = pkgs; + node.pkgs = pkgs.pkgsLinux; }; in { From 3ef5de94318c38468077604bce72d6eb89257558 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 1 Mar 2024 17:38:50 +0100 Subject: [PATCH 19/20] nixos/testing/pkgs: Warn darwin users about ambiguous `pkgs` --- nixos/lib/testing/pkgs.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nixos/lib/testing/pkgs.nix b/nixos/lib/testing/pkgs.nix index 22dd586868e30e6..46d82c65d26a316 100644 --- a/nixos/lib/testing/pkgs.nix +++ b/nixos/lib/testing/pkgs.nix @@ -2,7 +2,11 @@ { config = { # default pkgs for use in VMs - _module.args.pkgs = hostPkgs; + _module.args.pkgs = + # TODO: deprecate it everywhere; not just on darwin. Throw on darwin? + lib.warnIf hostPkgs.stdenv.hostPlatform.isDarwin + "Do not use the `pkgs` module argument in tests you want to run on darwin. It is ambiguous, and many tests are broken because of it. If you need to use a package on the VM host, use `hostPkgs`. Otherwise, use `config.node.pkgs`, or `config.nodes..nixpkgs.pkgs`." + hostPkgs; defaults = { # TODO: a module to set a shared pkgs, if options.nixpkgs.* is untouched by user (highestPrio) */ From c73be487db76a2efc85a4d1d6cd8c22a7e8663cf Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Fri, 1 Mar 2024 20:41:16 -0800 Subject: [PATCH 20/20] Exclude `pkgsLinux` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as suggested by @cole-h --- pkgs/top-level/release-attrpaths-superset.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/top-level/release-attrpaths-superset.nix b/pkgs/top-level/release-attrpaths-superset.nix index 673b63a5ac34f35..28fe933e54f3e23 100644 --- a/pkgs/top-level/release-attrpaths-superset.nix +++ b/pkgs/top-level/release-attrpaths-superset.nix @@ -53,6 +53,7 @@ let pkgsStatic = true; pkgsCross = true; pkgsi686Linux = true; + pkgsLinux = true; }; # No release package attrname may have any of these at a component