git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #106767 from erikarvstedt/fix-container-pkgs-2

nixos-container: fix `nixpkgs` container options being ignored

commit3c6035cd9a87e5363bff0792ce5588b179a50946 authoradisbladis <adisbladis@gmail.com> dateSat 6 Feb 2021, 11:57 (6 years ago) parentdf9dac59a283a0
2 files changed+57−48
nixos/modules/virtualisation/nixos-containers.nix+36−19 nixos/tests/containers-custom-pkgs.nix+21−29
nixos/modules/virtualisation/nixos-containers.nix+36−19
@@ -463,21 +463,15 @@ in
463463 { config, options, name, ... }:
464464 {
465465 options = {
466−
467466 config = mkOption {
468467 description = ''
469468 A specification of the desired configuration of this
470469 container, as a NixOS module.
471470 '';
472− type = let
473− confPkgs = if config.pkgs == null then pkgs else config.pkgs;
474− in lib.mkOptionType {
471+ type = lib.mkOptionType {
475472 name = "Toplevel NixOS config";
476− merge = loc: defs: (import (confPkgs.path + "/nixos/lib/eval-config.nix") {
473+ merge = loc: defs: (import "${toString config.nixpkgs}/nixos/lib/eval-config.nix" {
477474 inherit system;
478− pkgs = confPkgs;
479− baseModules = import (confPkgs.path + "/nixos/modules/module-list.nix");
480− inherit (confPkgs) lib;
481475 modules =
482476 let
483477 extraConfig = {
@@ -526,12 +520,18 @@ in
526520 '';
527521 };
528522
529− pkgs = mkOption {
530− type = types.nullOr types.attrs;
531− default = null;
532− example = literalExample "pkgs";
523+ nixpkgs = mkOption {
524+ type = types.path;
525+ default = pkgs.path;
526+ defaultText = "pkgs.path";
533527 description = ''
534− Customise which nixpkgs to use for this container.
528+ A path to the nixpkgs that provide the modules, pkgs and lib for evaluating the container.
529+
530+ To only change the <literal>pkgs</literal> argument used inside the container modules,
531+ set the <literal>nixpkgs.*</literal> options in the container <option>config</option>.
532+ Setting <literal>config.nixpkgs.pkgs = pkgs</literal> speeds up the container evaluation
533+ by reusing the system pkgs, but the <literal>nixpkgs.config</literal> option in the
534+ container config is ignored in this case.
535535 '';
536536 };
537537
@@ -672,14 +672,31 @@ in
672672 '';
673673 };
674674
675+ # Removed option. See `checkAssertion` below for the accompanying error message.
676+ pkgs = mkOption { visible = false; };
675677 } // networkOptions;
676678
677− config = mkMerge
678− [
679− (mkIf options.config.isDefined {
680− path = config.config.system.build.toplevel;
681− })
682− ];
679+ config = let
680+ # Throw an error when removed option `pkgs` is used.
681+ # Because this is a submodule we cannot use `mkRemovedOptionModule` or option `assertions`.
682+ optionPath = "containers.${name}.pkgs";
683+ files = showFiles options.pkgs.files;
684+ checkAssertion = if options.pkgs.isDefined then throw ''
685+ The option definition `${optionPath}' in ${files} no longer has any effect; please remove it.
686+
687+ Alternatively, you can use the following options:
688+ - containers.${name}.nixpkgs
689+ This sets the nixpkgs (and thereby the modules, pkgs and lib) that
690+ are used for evaluating the container.
691+
692+ - containers.${name}.config.nixpkgs.pkgs
693+ This only sets the `pkgs` argument used inside the container modules.
694+ ''
695+ else null;
696+ in {
697+ path = builtins.seq checkAssertion
698+ mkIf options.config.isDefined config.config.system.build.toplevel;
699+ };
683700 }));
684701
685702 default = {};
nixos/tests/containers-custom-pkgs.nix+21−29
@@ -1,42 +1,34 @@
1−# Test for NixOS' container support.
2−
31import ./make-test-python.nix ({ pkgs, lib, ...} : let
42
5− customPkgs = pkgs // {
6− hello = pkgs.hello.overrideAttrs(old: {
7− name = "custom-hello";
3+ customPkgs = pkgs.appendOverlays [ (self: super: {
4+ hello = super.hello.overrideAttrs (old: {
5+ name = "custom-hello";
86 });
9− };
7+ }) ];
108
119in {
12− name = "containers-hosts";
10+ name = "containers-custom-pkgs";
1311 meta = with lib.maintainers; {
14− maintainers = [ adisbladis ];
12+ maintainers = [ adisbladis earvstedt ];
1513 };
1614
17− machine =
18− { ... }:
19− {
20− virtualisation.memorySize = 256;
21− virtualisation.vlans = [];
15+ machine = { config, ... }: {
16+ assertions = let
17+ helloName = (builtins.head config.containers.test.config.system.extraDependencies).name;
18+ in [ {
19+ assertion = helloName == "custom-hello";
20+ message = "Unexpected value: ${helloName}";
21+ } ];
2222
23− containers.simple = {
24− autoStart = true;
25− pkgs = customPkgs;
26− config = {pkgs, config, ... }: {
27− environment.systemPackages = [
28− pkgs.hello
29− ];
30− };
23+ containers.test = {
24+ autoStart = true;
25+ config = { pkgs, config, ... }: {
26+ nixpkgs.pkgs = customPkgs;
27+ system.extraDependencies = [ pkgs.hello ];
3128 };
32−
3329 };
30+ };
3431
35− testScript = ''
36− start_all()
37− machine.wait_for_unit("default.target")
38− machine.succeed(
39− "test $(nixos-container run simple -- readlink -f /run/current-system/sw/bin/hello) = ${customPkgs.hello}/bin/hello"
40− )
41− '';
32+ # This test only consists of evaluating the test machine
33+ testScript = "";
4234})