diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 1269463d8ef5..ac333d2e9ea1 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -81,7 +81,7 @@ let # partitions and filesystems. testScriptFun = { bootLoader, createPartitions, grubDevice, grubUseEfi, grubIdentifier , postInstallCommands, postBootCommands, extraConfig - , testSpecialisationConfig, testFlakeSwitch, clevisTest, clevisFallbackTest + , testSpecialisationConfig, testFlakeSwitch, testByAttrSwitch, clevisTest, clevisFallbackTest , disableFileSystems }: let @@ -316,6 +316,119 @@ let target.shutdown() '' + + optionalString testByAttrSwitch '' + with subtest("Configure system with attribute set"): + target.succeed(""" + mkdir /root/my-config + mv /etc/nixos/hardware-configuration.nix /root/my-config/ + rm /etc/nixos/configuration.nix + """) + target.copy_from_host_via_shell( + "${makeConfig { + inherit bootLoader grubDevice grubIdentifier grubUseEfi extraConfig clevisTest; + forceGrubReinstallCount = 1; + withTestInstrumentation = false; + }}", + "/root/my-config/configuration.nix", + ) + target.copy_from_host_via_shell( + "${./installer/byAttrWithChannel.nix}", + "/root/my-config/default.nix", + ) + with subtest("Switch to attribute set based config with channels"): + target.succeed("nixos-rebuild switch --file /root/my-config/default.nix") + + target.shutdown() + + ${startTarget} + + target.succeed(""" + rm /root/my-config/default.nix + """) + target.copy_from_host_via_shell( + "${./installer/byAttrNoChannel.nix}", + "/root/my-config/default.nix", + ) + + target.succeed(""" + 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/default.nix + + """) + + with subtest("Switch to attribute set based config without channels"): + target.succeed("nixos-rebuild switch --file /root/my-config/default.nix") + + target.shutdown() + + ${startTarget} + + with subtest("nix-channel command is not available anymore"): + target.succeed("! which nix-channel") + + with subtest("builtins.nixPath is now empty"): + target.succeed(""" + [[ "[ ]" == "$(nix-instantiate builtins.nixPath --eval --expr)" ]] + """) + + with subtest(" does not resolve"): + target.succeed(""" + ! nix-instantiate '' --eval --expr + """) + + with subtest("Evaluate attribute set based config in fresh env without nix-channel"): + target.succeed("nixos-rebuild switch --file /root/my-config/default.nix") + + with subtest("Evaluate attribute set based config in fresh env without channel profiles"): + target.succeed(""" + ( + exec 1>&2 + mkdir -p /root/restore + mv -v /root/.nix-channels /root/restore/ + mv -v ~/.nix-defexpr /root/restore/ + mkdir -p /root/restore/channels + mv -v /nix/var/nix/profiles/per-user/root/channels* /root/restore/channels/ + ) + """) + target.succeed("nixos-rebuild switch --file /root/my-config/default.nix") + '' + + optionalString (testByAttrSwitch && testFlakeSwitch) '' + with subtest("Restore channel profiles"): + target.succeed(""" + ( + exec 1>&2 + mv -v /root/restore/.nix-channels /root/ + mv -v /root/restore/.nix-defexpr ~/.nix-defexpr + mv -v /root/restore/channels/* /nix/var/nix/profiles/per-user/root/ + rm -vrf /root/restore + ) + """) + + with subtest("Restore /etc/nixos"): + target.succeed(""" + mv -v /root/my-config/hardware-configuration.nix /etc/nixos/ + """) + target.copy_from_host_via_shell( + "${makeConfig { + inherit bootLoader grubDevice grubIdentifier grubUseEfi extraConfig clevisTest; + forceGrubReinstallCount = 1; + }}", + "/etc/nixos/configuration.nix", + ) + + with subtest("Restore /root/my-config"): + target.succeed(""" + rm -vrf /root/my-config + """) + + '' + + optionalString (testByAttrSwitch && !testFlakeSwitch) '' + target.shutdown() + '' + optionalString testFlakeSwitch '' ${startTarget} @@ -399,6 +512,7 @@ let , enableOCR ? false, meta ? {} , testSpecialisationConfig ? false , testFlakeSwitch ? false + , testByAttrSwitch ? false , clevisTest ? false , clevisFallbackTest ? false , disableFileSystems ? false @@ -533,7 +647,7 @@ let testScript = testScriptFun { inherit bootLoader createPartitions postInstallCommands postBootCommands grubDevice grubIdentifier grubUseEfi extraConfig - testSpecialisationConfig testFlakeSwitch clevisTest clevisFallbackTest + testSpecialisationConfig testFlakeSwitch testByAttrSwitch clevisTest clevisFallbackTest disableFileSystems; }; }; @@ -589,6 +703,15 @@ let testFlakeSwitch = true; }; + simple-test-config-by-attr = simple-test-config // { + testByAttrSwitch = true; + }; + + simple-test-config-from-by-attr-to-flake = simple-test-config // { + testByAttrSwitch = true; + testFlakeSwitch = true; + }; + simple-uefi-grub-config = { createPartitions = '' installer.succeed( @@ -772,6 +895,10 @@ in { switchToFlake = makeInstallerTest "switch-to-flake" simple-test-config-flake; + switchToByAttr = makeInstallerTest "switch-to-by-attr" simple-test-config-by-attr; + + switchFromByAttrToFlake = makeInstallerTest "switch-from-by-attr-to-flake" simple-test-config-from-by-attr-to-flake; + # Test cloned configurations with the simple grub configuration simpleSpecialised = makeInstallerTest "simpleSpecialised" (simple-test-config // specialisation-test-extraconfig); diff --git a/nixos/tests/installer/byAttrNoChannel.nix b/nixos/tests/installer/byAttrNoChannel.nix new file mode 100644 index 000000000000..03293cd4a0e3 --- /dev/null +++ b/nixos/tests/installer/byAttrNoChannel.nix @@ -0,0 +1,18 @@ +# This file gets copied into the installation + +let + nixpkgs = "@nixpkgs@"; +in + +{ evalConfig ? import "${nixpkgs}/nixos/lib/eval-config.nix" }: + +evalConfig { + modules = [ + ./configuration.nix + ( import "${nixpkgs}/nixos/modules/testing/test-instrumentation.nix" ) + { + # Disable nix channels + nix.channel.enable = false; + } + ]; +} diff --git a/nixos/tests/installer/byAttrWithChannel.nix b/nixos/tests/installer/byAttrWithChannel.nix new file mode 100644 index 000000000000..951231dcba3e --- /dev/null +++ b/nixos/tests/installer/byAttrWithChannel.nix @@ -0,0 +1,10 @@ +# This file gets copied into the installation + +{ evalConfig ? import }: + +evalConfig { + modules = [ + ./configuration.nix + ( import ) + ]; +}