Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/nix/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ struct CmdSearch : InstallableCommand, MixJSON
{
std::vector<std::string> res;

bool all = false;

CmdSearch()
{
addFlag({
.longName = "all",
.shortName = 'a',
.description = "Show all packages for a flake.",
.handler = {&all, true},
});

expectArgs("regex", &res);
}

Expand Down Expand Up @@ -55,11 +64,17 @@ struct CmdSearch : InstallableCommand, MixJSON
settings.readOnlyMode = true;
evalSettings.enableImportFromDerivation.setDefault(false);

// Empty search string should match all packages
// Use "^" here instead of ".*" due to differences in resulting highlighting
// (see #1893 -- libc++ claims empty search string is not in POSIX grammar)
if (res.empty())
res.push_back("^");
if (all) {
// Use "^" here instead of ".*" due to differences in resulting highlighting
// (see #1893 -- libc++ claims empty search string is not in POSIX grammar)
if (res.empty())
res.push_back("^");
else
throw Error("'--all' does not expect a search term");
} else {
if (res.empty())
throw Error("search term cannot be empty");
}

std::vector<std::regex> regexes;
regexes.reserve(res.size());
Expand Down
7 changes: 3 additions & 4 deletions src/nix/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ R""(
* Show all packages in the `nixpkgs` flake:

```console
# nix search nixpkgs
# nix search -a nixpkgs
* legacyPackages.x86_64-linux.AMB-plugins (0.8.1)
A set of ambisonics ladspa plugins

Expand Down Expand Up @@ -34,7 +34,7 @@ R""(
* Show all packages in the flake in the current directory:

```console
# nix search
# nix search -a
```

* Search for Firefox or Chromium:
Expand All @@ -56,8 +56,7 @@ flake) for packages whose name or description matches all of the
regular expressions *regex*. For each matching package, It prints the
full attribute name (from the root of the installable), the version
and the `meta.description` field, highlighting the substrings that
were matched by the regular expressions. If no regular expressions are
specified, all packages are shown.
were matched by the regular expressions.

# Flake output attributes

Expand Down
8 changes: 4 additions & 4 deletions tests/search.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ clearCache

## Search expressions

# Check that empty search string matches all
nix search -f search.nix '' |grep -q foo
nix search -f search.nix '' |grep -q bar
nix search -f search.nix '' |grep -q hello
# Check that '--all' flag matches all
nix search -af search.nix |grep -q foo
nix search --all -f search.nix |grep -q bar
nix search -af search.nix |grep -q hello

## Tests for multiple regex/match highlighting

Expand Down