diff --git a/src/nix/search.cc b/src/nix/search.cc index 87dc1c0dec8..4b465da6676 100644 --- a/src/nix/search.cc +++ b/src/nix/search.cc @@ -25,8 +25,17 @@ struct CmdSearch : InstallableCommand, MixJSON { std::vector res; + bool all = false; + CmdSearch() { + addFlag({ + .longName = "all", + .shortName = 'a', + .description = "Show all packages for a flake.", + .handler = {&all, true}, + }); + expectArgs("regex", &res); } @@ -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 regexes; regexes.reserve(res.size()); diff --git a/src/nix/search.md b/src/nix/search.md index d182788a6a6..306a27a70d0 100644 --- a/src/nix/search.md +++ b/src/nix/search.md @@ -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 @@ -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: @@ -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 diff --git a/tests/search.sh b/tests/search.sh index 52e12f3818f..073c0093d84 100644 --- a/tests/search.sh +++ b/tests/search.sh @@ -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