Home NixOS Getting Started With Nix Package Manager: A Beginner’s Guide 2024

Getting Started With Nix Package Manager: A Beginner’s Guide 2024

Essential Nix Commands: A Quick Reference Guide for New Users

By sk
4.6K views

This detailed tutorial explains how to do package management operations such as installing, removing, updating, and upgrading packages using Nix package manager in Linux.

We will start this guide with a brief introduction followed by a commonly used Nix commands with examples.

A Brief Introduction to Nix Package Manager

Nix is a package manager that makes installing and managing software easy and reliable on Linux and Unix systems. It solves common problems like software conflicts and makes sure every software install is predictable and safe.

With Nix, you can have different versions of a program on your computer without them interfering with each other. This means you can update, add, or remove software without worrying about breaking anything.

Nix is smart about how it handles software. It keeps track of all the bits and pieces that each program needs to run. This way, when you install a program, it doesn't mess up another program. If something goes wrong with a new software update, Nix lets you go back to the way things were before, easily.

The Nix package manager is not just for individual users but also great for developers and people managing servers. It helps keep everyone on the same page by making sure they all use the same software setup. This reduces surprises when moving software from one computer to another.

Nix has a big collection of software to choose from, and it's all maintained by a community of users. It is a solid choice for managing software whether you're working alone or with a team.

For more details about Nix, refer our previous guide:

Getting Started with Nix Package Manager

Make sure you have installed Nix package manager. If you haven't installed Nix yet, please refer the Nix installation guide given below.

Once Nix package manager is setup, you can start using it to install and manage your applications right away.

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:

$ nix-channel --update

Sample output:

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:

$ nix-channel --update

To remove a channel, just run:

$ nix-channel --remove nixpkgs

Search/Query Available Packages in a Channel

To view the list of available packages in the subscribed channel, run:

$ nix-env --query --available

Or shortly:

$ nix-env -qa

Here, The options -q indicates the query operation and -a indicates all available (i.e., installable) packages.

Sample output:

0ad-0.0.26
0ad-data-0.0.26
0verkill-unstable-2011-01-13
0x-unstable-2022-07-11
0xproto-1.603
0xtools-1.2.4
1oom-1.0
1password-8.10.27
1password-8.10.28-21.BETA
1password-cli-2.26.1
2048-cli-unstable-2019-12-10
2048-cli-unstable-2019-12-10
2048-in-terminal-unstable-2022-06-13
20kly-1.5.0
2bwm-0.3
2fa-1.2.0
3270font-3.0.1
389-ds-base-2.4.3
3dpong-0.5
3llo-1.3.1
3mux-1.1.0
3proxy-0.9.4
4th-3.64.1
[...]
List Available Packages using Nix Package Manager
List Available Packages using Nix Package Manager

If you want to query a particular package, the command would be:

$ nix-env -qa firefox

Output:

firefox-115.9.1esr
firefox-124.0.1
firefox-124.0.1
firefox-124.0.1
firefox-125.0b3
firefox-125.0b3

You can also use the following command to search for a particular package.

$ nix-env -qaP | grep python3-3

Sample output:

[...]
nixpkgs.python312                                                                                 python3-3.12.2
nixpkgs.python313                                                                                 python3-3.13.0a5
nixpkgs.python313Full                                                                             python3-3.13.0a5
nixpkgs.python39                                                                                  python3-3.9.19
nixpkgs.python39Full                                                                              python3-3.9.19

List Installed Packages

To list all installed packages, simply run:

$ nix-env -q

It is also possible to see the status of the available package. Look at the below example.

$ nix-env -qas gcc

Sample output:

IPS gcc-13.2.0

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.

chromium

Matches the package name chromium and any version.

chromium-123.0.6312.86

Matches the package name chromium and version 123.0.6312.86.

Example:

$ nix-env -qa chromium-123.0.6312.86

gtk\\+

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.

'.*zip.*'

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.

'.*(firefox|chromium).*'

Matches any package name containing the strings firefox or chromium.

Installing Packages

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

Or (shortly),

$ nix-env -i gcc

The above command will install the latest available gcc package.

Sample output of the above command would be:

installing ‘gcc-13.2.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-13.2.0
 /nix/store/f05gsfcilsczwic8aga0cgl0sygbw5lc-zlib-1.2.11
 /nix/store/f111ij1fc83965m48bf2zqgiaq88fqv5-glibc-2.25
 /nix/store/g25gnfmd8i392ahiip9nwfv80szqbkcs-gcc-13.2.0-man
 /nix/store/n5k6yqf81jp4qbay2czaqciimhxikcq8-linux-headers-4.4.10
 /nix/store/vrr9maj9lqj2xwndlx3kh07vhnc111i2-glibc-2.25-dev
 /nix/store/xfrkm34sk0a13ha9bpki61l2k5g1v8dh-gcc-13.2.0-lib
 /nix/store/zx0i63k1qswsfjj3kxhwk1vqa9i5ys8i-gcc-13.2.0-info
