mirror of
https://github.com/NixOS/nix.git
synced 2024-11-22 14:52:55 +00:00
Add 'nix upgrade-nix' command
This command upgrades Nix to the latest stable version by installing a store path obtained from https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix which is the same store path that the installer at https://nixos.org/nix/install.sh uses. The upgrade fails if Nix is not installed in a profile (e.g. on NixOS, or when installed outside of the Nix store).
This commit is contained in:
parent
8af911be5c
commit
6fa690291a
@ -79,6 +79,11 @@
|
||||
|
||||
<listitem><para><command>nix add-to-store</command> (970366266b8df712f5f9cedb45af183ef5a8357f).</para></listitem>
|
||||
|
||||
<listitem><para><command>nix upgrade-nix</command> upgrades Nix
|
||||
to the latest stable version. This requires that Nix is
|
||||
installed in a profile. (Thus it won’t work on NixOS, or if it’s
|
||||
installed outside of the Nix store.)</para></listitem>
|
||||
|
||||
<listitem><para>Progress indicator.</para></listitem>
|
||||
|
||||
<listitem><para>All options are available as flags now
|
||||
|
@ -5,6 +5,8 @@
|
||||
|
||||
namespace nix {
|
||||
|
||||
extern std::string programPath;
|
||||
|
||||
struct Value;
|
||||
class Bindings;
|
||||
class EvalState;
|
||||
|
@ -16,6 +16,8 @@ void chrootHelper(int argc, char * * argv);
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::string programPath;
|
||||
|
||||
struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
|
||||
{
|
||||
NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix")
|
||||
@ -78,7 +80,8 @@ void mainWrapped(int argc, char * * argv)
|
||||
initNix();
|
||||
initGC();
|
||||
|
||||
string programName = baseNameOf(argv[0]);
|
||||
programPath = argv[0];
|
||||
string programName = baseNameOf(programPath);
|
||||
|
||||
{
|
||||
auto legacy = (*RegisterLegacyCommand::commands)[programName];
|
||||
|
131
src/nix/upgrade-nix.cc
Normal file
131
src/nix/upgrade-nix.cc
Normal file
@ -0,0 +1,131 @@
|
||||
#include "command.hh"
|
||||
#include "store-api.hh"
|
||||
#include "download.hh"
|
||||
#include "eval.hh"
|
||||
#include "attr-path.hh"
|
||||
|
||||
using namespace nix;
|
||||
|
||||
struct CmdUpgradeNix : StoreCommand
|
||||
{
|
||||
Path profileDir;
|
||||
|
||||
CmdUpgradeNix()
|
||||
{
|
||||
mkFlag()
|
||||
.longName("profile")
|
||||
.shortName('p')
|
||||
.labels({"profile-dir"})
|
||||
.description("the Nix profile to upgrade")
|
||||
.dest(&profileDir);
|
||||
}
|
||||
|
||||
std::string name() override
|
||||
{
|
||||
return "upgrade-nix";
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
settings.pureEval = true;
|
||||
|
||||
if (profileDir == "")
|
||||
profileDir = getProfileDir(store);
|
||||
|
||||
printInfo("upgrading Nix in profile '%s'", profileDir);
|
||||
|
||||
Path storePath;
|
||||
{
|
||||
Activity act(*logger, lvlInfo, actUnknown, "querying latest Nix version");
|
||||
storePath = getLatestNix(store);
|
||||
}
|
||||
|
||||
{
|
||||
Activity act(*logger, lvlInfo, actUnknown, fmt("downloading '%s'...", storePath));
|
||||
store->ensurePath(storePath);
|
||||
}
|
||||
|
||||
{
|
||||
Activity act(*logger, lvlInfo, actUnknown, fmt("verifying that '%s' works...", storePath));
|
||||
auto program = storePath + "/bin/nix-env";
|
||||
auto s = runProgram(program, false, {"--version"});
|
||||
if (s.find("Nix") == std::string::npos)
|
||||
throw Error("could not verify that '%s' works", program);
|
||||
}
|
||||
|
||||
{
|
||||
Activity act(*logger, lvlInfo, actUnknown, fmt("installing '%s' into profile '%s'...", storePath, profileDir));
|
||||
runProgram(settings.nixBinDir + "/nix-env", false,
|
||||
{"--profile", profileDir, "-i", storePath, "--no-sandbox"});
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the profile in which Nix is installed. */
|
||||
Path getProfileDir(ref<Store> store)
|
||||
{
|
||||
Path where;
|
||||
|
||||
for (auto & dir : tokenizeString<Strings>(getEnv("PATH"), ":"))
|
||||
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'");
|
||||
|
||||
Path profileDir;
|
||||
Path userEnv;
|
||||
|
||||
if (baseNameOf(where) != "bin" ||
|
||||
!hasSuffix(userEnv = canonPath(profileDir = dirOf(where), true), "user-environment"))
|
||||
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
|
||||
|
||||
if (!store->isValidPath(userEnv))
|
||||
throw Error("directory '%s' is not in the Nix store", userEnv);
|
||||
|
||||
return profileDir;
|
||||
}
|
||||
|
||||
/* Return the store path of the latest stable Nix. */
|
||||
Path getLatestNix(ref<Store> store)
|
||||
{
|
||||
// FIXME: use nixos.org?
|
||||
auto req = DownloadRequest("https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix");
|
||||
auto res = getDownloader()->download(req);
|
||||
|
||||
EvalState state(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);
|
||||
|
||||
return state.forceString(*v2);
|
||||
}
|
||||
};
|
||||
|
||||
static RegisterCommand r1(make_ref<CmdUpgradeNix>());
|
Loading…
Reference in New Issue
Block a user