2010-04-19 13:47:56 +03:00
|
|
|
#include "util.hh"
|
|
|
|
#include "get-drvs.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"
|
|
|
|
#include "profiles.hh"
|
2010-04-19 13:47:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2010-04-19 15:10:04 +03:00
|
|
|
static void readLegacyManifest(const Path & path, DrvInfos & elems);
|
|
|
|
|
|
|
|
|
2010-04-19 13:47:56 +03:00
|
|
|
DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
|
|
|
|
{
|
2010-04-19 15:10:04 +03:00
|
|
|
DrvInfos elems;
|
2010-04-19 13:47:56 +03:00
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
Path manifestFile = userEnv + "/manifest.nix";
|
|
|
|
Path oldManifestFile = userEnv + "/manifest";
|
2010-04-19 13:47:56 +03:00
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
if (pathExists(manifestFile)) {
|
|
|
|
Value v;
|
2011-08-06 16:02:55 +03:00
|
|
|
state.evalFile(manifestFile, v);
|
2010-10-22 17:47:42 +03:00
|
|
|
Bindings bindings;
|
|
|
|
getDerivations(state, v, "", bindings, elems);
|
2010-04-21 18:08:58 +03:00
|
|
|
} else if (pathExists(oldManifestFile))
|
|
|
|
readLegacyManifest(oldManifestFile, elems);
|
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;
|
|
|
|
foreach (DrvInfos::const_iterator, i, elems)
|
|
|
|
if (i->queryDrvPath(state) != "")
|
|
|
|
drvsToBuild.insert(i->queryDrvPath(state));
|
|
|
|
|
|
|
|
debug(format("building user environment dependencies"));
|
2012-10-03 22:35:42 +03:00
|
|
|
store->buildPaths(drvsToBuild, state.repair);
|
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;
|
|
|
|
foreach (DrvInfos::iterator, i, elems) {
|
|
|
|
/* Create a pseudo-derivation containing the name, system,
|
|
|
|
output path, and optionally the derivation path, as well as
|
|
|
|
the meta attributes. */
|
|
|
|
Path drvPath = keepDerivations ? i->queryDrvPath(state) : "";
|
|
|
|
|
2010-10-23 21:18:07 +03:00
|
|
|
Value & v(*state.allocValue());
|
2010-04-21 18:08:58 +03:00
|
|
|
manifest.list.elems[n++] = &v;
|
2010-10-24 23:09:37 +03:00
|
|
|
state.mkAttrs(v, 8);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
2010-10-22 17:47:42 +03:00
|
|
|
mkString(*state.allocAttr(v, state.sType), "derivation");
|
|
|
|
mkString(*state.allocAttr(v, state.sName), i->name);
|
|
|
|
mkString(*state.allocAttr(v, state.sSystem), i->system);
|
|
|
|
mkString(*state.allocAttr(v, state.sOutPath), i->queryOutPath(state));
|
2010-04-21 18:08:58 +03:00
|
|
|
if (drvPath != "")
|
2010-10-22 17:47:42 +03:00
|
|
|
mkString(*state.allocAttr(v, state.sDrvPath), i->queryDrvPath(state));
|
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);
|
2010-10-24 22:52:33 +03:00
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
MetaInfo meta = i->queryMetaInfo(state);
|
|
|
|
|
|
|
|
foreach (MetaInfo::const_iterator, j, meta) {
|
2010-10-24 22:52:33 +03:00
|
|
|
Value & v2(*state.allocAttr(vMeta, state.symbols.create(j->first)));
|
2010-04-21 18:08:58 +03: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 21:18:07 +03:00
|
|
|
v2.list.elems[m] = state.allocValue();
|
2010-04-21 18:08:58 +03:00
|
|
|
mkString(*v2.list.elems[m++], *k);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-24 22:52:33 +03:00
|
|
|
vMeta.attrs->sort();
|
|
|
|
v.attrs->sort();
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
/* This is only necessary when installing store paths, e.g.,
|
|
|
|
`nix-env -i /nix/store/abcd...-foo'. */
|
|
|
|
store->addTempRoot(i->queryOutPath(state));
|
|
|
|
store->ensurePath(i->queryOutPath(state));
|
|
|
|
|
|
|
|
references.insert(i->queryOutPath(state));
|
|
|
|
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 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);
|
|
|
|
|
|
|
|
/* Evaluate it. */
|
|
|
|
debug("evaluating user environment builder");
|
|
|
|
DrvInfo topLevelDrv;
|
|
|
|
if (!getDerivation(state, topLevel, topLevelDrv))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
/* Realise the resulting store expression. */
|
|
|
|
debug("building user environment");
|
2012-10-03 22:35:42 +03:00
|
|
|
store->buildPaths(singleton<PathSet>(topLevelDrv.queryDrvPath(state)), state.repair);
|
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) {
|
|
|
|
printMsg(lvlError, format("profile `%1%' changed while we were busy; restarting") % profile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(format("switching to new user environment"));
|
|
|
|
Path generation = createGeneration(profile, topLevelDrv.queryOutPath(state));
|
|
|
|
switchLink(profile, generation);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 15:10:04 +03:00
|
|
|
/* Code for parsing manifests in the old textual ATerm format. */
|
|
|
|
|
|
|
|
static string parseStr(std::istream & str)
|
|
|
|
{
|
|
|
|
expect(str, "Str(");
|
|
|
|
string s = parseString(str);
|
|
|
|
expect(str, ",[])");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static string parseWord(std::istream & str)
|
|
|
|
{
|
|
|
|
string res;
|
|
|
|
while (isalpha(str.peek()))
|
|
|
|
res += str.get();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MetaInfo parseMeta(std::istream & str)
|
|
|
|
{
|
|
|
|
MetaInfo meta;
|
|
|
|
|
|
|
|
expect(str, "Attrs([");
|
|
|
|
while (!endOfList(str)) {
|
|
|
|
expect(str, "Bind(");
|
|
|
|
|
|
|
|
MetaValue value;
|
|
|
|
|
|
|
|
string name = parseString(str);
|
|
|
|
expect(str, ",");
|
|
|
|
|
|
|
|
string type = parseWord(str);
|
|
|
|
|
|
|
|
if (type == "Str") {
|
|
|
|
expect(str, "(");
|
|
|
|
value.type = MetaValue::tpString;
|
|
|
|
value.stringValue = parseString(str);
|
|
|
|
expect(str, ",[])");
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (type == "List") {
|
|
|
|
expect(str, "([");
|
|
|
|
value.type = MetaValue::tpStrings;
|
|
|
|
while (!endOfList(str))
|
|
|
|
value.stringValues.push_back(parseStr(str));
|
|
|
|
expect(str, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
else throw Error(format("unexpected token `%1%'") % type);
|
|
|
|
|
|
|
|
expect(str, ",NoPos)");
|
|
|
|
meta[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(str, ")");
|
|
|
|
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void readLegacyManifest(const Path & path, DrvInfos & elems)
|
|
|
|
{
|
|
|
|
string manifest = readFile(path);
|
|
|
|
std::istringstream str(manifest);
|
|
|
|
expect(str, "List([");
|
|
|
|
|
|
|
|
unsigned int n = 0;
|
|
|
|
|
|
|
|
while (!endOfList(str)) {
|
|
|
|
DrvInfo elem;
|
|
|
|
expect(str, "Attrs([");
|
|
|
|
|
|
|
|
while (!endOfList(str)) {
|
|
|
|
expect(str, "Bind(");
|
|
|
|
string name = parseString(str);
|
|
|
|
expect(str, ",");
|
|
|
|
|
|
|
|
if (name == "meta") elem.setMetaInfo(parseMeta(str));
|
|
|
|
else {
|
|
|
|
string value = parseStr(str);
|
|
|
|
if (name == "name") elem.name = value;
|
|
|
|
else if (name == "outPath") elem.setOutPath(value);
|
|
|
|
else if (name == "drvPath") elem.setDrvPath(value);
|
|
|
|
else if (name == "system") elem.system = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(str, ",NoPos)");
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(str, ")");
|
|
|
|
|
|
|
|
if (elem.name != "") {
|
|
|
|
elem.attrPath = int2String(n++);
|
|
|
|
elems.push_back(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(str, ")");
|
2010-04-19 13:47:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|