forked from lix-project/lix
fix nix upgrade-nix
on new-style profiles
nix3-profile automatically migrates any profile its used on to its style
of profile -- the ones with manifest.json instead of manifest.nix. On
non-NixOS systems, Nix is conventionally installed to the profile at
/nix/var/nix/profiles/default, so if a user passed that to `--profile`
of `nix profile`, then it would break upgrade-nix from ever working
again, without recreating the profile.
This commit fixes that, and allows upgrade-nix to work on either kind of
profile.
Fixes #16.
Change-Id: I4c49b1beba93bb50e8f8a107edc451affe08c3f7
This commit is contained in:
parent
d0a9e7c2ff
commit
3502e27f93
|
@ -141,10 +141,7 @@ ProfileManifest::ProfileManifest(EvalState & state, const Path & profile)
|
||||||
}
|
}
|
||||||
elements.emplace_back(std::move(element));
|
elements.emplace_back(std::move(element));
|
||||||
}
|
}
|
||||||
}
|
} else if (pathExists(profile + "/manifest.nix")) {
|
||||||
|
|
||||||
else if (pathExists(profile + "/manifest.nix"))
|
|
||||||
{
|
|
||||||
// FIXME: needed because of pure mode; ugly.
|
// FIXME: needed because of pure mode; ugly.
|
||||||
state.allowPath(state.store->followLinksToStore(profile));
|
state.allowPath(state.store->followLinksToStore(profile));
|
||||||
state.allowPath(state.store->followLinksToStore(profile + "/manifest.nix"));
|
state.allowPath(state.store->followLinksToStore(profile + "/manifest.nix"));
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "cmd-profiles.hh"
|
||||||
#include "command.hh"
|
#include "command.hh"
|
||||||
#include "common-args.hh"
|
#include "common-args.hh"
|
||||||
|
#include "local-fs-store.hh"
|
||||||
|
#include "logging.hh"
|
||||||
|
#include "profiles.hh"
|
||||||
#include "store-api.hh"
|
#include "store-api.hh"
|
||||||
#include "filetransfer.hh"
|
#include "filetransfer.hh"
|
||||||
#include "eval.hh"
|
#include "eval.hh"
|
||||||
|
@ -10,7 +16,7 @@
|
||||||
|
|
||||||
using namespace nix;
|
using namespace nix;
|
||||||
|
|
||||||
struct CmdUpgradeNix : MixDryRun, StoreCommand
|
struct CmdUpgradeNix : MixDryRun, EvalCommand
|
||||||
{
|
{
|
||||||
Path profileDir;
|
Path profileDir;
|
||||||
std::string storePathsUrl = "https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix";
|
std::string storePathsUrl = "https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix";
|
||||||
|
@ -59,12 +65,15 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
|
||||||
{
|
{
|
||||||
evalSettings.pureEval = true;
|
evalSettings.pureEval = true;
|
||||||
|
|
||||||
if (profileDir == "")
|
if (profileDir == "") {
|
||||||
profileDir = getProfileDir(store);
|
profileDir = getProfileDir(store);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto canonProfileDir = canonPath(profileDir, true);
|
||||||
|
|
||||||
printInfo("upgrading Nix in profile '%s'", profileDir);
|
printInfo("upgrading Nix in profile '%s'", profileDir);
|
||||||
|
|
||||||
auto storePath = getLatestNix(store);
|
StorePath storePath = getLatestNix(store);
|
||||||
|
|
||||||
auto version = DrvName(storePath.name()).version;
|
auto version = DrvName(storePath.name()).version;
|
||||||
|
|
||||||
|
@ -89,11 +98,28 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
|
||||||
|
|
||||||
stopProgressBar();
|
stopProgressBar();
|
||||||
|
|
||||||
{
|
auto const fullStorePath = store->printStorePath(storePath);
|
||||||
Activity act(*logger, lvlInfo, actUnknown,
|
|
||||||
fmt("installing '%s' into profile '%s'...", store->printStorePath(storePath), profileDir));
|
if (canonProfileDir.ends_with("user-environment")) {
|
||||||
runProgram(settings.nixBinDir + "/nix-env", false,
|
|
||||||
{"--profile", profileDir, "-i", store->printStorePath(storePath), "--no-sandbox"});
|
std::string nixEnvCmd = settings.nixBinDir + "/nix-env";
|
||||||
|
Strings upgradeArgs = {
|
||||||
|
"--profile",
|
||||||
|
this->profileDir,
|
||||||
|
"--install",
|
||||||
|
fullStorePath,
|
||||||
|
"--no-sandbox",
|
||||||
|
};
|
||||||
|
|
||||||
|
printTalkative("running %s %s", nixEnvCmd, concatStringsSep(" ", upgradeArgs));
|
||||||
|
runProgram(nixEnvCmd, false, upgradeArgs);
|
||||||
|
} else if (canonProfileDir.ends_with("profile")) {
|
||||||
|
this->upgradeNewStyleProfile(store, storePath);
|
||||||
|
} else {
|
||||||
|
// No I will not use std::unreachable.
|
||||||
|
// That is undefined behavior if you're wrong.
|
||||||
|
// This will have a half-decent error message and coredump.
|
||||||
|
assert("unreachable" == nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
printInfo(ANSI_GREEN "upgrade to version %s done" ANSI_NORMAL, version);
|
printInfo(ANSI_GREEN "upgrade to version %s done" ANSI_NORMAL, version);
|
||||||
|
@ -121,23 +147,94 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand
|
||||||
Path profileDir = dirOf(where);
|
Path profileDir = dirOf(where);
|
||||||
|
|
||||||
// Resolve profile to /nix/var/nix/profiles/<name> link.
|
// Resolve profile to /nix/var/nix/profiles/<name> link.
|
||||||
while (canonPath(profileDir).find("/profiles/") == std::string::npos && isLink(profileDir))
|
while (canonPath(profileDir).find("/profiles/") == std::string::npos && isLink(profileDir)) {
|
||||||
profileDir = readLink(profileDir);
|
profileDir = readLink(profileDir);
|
||||||
|
}
|
||||||
|
|
||||||
printInfo("found profile '%s'", profileDir);
|
printInfo("found profile '%s'", profileDir);
|
||||||
|
|
||||||
Path userEnv = canonPath(profileDir, true);
|
Path userEnv = canonPath(profileDir, true);
|
||||||
|
|
||||||
if (baseNameOf(where) != "bin" ||
|
if (baseNameOf(where) != "bin") {
|
||||||
!userEnv.ends_with("user-environment"))
|
if (!userEnv.ends_with("user-environment") && !userEnv.ends_with("profile")) {
|
||||||
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
|
throw Error("directory '%s' does not appear to be part of a Nix profile", where);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!store->isValidPath(store->parseStorePath(userEnv)))
|
if (!store->isValidPath(store->parseStorePath(userEnv))) {
|
||||||
throw Error("directory '%s' is not in the Nix store", userEnv);
|
throw Error("directory '%s' is not in the Nix store", userEnv);
|
||||||
|
}
|
||||||
|
|
||||||
return profileDir;
|
return profileDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Is there like, any good naming scheme that distinguishes
|
||||||
|
// "profiles which nix-env can use" and "profiles which nix profile can use"?
|
||||||
|
// You can't just say the manifest version since v2 and v3 are both the latter.
|
||||||
|
void upgradeNewStyleProfile(ref<Store> & store, StorePath const & newNix)
|
||||||
|
{
|
||||||
|
auto fsStore = store.dynamic_pointer_cast<LocalFSStore>();
|
||||||
|
// TODO(Qyriad): this check is here because we need to cast to a LocalFSStore,
|
||||||
|
// to pass to createGeneration(), ...but like, there's no way a remote store
|
||||||
|
// would work with the nix-env based upgrade either right?
|
||||||
|
if (!fsStore) {
|
||||||
|
throw Error("nix upgrade-nix cannot be used on a remote store");
|
||||||
|
}
|
||||||
|
|
||||||
|
// nb: nothing actually gets evaluated here.
|
||||||
|
// The ProfileManifest constructor only evaluates anything for manifest.nix
|
||||||
|
// profiles, which this is not.
|
||||||
|
auto evalState = this->getEvalState();
|
||||||
|
|
||||||
|
ProfileManifest manifest(*evalState, profileDir);
|
||||||
|
|
||||||
|
// Find which profile element has Nix in it.
|
||||||
|
// It should be impossible to *not* have Nix, since we grabbed this
|
||||||
|
// store path by looking for things with bin/nix-env in them anyway.
|
||||||
|
auto findNix = [&](ProfileElement const & elem) -> bool {
|
||||||
|
for (auto const & ePath : elem.storePaths) {
|
||||||
|
auto const nixEnv = store->printStorePath(ePath) + "/bin/nix-env";
|
||||||
|
if (pathExists(nixEnv)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We checked each store path in this element. No nixes here boss!
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
auto elemWithNix = std::find_if(
|
||||||
|
manifest.elements.begin(),
|
||||||
|
manifest.elements.end(),
|
||||||
|
findNix
|
||||||
|
);
|
||||||
|
// *Should* be impossible...
|
||||||
|
assert(elemWithNix != std::end(manifest.elements));
|
||||||
|
|
||||||
|
// Now create a new profile element for the new Nix version...
|
||||||
|
ProfileElement elemForNewNix = {
|
||||||
|
.storePaths = {newNix},
|
||||||
|
};
|
||||||
|
|
||||||
|
// ...and splork it into the manifest where the old profile element was.
|
||||||
|
// (Remember, elemWithNix is an iterator)
|
||||||
|
*elemWithNix = elemForNewNix;
|
||||||
|
|
||||||
|
// Build the new profile, and switch to it.
|
||||||
|
StorePath const newProfile = manifest.build(store);
|
||||||
|
printTalkative("built new profile '%s'", store->printStorePath(newProfile));
|
||||||
|
auto const newGeneration = createGeneration(*fsStore, this->profileDir, newProfile);
|
||||||
|
printTalkative(
|
||||||
|
"switching '%s' to newly created generation '%s'",
|
||||||
|
this->profileDir,
|
||||||
|
newGeneration
|
||||||
|
);
|
||||||
|
// TODO(Qyriad): use switchGeneration?
|
||||||
|
// switchLink's docstring seems to indicate that's preferred, but it's
|
||||||
|
// not used for any other `nix profile`-style profile code except for
|
||||||
|
// rollback, and it assumes you already have a generation number, which
|
||||||
|
// we don't.
|
||||||
|
switchLink(profileDir, newGeneration);
|
||||||
|
}
|
||||||
|
|
||||||
/* Return the store path of the latest stable Nix. */
|
/* Return the store path of the latest stable Nix. */
|
||||||
StorePath getLatestNix(ref<Store> store)
|
StorePath getLatestNix(ref<Store> store)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue