Change-Id: I4c49b1beba93bb50e8f8a107edc451affe08c3f7
This commit is contained in:
Qyriad 2024-04-27 17:28:56 -06:00
parent 49886ce0dc
commit a514712224

View file

@ -1,5 +1,6 @@
#include "command.hh"
#include "common-args.hh"
#include "logging.hh"
#include "store-api.hh"
#include "filetransfer.hh"
#include "eval.hh"
@ -15,6 +16,8 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
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 +28,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 +69,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,13 +102,41 @@ 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",
};
} else if (canonProfileDir.ends_with("profile")) {
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);
}
printTalkative("running %s %s", upgradeCmd, concatStringsSep(" ", upgradeArgs));
runProgram(upgradeCmd, false, upgradeArgs);
printInfo(ANSI_GREEN "upgrade to version %s done" ANSI_NORMAL, version);
}
@ -121,26 +162,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?