OleMussmann / Nix-Package-Search

Cache the nix package list, query and sort by relevance
MIT License
16 stars 1 forks source link

Nix-Package-Search

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 ...

... in configurable individual colors, optionally separated by a newline. Have a look:

Color output of nps neovim

Installation

Try It Out Without Installing

nix run github:OleMussmann/Nix-Package-Search

"Installing" the Cheater Way

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 -- "
};

Declarative Installation

:warning: 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
      ...
  ];

Local Installation

Directly installing in your nix profile is generally discouraged, since it is not declarative.

nix profile install github:OleMussmann/Nix-Package-Search

By Hand

Automate package scanning (optional)

Usage

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)

Configuration

Settings are configured via environment variables. Override them when calling nps, or in your *rc file.

NIX_PACKAGE_SEARCH_FLIP

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"

NIX_PACKAGE_SEARCH_FOLDER

In which folder is the cache located?

value: path

default: "${HOME}/.nix-package-search"

NIX_PACKAGE_SEARCH_CACHE_FILE

Name of the cache file

value: filename

default: "nps.cache"

NIX_PACKAGE_SEARCH_SHOW_PACKAGE_VERSION

Show the PACKAGE_VERSION column

value: "true" | "false"

default: "true"

NIX_PACKAGE_SEARCH_SHOW_PACKAGE_DESCRIPTION

Show the PACKAGE_DESCRIPTION column

value: "true" | "false"

default: "true"

NIX_PACKAGE_SEARCH_EXACT_COLOR

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"

NIX_PACKAGE_SEARCH_DIRECT_COLOR

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"

NIX_PACKAGE_SEARCH_INDIRECT_COLOR

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"

NIX_PACKAGE_SEARCH_COLOR_MODE

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"

NIX_PACKAGE_SEARCH_PRINT_SEPARATOR

Separate matches with a newline?

value: "true" | "false"

default: "true"

Acknowledgements

Bash argument parsing by Robert Siemer.

Contributing

  1. Check existing issues or open a new one to suggest a feature or report a bug
  2. Fork the repository and make your changes
  3. Open a pull request