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"
|
|
|
|
#include "download.hh"
|
|
|
|
#include "eval.hh"
|
|
|
|
#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;
|
2018-08-30 18:10:28 +03:00
|
|
|
std::string storePathsUrl = "https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix";
|
2018-01-31 17:14:47 +02:00
|
|
|
|
|
|
|
CmdUpgradeNix()
|
|
|
|
{
|
|
|
|
mkFlag()
|
|
|
|
.longName("profile")
|
|
|
|
.shortName('p')
|
|
|
|
.labels({"profile-dir"})
|
|
|
|
.description("the Nix profile to upgrade")
|
|
|
|
.dest(&profileDir);
|
2018-08-30 18:10:28 +03: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 17:14:47 +02: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 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();
|
|
|
|
printError("would upgrade to version %s", version);
|
|
|
|
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
|
|
|
|
|
|
|
printError(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?
|
2018-08-30 18:10:28 +03:00
|
|
|
auto req = DownloadRequest(storePathsUrl);
|
2018-01-31 17:14:47 +02:00
|
|
|
auto res = getDownloader()->download(req);
|
|
|
|
|
2018-06-12 18:26:36 +03: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));
|
|
|
|
auto v2 = findAlongAttrPath(*state, settings.thisSystem, bindings, *v);
|
2018-01-31 17:14:47 +02:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
return store->parseStorePath(state->forceString(*v2));
|
2018-01-31 17:14:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:01:35 +03:00
|
|
|
static auto r1 = registerCommand<CmdUpgradeNix>("upgrade-nix");
|