fix nix upgrade-nix on new-style profiles

nix3-profile automatically migrates any profile its used on to its style
of profile -- the ones with manifest.json instead of manifest.nix. On
non-NixOS systems, Nix is conventionally installed to the profile at
/nix/var/nix/profiles/default, so if a user passed that to `--profile`
of `nix profile`, then it would break upgrade-nix from ever working
again, without recreating the profile.

This commit fixes that, and allows upgrade-nix to work on either kind of
profile.

Fixes #16.

Change-Id: I4c49b1beba93bb50e8f8a107edc451affe08c3f7
This commit is contained in:
Qyriad 2024-04-27 17:28:56 -06:00
parent b49fd8324a
commit 9af156ecb4
2 changed files with 111 additions and 17 deletions

View file

@ -141,10 +141,7 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile)
}
elements.emplace_back(std::move(element));
}
}
else if (pathExists(profile + "/manifest.nix"))
{
} else if (pathExists(profile + "/manifest.nix")) {
// FIXME: needed because of pure mode; ugly.
state.allowPath(state.store->followLinksToStore(profile));
state.allowPath(state.store->followLinksToStore(profile + "/manifest.nix"));

View file

@ -1,5 +1,11 @@
#include <algorithm>
#include "cmd-profiles.hh"
#include "command.hh"
#include "common-args.hh"
#include "local-fs-store.hh"
#include "logging.hh"
#include "profiles.hh"
#include "store-api.hh"
#include "filetransfer.hh"
#include "eval.hh"
@ -10,7 +16,7 @@
using namespace nix;
struct CmdUpgradeNix : MixDryRun, StoreCommand
struct CmdUpgradeNix : MixDryRun, EvalCommand
{
Path profileDir;
std::string storePathsUrl = "https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix";
@ -59,12 +65,15 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
{
evalSettings.pureEval = true;
if (profileDir == "")
if (profileDir == "") {
profileDir = getProfileDir(store);
}
auto canonProfileDir = canonPath(profileDir, true);
printInfo("upgrading Nix in profile '%s'", profileDir);
auto storePath = getLatestNix(store);
StorePath storePath = getLatestNix(store);
auto version = DrvName(storePath.name()).version;
@ -89,11 +98,28 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
stopProgressBar();
{
Activity act(*logger, lvlInfo, actUnknown,
fmt("installing '%s' into profile '%s'...", store->printStorePath(storePath), profileDir));
runProgram(settings.nixBinDir + "/nix-env", false,
{"--profile", profileDir, "-i", store->printStorePath(storePath), "--no-sandbox"});
auto const fullStorePath = store->printStorePath(storePath);
if (canonProfileDir.ends_with("user-environment")) {
std::string nixEnvCmd = settings.nixBinDir + "/nix-env";
Strings upgradeArgs = {
"--profile",
this->profileDir,
"--install",
fullStorePath,
"--no-sandbox",
};
printTalkative("running %s %s", nixEnvCmd, concatStringsSep(" ", upgradeArgs));
runProgram(nixEnvCmd, false, upgradeArgs);
} else if (canonProfileDir.ends_with("profile")) {
this->upgradeNewStyleProfile(store, storePath);
} else {
// No I will not use std::unreachable.
// That is undefined behavior if you're wrong.
// This will have a half-decent error message and coredump.
assert("unreachable" == nullptr);
}
printInfo(ANSI_GREEN "upgrade to version %s done" ANSI_NORMAL, version);
@ -121,23 +147,94 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
Path profileDir = dirOf(where);
// Resolve profile to /nix/var/nix/profiles/<name> link.
while (canonPath(profileDir).find("/profiles/") == std::string::npos && isLink(profileDir))
while (canonPath(profileDir).find("/profiles/") == std::string::npos && isLink(profileDir)) {
profileDir = readLink(profileDir);
}
printInfo("found profile '%s'", profileDir);
Path userEnv = canonPath(profileDir, true);
if (baseNameOf(where) != "bin" ||
!userEnv.ends_with("user-environment"))
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
if (baseNameOf(where) != "bin") {
if (!userEnv.ends_with("user-environment") && !userEnv.ends_with("profile")) {
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
}
}
if (!store->isValidPath(store->parseStorePath(userEnv)))
if (!store->isValidPath(store->parseStorePath(userEnv))) {
throw Error("directory '%s' is not in the Nix store", userEnv);
}
return profileDir;
}
// TODO: Is there like, any good naming scheme that distinguishes
// "profiles which nix-env can use" and "profiles which nix profile can use"?
// You can't just say the manifest version since v2 and v3 are both the latter.
void upgradeNewStyleProfile(ref<Store> & store, StorePath const & newNix)
{
auto fsStore = store.dynamic_pointer_cast<LocalFSStore>();
// TODO(Qyriad): this check is here because we need to cast to a LocalFSStore,
// to pass to createGeneration(), ...but like, there's no way a remote store
// would work with the nix-env based upgrade either right?
if (!fsStore) {
throw Error("nix upgrade-nix cannot be used on a remote store");
}
// nb: nothing actually gets evaluated here.
// The ProfileManifest constructor only evaluates anything for manifest.nix
// profiles, which this is not.
auto evalState = this->getEvalState();
ProfileManifest manifest(*evalState, profileDir);
// Find which profile element has Nix in it.
// It should be impossible to *not* have Nix, since we grabbed this
// store path by looking for things with bin/nix-env in them anyway.
auto findNix = [&](ProfileElement const & elem) -> bool {
for (auto const & ePath : elem.storePaths) {
auto const nixEnv = store->printStorePath(ePath) + "/bin/nix-env";
if (pathExists(nixEnv)) {
return true;
}
}
// We checked each store path in this element. No nixes here boss!
return false;
};
auto elemWithNix = std::find_if(
manifest.elements.begin(),
manifest.elements.end(),
findNix
);
// *Should* be impossible...
assert(elemWithNix != std::end(manifest.elements));
// Now create a new profile element for the new Nix version...
ProfileElement elemForNewNix = {
.storePaths = {newNix},
};
// ...and splork it into the manifest where the old profile element was.
// (Remember, elemWithNix is an iterator)
*elemWithNix = elemForNewNix;
// Build the new profile, and switch to it.
StorePath const newProfile = manifest.build(store);
printTalkative("built new profile '%s'", store->printStorePath(newProfile));
auto const newGeneration = createGeneration(*fsStore, this->profileDir, newProfile);
printTalkative(
"switching '%s' to newly created generation '%s'",
this->profileDir,
newGeneration
);
// TODO(Qyriad): use switchGeneration?
// switchLink's docstring seems to indicate that's preferred, but it's
// not used for any other `nix profile`-style profile code except for
// rollback, and it assumes you already have a generation number, which
// we don't.
switchLink(profileDir, newGeneration);
}
/* Return the store path of the latest stable Nix. */
StorePath getLatestNix(ref<Store> store)
{