aboutsummaryrefslogtreecommitdiff
path: root/tests/nixos/nix-upgrade-nix.nix
diff options
context:
space:
mode:
authorQyriad <qyriad@qyriad.me>2024-04-28 17:23:31 -0600
committerQyriad <qyriad@qyriad.me>2024-04-29 01:19:21 +0000
commite2ab89a74b1d6044cea91e91f5c3d5fce203c2e8 (patch)
treea3e656b89afa103708897c071c842607616c01f1 /tests/nixos/nix-upgrade-nix.nix
parentee5a1b5a4c53918c029316fb9c961bee58e7d518 (diff)
add VM test for nix upgrade-nix
This commit adds a new NixOS VM test, which tests that `nix upgrade-nix` works on both kinds of profiles (manifest.nix and manifest.json). Done as a separate commit from 831d18a13, since it relies on the --store-path argument from 026c90e5f as well. Change-Id: I5fc94b751d252862cb6cffb541a4c072faad9f3b
Diffstat (limited to 'tests/nixos/nix-upgrade-nix.nix')
-rw-r--r--tests/nixos/nix-upgrade-nix.nix80
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/nixos/nix-upgrade-nix.nix b/tests/nixos/nix-upgrade-nix.nix
new file mode 100644
index 000000000..039b2d9b3
--- /dev/null
+++ b/tests/nixos/nix-upgrade-nix.nix
@@ -0,0 +1,80 @@
+{ lib, config, ... }:
+
+/**
+ * Test that nix upgrade-nix works regardless of whether /nix/var/nix/profiles/default
+ * is a nix-env style profile or a nix profile style profile.
+ */
+
+let
+ pkgs = config.nodes.machine.nixpkgs.pkgs;
+
+ lix = pkgs.nix;
+ lixVersion = lib.getVersion lix;
+
+ newNix = pkgs.nixVersions.unstable;
+ newNixVersion = lib.getVersion newNix;
+
+in {
+ name = "nix-upgrade-nix";
+
+ nodes = {
+ machine = { config, lib, pkgs, ... }: {
+ virtualisation.writableStore = true;
+ virtualisation.additionalPaths = [ pkgs.hello.drvPath ];
+ nix.settings.substituters = lib.mkForce [ ];
+ nix.settings.experimental-features = [ "nix-command" "flakes" ];
+ services.getty.autologinUser = "root";
+
+ };
+ };
+
+ testScript = { nodes }: ''
+ # fmt: off
+
+ start_all()
+
+ machine.succeed("nix --version >&2")
+
+ # Install Lix into the default profile, overriding /run/current-system/sw/bin/nix,
+ # and thus making Lix think we're not on NixOS.
+ machine.succeed("nix-env --install '${lib.getBin lix}' --profile /nix/var/nix/profiles/default >&2")
+
+ # Make sure that correctly got inserted into our PATH.
+ default_profile_nix_path = machine.succeed("command -v nix")
+ print(default_profile_nix_path)
+ assert default_profile_nix_path.strip() == "/nix/var/nix/profiles/default/bin/nix", \
+ f"{default_profile_nix_path.strip()=} != /nix/var/nix/profiles/default/bin/nix"
+
+ # And that it's the Nix we specified.
+ default_profile_version = machine.succeed("nix --version")
+ assert "${lixVersion}" in default_profile_version, f"${lixVersion} not in {default_profile_version}"
+
+ # Upgrade to a different version of Nix, and make sure that also worked.
+
+ machine.succeed("nix upgrade-nix --store-path ${newNix} >&2")
+ default_profile_version = machine.succeed("nix --version")
+ print(default_profile_version)
+ assert "${newNixVersion}" in default_profile_version, f"${newNixVersion} not in {default_profile_version}"
+
+ # Now 'break' this profile -- use nix profile on it so nix-env will no longer work on it.
+ machine.succeed(
+ "nix profile install --profile /nix/var/nix/profiles/default '${pkgs.hello.drvPath}^*' >&2"
+ )
+
+ # Confirm that nix-env is broken.
+ machine.fail(
+ "nix-env --query --installed --profile /nix/var/nix/profiles/default >&2"
+ )
+
+ # And use nix upgrade-nix one more time, on the `nix profile` style profile.
+ # (Specifying Lix by full path so we can use --store-path.)
+ machine.succeed(
+ "${lib.getBin lix}/bin/nix upgrade-nix --store-path '${lix}' >&2"
+ )
+
+ default_profile_version = machine.succeed("nix --version")
+ print(default_profile_version)
+ assert "${lixVersion}" in default_profile_version, f"${lixVersion} not in {default_profile_version}"
+ '';
+
+}