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