Change-Id: I4c49b1beba93bb50e8f8a107edc451affe08c3f7
This commit is contained in:
Qyriad 2024-04-27 17:28:56 -06:00
parent a00cb15b77
commit 05756b90cf
2 changed files with 178 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,11 +16,13 @@
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";
std::optional<Path> overrideStorePath;
CmdUpgradeNix()
{
addFlag({
@ -25,6 +33,13 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
.handler = {&profileDir}
});
addFlag({
.longName = "store-path",
.description = "A specific store path to upgrade Nix to",
.labels = {"store-path"},
.handler = {&overrideStorePath},
});
addFlag({
.longName = "nix-store-paths-url",
.description = "The URL of the file that contains the store paths of the latest Nix release.",
@ -59,12 +74,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 +107,94 @@ 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);
// Will be either nix-env or nix (for nix profile).
std::string upgradeCmd;
Strings upgradeArgs;
if (canonProfileDir.ends_with("user-environment")) {
upgradeCmd = settings.nixBinDir + "/nix-env";
upgradeArgs = {
"--profile",
this->profileDir,
"--install",
fullStorePath,
"--no-sandbox",
};
printTalkative("running %s %s", upgradeCmd, concatStringsSep(" ", upgradeArgs));
runProgram(upgradeCmd, false, upgradeArgs);
} else if (canonProfileDir.ends_with("profile")) {
auto localFsStore = store.dynamic_pointer_cast<LocalFSStore>();
// TODO(Qyriad): this check is here because we need to cast to a LocalFSStore,
// ...but like, there's no way a remote store would work with the nix-env
// based upgrade either right?
if (!localFsStore) {
throw Error("nix upgrade-nix cannot be used on a remote store");
}
auto evalState = this->getEvalState();
ProfileManifest manifest(*evalState, profileDir);
// Find which profile element has Nix in it.
// It *should* have Nix, since we grabbed this store path by looking
// for things with bin/nix-env anyway.
auto findNix = [&](ProfileElement const & elem) -> bool {
// Each profile element can itself contain multiple store paths.
// Check if any of the store paths in this profile match
// the store path we know our installed Nix to have.
printError("checking %s for %s", elem.identifier(), storePath.to_string());
for (auto const & profileElementStorePath : elem.storePaths) {
auto const elemPath = store->printStorePath(profileElementStorePath);
if (pathExists(elemPath + "/bin/nix-env")) {
return true;
}
}
return false;
};
auto elemWithNix = std::find_if(manifest.elements.begin(), manifest.elements.end(), findNix);
assert(elemWithNix != std::end(manifest.elements));
// Now create a new profile element for the new Nix version...
ProfileElement elemForNewNix = {
.storePaths = {storePath},
};
// ...and plork 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.
auto const newProfileStorePath = manifest.build(store);
printTalkative("built new profile '%s'", store->printStorePath(newProfileStorePath));
// nb: "profileDir" instead of "canonProfileDir" is load-bearing.
auto newGeneration = createGeneration(*localFsStore, profileDir, newProfileStorePath);
printTalkative("created generation '%s'", 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, and it doesn't
// do the quite the same thing.
switchLink(profileDir, newGeneration);
upgradeCmd = settings.nixBinDir + "/nix";
upgradeArgs = {
"profile",
"install",
"--profile",
this->profileDir,
fullStorePath,
"--no-sandbox",
};
} 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,26 +222,89 @@ 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;
}
void upgradeWithNixEnv(Store & store, StorePath const & newNix)
{
auto const fullStorePath = store.printStorePath(newNix);
// Starts the activity on construction and stops it on destruction.
Activity act(
*logger,
lvlInfo,
actUnknown,
fmt("installing '%s' into profile '%s'...", fullStorePath)
);
auto const nixEnv = settings.nixBinDir + "/nix-env";
Strings nixEnvArgs = {
"--profile",
this->profileDir,
"--install",
fullStorePath,
"--no-sandbox",
};
printTalkative("running %s %s", nixEnv, concatStringsSep(" ", nixEnvArgs));
runProgram(nixEnv, false, nixEnvArgs);
}
void upgradeWithNixProfile(Store & store, StorePath const & newNix)
{
auto const fullStorePath = store.printStorePath(newNix);
// Starts the activity on construction and stops it on destruction.
Activity act(
*logger,
lvlInfo,
actUnknown,
fmt("installing '%s' into profile '%s'...", fullStorePath, this->profileDir, this->profileDir)
);
auto const nixCmd = settings.nixBinDir + "/nix";
Strings nixCmdArgs = {
"profile",
"install",
"--profile",
this->profileDir,
fullStorePath,
"--no-sandbox",
};
printTalkative("running %s %s", nixCmd, concatStringsSep(" ", nixCmdArgs));
runProgram(nixCmd, false, nixCmdArgs);
}
/* Return the store path of the latest stable Nix. */
StorePath getLatestNix(ref<Store> store)
{
if (this->overrideStorePath) {
printTalkative(
"skipping Nix version query and using '%s' as latest Nix",
*this->overrideStorePath
);
return store->parseStorePath(*this->overrideStorePath);
}
Activity act(*logger, lvlInfo, actUnknown, "querying latest Nix version");
// FIXME: use nixos.org?