* Applied rbroek's patch from the branch at

https://svn.cs.uu.nl:12443/repos/trace/buildfarm-control/trunk/ext/nix/,
  with some modifications.  This allows `nix-env -qa' to show the
  attribute path that can be used to unambiguously install a package
  using `nix-env -i -A'.  Example:

    $ nix-env -f top-level/all-packages.nix -qaA subversion xorg-server
    subversionWithJava  subversion-1.2.3
    subversion          subversion-1.3.2
    subversion14        subversion-1.4.0pre-rc1
    xorg.xorgserver     xorg-server-1.1.0
This commit is contained in:
Eelco Dolstra 2006-07-25 16:40:38 +00:00
parent b11aeb2c4b
commit 0e6dc72a7a
4 changed files with 37 additions and 15 deletions

View file

@ -58,7 +58,7 @@ typedef set<Expr> Exprs;
it makes sense for the caller to recursively search for derivations it makes sense for the caller to recursively search for derivations
in `e'. */ in `e'. */
static bool getDerivation(EvalState & state, Expr e, static bool getDerivation(EvalState & state, Expr e,
DrvInfos & drvs, Exprs & doneExprs) DrvInfos & drvs, Exprs & doneExprs, string attributeName)
{ {
try { try {
@ -92,6 +92,8 @@ static bool getDerivation(EvalState & state, Expr e,
drv.attrs = attrs; drv.attrs = attrs;
drv.attrPath = attributeName;
drvs.push_back(drv); drvs.push_back(drv);
return false; return false;
@ -105,15 +107,22 @@ bool getDerivation(EvalState & state, Expr e, DrvInfo & drv)
{ {
Exprs doneExprs; Exprs doneExprs;
DrvInfos drvs; DrvInfos drvs;
getDerivation(state, e, drvs, doneExprs); getDerivation(state, e, drvs, doneExprs, "");
if (drvs.size() != 1) return false; if (drvs.size() != 1) return false;
drv = drvs.front(); drv = drvs.front();
return true; return true;
} }
static string addToPath(const string & s1, const string & s2)
{
return s1.empty() ? s2 : s1 + "." + s2;
}
static void getDerivations(EvalState & state, Expr e, static void getDerivations(EvalState & state, Expr e,
DrvInfos & drvs, Exprs & doneExprs, const string & attrPath) DrvInfos & drvs, Exprs & doneExprs, const string & attrPath,
const string & pathTaken)
{ {
/* Automatically call functions that have defaults for all /* Automatically call functions that have defaults for all
arguments. */ arguments. */
@ -129,7 +138,7 @@ static void getDerivations(EvalState & state, Expr e,
} }
getDerivations(state, getDerivations(state,
makeCall(e, makeAttrs(ATermMap(0))), makeCall(e, makeAttrs(ATermMap(0))),
drvs, doneExprs, attrPath); drvs, doneExprs, attrPath, pathTaken);
return; return;
} }
@ -160,7 +169,7 @@ static void getDerivations(EvalState & state, Expr e,
ATermList es; ATermList es;
DrvInfo drv; DrvInfo drv;
if (!getDerivation(state, e, drvs, doneExprs)) { if (!getDerivation(state, e, drvs, doneExprs, pathTaken)) {
if (apType != apNone) throw attrError; if (apType != apNone) throw attrError;
return; return;
} }
@ -175,7 +184,8 @@ static void getDerivations(EvalState & state, Expr e,
for (ATermMap::const_iterator i = drvMap.begin(); i != drvMap.end(); ++i) { for (ATermMap::const_iterator i = drvMap.begin(); i != drvMap.end(); ++i) {
startNest(nest, lvlDebug, startNest(nest, lvlDebug,
format("evaluating attribute `%1%'") % aterm2String(i->key)); format("evaluating attribute `%1%'") % aterm2String(i->key));
if (getDerivation(state, i->value, drvs, doneExprs)) { string pathTaken2 = addToPath(pathTaken, aterm2String(i->key));
if (getDerivation(state, i->value, drvs, doneExprs, pathTaken2)) {
/* If the value of this attribute is itself an /* If the value of this attribute is itself an
attribute set, should we recurse into it? attribute set, should we recurse into it?
=> Only if it has a `recurseForDerivations = true' => Only if it has a `recurseForDerivations = true'
@ -187,7 +197,7 @@ static void getDerivations(EvalState & state, Expr e,
queryAllAttrs(e, attrs, false); queryAllAttrs(e, attrs, false);
Expr e2 = attrs.get(toATerm("recurseForDerivations")); Expr e2 = attrs.get(toATerm("recurseForDerivations"));
if (e2 && evalBool(state, e2)) if (e2 && evalBool(state, e2))
getDerivations(state, e, drvs, doneExprs, attrPathRest); getDerivations(state, e, drvs, doneExprs, attrPathRest, pathTaken2);
} }
} }
} }
@ -196,9 +206,10 @@ static void getDerivations(EvalState & state, Expr e,
if (!e2) throw Error(format("attribute `%1%' in selection path not found") % attr); if (!e2) throw Error(format("attribute `%1%' in selection path not found") % attr);
startNest(nest, lvlDebug, startNest(nest, lvlDebug,
format("evaluating attribute `%1%'") % attr); format("evaluating attribute `%1%'") % attr);
getDerivation(state, e2, drvs, doneExprs); string pathTaken2 = addToPath(pathTaken, attr);
getDerivation(state, e2, drvs, doneExprs, pathTaken2);
if (!attrPath.empty()) if (!attrPath.empty())
getDerivations(state, e2, drvs, doneExprs, attrPathRest); getDerivations(state, e2, drvs, doneExprs, attrPathRest, pathTaken2);
} }
return; return;
} }
@ -206,19 +217,22 @@ static void getDerivations(EvalState & state, Expr e,
if (matchList(e, es)) { if (matchList(e, es)) {
if (apType != apNone && apType != apIndex) throw attrError; if (apType != apNone && apType != apIndex) throw attrError;
if (apType == apNone) { if (apType == apNone) {
for (ATermIterator i(es); i; ++i) { int n = 0;
for (ATermIterator i(es); i; ++i, ++n) {
startNest(nest, lvlDebug, startNest(nest, lvlDebug,
format("evaluating list element")); format("evaluating list element"));
if (getDerivation(state, *i, drvs, doneExprs)) string pathTaken2 = addToPath(pathTaken, (format("%1%") % n).str());
getDerivations(state, *i, drvs, doneExprs, attrPathRest); if (getDerivation(state, *i, drvs, doneExprs, pathTaken2))
getDerivations(state, *i, drvs, doneExprs, attrPathRest, pathTaken2);
} }
} else { } else {
Expr e2 = ATelementAt(es, attrIndex); Expr e2 = ATelementAt(es, attrIndex);
if (!e2) throw Error(format("list index %1% in selection path not found") % attrIndex); if (!e2) throw Error(format("list index %1% in selection path not found") % attrIndex);
startNest(nest, lvlDebug, startNest(nest, lvlDebug,
format("evaluating list element")); format("evaluating list element"));
if (getDerivation(state, e2, drvs, doneExprs)) string pathTaken2 = addToPath(pathTaken, (format("%1%") % attrIndex).str());
getDerivations(state, e2, drvs, doneExprs, attrPathRest); if (getDerivation(state, e2, drvs, doneExprs, pathTaken2))
getDerivations(state, e2, drvs, doneExprs, attrPathRest, pathTaken2);
} }
return; return;
} }
@ -231,5 +245,5 @@ void getDerivations(EvalState & state, Expr e, DrvInfos & drvs,
const string & attrPath) const string & attrPath)
{ {
Exprs doneExprs; Exprs doneExprs;
getDerivations(state, e, drvs, doneExprs, attrPath); getDerivations(state, e, drvs, doneExprs, attrPath, "");
} }

View file

@ -20,6 +20,7 @@ private:
public: public:
string name; string name;
string attrPath; /* path towards the derivation */
string system; string system;
shared_ptr<ATermMap> attrs; shared_ptr<ATermMap> attrs;

View file

@ -48,6 +48,8 @@ Query types:
--status / -s: print installed/present status --status / -s: print installed/present status
--no-name: hide derivation names --no-name: hide derivation names
--attr-path / -A: shows the unambiguous attribute name of the
derivation which can be used when installing with -A
--system: print the platform type of the derivation --system: print the platform type of the derivation
--compare-versions / -c: compare version to available or installed --compare-versions / -c: compare version to available or installed
--drv-path: print path of derivation --drv-path: print path of derivation

View file

@ -724,6 +724,7 @@ static void opQuery(Globals & globals,
{ {
bool printStatus = false; bool printStatus = false;
bool printName = true; bool printName = true;
bool printAttrPath = false;
bool printSystem = false; bool printSystem = false;
bool printDrvPath = false; bool printDrvPath = false;
bool printOutPath = false; bool printOutPath = false;
@ -747,6 +748,8 @@ static void opQuery(Globals & globals,
else if (*i == "--available" || *i == "-a") source = sAvailable; else if (*i == "--available" || *i == "-a") source = sAvailable;
else throw UsageError(format("unknown flag `%1%'") % *i); else throw UsageError(format("unknown flag `%1%'") % *i);
if (globals.instSource.type == srcAttrPath) printAttrPath = true; /* hack */
if (opArgs.size() == 0) { if (opArgs.size() == 0) {
printMsg(lvlInfo, "warning: you probably meant to specify the argument '*' to show all packages"); printMsg(lvlInfo, "warning: you probably meant to specify the argument '*' to show all packages");
} }
@ -809,6 +812,8 @@ static void opQuery(Globals & globals,
+ (subs.size() > 0 ? "S" : "-")); + (subs.size() > 0 ? "S" : "-"));
} }
if (printAttrPath) columns.push_back(i->attrPath);
if (printName) columns.push_back(i->name); if (printName) columns.push_back(i->name);
if (compareVersions) { if (compareVersions) {