From 34fcfc880bdc3c23c936e7b4d19bd0a93558d8f1 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Tue, 15 Dec 2020 20:24:57 +0100 Subject: tests/containers-custom-pkgs: improve test - Fix name - Remove unneeded leftovers that were copied from containers-hosts.nix - Remove redundant 'start_all()' --- nixos/tests/containers-custom-pkgs.nix | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'nixos') diff --git a/nixos/tests/containers-custom-pkgs.nix b/nixos/tests/containers-custom-pkgs.nix index 397a4a905e6d9..47509892e52e2 100644 --- a/nixos/tests/containers-custom-pkgs.nix +++ b/nixos/tests/containers-custom-pkgs.nix @@ -1,5 +1,3 @@ -# Test for NixOS' container support. - import ./make-test-python.nix ({ pkgs, lib, ...} : let customPkgs = pkgs // { @@ -9,34 +7,27 @@ import ./make-test-python.nix ({ pkgs, lib, ...} : let }; in { - name = "containers-hosts"; + name = "containers-custom-pkgs"; meta = with lib.maintainers; { maintainers = [ adisbladis ]; }; - machine = - { ... }: - { - virtualisation.memorySize = 256; - virtualisation.vlans = []; - - containers.simple = { - autoStart = true; - pkgs = customPkgs; - config = {pkgs, config, ... }: { - environment.systemPackages = [ - pkgs.hello - ]; - }; + machine = { ... }: { + containers.test = { + autoStart = true; + pkgs = customPkgs; + config = {pkgs, config, ... }: { + environment.systemPackages = [ + pkgs.hello + ]; }; - }; + }; testScript = '' - start_all() machine.wait_for_unit("default.target") machine.succeed( - "test $(nixos-container run simple -- readlink -f /run/current-system/sw/bin/hello) = ${customPkgs.hello}/bin/hello" + "test $(nixos-container run test -- readlink -f /run/current-system/sw/bin/hello) = ${customPkgs.hello}/bin/hello" ) ''; }) -- cgit 1.4.1 From 29385f0560ed3ed350f1af56b1ac3f827887ec56 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Tue, 15 Dec 2020 20:24:58 +0100 Subject: nixos-containers: remove redundant eval-config args The values of these args are identical to the default values defined in `eval-config.nix`. Note especially that `lib` is not reevaluated. --- nixos/modules/virtualisation/nixos-containers.nix | 2 -- 1 file changed, 2 deletions(-) (limited to 'nixos') diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index 26398afb3cf51..df9031c7e9ecb 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -476,8 +476,6 @@ in merge = loc: defs: (import (confPkgs.path + "/nixos/lib/eval-config.nix") { inherit system; pkgs = confPkgs; - baseModules = import (confPkgs.path + "/nixos/modules/module-list.nix"); - inherit (confPkgs) lib; modules = let extraConfig = { -- cgit 1.4.1 From 77c4fc2e89eca27e7ba559ed5b0f053a5ef122ce Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Tue, 15 Dec 2020 20:24:59 +0100 Subject: nixos-container: simplify 'pkgs' option type Set the default value directly instead of using a `null` proxy value. --- nixos/modules/virtualisation/nixos-containers.nix | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'nixos') diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index df9031c7e9ecb..5949c54052c47 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -469,13 +469,11 @@ in A specification of the desired configuration of this container, as a NixOS module. ''; - type = let - confPkgs = if config.pkgs == null then pkgs else config.pkgs; - in lib.mkOptionType { + type = lib.mkOptionType { name = "Toplevel NixOS config"; - merge = loc: defs: (import (confPkgs.path + "/nixos/lib/eval-config.nix") { + merge = loc: defs: (import (config.pkgs.path + "/nixos/lib/eval-config.nix") { inherit system; - pkgs = confPkgs; + pkgs = config.pkgs; modules = let extraConfig = { @@ -525,9 +523,9 @@ in }; pkgs = mkOption { - type = types.nullOr types.attrs; - default = null; - example = literalExample "pkgs"; + type = types.attrs; + default = pkgs; + defaultText = "pkgs"; description = '' Customise which nixpkgs to use for this container. ''; -- cgit 1.4.1 From 9a283a038d3c028f8b067c6b2e56f2e38bd93192 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Tue, 15 Dec 2020 20:25:00 +0100 Subject: nixos-container: fix `nixpkgs` container options being ignored Since the introduction of option `containers..pkgs`, the `nixpkgs.*` options (including `nixpkgs.pkgs`, `nixpkgs.config`, ...) were always ignored in container configs, which broke existing containers. This was due to `containers..pkgs` having two separate effects: (1) It sets the source for the modules that are used to evaluate the container. (2) It sets the `pkgs` arg (`_module.args.pkgs`) that is used inside the container modules. This happens even when the default value of `containers..pkgs` is unchanged, in which case the container `pkgs` arg is set to the pkgs of the host system. Previously, the `pkgs` arg was determined by the `containers..config.nixpkgs.*` options. This commit reverts the breaking change (2) while adding a backwards-compatible way to achieve (1). It removes option `pkgs` and adds option `nixpkgs` which implements (1). Existing users of `pkgs` are informed by an error message to use option `nixpkgs` or to achieve only (2) by setting option `containers..config.nixpkgs.pkgs`. --- nixos/modules/virtualisation/nixos-containers.nix | 49 ++++++++++++++++------- nixos/tests/containers-custom-pkgs.nix | 35 ++++++++-------- 2 files changed, 53 insertions(+), 31 deletions(-) (limited to 'nixos') diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index 5949c54052c47..c721858225c9d 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -463,7 +463,6 @@ in { config, options, name, ... }: { options = { - config = mkOption { description = '' A specification of the desired configuration of this @@ -471,9 +470,8 @@ in ''; type = lib.mkOptionType { name = "Toplevel NixOS config"; - merge = loc: defs: (import (config.pkgs.path + "/nixos/lib/eval-config.nix") { + merge = loc: defs: (import "${toString config.nixpkgs}/nixos/lib/eval-config.nix" { inherit system; - pkgs = config.pkgs; modules = let extraConfig = { @@ -522,12 +520,18 @@ in ''; }; - pkgs = mkOption { - type = types.attrs; - default = pkgs; - defaultText = "pkgs"; + nixpkgs = mkOption { + type = types.path; + default = pkgs.path; + defaultText = "pkgs.path"; description = '' - Customise which nixpkgs to use for this container. + A path to the nixpkgs that provide the modules, pkgs and lib for evaluating the container. + + To only change the pkgs argument used inside the container modules, + set the nixpkgs.* options in the container . + Setting config.nixpkgs.pkgs = pkgs speeds up the container evaluation + by reusing the system pkgs, but the nixpkgs.config option in the + container config is ignored in this case. ''; }; @@ -668,14 +672,31 @@ in ''; }; + # Removed option. See `checkAssertion` below for the accompanying error message. + pkgs = mkOption { visible = false; }; } // networkOptions; - config = mkMerge - [ - (mkIf options.config.isDefined { - path = config.config.system.build.toplevel; - }) - ]; + config = let + # Throw an error when removed option `pkgs` is used. + # Because this is a submodule we cannot use `mkRemovedOptionModule` or option `assertions`. + optionPath = "containers.${name}.pkgs"; + files = showFiles options.pkgs.files; + checkAssertion = if options.pkgs.isDefined then throw '' + The option definition `${optionPath}' in ${files} no longer has any effect; please remove it. + + Alternatively, you can use the following options: + - containers.${name}.nixpkgs + This sets the nixpkgs (and thereby the modules, pkgs and lib) that + are used for evaluating the container. + + - containers.${name}.config.nixpkgs.pkgs + This only sets the `pkgs` argument used inside the container modules. + '' + else null; + in { + path = builtins.seq checkAssertion + mkIf options.config.isDefined config.config.system.build.toplevel; + }; })); default = {}; diff --git a/nixos/tests/containers-custom-pkgs.nix b/nixos/tests/containers-custom-pkgs.nix index 47509892e52e2..1412c32bfb5f1 100644 --- a/nixos/tests/containers-custom-pkgs.nix +++ b/nixos/tests/containers-custom-pkgs.nix @@ -1,33 +1,34 @@ import ./make-test-python.nix ({ pkgs, lib, ...} : let - customPkgs = pkgs // { - hello = pkgs.hello.overrideAttrs(old: { - name = "custom-hello"; + customPkgs = pkgs.appendOverlays [ (self: super: { + hello = super.hello.overrideAttrs (old: { + name = "custom-hello"; }); - }; + }) ]; in { name = "containers-custom-pkgs"; meta = with lib.maintainers; { - maintainers = [ adisbladis ]; + maintainers = [ adisbladis earvstedt ]; }; - machine = { ... }: { + machine = { config, ... }: { + assertions = let + helloName = (builtins.head config.containers.test.config.system.extraDependencies).name; + in [ { + assertion = helloName == "custom-hello"; + message = "Unexpected value: ${helloName}"; + } ]; + containers.test = { autoStart = true; - pkgs = customPkgs; - config = {pkgs, config, ... }: { - environment.systemPackages = [ - pkgs.hello - ]; + config = { pkgs, config, ... }: { + nixpkgs.pkgs = customPkgs; + system.extraDependencies = [ pkgs.hello ]; }; }; }; - testScript = '' - machine.wait_for_unit("default.target") - machine.succeed( - "test $(nixos-container run test -- readlink -f /run/current-system/sw/bin/hello) = ${customPkgs.hello}/bin/hello" - ) - ''; + # This test only consists of evaluating the test machine + testScript = ""; }) -- cgit 1.4.1