From de83fcb2df588530c8e7257d62537adab0b325fd Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Sun, 18 Aug 2024 11:32:46 -0700 Subject: [PATCH 1/3] containers.*.config: reuse host `nixpkgs.pkgs` if defined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The minimum reproduction for the problem I'm trying to solve is that the following NixOS test with a trivial NixOS container: ``` { inputs = { nixpkgs.url = "github:NixOS/nixpkgs/24.05"; flake-utils.url = "github:numtide/flake-utils/v1.0.0"; }; outputs = { flake-utils, nixpkgs, self, ... }: flake-utils.lib.eachDefaultSystem (system: { checks.default = nixpkgs.legacyPackages."${system}".nixosTest { name = "test"; nodes.machine.containers.tutorial.config = { }; testScript = ""; }; }); } ``` … fails with the following error message: ``` error: Neither nodes.machine.nixpkgs.hostPlatform nor the legacy option nodes.machine.nixpkgs.system has been set. You can set nodes.machine.nixpkgs.hostPlatform in hardware-configuration.nix by re-running a recent version of nixos-generate-config. The option nodes.machine.nixpkgs.system is still fully supported for NixOS 22.05 interoperability, but will be deprecated in the future, so we recommend to set nodes.machine.nixpkgs.hostPlatform. ``` The root of the problem appears to be that in `nixos/modules/virtualisation/nixos-containers.nix` there is support for deriving the guest's `nixpkgs.hostPlatform` or `nixpkgs.localSystem` from the corresponding host's values, but this doesn't work if the host sets `nixpkgs.pkgs` instead of one of those values. In fact, this is what happens when using `pkgs.nixosTest` (which sets `nixpkgs.pkgs` in `pkgs/build-support/testers/default.nix`). The solution I went with was to forward the `nixpkgs.pkgs` setting from the host to the guest, but only if it is defined (matching the same treatment as `nixpkgs.hostPlatform` and `nixpkgs.localSystem`. --- nixos/modules/virtualisation/nixos-containers.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index 1f96fa2de491..7dc0eba205c6 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -488,7 +488,9 @@ in extraConfig = { options, ... }: { _file = "module at ${__curPos.file}:${toString __curPos.line}"; config = { - nixpkgs = if options.nixpkgs?hostPlatform && host.options.nixpkgs.hostPlatform.isDefined + nixpkgs = if options.nixpkgs?pkgs && host.options.nixpkgs.pkgs.isDefined + then { inherit (host.config.nixpkgs) pkgs; } + else if options.nixpkgs?hostPlatform && host.options.nixpkgs.hostPlatform.isDefined then { inherit (host.config.nixpkgs) hostPlatform; } else { inherit (host.config.nixpkgs) localSystem; } ; From 0600255046b085870a44a6385bd66dd9f171661b Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Sun, 18 Aug 2024 16:49:57 -0700 Subject: [PATCH 2/3] Use `host.pkgs.stdenv.hostPlatform` --- nixos/modules/virtualisation/nixos-containers.nix | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index 7dc0eba205c6..32ec588d0f00 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -488,12 +488,7 @@ in extraConfig = { options, ... }: { _file = "module at ${__curPos.file}:${toString __curPos.line}"; config = { - nixpkgs = if options.nixpkgs?pkgs && host.options.nixpkgs.pkgs.isDefined - then { inherit (host.config.nixpkgs) pkgs; } - else if options.nixpkgs?hostPlatform && host.options.nixpkgs.hostPlatform.isDefined - then { inherit (host.config.nixpkgs) hostPlatform; } - else { inherit (host.config.nixpkgs) localSystem; } - ; + nixpkgs = { inherit (host.pkgs.stdenv) hostPlatform; }; boot.isContainer = true; networking.hostName = mkDefault name; networking.useDHCP = false; From 79e5dbb2626735cffaff1c660425e8ce3927b399 Mon Sep 17 00:00:00 2001 From: Gabriella Gonzalez Date: Sun, 18 Aug 2024 17:00:16 -0700 Subject: [PATCH 3/3] Restore check for container not defining `nixpkgs.hostPlatform` option --- nixos/modules/virtualisation/nixos-containers.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index 32ec588d0f00..ba5112aa6cec 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -488,7 +488,11 @@ in extraConfig = { options, ... }: { _file = "module at ${__curPos.file}:${toString __curPos.line}"; config = { - nixpkgs = { inherit (host.pkgs.stdenv) hostPlatform; }; + nixpkgs = + if options.nixpkgs?hostPlatform + then { inherit (host.pkgs.stdenv) hostPlatform; } + else { localSystem = host.pkgs.stdenv.hostPlatform; } + ; boot.isContainer = true; networking.hostName = mkDefault name; networking.useDHCP = false;