* `nix-env -q' now accepts arguments that allow specific derivations

to be queried, e.g., `nix-env -qa firefox'.  This does require the
  argument '*' to be passed if one wants information about all
  derivations, so the old `nix-env -qa' now is `nix-env -qa "*"'.
This commit is contained in:
Eelco Dolstra 2006-03-10 10:24:46 +00:00
parent 9811815429
commit 4ada6db1fc
2 changed files with 49 additions and 45 deletions

View file

@ -217,19 +217,21 @@ static DrvInfos filterBySelector(EvalState & state,
DrvNames selectors = drvNamesFromArgs(args); DrvNames selectors = drvNamesFromArgs(args);
DrvInfos elems; DrvInfos elems;
PathSet done; set<unsigned int> done;
for (DrvNames::iterator i = selectors.begin(); for (DrvNames::iterator i = selectors.begin();
i != selectors.end(); ++i) i != selectors.end(); ++i)
{ {
DrvInfos matches; typedef list<pair<DrvInfo, unsigned int> > Matches;
Matches matches;
unsigned int n = 0;
for (DrvInfos::const_iterator j = allElems.begin(); for (DrvInfos::const_iterator j = allElems.begin();
j != allElems.end(); ++j) j != allElems.end(); ++j, ++n)
{ {
DrvName drvName(j->name); DrvName drvName(j->name);
if (i->matches(drvName)) { if (i->matches(drvName)) {
i->hits++; i->hits++;
matches.push_back(*j); matches.push_back(pair<DrvInfo, unsigned int>(*j, n));
} }
} }
@ -240,40 +242,37 @@ static DrvInfos filterBySelector(EvalState & state,
if (newestOnly) { if (newestOnly) {
/* Map from package names to derivations. */ /* Map from package names to derivations. */
map<string, DrvInfo> newest; typedef map<string, pair<DrvInfo, unsigned int> > Newest;
Newest newest;
StringSet multiple; StringSet multiple;
for (DrvInfos::const_iterator j = matches.begin(); for (Matches::iterator j = matches.begin(); j != matches.end(); ++j) {
j != matches.end(); ++j) DrvName drvName(j->first.name);
{ Newest::iterator k = newest.find(drvName.name);
DrvName drvName(j->name);
map<string, DrvInfo>::iterator k = newest.find(drvName.name);
if (k != newest.end()) { if (k != newest.end()) {
int d = compareVersions(drvName.version, DrvName(k->second.name).version); int d = compareVersions(drvName.version, DrvName(k->second.first.name).version);
if (d > 0) newest[drvName.name] = *j; if (d > 0) newest[drvName.name] = *j;
else if (d == 0) multiple.insert(j->name); else if (d == 0) multiple.insert(j->first.name);
} else } else
newest[drvName.name] = *j; newest[drvName.name] = *j;
} }
matches.clear(); matches.clear();
for (map<string, DrvInfo>::iterator j = newest.begin(); for (Newest::iterator j = newest.begin(); j != newest.end(); ++j) {
j != newest.end(); ++j) { if (multiple.find(j->second.first.name) != multiple.end())
if (multiple.find(j->second.name) != multiple.end())
printMsg(lvlInfo, printMsg(lvlInfo,
format("warning: there are multiple derivations named `%1%'; using the first one") format("warning: there are multiple derivations named `%1%'; using the first one")
% j->second.name); % j->second.first.name);
matches.push_back(j->second); matches.push_back(j->second);
} }
} }
/* Insert only those elements in the final list that we /* Insert only those elements in the final list that we
haven't inserted before. */ haven't inserted before. */
for (DrvInfos::const_iterator j = matches.begin(); for (Matches::iterator j = matches.begin(); j != matches.end(); ++j)
j != matches.end(); ++j) if (done.find(j->second) == done.end()) {
if (done.find(j->queryOutPath(state)) == done.end()) { done.insert(j->second);
done.insert(j->queryOutPath(state)); elems.push_back(j->first);
elems.push_back(*j);
} }
} }
@ -718,7 +717,9 @@ 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 (opArgs.size() != 0) throw UsageError("no arguments expected"); if (opArgs.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. */
@ -733,7 +734,10 @@ static void opQuery(Globals & globals,
globals.instSource.systemFilter, availElems); globals.instSource.systemFilter, availElems);
} }
DrvInfos & elems(source == sInstalled ? installedElems : availElems); DrvInfos elems = filterBySelector(globals.state,
source == sInstalled ? installedElems : availElems,
opArgs, false);
DrvInfos & otherElems(source == sInstalled ? availElems : installedElems); DrvInfos & otherElems(source == sInstalled ? availElems : installedElems);

