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()
|
|
|
|
{
|
|
|
|
mkFlag()
|
|
|
|
.longName("profile")
|
|
|
|
.shortName('p')
|
|
|
|
.labels({"profile-dir"})
|
|
|
|
.description("the Nix profile to upgrade")
|
|
|
|
.dest(&profileDir);
|
2018-08-30 15:10:28 +00:00
|
|
|
|
|
|
|
mkFlag()
|
|
|
|
.longName("nix-store-paths-url")
|
|
|
|
.labels({"url"})
|
|
|
|
.description("URL of the file that contains the store paths of the latest Nix release")
|
|
|
|
.dest(&storePathsUrl);
|
2018-01-31 15:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "upgrade Nix to the latest stable version";
|
|
|
|
}
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To upgrade Nix to the latest stable version:",
|
|
|
|
"nix upgrade-nix"
|
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To upgrade Nix in a specific profile:",
|
|
|
|
"nix upgrade-nix -p /nix/var/nix/profiles/per-user/alice/profile"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
printError("would upgrade to version %s", version);
|
|
|
|
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
|
|
|
|
|
|
|
printError(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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 14:01:35 +00:00
|
|
|
static auto r1 = registerCommand<CmdUpgradeNix>("upgrade-nix");
|