2023-10-25 07:43:36 +03:00
|
|
|
#include "processes.hh"
|
2018-01-31 17:14:47 +02:00
|
|
|
#include "command.hh"
|
2018-08-25 21:25:43 +03:00
|
|
|
#include "common-args.hh"
|
2018-01-31 17:14:47 +02:00
|
|
|
#include "store-api.hh"
|
2020-04-07 00:57:28 +03:00
|
|
|
#include "filetransfer.hh"
|
2018-01-31 17:14:47 +02:00
|
|
|
#include "eval.hh"
|
2023-07-31 16:19:19 +03:00
|
|
|
#include "eval-settings.hh"
|
2018-01-31 17:14:47 +02:00
|
|
|
#include "attr-path.hh"
|
2018-08-30 22:18:56 +03:00
|
|
|
#include "names.hh"
|
|
|
|
#include "progress-bar.hh"
|
2018-01-31 17:14:47 +02:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2018-08-25 21:25:43 +03:00
|
|
|
struct CmdUpgradeNix : MixDryRun, StoreCommand
|
2018-01-31 17:14:47 +02:00
|
|
|
{
|
|
|
|
Path profileDir;
|
|
|
|
|
|
|
|
CmdUpgradeNix()
|
|
|
|
{
|
2020-05-04 23:40:19 +03:00
|
|
|
addFlag({
|
|
|
|
.longName = "profile",
|
|
|
|
.shortName = 'p',
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = "The path to the Nix profile to upgrade.",
|
2020-05-04 23:40:19 +03:00
|
|
|
.labels = {"profile-dir"},
|
|
|
|
.handler = {&profileDir}
|
|
|
|
});
|
2018-08-30 18:10:28 +03:00
|
|
|
|
2020-05-04 23:40:19 +03:00
|
|
|
addFlag({
|
|
|
|
.longName = "nix-store-paths-url",
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = "The URL of the file that contains the store paths of the latest Nix release.",
|
2020-05-04 23:40:19 +03:00
|
|
|
.labels = {"url"},
|
2023-11-10 19:12:13 +02:00
|
|
|
.handler = {&(std::string&) settings.upgradeNixStorePathUrl}
|
2020-05-04 23:40:19 +03:00
|
|
|
});
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 06:01:18 +02:00
|
|
|
/**
|
|
|
|
* This command is stable before the others
|
|
|
|
*/
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature() override
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:14:47 +02:00
|
|
|
std::string description() override
|
|
|
|
{
|
2023-11-10 19:12:13 +02:00
|
|
|
return "upgrade Nix to the latest stable version";
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:11:48 +02:00
|
|
|
std::string doc() override
|
2018-01-31 17:14:47 +02:00
|
|
|
{
|
2020-12-09 22:11:48 +02:00
|
|
|
return
|
|
|
|
#include "upgrade-nix.md"
|
|
|
|
;
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
Category category() override { return catNixInstallation; }
|
|
|
|
|
2018-01-31 17:14:47 +02:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2018-03-27 20:02:22 +03:00
|
|
|
evalSettings.pureEval = true;
|
2018-01-31 17:14:47 +02:00
|
|
|
|
|
|
|
if (profileDir == "")
|
|
|
|
profileDir = getProfileDir(store);
|
|
|
|
|
|
|
|
printInfo("upgrading Nix in profile '%s'", profileDir);
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
auto storePath = getLatestNix(store);
|
2018-01-31 17:14:47 +02:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
auto version = DrvName(storePath.name()).version;
|
2018-08-30 22:18:56 +03:00
|
|
|
|
|
|
|
if (dryRun) {
|
|
|
|
stopProgressBar();
|
2021-01-21 01:27:36 +02:00
|
|
|
warn("would upgrade to version %s", version);
|
2018-08-30 22:18:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:14:47 +02:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
Activity act(*logger, lvlInfo, actUnknown, fmt("downloading '%s'...", store->printStorePath(storePath)));
|
2018-08-30 22:18:56 +03:00
|
|
|
store->ensurePath(storePath);
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
Activity act(*logger, lvlInfo, actUnknown, fmt("verifying that '%s' works...", store->printStorePath(storePath)));
|
|
|
|
auto program = store->printStorePath(storePath) + "/bin/nix-env";
|
2018-08-30 22:18:56 +03:00
|
|
|
auto s = runProgram(program, false, {"--version"});
|
|
|
|
if (s.find("Nix") == std::string::npos)
|
|
|
|
throw Error("could not verify that '%s' works", program);
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:56 +03:00
|
|
|
stopProgressBar();
|
|
|
|
|
2018-01-31 17:14:47 +02:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
Activity act(*logger, lvlInfo, actUnknown,
|
|
|
|
fmt("installing '%s' into profile '%s'...", store->printStorePath(storePath), profileDir));
|
2018-08-30 22:18:56 +03:00
|
|
|
runProgram(settings.nixBinDir + "/nix-env", false,
|
2019-12-05 20:11:09 +02:00
|
|
|
{"--profile", profileDir, "-i", store->printStorePath(storePath), "--no-sandbox"});
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
2018-08-30 22:18:56 +03:00
|
|
|
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo(ANSI_GREEN "upgrade to version %s done" ANSI_NORMAL, version);
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the profile in which Nix is installed. */
|
|
|
|
Path getProfileDir(ref<Store> store)
|
|
|
|
{
|
|
|
|
Path where;
|
|
|
|
|
2019-11-22 17:06:44 +02:00
|
|
|
for (auto & dir : tokenizeString<Strings>(getEnv("PATH").value_or(""), ":"))
|
2018-01-31 17:14:47 +02:00
|
|
|
if (pathExists(dir + "/nix-env")) {
|
|
|
|
where = dir;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (where == "")
|
|
|
|
throw Error("couldn't figure out how Nix is installed, so I can't upgrade it");
|
|
|
|
|
|
|
|
printInfo("found Nix in '%s'", where);
|
|
|
|
|
|
|
|
if (hasPrefix(where, "/run/current-system"))
|
|
|
|
throw Error("Nix on NixOS must be upgraded via 'nixos-rebuild'");
|
|
|
|
|
2018-08-25 21:33:01 +03:00
|
|
|
Path profileDir = dirOf(where);
|
|
|
|
|
|
|
|
// Resolve profile to /nix/var/nix/profiles/<name> link.
|
2018-08-30 22:18:56 +03:00
|
|
|
while (canonPath(profileDir).find("/profiles/") == std::string::npos && isLink(profileDir))
|
2018-08-25 21:33:01 +03:00
|
|
|
profileDir = readLink(profileDir);
|
|
|
|
|
|
|
|
printInfo("found profile '%s'", profileDir);
|
|
|
|
|
|
|
|
Path userEnv = canonPath(profileDir, true);
|
2018-01-31 17:14:47 +02:00
|
|
|
|
|
|
|
if (baseNameOf(where) != "bin" ||
|
2018-08-25 21:33:01 +03:00
|
|
|
!hasSuffix(userEnv, "user-environment"))
|
2018-01-31 17:14:47 +02:00
|
|
|
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
if (!store->isValidPath(store->parseStorePath(userEnv)))
|
2018-01-31 17:14:47 +02:00
|
|
|
throw Error("directory '%s' is not in the Nix store", userEnv);
|
|
|
|
|
|
|
|
return profileDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the store path of the latest stable Nix. */
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePath getLatestNix(ref<Store> store)
|
2018-01-31 17:14:47 +02:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
Activity act(*logger, lvlInfo, actUnknown, "querying latest Nix version");
|
|
|
|
|
2018-01-31 17:14:47 +02:00
|
|
|
// FIXME: use nixos.org?
|
2023-11-10 19:12:13 +02:00
|
|
|
auto req = FileTransferRequest((std::string&) settings.upgradeNixStorePathUrl);
|
2020-04-07 00:43:43 +03:00
|
|
|
auto res = getFileTransfer()->download(req);
|
2018-01-31 17:14:47 +02:00
|
|
|
|
2023-06-23 20:51:25 +03:00
|
|
|
auto state = std::make_unique<EvalState>(SearchPath{}, store);
|
2018-06-12 18:26:36 +03:00
|
|
|
auto v = state->allocValue();
|
2023-04-06 14:15:50 +03:00
|
|
|
state->eval(state->parseExprFromString(res.data, state->rootPath(CanonPath("/no-such-path"))), *v);
|
2018-06-12 18:26:36 +03:00
|
|
|
Bindings & bindings(*state->allocBindings(0));
|
2020-02-07 15:08:24 +02:00
|
|
|
auto v2 = findAlongAttrPath(*state, settings.thisSystem, bindings, *v).first;
|
2018-01-31 17:14:47 +02:00
|
|
|
|
2023-01-19 14:23:04 +02:00
|
|
|
return store->parseStorePath(state->forceString(*v2, noPos, "while evaluating the path tho latest nix version"));
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static auto rCmdUpgradeNix = registerCommand<CmdUpgradeNix>("upgrade-nix");
|