View file

@ -6,19 +6,19 @@ profiles="$NIX_STATE_DIR"/profiles
test "$($nixenv -p $profiles/test -q | wc -l)" -eq 0 test "$($nixenv -p $profiles/test -q | wc -l)" -eq 0
# Query available: should contain several. # Query available: should contain several.
test "$($nixenv -p $profiles/test -f ./user-envs.nix -qa | wc -l)" -eq 5 test "$($nixenv -p $profiles/test -f ./user-envs.nix -qa '*' | wc -l)" -eq 5
# Install "foo-1.0". # Install "foo-1.0".
$nixenv -p $profiles/test -f ./user-envs.nix -i foo-1.0 $nixenv -p $profiles/test -f ./user-envs.nix -i foo-1.0
# Query installed: should contain foo-1.0 now (which should be # Query installed: should contain foo-1.0 now (which should be
# executable). # executable).
test "$($nixenv -p $profiles/test -q | wc -l)" -eq 1 test "$($nixenv -p $profiles/test -q '*' | wc -l)" -eq 1
$nixenv -p $profiles/test -q | grep -q foo-1.0 $nixenv -p $profiles/test -q '*' | grep -q foo-1.0
test "$($profiles/test/bin/foo)" = "foo-1.0" test "$($profiles/test/bin/foo)" = "foo-1.0"
# Store the path of foo-1.0. # Store the path of foo-1.0.
outPath10=$($nixenv -p $profiles/test -q --out-path --no-name | grep foo-1.0) outPath10=$($nixenv -p $profiles/test -q --out-path --no-name '*' | grep foo-1.0)
echo "foo-1.0 = $outPath10" echo "foo-1.0 = $outPath10"
test -n "$outPath10" test -n "$outPath10"
@ -26,20 +26,20 @@ test -n "$outPath10"
$nixenv -p $profiles/test -f ./user-envs.nix -i foo-2.0pre1 $nixenv -p $profiles/test -f ./user-envs.nix -i foo-2.0pre1
# Query installed: should contain foo-2.0pre1 now. # Query installed: should contain foo-2.0pre1 now.
test "$($nixenv -p $profiles/test -q | wc -l)" -eq 1 test "$($nixenv -p $profiles/test -q '*' | wc -l)" -eq 1
$nixenv -p $profiles/test -q | grep -q foo-2.0pre1 $nixenv -p $profiles/test -q '*' | grep -q foo-2.0pre1
test "$($profiles/test/bin/foo)" = "foo-2.0pre1" test "$($profiles/test/bin/foo)" = "foo-2.0pre1"
# Upgrade "foo": should install foo-2.0. # Upgrade "foo": should install foo-2.0.
$nixenv -p $profiles/test -f ./user-envs.nix -u foo $nixenv -p $profiles/test -f ./user-envs.nix -u foo
# Query installed: should contain foo-2.0 now. # Query installed: should contain foo-2.0 now.
test "$($nixenv -p $profiles/test -q | wc -l)" -eq 1 test "$($nixenv -p $profiles/test -q '*' | wc -l)" -eq 1
$nixenv -p $profiles/test -q | grep -q foo-2.0 $nixenv -p $profiles/test -q '*' | grep -q foo-2.0
test "$($profiles/test/bin/foo)" = "foo-2.0" test "$($profiles/test/bin/foo)" = "foo-2.0"
# Store the path of foo-2.0. # Store the path of foo-2.0.
outPath20=$($nixenv -p $profiles/test -q --out-path --no-name | grep foo-2.0) outPath20=$($nixenv -p $profiles/test -q --out-path --no-name '*' | grep foo-2.0)
test -n "$outPath20" test -n "$outPath20"
# Install bar-0.1, uninstall foo. # Install bar-0.1, uninstall foo.
@ -47,18 +47,18 @@ $nixenv -p $profiles/test -f ./user-envs.nix -i bar-0.1
$nixenv -p $profiles/test -f ./user-envs.nix -e foo $nixenv -p $profiles/test -f ./user-envs.nix -e foo
# Query installed: should only contain bar-0.1 now. # Query installed: should only contain bar-0.1 now.
if $nixenv -p $profiles/test -q | grep -q foo; then false; fi if $nixenv -p $profiles/test -q '*' | grep -q foo; then false; fi
$nixenv -p $profiles/test -q | grep -q bar $nixenv -p $profiles/test -q '*' | grep -q bar
# Rollback: should bring "foo" back. # Rollback: should bring "foo" back.
$nixenv -p $profiles/test --rollback $nixenv -p $profiles/test --rollback
$nixenv -p $profiles/test -q | grep -q foo-2.0 $nixenv -p $profiles/test -q '*' | grep -q foo-2.0
$nixenv -p $profiles/test -q | grep -q bar $nixenv -p $profiles/test -q '*' | grep -q bar
# Rollback again: should remove "bar". # Rollback again: should remove "bar".
$nixenv -p $profiles/test --rollback $nixenv -p $profiles/test --rollback
$nixenv -p $profiles/test -q | grep -q foo-2.0 $nixenv -p $profiles/test -q '*' | grep -q foo-2.0
if $nixenv -p $profiles/test -q | grep -q bar; then false; fi if $nixenv -p $profiles/test -q '*' | grep -q bar; then false; fi
# Count generations. # Count generations.
test "$($nixenv -p $profiles/test --list-generations | wc -l)" -eq 5 test "$($nixenv -p $profiles/test --list-generations | wc -l)" -eq 5
@ -66,7 +66,7 @@ test "$($nixenv -p $profiles/test --list-generations | wc -l)" -eq 5
# Install foo-1.0, now using its store path. # Install foo-1.0, now using its store path.
echo $outPath10 echo $outPath10
$nixenv -p $profiles/test -i "$outPath10" $nixenv -p $profiles/test -i "$outPath10"
$nixenv -p $profiles/test -q | grep -q foo-1.0 $nixenv -p $profiles/test -q '*' | grep -q foo-1.0
# Delete all old generations. # Delete all old generations.
$nixenv -p $profiles/test --delete-generations old $nixenv -p $profiles/test --delete-generations old
@ -79,12 +79,12 @@ if test -e "$outPath20"; then false; fi
# Uninstall everything # Uninstall everything
$nixenv -p $profiles/test -f ./user-envs.nix -e '*' $nixenv -p $profiles/test -f ./user-envs.nix -e '*'
test "$($nixenv -p $profiles/test -q | wc -l)" -eq 0 test "$($nixenv -p $profiles/test -q '*' | wc -l)" -eq 0
# Installing "foo" should only install the newest foo. # Installing "foo" should only install the newest foo.
$nixenv -p $profiles/test -f ./user-envs.nix -i foo $nixenv -p $profiles/test -f ./user-envs.nix -i foo
test "$($nixenv -p $profiles/test -q | grep foo- | wc -l)" -eq 1 test "$($nixenv -p $profiles/test -q '*' | grep foo- | wc -l)" -eq 1
$nixenv -p $profiles/test -q | grep -q foo-2.0 $nixenv -p $profiles/test -q '*' | grep -q foo-2.0
# On the other hand, this should install both (and should fail due to # On the other hand, this should install both (and should fail due to
# a collision). # a collision).
@ -94,6 +94,6 @@ if $nixenv -p $profiles/test -f ./user-envs.nix -i foo-1.0 foo-2.0; then false;
# Installing "*" should install one foo and one bar. # Installing "*" should install one foo and one bar.
$nixenv -p $profiles/test -f ./user-envs.nix -e '*' $nixenv -p $profiles/test -f ./user-envs.nix -e '*'
$nixenv -p $profiles/test -f ./user-envs.nix -i '*' $nixenv -p $profiles/test -f ./user-envs.nix -i '*'
test "$($nixenv -p $profiles/test -q | wc -l)" -eq 2 test "$($nixenv -p $profiles/test -q '*' | wc -l)" -eq 2
$nixenv -p $profiles/test -q | grep -q foo-2.0 $nixenv -p $profiles/test -q '*' | grep -q foo-2.0
$nixenv -p $profiles/test -q | grep -q bar-0.1.1 $nixenv -p $profiles/test -q '*' | grep -q bar-0.1.1