2013-11-22 17:41:48 +02:00
|
|
|
|
#include "user-env.hh"
|
2010-04-19 13:47:56 +03:00
|
|
|
|
#include "util.hh"
|
2010-04-21 18:08:58 +03:00
|
|
|
|
#include "derivations.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "globals.hh"
|
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "eval.hh"
|
2013-11-19 15:09:03 +02:00
|
|
|
|
#include "eval-inline.hh"
|
2010-04-21 18:08:58 +03:00
|
|
|
|
#include "profiles.hh"
|
2010-04-19 13:47:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
|
|
|
|
|
{
|
2010-04-19 15:10:04 +03:00
|
|
|
|
DrvInfos elems;
|
2010-04-21 18:08:58 +03:00
|
|
|
|
Path manifestFile = userEnv + "/manifest.nix";
|
|
|
|
|
if (pathExists(manifestFile)) {
|
|
|
|
|
Value v;
|
2011-08-06 16:02:55 +03:00
|
|
|
|
state.evalFile(manifestFile, v);
|
2014-09-19 17:49:41 +03:00
|
|
|
|
Bindings & bindings(*state.allocBindings(0));
|
2012-10-04 22:22:25 +03:00
|
|
|
|
getDerivations(state, v, "", bindings, elems, false);
|
2013-11-19 12:18:13 +02:00
|
|
|
|
}
|
2010-04-19 13:47:56 +03:00
|
|
|
|
return elems;
|
2010-04-19 15:10:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
|
bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|
|
|
|
const Path & profile, bool keepDerivations,
|
|
|
|
|
const string & lockToken)
|
|
|
|
|
{
|
|
|
|
|
/* Build the components in the user environment, if they don't
|
|
|
|
|
exist already. */
|
|
|
|
|
PathSet drvsToBuild;
|
2015-07-17 20:24:28 +03:00
|
|
|
|
for (auto & i : elems)
|
|
|
|
|
if (i.queryDrvPath() != "")
|
|
|
|
|
drvsToBuild.insert(i.queryDrvPath());
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
|
|
debug(format("building user environment dependencies"));
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
state.store->buildPaths(drvsToBuild, state.repair ? bmRepair : bmNormal);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
|
|
/* Construct the whole top level derivation. */
|
|
|
|
|
PathSet references;
|
|
|
|
|
Value manifest;
|
|
|
|
|
state.mkList(manifest, elems.size());
|
|
|
|
|
unsigned int n = 0;
|
2015-07-17 20:24:28 +03:00
|
|
|
|
for (auto & i : elems) {
|
2010-04-21 18:08:58 +03:00
|
|
|
|
/* Create a pseudo-derivation containing the name, system,
|
2012-12-04 15:20:36 +02:00
|
|
|
|
output paths, and optionally the derivation path, as well
|
|
|
|
|
as the meta attributes. */
|
2015-07-17 20:24:28 +03:00
|
|
|
|
Path drvPath = keepDerivations ? i.queryDrvPath() : "";
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
2010-10-23 21:18:07 +03:00
|
|
|
|
Value & v(*state.allocValue());
|
2015-07-23 23:05:09 +03:00
|
|
|
|
manifest.listElems()[n++] = &v;
|
2012-12-04 15:20:36 +02:00
|
|
|
|
state.mkAttrs(v, 16);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
2010-10-22 17:47:42 +03:00
|
|
|
|
mkString(*state.allocAttr(v, state.sType), "derivation");
|
2015-07-17 20:24:28 +03:00
|
|
|
|
mkString(*state.allocAttr(v, state.sName), i.name);
|
|
|
|
|
if (!i.system.empty())
|
|
|
|
|
mkString(*state.allocAttr(v, state.sSystem), i.system);
|
|
|
|
|
mkString(*state.allocAttr(v, state.sOutPath), i.queryOutPath());
|
2010-04-21 18:08:58 +03:00
|
|
|
|
if (drvPath != "")
|
2015-07-17 20:24:28 +03:00
|
|
|
|
mkString(*state.allocAttr(v, state.sDrvPath), i.queryDrvPath());
|
2010-10-24 22:52:33 +03:00
|
|
|
|
|
2012-12-04 15:20:36 +02:00
|
|
|
|
// Copy each output.
|
2015-07-17 20:24:28 +03:00
|
|
|
|
DrvInfo::Outputs outputs = i.queryOutputs();
|
2012-12-04 15:20:36 +02:00
|
|
|
|
Value & vOutputs = *state.allocAttr(v, state.sOutputs);
|
|
|
|
|
state.mkList(vOutputs, outputs.size());
|
|
|
|
|
unsigned int m = 0;
|
2015-07-17 20:24:28 +03:00
|
|
|
|
for (auto & j : outputs) {
|
2015-07-23 23:05:09 +03:00
|
|
|
|
mkString(*(vOutputs.listElems()[m++] = state.allocValue()), j.first);
|
2015-07-17 20:24:28 +03:00
|
|
|
|
Value & vOutputs = *state.allocAttr(v, state.symbols.create(j.first));
|
2012-12-04 15:20:36 +02:00
|
|
|
|
state.mkAttrs(vOutputs, 2);
|
2015-07-17 20:24:28 +03:00
|
|
|
|
mkString(*state.allocAttr(vOutputs, state.sOutPath), j.second);
|
2012-12-04 15:20:36 +02:00
|
|
|
|
|
|
|
|
|
/* This is only necessary when installing store paths, e.g.,
|
|
|
|
|
`nix-env -i /nix/store/abcd...-foo'. */
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
state.store->addTempRoot(j.second);
|
|
|
|
|
state.store->ensurePath(j.second);
|
2012-12-04 15:20:36 +02:00
|
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
|
references.insert(j.second);
|
2012-12-04 15:20:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the meta attributes.
|
2010-10-24 22:52:33 +03:00
|
|
|
|
Value & vMeta = *state.allocAttr(v, state.sMeta);
|
2010-10-24 23:09:37 +03:00
|
|
|
|
state.mkAttrs(vMeta, 16);
|
2015-07-17 20:24:28 +03:00
|
|
|
|
StringSet metaNames = i.queryMetaNames();
|
|
|
|
|
for (auto & j : metaNames) {
|
|
|
|
|
Value * v = i.queryMeta(j);
|
2013-11-19 15:29:39 +02:00
|
|
|
|
if (!v) continue;
|
2015-07-17 20:24:28 +03:00
|
|
|
|
vMeta.attrs->push_back(Attr(state.symbols.create(j), v));
|
2010-04-21 18:08:58 +03:00
|
|
|
|
}
|
2014-03-11 18:31:13 +02:00
|
|
|
|
vMeta.attrs->sort();
|
2010-10-24 22:52:33 +03:00
|
|
|
|
v.attrs->sort();
|
2012-12-03 19:19:49 +02:00
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
|
if (drvPath != "") references.insert(drvPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Also write a copy of the list of user environment elements to
|
|
|
|
|
the store; we need it for future modifications of the
|
|
|
|
|
environment. */
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
Path manifestFile = state.store->addTextToStore("env-manifest.nix",
|
2010-04-21 18:08:58 +03:00
|
|
|
|
(format("%1%") % manifest).str(), references);
|
|
|
|
|
|
|
|
|
|
/* Get the environment builder expression. */
|
|
|
|
|
Value envBuilder;
|
2012-01-03 02:16:29 +02:00
|
|
|
|
state.evalFile(state.findFile("nix/buildenv.nix"), envBuilder);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
|
|
/* Construct a Nix expression that calls the user environment
|
|
|
|
|
builder with the manifest as argument. */
|
|
|
|
|
Value args, topLevel;
|
2010-10-24 23:09:37 +03:00
|
|
|
|
state.mkAttrs(args, 3);
|
2010-10-22 17:47:42 +03:00
|
|
|
|
mkString(*state.allocAttr(args, state.symbols.create("manifest")),
|
2010-04-21 18:08:58 +03:00
|
|
|
|
manifestFile, singleton<PathSet>(manifestFile));
|
2010-10-24 22:52:33 +03:00
|
|
|
|
args.attrs->push_back(Attr(state.symbols.create("derivations"), &manifest));
|
|
|
|
|
args.attrs->sort();
|
2010-04-21 18:08:58 +03:00
|
|
|
|
mkApp(topLevel, envBuilder, args);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
|
/* Evaluate it. */
|
|
|
|
|
debug("evaluating user environment builder");
|
2013-11-19 15:09:03 +02:00
|
|
|
|
state.forceValue(topLevel);
|
|
|
|
|
PathSet context;
|
2014-04-04 23:19:33 +03:00
|
|
|
|
Attr & aDrvPath(*topLevel.attrs->find(state.sDrvPath));
|
|
|
|
|
Path topLevelDrv = state.coerceToPath(aDrvPath.pos ? *(aDrvPath.pos) : noPos, *(aDrvPath.value), context);
|
|
|
|
|
Attr & aOutPath(*topLevel.attrs->find(state.sOutPath));
|
|
|
|
|
Path topLevelOut = state.coerceToPath(aOutPath.pos ? *(aOutPath.pos) : noPos, *(aOutPath.value), context);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
|
/* Realise the resulting store expression. */
|
|
|
|
|
debug("building user environment");
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
state.store->buildPaths(singleton<PathSet>(topLevelDrv), state.repair ? bmRepair : bmNormal);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
|
|
/* Switch the current user environment to the output path. */
|
|
|
|
|
PathLocks lock;
|
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
|
|
Path lockTokenCur = optimisticLockProfile(profile);
|
|
|
|
|
if (lockToken != lockTokenCur) {
|
2014-08-20 18:00:17 +03:00
|
|
|
|
printMsg(lvlError, format("profile ‘%1%’ changed while we were busy; restarting") % profile);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
|
debug(format("switching to new user environment"));
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
|
Path generation = createGeneration(state.store, profile, topLevelOut);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
switchLink(profile, generation);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-19 13:47:56 +03:00
|
|
|
|
}
|