From c4dffb6513d88afac521dd2b009b3adbca3e215f Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Sun, 18 Aug 2024 00:31:38 -0400 Subject: [PATCH] nixosTest.nix-upgrade: init Test out both nix upgrade-nix and a NixOS upgrade. Inject a fake fallback-paths.nix assuming a stable -> latest upgrade. The NixOS upgrade does not use nixos-rebuild switch due to the cost+annoyance of the instantiation needing system.includeBuildDependencies. --- nixos/tests/all-tests.nix | 1 + nixos/tests/nix/upgrade.nix | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 nixos/tests/nix/upgrade.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 1fade3d88a7b..99769423d940 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -672,6 +672,7 @@ in { nix-config = handleTest ./nix-config.nix {}; nix-ld = handleTest ./nix-ld.nix {}; nix-misc = handleTest ./nix/misc.nix {}; + nix-upgrade = handleTest ./nix/upgrade.nix {inherit (pkgs) nixVersions;}; nix-required-mounts = runTest ./nix-required-mounts; nix-serve = handleTest ./nix-serve.nix {}; nix-serve-ssh = handleTest ./nix-serve-ssh.nix {}; diff --git a/nixos/tests/nix/upgrade.nix b/nixos/tests/nix/upgrade.nix new file mode 100644 index 000000000000..b0d46d03b1fb --- /dev/null +++ b/nixos/tests/nix/upgrade.nix @@ -0,0 +1,104 @@ +{ pkgs, nixVersions, ... }: +let + lib = pkgs.lib; + + fallback-paths-external = pkgs.writeTextDir "fallback-paths.nix" '' + { + ${pkgs.system} = "${nixVersions.latest}"; + }''; + + inputDrv = import ../.. { + configuration = + { + imports = [ nixos-module ]; + nix.package = nixVersions.latest; + boot.isContainer = true; + }; + system = pkgs.system; + }; + + nixos-module = builtins.toFile "nixos-module.nix" '' + { lib, pkgs, modulesPath, ... }: + { + imports = [ + (modulesPath + "/profiles/minimal.nix") + (modulesPath + "/testing/test-instrumentation.nix") + ]; + + hardware.enableAllFirmware = lib.mkForce false; + + nix.settings.substituters = lib.mkForce []; + nix.settings.hashed-mirrors = null; + nix.settings.connect-timeout = 1; + nix.extraOptions = "experimental-features = nix-command"; + + environment.localBinInPath = true; + users.users.alice = { + isNormalUser = true; + packages = [ pkgs.nixVersions.latest ]; + }; + documentation.enable = false; + } + ''; +in + +pkgs.testers.nixosTest { + name = "nix-upgrade-${nixVersions.stable.version}-${nixVersions.latest.version}"; + meta.maintainers = with lib.maintainers; [ tomberek ]; + + nodes.machine = { + imports = [ nixos-module ]; + + nix.package = nixVersions.stable; + system.extraDependencies = [ + fallback-paths-external + inputDrv.system + ]; + }; + + testScript = '' + machine.start() + machine.wait_for_unit("multi-user.target") + + with subtest("nix-current"): + # Create a profile to pretend we are on non-NixOS + + print(machine.succeed("nix --version")) + print(machine.succeed("nix-env -i /run/current-system/sw/bin/nix -p /root/.local")) + + with subtest("nix-upgrade"): + print(machine.succeed("nix upgrade-nix --nix-store-paths-url file://${fallback-paths-external}/fallback-paths.nix --profile /root/.local")) + result = machine.succeed("nix --version") + print(result) + + import re + match = re.match(r".*${nixVersions.latest.version}$",result) + if not match: raise Exception("Couldn't find new version in output: " + result) + + with subtest("nix-build-with-mismatch-daemon"): + machine.succeed("nix build --expr 'derivation {name =\"test\"; system = \"${pkgs.system}\";builder = \"/bin/sh\"; args = [\"-c\" \"echo test > $out\"];}' --print-out-paths") + + + with subtest("remove-new-nix"): + machine.succeed("rm -rf /root/.local") + + result = machine.succeed("nix --version") + print(result) + + import re + match = re.match(r".*${nixVersions.stable.version}$",result) + + with subtest("upgrade-via-switch-to-configuration"): + # not using nixos-rebuild due to nix-instantiate being called and forcing all drv's to be rebuilt + print(machine.succeed("${inputDrv.system.outPath}/bin/switch-to-configuration switch")) + result = machine.succeed("nix --version") + print(result) + + import re + match = re.match(r".*${nixVersions.latest.version}$",result) + if not match: raise Exception("Couldn't find new version in output: " + result) + + with subtest("nix-build-with-new-daemon"): + machine.succeed("nix build --expr 'derivation {name =\"test-new\"; system = \"${pkgs.system}\";builder = \"/bin/sh\"; args = [\"-c\" \"echo test > $out\"];}' --print-out-paths") + ''; +}