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'
  • Loading branch information
edolstra committed Jan 9, 2025
1 parent 2d9b213 commit fccfdbe
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/nix/upgrade-nix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace nix;

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

CmdUpgradeNix()
{
Expand Down Expand Up @@ -64,7 +64,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 @@ -93,7 +93,9 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand

{
Activity act(*logger, lvlInfo, actUnknown,
fmt("installing '%s' into profile '%s'...", store->printStorePath(storePath), profileDir));
fmt("installing '%s' into profile %s...", store->printStorePath(storePath), profileDir));

// FIXME: don't call an external process.
runProgram(getNixBin("nix-env").string(), false,
{"--profile", profileDir, "-i", store->printStorePath(storePath), "--no-sandbox"});
}
Expand All @@ -102,31 +104,33 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
}

/* Return the profile in which Nix is installed. */
Path getProfileDir(ref<Store> store)
std::filesystem::path getProfileDir(ref<Store> store)
{
auto whereOpt = ExecutablePath::load().findName(OS_STR("nix-env"));
if (!whereOpt)
throw Error("couldn't figure out how Nix is installed, so I can't upgrade it");
const auto & where = whereOpt->parent_path();

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

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

Path profileDir = where.parent_path().string();
auto profileDir = where.parent_path();

// 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.string(), true);

Path userEnv = canonPath(profileDir, true);
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 (where.filename() != "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.nix"))
throw Error("directory %s does not appear to be part of a Nix profile", profileDir);

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

0 comments on commit fccfdbe

Please sign in to comment.