forked from lix-project/lix
Merge pull request #9481 from iFreilicht/disallow-nix-search-without-search-terms
nix search: Disallow empty regex
This commit is contained in:
commit
1c260fa6d1
8
doc/manual/rl-next/empty-search-regex.md
Normal file
8
doc/manual/rl-next/empty-search-regex.md
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
synopsis: Disallow empty search regex in `nix search`
|
||||||
|
prs: #9481
|
||||||
|
description: {
|
||||||
|
|
||||||
|
[`nix search`](@docroot@/command-ref/new-cli/nix3-search.md) now requires a search regex to be passed. To show all packages, use `^`.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -67,11 +67,9 @@ struct CmdSearch : InstallableValueCommand, MixJSON
|
||||||
settings.readOnlyMode = true;
|
settings.readOnlyMode = true;
|
||||||
evalSettings.enableImportFromDerivation.setDefault(false);
|
evalSettings.enableImportFromDerivation.setDefault(false);
|
||||||
|
|
||||||
// Empty search string should match all packages
|
// Recommend "^" here instead of ".*" due to differences in resulting highlighting
|
||||||
// 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())
|
if (res.empty())
|
||||||
res.push_back("^");
|
throw UsageError("Must provide at least one regex! To match all packages, use '%s'.", "nix search <installable> ^");
|
||||||
|
|
||||||
std::vector<std::regex> regexes;
|
std::vector<std::regex> regexes;
|
||||||
std::vector<std::regex> excludeRegexes;
|
std::vector<std::regex> excludeRegexes;
|
||||||
|
|
|
@ -5,7 +5,7 @@ R""(
|
||||||
* Show all packages in the `nixpkgs` flake:
|
* Show all packages in the `nixpkgs` flake:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
# nix search nixpkgs
|
# nix search nixpkgs ^
|
||||||
* legacyPackages.x86_64-linux.AMB-plugins (0.8.1)
|
* legacyPackages.x86_64-linux.AMB-plugins (0.8.1)
|
||||||
A set of ambisonics ladspa plugins
|
A set of ambisonics ladspa plugins
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ R""(
|
||||||
* Show all packages in the flake in the current directory:
|
* Show all packages in the flake in the current directory:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
# nix search
|
# nix search . ^
|
||||||
```
|
```
|
||||||
|
|
||||||
* Search for Firefox or Chromium:
|
* Search for Firefox or Chromium:
|
||||||
|
@ -67,8 +67,13 @@ flake or Nix expression, but not a store path or store derivation path) for pack
|
||||||
regular expressions *regex*. For each matching package, It prints the
|
regular expressions *regex*. For each matching package, It prints the
|
||||||
full attribute name (from the root of the [installable](./nix.md#installables)), the version
|
full attribute name (from the root of the [installable](./nix.md#installables)), the version
|
||||||
and the `meta.description` field, highlighting the substrings that
|
and the `meta.description` field, highlighting the substrings that
|
||||||
were matched by the regular expressions. If no regular expressions are
|
were matched by the regular expressions.
|
||||||
specified, all packages are shown.
|
|
||||||
|
To show all packages, use the regular expression `^`. In contrast to `.*`,
|
||||||
|
it avoids highlighting the entire name and description of every package.
|
||||||
|
|
||||||
|
> Note that in this context, `^` is the regex character to match the beginning of a string, *not* the delimiter for
|
||||||
|
> [selecting a derivation output](@docroot@/command-ref/new-cli/nix.md#derivation-output-selection).
|
||||||
|
|
||||||
# Flake output attributes
|
# Flake output attributes
|
||||||
|
|
||||||
|
|
|
@ -17,12 +17,15 @@ clearCache
|
||||||
# Multiple arguments will not exist
|
# Multiple arguments will not exist
|
||||||
(( $(nix search -f search.nix '' hello broken | wc -l) == 0 ))
|
(( $(nix search -f search.nix '' hello broken | wc -l) == 0 ))
|
||||||
|
|
||||||
|
# No regex should return an error
|
||||||
|
(( $(nix search -f search.nix '' | wc -l) == 0 ))
|
||||||
|
|
||||||
## Search expressions
|
## Search expressions
|
||||||
|
|
||||||
# Check that empty search string matches all
|
# Check that empty search string matches all
|
||||||
nix search -f search.nix '' |grepQuiet foo
|
nix search -f search.nix '' ^ | grepQuiet foo
|
||||||
nix search -f search.nix '' |grepQuiet bar
|
nix search -f search.nix '' ^ | grepQuiet bar
|
||||||
nix search -f search.nix '' |grepQuiet hello
|
nix search -f search.nix '' ^ | grepQuiet hello
|
||||||
|
|
||||||
## Tests for multiple regex/match highlighting
|
## Tests for multiple regex/match highlighting
|
||||||
|
|
||||||
|
@ -39,8 +42,8 @@ e=$'\x1b' # grep doesn't support \e, \033 or even \x1b
|
||||||
(( $(nix search -f search.nix '' 'b' | grep -Eo "$e\[32;1mb$e\[(0|0;1)m" | wc -l) == 3 ))
|
(( $(nix search -f search.nix '' 'b' | grep -Eo "$e\[32;1mb$e\[(0|0;1)m" | wc -l) == 3 ))
|
||||||
|
|
||||||
## Tests for --exclude
|
## Tests for --exclude
|
||||||
(( $(nix search -f search.nix -e hello | grep -c hello) == 0 ))
|
(( $(nix search -f search.nix ^ -e hello | grep -c hello) == 0 ))
|
||||||
|
|
||||||
(( $(nix search -f search.nix foo --exclude 'foo|bar' | grep -Ec 'foo|bar') == 0 ))
|
(( $(nix search -f search.nix foo ^ --exclude 'foo|bar' | grep -Ec 'foo|bar') == 0 ))
|
||||||
(( $(nix search -f search.nix foo -e foo --exclude bar | grep -Ec 'foo|bar') == 0 ))
|
(( $(nix search -f search.nix foo ^ -e foo --exclude bar | grep -Ec 'foo|bar') == 0 ))
|
||||||
[[ $(nix search -f search.nix -e bar --json | jq -c 'keys') == '["foo","hello"]' ]]
|
[[ $(nix search -f search.nix '' ^ -e bar --json | jq -c 'keys') == '["foo","hello"]' ]]
|
||||||
|
|
Loading…
Reference in a new issue