fetching path ‘/nix/store/zx0i63k1qswsfjj3kxhwk1vqa9i5ys8i-gcc-13.2.0-info’...

[...]

fetching path ‘/nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-13.2.0’...

*** Downloading ‘https://cache.nixos.org/nar/00y98z7i8a1a8d3nkv4dbbfbwwvznccbvmjdhaay0gqc0gfgb020.nar.xz’ to ‘/nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-13.2.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:

$ gcc -v

Sample output:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-13.2.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 13.2.0 (GCC)

As you noticed in the above output, the gcc has been in a sub-directory named dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-13.2.0 under /nix/store.

Here, we call the directory dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-13.2.0 as an unique identifier. This subdirectory holds all required dependencies and libraries of gcc package.

Test Packages without Installing using Nix

Another notable feature of Nix package manager is that you can test a package without installing it on your system. Refer the following guide to know how to test a package without installing it.

Upgrading Packages

To upgrade a package to the next available version, just run:

$ nix-env --upgrade vim

Or, just:

$ 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:

$ nix-env -u

Rollback Packages

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:

$ nix-env --rollback

Uninstalling Packages

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

Sample output:

uninstalling ‘vim-9.1.0148’
uninstalling ‘gcc-13.2.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:

$ nix-collect-garbage -d

Sample output:

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.

Nix Commands Cheatsheet

Here's a neat cheatsheet for the Nix package manager commands. Print it and keep it near your desk for easy reference.

ActionCommandExample
Install Packagenix-env -i <package-name>nix-env -i firefox
Uninstall Packagenix-env -e <package-name>nix-env -e firefox
Search for Packagenix-env -qaP <pattern>nix-env -qaP python
Update Packagenix-env -u <package-name>nix-env -u firefox
List Installed Packagesnix-env -qnix-env -q
Roll Back Changesnix-env --rollbacknix-env --rollback
Garbage Collectionnix-collect-garbage -dnix-collect-garbage -d
Nix Commands Cheatsheet

This cheatsheet covers the basic commands you'll use to manage packages with Nix, offering a straightforward guide to getting started and maintaining your system.

Frequently Asked Questions

Here's most frequently asked questions (FAQ) about Nix package manager.

Q: What is Nix?

A: Nix is a powerful package manager for Linux and Unix systems that focuses on reproducibility, reliability, and portability. It allows users to install, manage, and switch between different versions of software and their dependencies easily.

Q: How do I install a package using Nix?

A: To install a package, use the nix-env -i command followed by the package name. For example, nix-env -i firefox installs the Firefox web browser.

Q: How can I uninstall a package?

A: To uninstall a package, use the nix-env -e command followed by the package name. For example, nix-env -e thunderbird uninstalls the Thunderbird email client.

Q: How do I search for available packages?

A: To search for a package, use nix-env -qaP <pattern>. For example, to find packages related to Python, you can use nix-env -qaP python.

Q: Can I update a specific package?

A: Yes, to update a specific package, run nix-env -u <package-name>. To update all installed packages, simply use nix-env -u.

Q: How do I see what packages I have installed?

A: To list all installed packages, use the command nix-env -q.

Q: How can I list all available packages from a channel?

A: To list all available packages from a channel, run nix-env -qa.

Q: How do I revert to a previous state or "roll back" changes?

A: Nix keeps track of changes in "generations." To roll back to the previous generation, use nix-env --rollback. You can list available generations with nix-env --list-generations.

Q: How do I keep my Nix system clean?

A: Use nix-collect-garbage -d to remove unused packages and free up space. This command cleans up packages not referenced by any profile.

Conclusion

The Nix package manager offers a robust and flexible way to manage software packages on Linux and Unix systems.

Its approach to handling packages ensures that you can install, update, and switch between different versions of software without conflicts or dependencies issues.

Although Nix may have a bit of a learning curve, especially for those new to its concepts, the benefits of precise, reproducible package management are significant.

Whether you're a developer, system administrator, or just a curious user, taking the time to learn Nix can greatly enhance your control over software environments and streamline your workflow.

That's all for now. I hope you've gotten a clear idea of the basic usage of the Nix package manager. What we've covered here should be enough to get you started with Nix.

Of course, there are so many commands. To learn more Nix commands, I strongly recommend you to refer the Nix official manual given below.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More