* Cleanup: use the code shared with nix-env.

This commit is contained in:
Eelco Dolstra 2006-02-10 15:14:57 +00:00
parent 4eb637c799
commit f848a45739

View file

@ -8,6 +8,7 @@
#include "eval.hh" #include "eval.hh"
#include "parser.hh" #include "parser.hh"
#include "nixexpr-ast.hh" #include "nixexpr-ast.hh"
#include "get-drvs.hh"
#include "help.txt.hh" #include "help.txt.hh"
@ -32,22 +33,15 @@ static int rootNr = 0;
static bool indirectRoot = false; static bool indirectRoot = false;
/* Print out the paths of the resulting derivation(s). If the user static void printResult(EvalState & state, Expr e, bool evalOnly)
specified the `--add-root' flag, we register the derivation as a
garbage collection root and print out the path of the GC root
symlink instead. */
static void printDrvPaths(EvalState & state, Expr e)
{ {
ATermList es; if (evalOnly)
cout << format("%1%\n") % e;
/* !!! duplication w.r.t. parseDerivations in nix-env */ else {
DrvInfos drvs;
if (matchAttrs(e, es)) { getDerivations(state, e, drvs);
Expr a = queryAttr(e, "type"); for (DrvInfos::iterator i = drvs.begin(); i != drvs.end(); ++i) {
if (a && evalString(state, a) == "derivation") { Path drvPath = i->queryDrvPath(state);
a = queryAttr(e, "drvPath");
if (a) {
Path drvPath = evalPath(state, a);
if (gcRoot == "") if (gcRoot == "")
printGCWarning(); printGCWarning();
else else
@ -55,51 +49,8 @@ static void printDrvPaths(EvalState & state, Expr e)
makeRootName(gcRoot, rootNr), makeRootName(gcRoot, rootNr),
indirectRoot); indirectRoot);
cout << format("%1%\n") % drvPath; cout << format("%1%\n") % drvPath;
return;
}
throw Error("bad derivation");
} else {
ATermMap drvMap;
queryAllAttrs(e, drvMap);
for (ATermIterator i(drvMap.keys()); i; ++i)
printDrvPaths(state, evalExpr(state, drvMap.get(*i)));
return;
} }
} }
if (matchList(e, es)) {
for (ATermIterator i(es); i; ++i)
printDrvPaths(state, evalExpr(state, *i));
return;
}
ATermList formals;
ATerm body, pos;
if (matchFunction(e, formals, body, pos)) {
for (ATermIterator i(formals); i; ++i) {
Expr name, def;
if (matchNoDefFormal(*i, name))
throw Error(format("expression evaluates to a function with no-default arguments (`%1%')")
% aterm2String(name));
else if (!matchDefFormal(*i, name, def))
abort(); /* can't happen */
}
printDrvPaths(state, evalExpr(state,
makeCall(e, makeAttrs(ATermMap()))));
return;
}
throw Error("expression does not evaluate to one or more derivations");
}
static void printResult(EvalState & state, Expr e, bool evalOnly)
{
if (evalOnly)
cout << format("%1%\n") % e;
else
printDrvPaths(state, e);
} }