Cache the nix package list, query and sort by relevance.
Searching for installable packages in NixOS can be painful. nps
to the rescue! Find packages at lightning speed and sort the result by relevance, split by ...
- exact hits, the package matching your exact string
- direct hits, packages that start with the search string
- indirect hits, packages that contain the search string
... in configurable individual colors, optionally separated by a newline. Have a look:
nix run github:OleMussmann/Nix-Package-Search
Add nps = "nix run github:OleMussmann/Nix-Package-Search -- "
to your shell aliases. Don't forget the trailing double-dash. The program might be garbage collected every once in a while and will be automatically downloaded when needed.
programs.bash.shellAliases = { # Replace `bash` with your shell name, if necessary.
nps = "nix run github:OleMussmann/Nix-Package-Search -- "
};
⚠️ The way of installing third-party flakes is highly dependent on your personal configuration. As far as I know there is no standardized, canonical way to do this. Instead, here is a generic approach via overlays. You will need to adapt it to your config files.
Add nps
to your inputs:
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
nps.url = "github:OleMussmann/Nix-Package-Search";
nps.inputs.nixpkgs.follows = "nixpkgs";
};
Add an overlay to your outputs:
outputs = { self, nixpkgs, ... }@inputs:
let
overlays-third-party = final: prev: {
nps = inputs.nps.defaultPackage.${prev.system};
<other third party flakes you have>
};
in {
nixosConfigurations."<hostname>" = nixpkgs.lib.nixosSystem {
system = "<your_system_architecture>";
modules = [
({ config, pkgs, ... }: { nixpkgs.overlays = [ overlays-third-party ]; })
./configuration.nix
];
};
};
Finally, add nps
to your systemPackages
in configuration.nix
:
environment.systemPackages = with pkgs; [
git
nps
...
];
Directly installing in your nix profile
is generally discouraged, since it is not declarative.
nix profile install github:OleMussmann/Nix-Package-Search
- Clone this repository.
- Copy or symlink the
nps
script to a folder in yourPATH
, or include theNix-Package-Search
folder in yourPATH
. - Dependencies:
ripgrep
and GNUgetopt
- Set up a cron job or a systemd timer for
nps -r
at regular intervals. Make sure to do so with your local user environment.
Usage: nps [OPTION]... SEARCH_TERM
Find SEARCH_TERM in available nix packages and sort results by relevance.
List up to three columns, the latter two being optional:
channel.PACKAGE_NAME [PACKAGE_VERSION] [PACKAGE_DESCRIPTION]
Mandatory arguments to long options are mandatory for short options too.
-c, --color=WHEN highlight search matches in color,
--colour=WHEN WHEN=
{always} always emit color codes
never never emit color codes
auto only emit color codes when stdout
is a terminal
-C, --columns=COLUMNS choose columns to show,
COLUMNS=
{all} show all columns
none show only PACKAGE_NAME
version also show PACKAGE_VERSION
description also show PACKAGE_DESCRIPTION
-f, --flip=true|false flip the order of sorting {false}
-h, --help display a short help message and exit
-l, --long-help display a long help message and exit
-r, --refresh refresh package cache
-s, --separator=true|false separate match types with a newline {true}
-v, --version print `nps` version and exit"
The `nps --color=WHEN` option follows the `grep` color option, except that
here the WHEN option is mandatory. Be aware that color codes can trip up
subsequent commands like `grep`, if they occur within a match string.
Matches are sorted by type. Show 'exact' matches first, then 'direct' matches,
and finally 'indirect' matches.
exact channel.SEARCH_TERM
direct channel.SEARCH_TERM-bar
indirect channel.foo-SEARCH_TERM-bar (or match other columns)
nps PACKAGE_NAME
searches the cache file for packages matching thePACKAGE_NAME
search string, see image above.- The cache is created on the first call. Be patient, it might take a while. This is done under the hood by calling
nix-env -qaP
and writing the output to a cache file. Subsequent queries are much faster.
Settings are configured via environment variables. Override them when calling nps
, or in your *rc
file.
Flip the order of matches? By default most relevant matches appear first. Flipping the order makes them appear last and is thus easier to read with long output.
value: "true"
| "false"
default: "false"
In which folder is the cache located?
value: path
default: "${HOME}/.nix-package-search"
Name of the cache file
value: filename
default: "nps.cache"
Show the PACKAGE_VERSION
column
value: "true"
| "false"
default: "true"
Show the PACKAGE_DESCRIPTION
column
value: "true"
| "false"
default: "true"
Color of EXACT matches channel.MATCH
value: "black"
"blue"
"green"
"red"
"cyan"
"magenta"
"yellow"
"white"
for advanced color options, see https://github.com/BurntSushi/ripgrep/blob/master/FAQ.md#how-do-i-configure-ripgreps-colors
default: "purple"
Color of DIRECT matches channel.MATCH-bar
value: "black"
"blue"
"green"
"red"
"cyan"
"magenta"
"yellow"
"white"
for advanced color options, see https://github.com/BurntSushi/ripgrep/blob/master/FAQ.md#how-do-i-configure-ripgreps-colors
default: "blue"
Color of INDIRECT matches channel.foo-MATCH-bar
channel.foo description MATCH more description
value: "black"
"blue"
"green"
"red"
"cyan"
"magenta"
"yellow"
"white"
for advanced color options, see https://github.com/BurntSushi/ripgrep/blob/master/FAQ.md#how-do-i-configure-ripgreps-colors
default: "green"
grep
color mode, show search matches in color
value | effect |
---|---|
never | Never show color |
always | Always show color |
auto | Only show color if stdout is in terminal, suppress if e.g. piped |
default: "auto"
Separate matches with a newline?
value: "true"
| "false"
default: "true"
Bash argument parsing by Robert Siemer.
- Check existing issues or open a new one to suggest a feature or report a bug
- Fork the repository and make your changes
- Open a pull request