From 8fd49c116bcd256263c7aad8ca5d4b7fa10d4ca2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 12:41:12 +0100 Subject: [PATCH 1/9] nixos/default.nix: Use extendModules --- nixos/default.nix | 35 ++++++++++++++++------------------- nixos/lib/eval-config.nix | 2 +- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/nixos/default.nix b/nixos/default.nix index c11872f1441..01605e1d577 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -10,25 +10,22 @@ let }; # This is for `nixos-rebuild build-vm'. - vmConfig = (import ./lib/eval-config.nix { - inherit system; - modules = [ configuration ./modules/virtualisation/qemu-vm.nix ]; - }).config; + vm = eval.extendModules { + modules = [ ./modules/virtualisation/qemu-vm.nix ]; + }; # This is for `nixos-rebuild build-vm-with-bootloader'. - vmWithBootLoaderConfig = (import ./lib/eval-config.nix { - inherit system; - modules = - [ configuration - ./modules/virtualisation/qemu-vm.nix - { virtualisation.useBootLoader = true; } - ({ config, ... }: { - virtualisation.useEFIBoot = - config.boot.loader.systemd-boot.enable || - config.boot.loader.efi.canTouchEfiVariables; - }) - ]; - }).config; + vmWithBootLoader = vm.extendModules { + modules = [ + ({ config, ... }: { + _file = "nixos/default.nix##vmWithBootLoader"; + virtualisation.useBootLoader = true; + virtualisation.useEFIBoot = + config.boot.loader.systemd-boot.enable || + config.boot.loader.efi.canTouchEfiVariables; + }) + ]; + }; in @@ -37,7 +34,7 @@ in system = eval.config.system.build.toplevel; - vm = vmConfig.system.build.vm; + vm = vm.config.system.build.vm; - vmWithBootLoader = vmWithBootLoaderConfig.system.build.vm; + vmWithBootLoader = vmWithBootLoader.config.system.build.vm; } diff --git a/nixos/lib/eval-config.nix b/nixos/lib/eval-config.nix index 62d09b8173b..89fb93ba70a 100644 --- a/nixos/lib/eval-config.nix +++ b/nixos/lib/eval-config.nix @@ -92,7 +92,7 @@ in withWarnings { # Merge the option definitions in all modules, forming the full # system configuration. - inherit (nixosWithUserModules) config options _module type; + inherit (nixosWithUserModules) config options _module type extendModules; inherit extraArgs; From f72432aeb2a3aa1d75bd56204571fb394fcc9abb Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 13:15:55 +0100 Subject: [PATCH 2/9] nixos: Move build-vm into virtualisation.vmVariant ... which is like a specialisation, but for nixos-rebuild build-vm --- nixos/default.nix | 22 +---------- nixos/modules/module-list.nix | 1 + nixos/modules/virtualisation/build-vm.nix | 46 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 nixos/modules/virtualisation/build-vm.nix diff --git a/nixos/default.nix b/nixos/default.nix index 01605e1d577..27a0fd73ecd 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -9,24 +9,6 @@ let modules = [ configuration ]; }; - # This is for `nixos-rebuild build-vm'. - vm = eval.extendModules { - modules = [ ./modules/virtualisation/qemu-vm.nix ]; - }; - - # This is for `nixos-rebuild build-vm-with-bootloader'. - vmWithBootLoader = vm.extendModules { - modules = [ - ({ config, ... }: { - _file = "nixos/default.nix##vmWithBootLoader"; - virtualisation.useBootLoader = true; - virtualisation.useEFIBoot = - config.boot.loader.systemd-boot.enable || - config.boot.loader.efi.canTouchEfiVariables; - }) - ]; - }; - in { @@ -34,7 +16,7 @@ in system = eval.config.system.build.toplevel; - vm = vm.config.system.build.vm; + vm = eval.config.virtualisation.vmVariant.system.build.vm; - vmWithBootLoader = vmWithBootLoader.config.system.build.vm; + vmWithBootLoader = eval.config.virtualisation.vmVariantWithBootLoader.system.build.vm; } diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index cb2dd530de1..5fa8220fa0e 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -1179,6 +1179,7 @@ ./tasks/powertop.nix ./testing/service-runner.nix ./virtualisation/anbox.nix + ./virtualisation/build-vm.nix ./virtualisation/container-config.nix ./virtualisation/containerd.nix ./virtualisation/containers.nix diff --git a/nixos/modules/virtualisation/build-vm.nix b/nixos/modules/virtualisation/build-vm.nix new file mode 100644 index 00000000000..d856b1b502b --- /dev/null +++ b/nixos/modules/virtualisation/build-vm.nix @@ -0,0 +1,46 @@ +{ extendModules, lib, ... }: +let + + inherit (lib) + mkOption + ; + + vmVariant = extendModules { + modules = [ ./qemu-vm.nix ]; + }; + + vmVariantWithBootLoader = vmVariant.extendModules { + modules = [ + ({ config, ... }: { + _file = "nixos/default.nix##vmWithBootLoader"; + virtualisation.useBootLoader = true; + virtualisation.useEFIBoot = + config.boot.loader.systemd-boot.enable || + config.boot.loader.efi.canTouchEfiVariables; + }) + ]; + }; +in +{ + options = { + + virtualisation.vmVariant = mkOption { + description = '' + Machine configuration to be added for the vm script produced by nixos-rebuild build-vm. + ''; + inherit (vmVariant) type; + default = {}; + visible = "shallow"; + }; + + virtualisation.vmVariantWithBootLoader = mkOption { + description = '' + Machine configuration to be added for the vm script produced by nixos-rebuild build-vm-with-bootloader. + ''; + inherit (vmVariantWithBootLoader) type; + default = {}; + visible = "shallow"; + }; + + }; +} From 537db62345147565ae592d2b6641a662e07a152a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:04:03 +0100 Subject: [PATCH 3/9] flake.nix: Deduplicate vmConfig, vmWithBootloaderConfig --- flake.nix | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/flake.nix b/flake.nix index 1e20fcd40eb..d1ec53f5931 100644 --- a/flake.nix +++ b/flake.nix @@ -22,24 +22,6 @@ import ./nixos/lib/eval-config.nix (args // { modules = let - vmConfig = (import ./nixos/lib/eval-config.nix - (args // { - modules = modules ++ [ ./nixos/modules/virtualisation/qemu-vm.nix ]; - })).config; - - vmWithBootLoaderConfig = (import ./nixos/lib/eval-config.nix - (args // { - modules = modules ++ [ - ./nixos/modules/virtualisation/qemu-vm.nix - { virtualisation.useBootLoader = true; } - ({ config, ... }: { - virtualisation.useEFIBoot = - config.boot.loader.systemd-boot.enable || - config.boot.loader.efi.canTouchEfiVariables; - }) - ]; - })).config; - moduleDeclarationFile = let # Even though `modules` is a mandatory argument for `nixosSystem`, it doesn't @@ -59,16 +41,16 @@ in map addModuleDeclarationFile modules ++ [ - { + ({ config, ... }: { system.nixos.versionSuffix = ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}"; system.nixos.revision = final.mkIf (self ? rev) self.rev; system.build = { - vm = vmConfig.system.build.vm; - vmWithBootLoader = vmWithBootLoaderConfig.system.build.vm; + vm = config.virtualisation.vmVariant.system.build.vm; + vmWithBootLoader = config.virtualisation.vmVariantWithBootLoader.system.build.vm; }; - } + }) ]; }); }); From 9fd9c617a9c84293b67b2a43ca752b30565f2b88 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:07:47 +0100 Subject: [PATCH 4/9] nixos/lib/eval-config.nix: Return all of evalModules return attrs We were exposing everything pointwise anyway. If any new attrs are added, there's a good chance we'll want to expose them anyway. --- nixos/lib/eval-config.nix | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/nixos/lib/eval-config.nix b/nixos/lib/eval-config.nix index 89fb93ba70a..00e58e24e92 100644 --- a/nixos/lib/eval-config.nix +++ b/nixos/lib/eval-config.nix @@ -88,13 +88,8 @@ let nixosWithUserModules = noUserModules.extendModules { modules = allUserModules; }; -in withWarnings { - - # Merge the option definitions in all modules, forming the full - # system configuration. - inherit (nixosWithUserModules) config options _module type extendModules; - +in +withWarnings nixosWithUserModules // { inherit extraArgs; - inherit (nixosWithUserModules._module.args) pkgs; } From a0ad8dcd354c67f084511e4ae78a27af83df95fd Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:12:47 +0100 Subject: [PATCH 5/9] flake.nix: lib.nixosSystem: Set system.build.vm* with lib.mkDefault This will help anyone who imports the qemu module themselves, to avoid a collision. --- flake.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index d1ec53f5931..3060ffdd649 100644 --- a/flake.nix +++ b/flake.nix @@ -47,8 +47,8 @@ system.nixos.revision = final.mkIf (self ? rev) self.rev; system.build = { - vm = config.virtualisation.vmVariant.system.build.vm; - vmWithBootLoader = config.virtualisation.vmVariantWithBootLoader.system.build.vm; + vm = lib.mkDefault config.virtualisation.vmVariant.system.build.vm; + vmWithBootLoader = lib.mkDefault config.virtualisation.vmVariantWithBootLoader.system.build.vm; }; }) ]; From 4014fb6a64bc5f68326fc08cbaa83475db1fae8e Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:32:55 +0100 Subject: [PATCH 6/9] nixos: Make system.build a lazyAttrsOf unspecified Legacy types.attrs has really bad merging behavior and does not support priorities. f build --- nixos/modules/system/activation/top-level.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/system/activation/top-level.nix b/nixos/modules/system/activation/top-level.nix index 501998fa399..42e6dd689f9 100644 --- a/nixos/modules/system/activation/top-level.nix +++ b/nixos/modules/system/activation/top-level.nix @@ -148,7 +148,7 @@ in system.build = mkOption { internal = true; default = {}; - type = types.attrs; + type = types.lazyAttrsOf types.unspecified; description = '' Attribute set of derivations used to setup the system. ''; From 6510ec5acdd465a016e5671ffa99460ef70e6c25 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:34:30 +0100 Subject: [PATCH 7/9] nixos: Make system.build.vm a standard attribute based on vmVariant --- flake.nix | 5 ----- nixos/default.nix | 4 +--- nixos/modules/virtualisation/build-vm.nix | 11 ++++++++++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/flake.nix b/flake.nix index 3060ffdd649..01d52ae3862 100644 --- a/flake.nix +++ b/flake.nix @@ -45,11 +45,6 @@ system.nixos.versionSuffix = ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}"; system.nixos.revision = final.mkIf (self ? rev) self.rev; - - system.build = { - vm = lib.mkDefault config.virtualisation.vmVariant.system.build.vm; - vmWithBootLoader = lib.mkDefault config.virtualisation.vmVariantWithBootLoader.system.build.vm; - }; }) ]; }); diff --git a/nixos/default.nix b/nixos/default.nix index 27a0fd73ecd..6beb4cd3a7d 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -16,7 +16,5 @@ in system = eval.config.system.build.toplevel; - vm = eval.config.virtualisation.vmVariant.system.build.vm; - - vmWithBootLoader = eval.config.virtualisation.vmVariantWithBootLoader.system.build.vm; + inherit (eval.config.system.build) vm vmWithBootLoader; } diff --git a/nixos/modules/virtualisation/build-vm.nix b/nixos/modules/virtualisation/build-vm.nix index d856b1b502b..3baa84cce01 100644 --- a/nixos/modules/virtualisation/build-vm.nix +++ b/nixos/modules/virtualisation/build-vm.nix @@ -1,4 +1,4 @@ -{ extendModules, lib, ... }: +{ config, extendModules, lib, ... }: let inherit (lib) @@ -43,4 +43,13 @@ in }; }; + + config = { + + system.build = { + vm = lib.mkDefault config.virtualisation.vmVariant.system.build.vm; + vmWithBootLoader = lib.mkDefault config.virtualisation.vmVariantWithBootLoader.system.build.vm; + }; + + }; } From 439d7d493dc74ecf3306091a5b17121ecedb444d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:39:58 +0100 Subject: [PATCH 8/9] nixos: Add release note about vmVariant --- .../from_md/release-notes/rl-2205.section.xml | 22 +++++++++++++++++++ .../manual/release-notes/rl-2205.section.md | 10 +++++++++ 2 files changed, 32 insertions(+) diff --git a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml index 2dd27649c52..e87fec9a2e5 100644 --- a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml +++ b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml @@ -132,6 +132,28 @@ socket /run/redis-${serverName}/redis.sock. + + + The option + virtualisation.vmVariant + was added to allow users to make changes to the + nixos-rebuild build-vm configuration that + do not apply to their normal system. + + + The config.system.build.vm attribute now + always exists and defaults to the value from + vmVariant. Configurations that import the + virtualisation/qemu-vm.nix module + themselves will override this value, such that + vmVariant is not used. + + + Similarly + virtualisation.vmVariantWithBootloader + was added. + + The diff --git a/nixos/doc/manual/release-notes/rl-2205.section.md b/nixos/doc/manual/release-notes/rl-2205.section.md index 595785e732a..9d13697a41f 100644 --- a/nixos/doc/manual/release-notes/rl-2205.section.md +++ b/nixos/doc/manual/release-notes/rl-2205.section.md @@ -57,4 +57,14 @@ In addition to numerous new and upgraded packages, this release has the followin to the members of the Unix group `redis-${serverName}` through the Unix socket `/run/redis-${serverName}/redis.sock`. +- The option [virtualisation.vmVariant](#opt-virtualisation.vmVariant) was added + to allow users to make changes to the `nixos-rebuild build-vm` configuration + that do not apply to their normal system. + + The `config.system.build.vm` attribute now always exists and defaults to the + value from `vmVariant`. Configurations that import the `virtualisation/qemu-vm.nix` + module themselves will override this value, such that `vmVariant` is not used. + + Similarly [virtualisation.vmVariantWithBootloader](#opt-virtualisation.vmVariantWithBootLoader) was added. + - The `writers.writePyPy2`/`writers.writePyPy3` and corresponding `writers.writePyPy2Bin`/`writers.writePyPy3Bin` convenience functions to create executable Python 2/3 scripts using the PyPy interpreter were added. From a2710255c988de1f8ab17edea20f13f8b6c70efe Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 17 Dec 2021 14:44:38 +0100 Subject: [PATCH 9/9] flake.nix: Remove redundant module lambda --- flake.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index 01d52ae3862..bb76fae486d 100644 --- a/flake.nix +++ b/flake.nix @@ -41,11 +41,11 @@ in map addModuleDeclarationFile modules ++ [ - ({ config, ... }: { + { system.nixos.versionSuffix = ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}"; system.nixos.revision = final.mkIf (self ? rev) self.rev; - }) + } ]; }); });