2010-04-19 10:47:56 +00:00
|
|
|
#include "util.hh"
|
|
|
|
#include "get-drvs.hh"
|
2010-04-21 15:08:58 +00:00
|
|
|
#include "derivations.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "eval.hh"
|
|
|
|
#include "profiles.hh"
|
2010-04-19 10:47:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
|
|
|
|
{
|
2010-04-19 12:10:04 +00:00
|
|
|
DrvInfos elems;
|
2010-04-21 15:08:58 +00:00
|
|
|
Path manifestFile = userEnv + "/manifest.nix";
|
|
|
|
if (pathExists(manifestFile)) {
|
|
|
|
Value v;
|
2011-08-06 13:02:55 +00:00
|
|
|
state.evalFile(manifestFile, v);
|
2010-10-22 14:47:42 +00:00
|
|
|
Bindings bindings;
|
2012-10-04 19:22:25 +00:00
|
|
|
getDerivations(state, v, "", bindings, elems, false);
|
2013-11-19 10:18:13 +00:00
|
|
|
}
|
2010-04-19 10:47:56 +00:00
|
|
|
return elems;
|
2010-04-19 12:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 15:08:58 +00: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;
|
|
|
|
foreach (DrvInfos::const_iterator, i, elems)
|
|
|
|
if (i->queryDrvPath(state) != "")
|
|
|
|
drvsToBuild.insert(i->queryDrvPath(state));
|
|
|
|
|
|
|
|
debug(format("building user environment dependencies"));
|
2012-10-03 19:35:42 +00:00
|
|
|
store->buildPaths(drvsToBuild, state.repair);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Construct the whole top level derivation. */
|
|
|
|
PathSet references;
|
|
|
|
Value manifest;
|
|
|
|
state.mkList(manifest, elems.size());
|
|
|
|
unsigned int n = 0;
|
|
|
|
foreach (DrvInfos::iterator, i, elems) {
|
|
|
|
/* Create a pseudo-derivation containing the name, system,
|
2012-12-04 13:20:36 +00:00
|
|
|
output paths, and optionally the derivation path, as well
|
|
|
|
as the meta attributes. */
|
2010-04-21 15:08:58 +00:00
|
|
|
Path drvPath = keepDerivations ? i->queryDrvPath(state) : "";
|
|
|
|
|
2010-10-23 18:18:07 +00:00
|
|
|
Value & v(*state.allocValue());
|
2010-04-21 15:08:58 +00:00
|
|
|
manifest.list.elems[n++] = &v;
|
2012-12-04 13:20:36 +00:00
|
|
|
state.mkAttrs(v, 16);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
2010-10-22 14:47:42 +00:00
|
|
|
mkString(*state.allocAttr(v, state.sType), "derivation");
|
|
|
|
mkString(*state.allocAttr(v, state.sName), i->name);
|
2012-12-04 13:20:36 +00:00
|
|
|
if (!i->system.empty())
|
|
|
|
mkString(*state.allocAttr(v, state.sSystem), i->system);
|
2010-10-22 14:47:42 +00:00
|
|
|
mkString(*state.allocAttr(v, state.sOutPath), i->queryOutPath(state));
|
2010-04-21 15:08:58 +00:00
|
|
|
if (drvPath != "")
|
2010-10-22 14:47:42 +00:00
|
|
|
mkString(*state.allocAttr(v, state.sDrvPath), i->queryDrvPath(state));
|
2010-10-24 19:52:33 +00:00
|
|
|
|
2012-12-04 13:20:36 +00:00
|
|
|
// Copy each output.
|
|
|
|
DrvInfo::Outputs outputs = i->queryOutputs(state);
|
|
|
|
Value & vOutputs = *state.allocAttr(v, state.sOutputs);
|
|
|
|
state.mkList(vOutputs, outputs.size());
|
|
|
|
unsigned int m = 0;
|
|
|
|
foreach (DrvInfo::Outputs::iterator, j, outputs) {
|
|
|
|
mkString(*(vOutputs.list.elems[m++] = state.allocValue()), j->first);
|
|
|
|
Value & vOutputs = *state.allocAttr(v, state.symbols.create(j->first));
|
|
|
|
state.mkAttrs(vOutputs, 2);
|
|
|
|
mkString(*state.allocAttr(vOutputs, state.sOutPath), j->second);
|
|
|
|
|
|
|
|
/* This is only necessary when installing store paths, e.g.,
|
|
|
|
`nix-env -i /nix/store/abcd...-foo'. */
|
|
|
|
store->addTempRoot(j->second);
|
|
|
|
store->ensurePath(j->second);
|
|
|
|
|
|
|
|
references.insert(j->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the meta attributes.
|
2010-10-24 19:52:33 +00:00
|
|
|
Value & vMeta = *state.allocAttr(v, state.sMeta);
|
2010-10-24 20:09:37 +00:00
|
|
|
state.mkAttrs(vMeta, 16);
|
2010-10-24 19:52:33 +00:00
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
MetaInfo meta = i->queryMetaInfo(state);
|
|
|
|
|
|
|
|
foreach (MetaInfo::const_iterator, j, meta) {
|
2010-10-24 19:52:33 +00:00
|
|
|
Value & v2(*state.allocAttr(vMeta, state.symbols.create(j->first)));
|
2010-04-21 15:08:58 +00:00
|
|
|
switch (j->second.type) {
|
|
|
|
case MetaValue::tpInt: mkInt(v2, j->second.intValue); break;
|
|
|
|
case MetaValue::tpString: mkString(v2, j->second.stringValue); break;
|
|
|
|
case MetaValue::tpStrings: {
|
|
|
|
state.mkList(v2, j->second.stringValues.size());
|
|
|
|
unsigned int m = 0;
|
|
|
|
foreach (Strings::const_iterator, k, j->second.stringValues) {
|
2010-10-23 18:18:07 +00:00
|
|
|
v2.list.elems[m] = state.allocValue();
|
2010-04-21 15:08:58 +00:00
|
|
|
mkString(*v2.list.elems[m++], *k);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-10-24 19:52:33 +00:00
|
|
|
vMeta.attrs->sort();
|
|
|
|
v.attrs->sort();
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-04-21 15:08:58 +00: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. */
|
|
|
|
Path manifestFile = store->addTextToStore("env-manifest.nix",
|
|
|
|
(format("%1%") % manifest).str(), references);
|
|
|
|
|
|
|
|
/* Get the environment builder expression. */
|
|
|
|
Value envBuilder;
|
2012-01-03 00:16:29 +00:00
|
|
|
state.evalFile(state.findFile("nix/buildenv.nix"), envBuilder);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Construct a Nix expression that calls the user environment
|
|
|
|
builder with the manifest as argument. */
|
|
|
|
Value args, topLevel;
|
2010-10-24 20:09:37 +00:00
|
|
|
state.mkAttrs(args, 3);
|
2010-10-22 14:47:42 +00:00
|
|
|
mkString(*state.allocAttr(args, state.symbols.create("manifest")),
|
2010-04-21 15:08:58 +00:00
|
|
|
manifestFile, singleton<PathSet>(manifestFile));
|
2010-10-24 19:52:33 +00:00
|
|
|
args.attrs->push_back(Attr(state.symbols.create("derivations"), &manifest));
|
|
|
|
args.attrs->sort();
|
2010-04-21 15:08:58 +00:00
|
|
|
mkApp(topLevel, envBuilder, args);
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
/* Evaluate it. */
|
|
|
|
debug("evaluating user environment builder");
|
|
|
|
DrvInfo topLevelDrv;
|
2012-10-04 19:22:25 +00:00
|
|
|
if (!getDerivation(state, topLevel, topLevelDrv, false))
|
2010-04-21 15:08:58 +00:00
|
|
|
abort();
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
/* Realise the resulting store expression. */
|
|
|
|
debug("building user environment");
|
2012-10-03 19:35:42 +00:00
|
|
|
store->buildPaths(singleton<PathSet>(topLevelDrv.queryDrvPath(state)), state.repair);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Switch the current user environment to the output path. */
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
Path lockTokenCur = optimisticLockProfile(profile);
|
|
|
|
if (lockToken != lockTokenCur) {
|
|
|
|
printMsg(lvlError, format("profile `%1%' changed while we were busy; restarting") % profile);
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
debug(format("switching to new user environment"));
|
|
|
|
Path generation = createGeneration(profile, topLevelDrv.queryOutPath(state));
|
|
|
|
switchLink(profile, generation);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 10:47:56 +00:00
|
|
|
}
|