Skip to content

Commit

Permalink
nix upgrade-nix: Give a better error message if the profile is using …
Browse files Browse the repository at this point in the history
…'nix profile'

(cherry picked from commit fccfdbe)

# Conflicts:
#	src/nix/upgrade-nix.cc
  • Loading branch information
edolstra authored and mergify[bot] committed Jan 11, 2025
1 parent e1e219a commit 75c252b
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/nix/upgrade-nix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace nix;

struct CmdUpgradeNix : MixDryRun, StoreCommand
{
Path profileDir;
std::filesystem::path profileDir;

CmdUpgradeNix()
{
Expand Down Expand Up @@ -62,7 +62,7 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
if (profileDir == "")
profileDir = getProfileDir(store);

printInfo("upgrading Nix in profile '%s'", profileDir);
printInfo("upgrading Nix in profile %s", profileDir);

auto storePath = getLatestNix(store);

Expand Down Expand Up @@ -91,16 +91,23 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand

{
Activity act(*logger, lvlInfo, actUnknown,
<<<<<<< HEAD
fmt("installing '%s' into profile '%s'...", store->printStorePath(storePath), profileDir));
runProgram(settings.nixBinDir + "/nix-env", false,
=======
fmt("installing '%s' into profile %s...", store->printStorePath(storePath), profileDir));

// FIXME: don't call an external process.
runProgram(getNixBin("nix-env").string(), false,
>>>>>>> fccfdbea5 (nix upgrade-nix: Give a better error message if the profile is using 'nix profile')
{"--profile", profileDir, "-i", store->printStorePath(storePath), "--no-sandbox"});
}

printInfo(ANSI_GREEN "upgrade to version %s done" ANSI_NORMAL, version);
}

/* Return the profile in which Nix is installed. */
Path getProfileDir(ref<Store> store)
std::filesystem::path getProfileDir(ref<Store> store)
{
Path where;

Expand All @@ -113,24 +120,36 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
if (where == "")
throw Error("couldn't figure out how Nix is installed, so I can't upgrade it");

printInfo("found Nix in '%s'", where);
printInfo("found Nix in %s", where);

if (hasPrefix(where, "/run/current-system"))
throw Error("Nix on NixOS must be upgraded via 'nixos-rebuild'");

<<<<<<< HEAD
Path profileDir = dirOf(where);
=======
auto profileDir = where.parent_path();
>>>>>>> fccfdbea5 (nix upgrade-nix: Give a better error message if the profile is using 'nix profile')

// Resolve profile to /nix/var/nix/profiles/<name> link.
while (canonPath(profileDir).find("/profiles/") == std::string::npos && std::filesystem::is_symlink(profileDir))
while (canonPath(profileDir.string()).find("/profiles/") == std::string::npos && std::filesystem::is_symlink(profileDir))
profileDir = readLink(profileDir);

printInfo("found profile '%s'", profileDir);
printInfo("found profile %s", profileDir);

Path userEnv = canonPath(profileDir, true);
Path userEnv = canonPath(profileDir.string(), true);

<<<<<<< HEAD
if (baseNameOf(where) != "bin" ||
!hasSuffix(userEnv, "user-environment"))
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
=======
if (std::filesystem::exists(profileDir / "manifest.json"))
throw Error("directory %s is managed by 'nix profile' and currently cannot be upgraded by 'nix upgrade-nix'", profileDir);

if (!std::filesystem::exists(profileDir / "manifest.nix"))
throw Error("directory %s does not appear to be part of a Nix profile", profileDir);
>>>>>>> fccfdbea5 (nix upgrade-nix: Give a better error message if the profile is using 'nix profile')

if (!store->isValidPath(store->parseStorePath(userEnv)))
throw Error("directory '%s' is not in the Nix store", userEnv);
Expand Down

0 comments on commit 75c252b

Please sign in to comment.