* nix-env -qa: make the "-A" flag do the expected thing, namely follow

the given attribute path (just as -A does with other option)
  (NIX-83).  So you can now say

  $ nix-env -qa -A nixpkgs_unstable.gnome \*
  atk-1.12.4
  esound-0.2.36
  ...

  to see the packages in the "gnome" attribute in Nixpkgs.

  To *print* the attribute path, you should now use "--attr-path" /
  "-P" (running out of letters...).
This commit is contained in:
Eelco Dolstra 2007-09-17 19:24:07 +00:00
parent 055608227f
commit c8ea8a09b8

View file

@ -65,7 +65,7 @@ struct Globals
typedef void (* Operation) (Globals & globals, typedef void (* Operation) (Globals & globals,
Strings opFlags, Strings opArgs); Strings args, Strings opFlags, Strings opArgs);
void printHelp() void printHelp()
@ -74,6 +74,31 @@ void printHelp()
} }
static string needArg(Strings::iterator & i,
Strings & args, const string & arg)
{
if (i == args.end()) throw UsageError(
format("`%1%' requires an argument") % arg);
return *i++;
}
static bool parseInstallSourceOptions(Globals & globals,
Strings::iterator & i, Strings & args, const string & arg)
{
if (arg == "--from-expression" || arg == "-E")
globals.instSource.type = srcNixExprs;
else if (arg == "--from-profile") {
globals.instSource.type = srcProfile;
globals.instSource.profile = needArg(i, args, arg);
}
else if (arg == "--attr" || arg == "-A")
globals.instSource.type = srcAttrPath;
else return false;
return true;
}
static bool isNixExpr(const Path & path) static bool isNixExpr(const Path & path)
{ {
struct stat st; struct stat st;
@ -93,7 +118,7 @@ static void getAllExprs(EvalState & state,
Path path2 = path + "/" + *i; Path path2 = path + "/" + *i;
if (isNixExpr(path2)) if (isNixExpr(path2))
attrs.set(toATerm(*i), makeAttrRHS( attrs.set(toATerm(*i), makeAttrRHS(
parseExprFromFile(state, absPath(path2)), makeNoPos())); parseExprFromFile(state, absPath(path2)), makeNoPos()));
else else
getAllExprs(state, path2, attrs); getAllExprs(state, path2, attrs);
} }
@ -118,9 +143,12 @@ static Expr loadSourceExpr(EvalState & state, const Path & path)
static void loadDerivations(EvalState & state, Path nixExprPath, static void loadDerivations(EvalState & state, Path nixExprPath,
string systemFilter, const ATermMap & autoArgs, DrvInfos & elems) string systemFilter, const ATermMap & autoArgs,
const string & pathPrefix, DrvInfos & elems)
{ {
getDerivations(state, loadSourceExpr(state, nixExprPath), "", autoArgs, elems); getDerivations(state,
findAlongAttrPath(state, pathPrefix, autoArgs, loadSourceExpr(state, nixExprPath)),
pathPrefix, autoArgs, elems);
/* Filter out all derivations not applicable to the current /* Filter out all derivations not applicable to the current
system. */ system. */
@ -394,7 +422,7 @@ static void queryInstSources(EvalState & state,
Nix expression. */ Nix expression. */
DrvInfos allElems; DrvInfos allElems;
loadDerivations(state, instSource.nixExprPath, loadDerivations(state, instSource.nixExprPath,
instSource.systemFilter, instSource.autoArgs, allElems); instSource.systemFilter, instSource.autoArgs, "", allElems);
elems = filterBySelector(state, allElems, args, newestOnly); elems = filterBySelector(state, allElems, args, newestOnly);
@ -570,10 +598,15 @@ static void installDerivations(Globals & globals,
static void opInstall(Globals & globals, static void opInstall(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); string arg = *i++;
if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
else if (arg == "--preserve-installed" || arg == "-P")
globals.preserveInstalled = true;
else throw UsageError(format("unknown flag `%1%'") % arg);
}
installDerivations(globals, opArgs, globals.profile); installDerivations(globals, opArgs, globals.profile);
} }
@ -668,16 +701,18 @@ static void upgradeDerivations(Globals & globals,
static void opUpgrade(Globals & globals, static void opUpgrade(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
UpgradeType upgradeType = utLt; UpgradeType upgradeType = utLt;
for (Strings::iterator i = opFlags.begin(); for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
i != opFlags.end(); ++i) string arg = *i++;
if (*i == "--lt") upgradeType = utLt; if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
else if (*i == "--leq") upgradeType = utLeq; else if (arg == "--lt") upgradeType = utLt;
else if (*i == "--eq") upgradeType = utEq; else if (arg == "--leq") upgradeType = utLeq;
else if (*i == "--always") upgradeType = utAlways; else if (arg == "--eq") upgradeType = utEq;
else throw UsageError(format("unknown flag `%1%'") % *i); else if (arg == "--always") upgradeType = utAlways;
else throw UsageError(format("unknown flag `%1%'") % arg);
}
upgradeDerivations(globals, opArgs, upgradeType); upgradeDerivations(globals, opArgs, upgradeType);
} }
@ -693,7 +728,7 @@ static void setMetaFlag(EvalState & state, DrvInfo & drv,
static void opSetFlag(Globals & globals, static void opSetFlag(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -732,10 +767,13 @@ static void opSetFlag(Globals & globals,
static void opSet(Globals & globals, static void opSet(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); string arg = *i++;
if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
else throw UsageError(format("unknown flag `%1%'") % arg);
}
DrvInfos elems; DrvInfos elems;
queryInstSources(globals.state, globals.instSource, opArgs, elems, true); queryInstSources(globals.state, globals.instSource, opArgs, elems, true);
@ -788,7 +826,7 @@ static void uninstallDerivations(Globals & globals, DrvNames & selectors,
static void opUninstall(Globals & globals, static void opUninstall(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -895,9 +933,11 @@ static string colorString(const string & s)
static void opQuery(Globals & globals, static void opQuery(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
typedef vector< map<string, string> > ResultSet; typedef vector< map<string, string> > ResultSet;
Strings remaining;
string attrPath;
bool printStatus = false; bool printStatus = false;
bool printName = true; bool printName = true;
@ -915,45 +955,46 @@ static void opQuery(Globals & globals,
readOnlyMode = true; /* makes evaluation a bit faster */ readOnlyMode = true; /* makes evaluation a bit faster */
for (Strings::iterator i = opFlags.begin(); for (Strings::iterator i = args.begin(); i != args.end(); ) {
i != opFlags.end(); ++i) string arg = *i++;
if (*i == "--status" || *i == "-s") printStatus = true; if (arg == "--status" || arg == "-s") printStatus = true;
else if (*i == "--no-name") printName = false; else if (arg == "--no-name") printName = false;
else if (*i == "--system") printSystem = true; else if (arg == "--system") printSystem = true;
else if (*i == "--description") printDescription = true; else if (arg == "--description") printDescription = true;
else if (*i == "--compare-versions" || *i == "-c") compareVersions = true; else if (arg == "--compare-versions" || arg == "-c") compareVersions = true;
else if (*i == "--drv-path") printDrvPath = true; else if (arg == "--drv-path") printDrvPath = true;
else if (*i == "--out-path") printOutPath = true; else if (arg == "--out-path") printOutPath = true;
else if (*i == "--meta") printMeta = true; else if (arg == "--meta") printMeta = true;
else if (*i == "--installed") source = sInstalled; else if (arg == "--installed") source = sInstalled;
else if (*i == "--available" || *i == "-a") source = sAvailable; else if (arg == "--available" || arg == "-a") source = sAvailable;
else if (*i == "--prebuilt-only" || *i == "-b") prebuiltOnly = true; else if (arg == "--prebuilt-only" || arg == "-b") prebuiltOnly = true;
else if (*i == "--xml") xmlOutput = true; else if (arg == "--xml") xmlOutput = true;
else throw UsageError(format("unknown flag `%1%'") % *i); else if (arg == "--attr-path" || arg == "-P") printAttrPath = true;
else if (arg == "--attr" || arg == "-A")
if (globals.instSource.type == srcAttrPath) printAttrPath = true; /* hack */ attrPath = needArg(i, args, arg);
else if (arg[0] == '-')
if (opArgs.size() == 0) { throw UsageError(format("unknown flag `%1%'") % arg);
printMsg(lvlInfo, "warning: you probably meant to specify the argument '*' to show all packages"); else remaining.push_back(arg);
} }
if (remaining.size() == 0)
printMsg(lvlInfo, "warning: you probably meant to specify the argument '*' to show all packages");
/* Obtain derivation information from the specified source. */ /* Obtain derivation information from the specified source. */
DrvInfos availElems, installedElems; DrvInfos availElems, installedElems;
if (source == sInstalled || compareVersions || printStatus) { if (source == sInstalled || compareVersions || printStatus)
installedElems = queryInstalled(globals.state, globals.profile); installedElems = queryInstalled(globals.state, globals.profile);
}
if (source == sAvailable || compareVersions) { if (source == sAvailable || compareVersions)
loadDerivations(globals.state, globals.instSource.nixExprPath, loadDerivations(globals.state, globals.instSource.nixExprPath,
globals.instSource.systemFilter, globals.instSource.autoArgs, globals.instSource.systemFilter, globals.instSource.autoArgs,
availElems); attrPath, availElems);
}
DrvInfos elems = filterBySelector(globals.state, DrvInfos elems = filterBySelector(globals.state,
source == sInstalled ? installedElems : availElems, source == sInstalled ? installedElems : availElems,
opArgs, false); remaining, false);
DrvInfos & otherElems(source == sInstalled ? availElems : installedElems); DrvInfos & otherElems(source == sInstalled ? availElems : installedElems);
@ -1113,7 +1154,7 @@ static void opQuery(Globals & globals,
static void opSwitchProfile(Globals & globals, static void opSwitchProfile(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -1161,7 +1202,7 @@ static void switchGeneration(Globals & globals, int dstGen)
static void opSwitchGeneration(Globals & globals, static void opSwitchGeneration(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -1177,7 +1218,7 @@ static void opSwitchGeneration(Globals & globals,
static void opRollback(Globals & globals, static void opRollback(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -1189,7 +1230,7 @@ static void opRollback(Globals & globals,
static void opListGenerations(Globals & globals, static void opListGenerations(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -1222,7 +1263,7 @@ static void deleteGeneration2(const Path & profile, unsigned int gen)
static void opDeleteGenerations(Globals & globals, static void opDeleteGenerations(Globals & globals,
Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
if (opFlags.size() > 0) if (opFlags.size() > 0)
throw UsageError(format("unknown flag `%1%'") % opFlags.front()); throw UsageError(format("unknown flag `%1%'") % opFlags.front());
@ -1260,18 +1301,9 @@ static void opDeleteGenerations(Globals & globals,
} }
static string needArg(Strings::iterator & i,
Strings & args, const string & arg)
{
if (i == args.end()) throw UsageError(
format("`%1%' requires an argument") % arg);
return *i++;
}
void run(Strings args) void run(Strings args)
{ {
Strings opFlags, opArgs; Strings opFlags, opArgs, remaining;
Operation op = 0; Operation op = 0;
Globals globals; Globals globals;
@ -1293,14 +1325,6 @@ void run(Strings args)
if (arg == "--install" || arg == "-i") if (arg == "--install" || arg == "-i")
op = opInstall; op = opInstall;
else if (arg == "--from-expression" || arg == "-E")
globals.instSource.type = srcNixExprs;
else if (arg == "--from-profile") {
globals.instSource.type = srcProfile;
globals.instSource.profile = needArg(i, args, arg);
}
else if (arg == "--attr" || arg == "-A")
globals.instSource.type = srcAttrPath;
else if (parseOptionArg(arg, i, args.end(), else if (parseOptionArg(arg, i, args.end(),
globals.state, globals.instSource.autoArgs)) globals.state, globals.instSource.autoArgs))
; ;
@ -1334,14 +1358,15 @@ void run(Strings args)
printMsg(lvlInfo, "(dry run; not doing anything)"); printMsg(lvlInfo, "(dry run; not doing anything)");
globals.dryRun = true; globals.dryRun = true;
} }
else if (arg == "--preserve-installed" || arg == "-P")
globals.preserveInstalled = true;
else if (arg == "--system-filter") else if (arg == "--system-filter")
globals.instSource.systemFilter = needArg(i, args, arg); globals.instSource.systemFilter = needArg(i, args, arg);
else if (arg[0] == '-') else {
opFlags.push_back(arg); remaining.push_back(arg);
else if (arg[0] == '-')
opArgs.push_back(arg); opFlags.push_back(arg);
else
opArgs.push_back(arg);
}
if (oldOp && oldOp != op) if (oldOp && oldOp != op)
throw UsageError("only one operation may be specified"); throw UsageError("only one operation may be specified");
@ -1358,7 +1383,7 @@ void run(Strings args)
store = openStore(); store = openStore();
op(globals, opFlags, opArgs); op(globals, remaining, opFlags, opArgs);
printEvalStats(globals.state); printEvalStats(globals.state);
} }