Nix is very capable of managing dependencies and creating programming environments. Everything is bundled up as Nix packages. However, when we look for an app in a specific version, it can be difficult to find the right Nix package name.
Once we have the Nix Package Manager installed, there are several ways we can search for a Nix package name:
Nix Package Name vs. Package Name
Before we can search for Nix packages, we have to know the slight difference between the “package name” and the “Nix package name”. We can think of the collection of all Nix packages as a big Json-like object with key-value attribute pairs:
# import <nixpkgs> {}
{
git = { name="git-2.25.4"; .. };
..
jdk = { name="openjdk-8u222-ga"; .. };
jdk11 = { name="openjdk-11.0.6-ga"; .. };
..
nodejs = { name="nodejs-12.21.0"; .. };
nodejs-14_x = { name="nodejs-14.16.0"; .. };
..
}
A key like nodejs
is a “Nix package name” and the value { name="nodejs-12.21.0"; ... };
is a nested Json-like object that represents an actual Nix package. Within each Nix package we have a “package name” like "nodejs-12.21.0"
, which describes the content of that Nix package; what actual app and version is bundled up:
nodejs = { name="nodejs-12.21.0"; .. };
#|-Nix package name-| |-package name-|
#|-/attribute name--|
A Json-like object is called an “attribute set” in the Nix language, because it’s a collection of attributes like nodejs
. That’s why a “Nix package name” is also called an “attribute name”.
More: Learn about attribute sets and the Nix language
Search In Terminal With Nix Shell
Most often the Nix package name is the same as the app that we search, so most often we can use the autocomplete in the nix-shell
terminal app, which comes with the Nix package manager, to find it. For example, if we look for a specific Java JDK we can type
$ nix-shell -p jdk
and press Tab. We then see all Nix package names that begin with this text:
$ nix-shell -p jdk
jdk jdk11 jdk12 ..
Element | Note |
---|---|
$
|
Interactive shell prompt symbol allowing us to type terminal commands. |
nix-shell
|
Terminal app from the Nix package manager. It creates temporary shell environements. ➡ Examples |
-p
|
Flag for nix-shell . Short for --packages . It makes the following Nix packages (=usually apps) available in the resulting shell environment.
|
jdk
|
Nix package name that contains the Java Development Kit apps java , javac etc..
|
Nix shell can only autocomplete on the Nix package name.
If we choose one of those options, say jdk
by pressing Enter, we enter a shell environment where the Java JDK 8 is available:
[nix-shell:~]$ javac -version
javac 1.8.0_222
More: Whole programming environments with Nix shell
Search Packages From Other Ecosystems
The big Nix package (Json-like) attribute set actually contains nested attribute sets that group together packages coming from other ecosystems like Haskell, Python and Javascript:
# import <nixpkgs> {}
{
git = { name="git-2.25.4"; ... };
...
haskellPackages = {
hakyll = { name="hakyll-4.13.0.1"; ... };
...
pandoc = { name="pandoc-2.7.3"; ... };
...
xmonad = { name="xmonad-0.15"; ... };
...
}
...
python3Packages = {
numpy = ...
scipy = ...
};
...
nodePackages = ...
...
javaPackages = ...
...
}
Those nested packages are slightly harder to find with nix-shell
autocomplete. For example, when we search the static site generator Hakyll of the Haskell ecosystem, we have to type at least
$ nix-shell -p haskellPackages.h
haskellPackages.hakyll ..
and Tab to find the desired Hakyll Nix package name.
The reason for this quirk is that in order to improve the autocomplete performance of nix-shell -p
, nested packages are usually hidden; otherwise the more than 14000 packages in haskellPackages
alone would have to be matched and printed when you try to autocomplete h
or haskell
.
More: Learn about the dot notation and the Nix language More: Programming environments with Nix shell
Search With Terminal Nix Search
Another tool to search is the standard nix
terminal app:
$ nix search nodejs
..
* nixpkgs.nodejs (nodejs)
..
* nixpkgs.nodejs-13_x (nodejs)
..
This command has no autcomplete but does a full-text search. In the result list we can find the Nix package name directly after the nixpkgs.
prefix:
* nixpkgs.nodejs-14_x (..)
|-Nix package name-|
Here, we get a hint that this Nix package contains Nodejs in version 13, but to get the precise version number we add the --json
flag to get more information in the output:
$ nix search --json nodejs | jq
..
{
"nixpkgs.nodejs": {
"pkgName": "nodejs",
"version": "12.21.0",
..
},
"nixpkgs.nodejs-14_x": {
"pkgName": "nodejs",
"version": "14.16.0",
..
},
..
}
Here we can see that currently the Nix package nodejs
ships version 12.21
, and nodejs-14_x
contains version 14.16.0
.
We use the handy Json terminal app jq
to format the json output in a readable way. If you don’t have it installed, you can make it available by running:
~$ nix-shell -p jq
Search On The Nix Website
Nix has a convenient webpage to do a full-text search for Nix packages same as with the nix search
terminal command: We again see that the the Nix package nodejs
currently ships with version 12.21.0
.
See: Official Nix Search Webpage.
To search faster for Nix packages we can add a special keyword-bookmark to Firefox with keyword n
and location
https://search.nixos.org/packages?query=%s
Now we can type n nodejs
into the browser a quickly see the Nix package results (%s
in the location link is simply replaced with nodejs
).