From 218ef2f4059f7b031f93cd4ac3fab9a3faf81b8b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 7 Jul 2023 16:41:40 +0200 Subject: [PATCH 1/5] nixosTests.installer: Make sure we boot into the config we generated --- nixos/tests/installer.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 4f4b91518845..c800299db115 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -21,6 +21,8 @@ let ]; + networking.hostName = "thatworked"; + documentation.enable = false; # To ensure that we can rebuild the grub configuration on the nixos-rebuild @@ -232,6 +234,11 @@ let machine = create_machine_named("boot-after-rebuild-switch") ${preBootCommands} machine.wait_for_unit("network.target") + + # Sanity check, is it the configuration.nix we generated? + hostname = machine.succeed("hostname").strip() + assert hostname == "thatworked" + ${postBootCommands} machine.shutdown() From faa1b3babcd338bf1a67486071b71e9514b86060 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 7 Jul 2023 17:32:03 +0200 Subject: [PATCH 2/5] nixosTests.installer: Fix driverInteractive --- nixos/tests/installer.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index c800299db115..7ba64602d76c 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -88,9 +88,14 @@ let qemu_flags = {"qemuFlags": assemble_qemu_flags()} + import os + + image_dir = machine.state_dir + disk_image = os.path.join(image_dir, "machine.qcow2") + hd_flags = { "hdaInterface": "${iface}", - "hda": "vm-state-machine/machine.qcow2", + "hda": disk_image, } ${optionalString isEfi '' hd_flags.update( From d00e242b805a5580e542dd5fb7c6b3e035be8697 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 7 Jul 2023 18:53:19 +0200 Subject: [PATCH 3/5] nixos: Add nixos.channel.enable For those who wish to get rid of nix-channel. --- nixos/modules/config/nix-channel.nix | 53 +++++++++++++---- nixos/tests/installer.nix | 86 ++++++++++++++++++++++++++-- nixos/tests/installer/flake.nix | 20 +++++++ 3 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 nixos/tests/installer/flake.nix diff --git a/nixos/modules/config/nix-channel.nix b/nixos/modules/config/nix-channel.nix index 557f17d8b3b7..f211b3dbe28a 100644 --- a/nixos/modules/config/nix-channel.nix +++ b/nixos/modules/config/nix-channel.nix @@ -21,13 +21,42 @@ in { options = { nix = { + channel = { + enable = mkOption { + description = lib.mdDoc '' + Whether the `nix-channel` command and state files are made available on the machine. + + The following files are initialized when enabled: + - `/nix/var/nix/profiles/per-user/root/channels` + - `/root/.nix-channels` + - `$HOME/.nix-defexpr/channels` (on login) + + Disabling this option will not remove the state files from the system. + ''; + type = types.bool; + default = true; + }; + }; + nixPath = mkOption { type = types.listOf types.str; - default = [ - "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" - "nixos-config=/etc/nixos/configuration.nix" - "/nix/var/nix/profiles/per-user/root/channels" - ]; + default = + if cfg.channel.enable + then [ + "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" + "nixos-config=/etc/nixos/configuration.nix" + "/nix/var/nix/profiles/per-user/root/channels" + ] + else []; + defaultText = '' + if nix.channel.enable + then [ + "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos" + "nixos-config=/etc/nixos/configuration.nix" + "/nix/var/nix/profiles/per-user/root/channels" + ] + else []; + ''; description = lib.mdDoc '' The default Nix expression search path, used by the Nix evaluator to look up paths enclosed in angle brackets @@ -49,22 +78,26 @@ in config = mkIf cfg.enable { environment.extraInit = - '' + mkIf cfg.channel.enable '' if [ -e "$HOME/.nix-defexpr/channels" ]; then export NIX_PATH="$HOME/.nix-defexpr/channels''${NIX_PATH:+:$NIX_PATH}" fi ''; - environment.sessionVariables = { + environment.extraSetup = mkIf (! cfg.channel.enable) '' + rm $out/bin/nix-channel + ''; + + environment.sessionVariables = mkIf (cfg.nixPath != []) { NIX_PATH = cfg.nixPath; }; - system.activationScripts.nix-channel = stringAfter [ "etc" "users" ] - '' + system.activationScripts.nix-channel = mkIf cfg.channel.enable + (stringAfter [ "etc" "users" ] '' # Subscribe the root user to the NixOS channel by default. if [ ! -e "/root/.nix-channels" ]; then echo "${config.system.defaultChannel} nixos" > "/root/.nix-channels" fi - ''; + ''); }; } diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 7ba64602d76c..38a9da13bcd9 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -11,14 +11,16 @@ let # The configuration to install. makeConfig = { bootLoader, grubDevice, grubIdentifier, grubUseEfi - , extraConfig, forceGrubReinstallCount ? 0 + , extraConfig, forceGrubReinstallCount ? 0, flake ? false }: pkgs.writeText "configuration.nix" '' { config, lib, pkgs, modulesPath, ... }: { imports = [ ./hardware-configuration.nix - + ${if flake + then "" # Still included, but via installer/flake.nix + else ""} ]; networking.hostName = "thatworked"; @@ -69,7 +71,7 @@ let # partitions and filesystems. testScriptFun = { bootLoader, createPartitions, grubDevice, grubUseEfi , grubIdentifier, preBootCommands, postBootCommands, extraConfig - , testSpecialisationConfig + , testSpecialisationConfig, testFlakeSwitch }: let iface = "virtio"; isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi); @@ -284,6 +286,75 @@ let ${postBootCommands} machine.shutdown() + '' + + optionalString testFlakeSwitch '' + ${preBootCommands} + machine.start() + + with subtest("Configure system with flake"): + # TODO: evaluate as user? + machine.succeed(""" + mkdir /root/my-config + mv /etc/nixos/hardware-configuration.nix /root/my-config/ + mv /etc/nixos/secret /root/my-config/ + rm /etc/nixos/configuration.nix + """) + machine.copy_from_host_via_shell( + "${ makeConfig { + inherit bootLoader grubDevice grubIdentifier + grubUseEfi extraConfig; + forceGrubReinstallCount = 1; + flake = true; + } + }", + "/root/my-config/configuration.nix", + ) + machine.copy_from_host_via_shell( + "${./installer/flake.nix}", + "/root/my-config/flake.nix", + ) + machine.succeed(""" + # for some reason the image does not have `pkgs.path`, so + # we use readlink to find a Nixpkgs source. + pkgs=$(readlink -f /nix/var/nix/profiles/per-user/root/channels)/nixos + if ! [[ -e $pkgs/pkgs/top-level/default.nix ]]; then + echo 1>&2 "$pkgs does not seem to be a nixpkgs source. Please fix the test so that pkgs points to a nixpkgs source."; + exit 1; + fi + sed -e s^@nixpkgs@^$pkgs^ -i /root/my-config/flake.nix + """) + + with subtest("Switch to flake based config"): + machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") + + ${postBootCommands} + machine.shutdown() + + ${preBootCommands} + machine.start() + + machine.wait_for_unit("multi-user.target") + + with subtest("nix-channel command is not available anymore"): + machine.succeed("! which nix-channel") + + with subtest("Evaluate flake config in fresh env without nix-channel"): + machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") + + with subtest("Evaluate flake config in fresh env without channel profiles"): + machine.succeed(""" + ( + exec 1>&2 + rm -v /root/.nix-channels + rm -vrf ~/.nix-defexpr + rm -vrf /nix/var/nix/profiles/per-user/root/channels* + ) + """) + machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") + + ${postBootCommands} + machine.shutdown() + ''; @@ -294,6 +365,7 @@ let , grubDevice ? "/dev/vda", grubIdentifier ? "uuid", grubUseEfi ? false , enableOCR ? false, meta ? {} , testSpecialisationConfig ? false + , testFlakeSwitch ? false }: makeTest { inherit enableOCR; @@ -399,7 +471,7 @@ let testScript = testScriptFun { inherit bootLoader createPartitions preBootCommands postBootCommands grubDevice grubIdentifier grubUseEfi extraConfig - testSpecialisationConfig; + testSpecialisationConfig testFlakeSwitch; }; }; @@ -451,6 +523,10 @@ let ''; }; + simple-test-config-flake = simple-test-config // { + testFlakeSwitch = true; + }; + simple-uefi-grub-config = { createPartitions = '' machine.succeed( @@ -505,6 +581,8 @@ in { # one big filesystem partition. simple = makeInstallerTest "simple" simple-test-config; + switchToFlake = makeInstallerTest "switch-to-flake" simple-test-config-flake; + # Test cloned configurations with the simple grub configuration simpleSpecialised = makeInstallerTest "simpleSpecialised" (simple-test-config // specialisation-test-extraconfig); diff --git a/nixos/tests/installer/flake.nix b/nixos/tests/installer/flake.nix new file mode 100644 index 000000000000..4bbef44e34fc --- /dev/null +++ b/nixos/tests/installer/flake.nix @@ -0,0 +1,20 @@ +# This file gets copied into the installation + +{ + # To keep things simple, we'll use an absolute path dependency here. + inputs.nixpkgs.url = "@nixpkgs@"; + + outputs = { nixpkgs, ... }: { + + nixosConfigurations.xyz = nixpkgs.lib.nixosSystem { + modules = [ + ./configuration.nix + ( nixpkgs + "/nixos/modules/testing/test-instrumentation.nix" ) + { + # We don't need nix-channel anymore + nix.channel.enable = false; + } + ]; + }; + }; +} From 61afc4d1662fe426f02b28e386f2e053f887b6d6 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 7 Jul 2023 23:12:39 +0200 Subject: [PATCH 4/5] nixos/nix-channel: Take care of NIX_PATH's non-empty default when disabled --- nixos/modules/config/nix-channel.nix | 7 ++++++- nixos/tests/installer.nix | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/nixos/modules/config/nix-channel.nix b/nixos/modules/config/nix-channel.nix index f211b3dbe28a..f90da62ec834 100644 --- a/nixos/modules/config/nix-channel.nix +++ b/nixos/modules/config/nix-channel.nix @@ -9,6 +9,7 @@ { config, lib, ... }: let inherit (lib) + mkDefault mkIf mkOption stringAfter @@ -88,10 +89,14 @@ in rm $out/bin/nix-channel ''; - environment.sessionVariables = mkIf (cfg.nixPath != []) { + # NIX_PATH has a non-empty default according to Nix docs, so we don't unset + # it when empty. + environment.sessionVariables = { NIX_PATH = cfg.nixPath; }; + nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault ""); + system.activationScripts.nix-channel = mkIf cfg.channel.enable (stringAfter [ "etc" "users" ] '' # Subscribe the root user to the NixOS channel by default. diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 38a9da13bcd9..5c6b70732fea 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -338,6 +338,18 @@ let with subtest("nix-channel command is not available anymore"): machine.succeed("! which nix-channel") + # Note that the channel profile is still present on disk, but configured + # not to be used. + with subtest("builtins.nixPath is now empty"): + machine.succeed(""" + [[ "[ ]" == "$(nix-instantiate builtins.nixPath --eval --expr)" ]] + """) + + with subtest(" does not resolve"): + machine.succeed(""" + ! nix-instantiate '' --eval --expr + """) + with subtest("Evaluate flake config in fresh env without nix-channel"): machine.succeed("nixos-rebuild switch --flake /root/my-config#xyz") From a1d0ee8c505472625e1c48a73843447269878bf0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 8 Jul 2023 20:49:37 +0200 Subject: [PATCH 5/5] nixos/nix-channel: Apply suggestions from code review Co-authored-by: Sandro --- nixos/modules/config/nix-channel.nix | 2 +- nixos/tests/installer.nix | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/nixos/modules/config/nix-channel.nix b/nixos/modules/config/nix-channel.nix index f90da62ec834..8f6caaabde86 100644 --- a/nixos/modules/config/nix-channel.nix +++ b/nixos/modules/config/nix-channel.nix @@ -85,7 +85,7 @@ in fi ''; - environment.extraSetup = mkIf (! cfg.channel.enable) '' + environment.extraSetup = mkIf (!cfg.channel.enable) '' rm $out/bin/nix-channel ''; diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 5c6b70732fea..4941eb9a6eba 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -300,13 +300,11 @@ let rm /etc/nixos/configuration.nix """) machine.copy_from_host_via_shell( - "${ makeConfig { - inherit bootLoader grubDevice grubIdentifier - grubUseEfi extraConfig; - forceGrubReinstallCount = 1; - flake = true; - } - }", + "${makeConfig { + inherit bootLoader grubDevice grubIdentifier grubUseEfi extraConfig; + forceGrubReinstallCount = 1; + flake = true; + }}", "/root/my-config/configuration.nix", ) machine.copy_from_host_via_shell( @@ -366,7 +364,6 @@ let ${postBootCommands} machine.shutdown() - '';