Getting Started With Nix Package Manager
This tutorial explains how to do package management operations such as installing, removing, updating, and upgrading packages using Nix package manager. If you haven’t installed Nix package manager yet, refer our previous guide “Nix – A Powerful Package Manager For Linux And Unix”. As you might already know, you don’t need to be a root or sudo user to do all package management operations using Nix.
Getting Started With Nix Package Manager
Nix has many commands. The main command for the package management is nix-env. This command is used to list, install, update, rollback, remove, query packages. Let us see some commands with examples.
Updating Nix channels
A Nix channel is just a URL that points to a place that contains a set of Nix expressions and a manifest. By default, Nixpkgs channel is automatically added to your list of “subscribed” channels when you install Nix.
To stay up to date with Nix channel, run the following command:
downloading Nix expressions from ‘https://d3g5gsiof5omrk.cloudfront.net/nixpkgs/nixpkgs-17.09pre108299.ec9a23332f/nixexprs.tar.xz’... downloading ‘https://d3g5gsiof5omrk.cloudfront.net/nixpkgs/nixpkgs-17.09pre108299.ec9a23332f/nixexprs.tar.xz’... [8335/8656 KiB, 206.5 KiB/s] unpacking channels...
You can also manually add a channel to your system as shown below.
nix-channel --add https://nixos.org/channels/nixpkgs-unstable
After subscribing(adding) a channel, do not forget to update the channel to obtain the latest available Nix expressions using command:
To remove a channel, just run:
nix-channel --remove nixpkgs
Search/query available packages
To view the list of available packages in the subscribed channel, run:
Sample output of the above command would be:
2048-in-terminal-2015-01-15 2bwm-0.2 389-ds-base-126.96.36.199 3dpong-0.5 4store-1.1.6 8086tiny-1.25 90secondportraits-1.01b 915resolution-0.5.3 9pfs a2jmidid-8 a2ps-4.14 a52dec-0.7.4p4 aacgain-1.9.0 aacskeys-0.4.0e aalib-1.4rc5 abc-verifier-20160818 abcde-2.7.2 abcl-1.4.0 [...]
Here, “-q” indicates the query operation. “-a” indicates all available (i.e., installable) packages.
You can query a particular package, as shown below.
nix-env -qa chromium
Also, you can use the following command to search for a particular package.
nix-env -qaP | grep python3-3
nixpkgs.python33 python3-3.3.6 nixpkgs.python33Full python3-3.3.6 nixpkgs.python34 python3-3.4.6 nixpkgs.python34Full python3-3.4.6 nixpkgs.python35 python3-3.5.3 nixpkgs.python35Full python3-3.5.3 nixpkgs.python3Full python3-3.6.1 nixpkgs.python36Full python3-3.6.1 nixpkgs.python3 python3-3.6.1
To list all installed packages, simply run:
It is also possible to see the status of the available package. Look at the below example.
nix-env -qas gcc
Here, “I” indicates that the specified package is installed, “P” indicates the package is present on our system, and “S” indicates that whether there is a so-called substitute for the package.
You can query/list packages using regular expressions. Here are some examples of regular expressions.
Matches the package name chromium and any version.
Matches the package name chromium and version 58.0.
nix-env -qa chromium-58.0
Matches the package name gtk+. The + character must be escaped using a backslash to prevent it from being interpreted as a quantifier, and the backslash must be escaped in turn with another backslash to ensure that the shell passes it on.
Matches any package name. This is the default for most commands.
Matches any package name containing the string zip. Note the dots: ‘*zip*’ does not work, because in a regular expression, the character * is interpreted as a quantifier.
Matches any package name containing the strings firefox or chromium.
As I mentioned in the previous tutorial, all packages will be stored in Nix Store, usually the directory /nix/store.
To install a package, just run:
nix-env --install gcc
nix-env -i gcc
The above command will install the latest available gcc package.
Sample output of the above command would be:
installing ‘gcc-5.4.0’ download-from-binary-cache.pl: still waiting for ‘https://cache.nixos.org/zx0i63k1qswsfjj3kxhwk1vqa9i5ys8i.narinfo’ after 5 seconds... download-from-binary-cache.pl: still waiting for ‘https://cache.nixos.org/dyj2k6ch35r1ips4vr97md2i0yvl4r5c.narinfo’ after 5 seconds... download-from-binary-cache.pl: still waiting for ‘https://cache.nixos.org/g25gnfmd8i392ahiip9nwfv80szqbkcs.narinfo’ after 5 seconds... these paths will be fetched (38.94 MiB download, 150.45 MiB unpacked): /nix/store/7n45x3waczv1smsdkax2dy4j2zhbfk82-glibc-2.25-bin /nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0 /nix/store/f05gsfcilsczwic8aga0cgl0sygbw5lc-zlib-1.2.11 /nix/store/f111ij1fc83965m48bf2zqgiaq88fqv5-glibc-2.25 /nix/store/g25gnfmd8i392ahiip9nwfv80szqbkcs-gcc-5.4.0-man /nix/store/n5k6yqf81jp4qbay2czaqciimhxikcq8-linux-headers-4.4.10 /nix/store/vrr9maj9lqj2xwndlx3kh07vhnc111i2-glibc-2.25-dev /nix/store/xfrkm34sk0a13ha9bpki61l2k5g1v8dh-gcc-5.4.0-lib /nix/store/zx0i63k1qswsfjj3kxhwk1vqa9i5ys8i-gcc-5.4.0-info fetching path ‘/nix/store/zx0i63k1qswsfjj3kxhwk1vqa9i5ys8i-gcc-5.4.0-info’... [...] fetching path ‘/nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0’... *** Downloading ‘https://cache.nixos.org/nar/00y98z7i8a1a8d3nkv4dbbfbwwvznccbvmjdhaay0gqc0gfgb020.nar.xz’ to ‘/nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0’... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 29.5M 100 29.5M 0 0 373k 0 0:01:21 0:01:21 --:--:-- 365k building path(s) ‘/nix/store/nxx0bfg3n685fl0l3m2gbjqannh0wb9i-user-environment’ created 78 symlinks in user environment
Let us check if gcc is installed or not using command:
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: Thread model: posix gcc version 5.4.0 (GCC)
As you noticed in the above output, the gcc has been in a sub-directory named “dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0” under /nix/store. Here, we call dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0 as unique identifier. This subdirectory holds all required dependencies and libraries of gcc package.
Test packages without installing
Another notable feature of Nix package manager is that you can test a package without installing it on your system.
nix-shell -p gcc
This command builds or downloads gcc package and its dependencies, then drops you into a Bash shell where the gcc command is present, all without affecting your normal environment.
Once you done with testing, you can go back by simply typing exit.
[nix-shell:~]$ exit exit
To upgrade a package to the next available version, just run:
nix-env --upgrade vim
nix-env -u vim
The above command will only upgrade vim package if there is a “newer” version.
Also, you can upgrade all installed packages at once by running the following command:
You installed a package, but it is not useful or not working properly. You need the lower version of the same package. What will you do? Simple. Just rollback to the previous working version using the following command:
To uninstall a package, run:
nix-env -e gcc
The above command will uninstall the package called gcc from your system.
You can remove multiple packages as shown below.
nix-env -e gcc vim
uninstalling ‘vim-8.0.0442’ uninstalling ‘gcc-5.4.0’ building path(s) ‘/nix/store/mxpikbq3l08379h8ik8mrj3fcw6mh6y4-user-environment’ created 6 symlinks in user environment
Removing unused packages
When a package is uninstalled, it’s not removed from Nix Store (i.e /nix/store/ directory). Only, the symlinks will removed from your profile.
To actually remove uninstalled packages, run:
finding garbage collector roots... deleting garbage... deleting ‘/nix/store/s4jr4dc9gghldr3xza23rw0gm9kp21kl-nix-prefetch-scripts.drv’ deleting ‘/nix/store/km2gyzlvs9vkrr52wxfyhinv4r52ksrj-nix-prefetch-bzr.drv’ deleting ‘/nix/store/7mi73sdc1p349vmpb5nyxsrv8ayk5hly-bazaar-2.7.0.drv’ [...] deleting ‘/nix/store/8ckmcs9hx1qm0yxdnv892vrvx49zm1sq-setup-hook-2.0.sh’ deleting ‘/nix/store/trash’ deleting unused links... note: currently hard linking saves -0.00 MiB 1447 store paths deleted, 12.65 MiB freed
You should run this command periodically to get rid of unused packages from your system.
And, that’s all for now. I hope you have an idea of basic usage of Nix package manager. What I have covered here are just enough to get started with Nix package manager. Of course, there are many commands. To learn more Nix commands, refer the Nix manual given at end of this guide.
If you find this guide useful, please share them on your social, professional networks and support OSTechNix. More good stuffs to come. Staty tuned!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!