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
Published: Last Updated on 5.4K 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.

Working with Nix Channels

Nix channels play an important role in the Nix Package Manager ecosystem.

The Nixpkgs repository contains all the packages, NixOS modules, and expressions. While it's possible to install packages directly from the master branch of Nixpkgs, this approach can be risky because commits are merged into master before thorough testing.

A channel represents the latest "verified" git commits in Nixpkgs. Each channel has its own definition of what "verified" means.

When a new git commit is verified, the corresponding channel gets updated. Channel users benefit from both verified commits and binary packages from the binary cache.

To put this in simple words, a Nix channel is just a URL that points to a place that contains a set of Nix expressions and a manifest.

Types of Channels

There are several channels and they can be broadly categorized into stable and unstable channels, as well as large and small channels:


  • Stable Channels: These provide conservative updates for bug fixes and security vulnerabilities. Major updates are not introduced after the initial release. New stable channels are released every six months.
  • Unstable Channels: Corresponding to the main development branch (unstable) of Nixpkgs, these deliver the latest tested updates on a rolling basis.


  • Large Channels: These provide binary builds for the full breadth of Nixpkgs.
  • Small Channels: Similar to large channels but with fewer binaries. They update faster but require more building from source.

Default Channel

By default, Nixpkgs channel is automatically added to your list of "subscribed" channels when you install Nix.

View Subscribed Channels

The list of subscribed channels is stored in ~/.nix-channels file. You can verify it by looking at its contents.

$ cat ~/.nix-channels
https://nixos.org/channels/nixpkgs-unstable nixpkgs

As you see, Nix has added the nixpkgs channel by default.

Add Channels

You can also manually add a channel to your system as shown below.

$ nix-channel --add https://nixos.org/channels/nixos-23.11

The official channels are listed at https://nixos.org/channels.

Update Channels

After subscribing (adding) a channel, you should update the channel list to obtain the latest available Nix expressions using command:

$ nix-channel --update

This command will update all channels. If you want to update a specific channel, say nixos-23.11, the command would be:

$ nix-channel --update nixos-23.11

List Channels

To print the names and URLs of all subscribed channels on standard output, run:

$ nix-channel --list

Sample Output:

nixpkgs https://nixos.org/channels/nixpkgs-unstable
nixos https://nixos.org/channels/nixos-23.11

As you noticed, now I have added two channels namely nixpkgs-unstable and nixos-23.11. Please note that nixpkgs and nixos are just the aliases to nixpkgs-unstable and nixos-23.11.

Remove Channels

To remove a subscribed channel, for example nixos-23.11, just run:

$ nix-channel --remove nixos-23.11

After removing the channel, update the channel list using command:

$ nix-channel --update

Search/Query Packages

The nix-env --query command provides information about packages in the Nix package manager.


The nix-env --query operation displays information about either:

  • Store paths that are installed in the current generation of the active profile (--installed).
  • Derivations available for installation in the active Nix expression (--available).

It only prints information about derivations whose symbolic name matches one of the specified names.

Flags and Options:

The common flags and options that can be used nix-env --query command are:

  • --installed: Displays information about installed store paths (default behavior).
  • --available or -a: Shows information about derivations available for installation.
  • --status or -s: Prints the status of the derivation (installed, present on the system, substitute available).
  • --attr-path or -P: Prints the attribute path of the derivation (used for unambiguous selection).
  • --no-name: Suppresses printing of the derivation name.
  • --compare-versions or -c: Compares installed versions to available versions.
  • --xml: Prints results in XML format for automated processing.
  • --json: Prints results in JSON format for automated processing.
  • --prebuilt-only or -b: Shows only derivations with registered substitutes (pre-built binaries).
  • --system: Operates on the system-wide profile.
  • --drv-path, --out-path, --description, --meta: Additional information flags.

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

$ nix-env --query --available

Or shortly:

$ nix-env -qa

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

Sample output:

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



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

Query Packages using Regular Expressions

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


$ nix-env -qa chromium-123.0.6312.86


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.

List Installed Packages

To list all installed packages, simply run:

$ nix-env -q

Sample Output:


Since it is a fresh system, our system displays only the nix package manager.

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.

If the gcc package is not installed, you would see:

-PS  gcc-13.2.0

Installing Packages

In NixOS, 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'
these 3 paths will be fetched (1.58 MiB download, 7.24 MiB unpacked):
copying path '/nix/store/4i3ml2pzzgjwas18w31zzhn9f41qyshy-gcc-13.2.0-info' from 'https://cache.nixos.org'...
copying path '/nix/store/smq6f1jz9a5l6l5yjis4s85mq01xww33-gcc-13.2.0-man' from 'https://cache.nixos.org'...
copying path '/nix/store/3cfxjb2nkjkfiv0dq54kkfy5ysjnfs3k-gcc-13.2.0-checksum' from 'https://cache.nixos.org'...
building '/nix/store/xx629d9kbbdnnsyf4ihzhlmalfz6nm3g-user-environment.drv'...

Let us check if gcc is installed or not using command:

$ gcc -v

Sample output:

Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-13.2.0/configure --prefix=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gcc-13.2.0 --with-gmp-include=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gmp-6.3.0-dev/include --with-gmp-lib=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gmp-6.3.0/lib --with-mpfr-include=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-mpfr-4.2.1-dev/include --with-mpfr-lib=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-mpfr-4.2.1/lib --with-mpc=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-libmpc-1.3.1 --with-native-system-header-dir=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-glibc-2.38-44-dev/include --with-build-sysroot=/ --with-gxx-include-dir=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-gcc-13.2.0/include/c++/13.2.0/ --program-prefix= --enable-lto --disable-libstdcxx-pch --without-included-gettext --with-system-zlib --enable-static --enable-languages=c,c++ --disable-multilib --enable-plugin --disable-libcc1 --with-isl=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-isl-0.20 --disable-bootstrap --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=x86_64-unknown-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.2.0 (GCC) 

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

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

If you install another version of gcc, Nix will create a new sub-directory, for example /nix/store/rfhfyuq.........43d8jfuh-gcc-x.x.x, and install the gcc in that directory. This way these two versions of gcc will not conflict with each other.

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.

Create Development Environments with Nix-shell

Nix-shell allows you create customized development environments for each project, complete with all the necessary tools and libraries. It is part of Nix package manager.

To learn how to create reproducible and isolated development environments, refer the following guide:

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

Update NixOS

Please note that the following instructions are specifically for NixOS users.

Update the NixOS Channel

First, update your channel to ensure you have the latest package definitions. You can do this with the following command:

$ nix-channel --update

This command updates all channels you're subscribed to, including the NixOS channel, which contains the system's package and configuration definitions.

Applying the Updates

Once the channel is updated, you apply the updates to your system. This is done by rebuilding your system configuration using the latest packages from the channel. To do so, run:

$ sudo nixos-rebuild switch

The nixos-rebuild switch command rebuilds your system's configuration based on your configuration file (/etc/nixos/configuration.nix), using the latest packages from the channel. After rebuilding, it switches to the new configuration immediately.

Additional Tips:

  • Check for Available Updates: There isn't a direct NixOS command to check for available updates like in some other Linux distributions. However, because updates are based on the channel content, running sudo nix-channel --update ensures you have the latest definitions.
  • Upgrade Specific Packages: If you want to upgrade specific packages managed by nix-env, you can use nix-env -u <package_name> or nix-env -u '*' to update all user-installed packages.

Remember, NixOS updates are generally smooth and well-tested, thanks to the declarative configuration and the reproducibility of Nix. Always ensure your system configuration files are up-to-date and version-controlled for a seamless update experience.

Rollback Packages

This is a fantastic feature of the Nix package manager. Before we are going to see how to rollback packages, let us understand what are Profiles, User environments and Generations.

In Nix, profiles, user environments, and generations are related concepts that enable managing packages and configurations in a safe and reproducible manner.


A profile is a collection of packages and configurations for a specific purpose or user. Profiles allow multiple users to have their customized setups without affecting each other. You can switch between different profiles, effectively managing various environments or sets of applications.

There are two main types of profiles:

  • User Profiles: Each user account has its own profile, located at /nix/var/nix/profiles/per-user/$USER. This profile contains the packages that the user has installed for personal use.
  • System Profile: The system-wide profile is located at /nix/var/nix/profiles/system. It contains packages available globally on the system, typically managed by the system administrator.

User Environments

A user environment is a specific set of packages and their dependencies installed in the Nix store (e.g., /nix/store/3aw2pdyx2jfc...-user-env).

It is created whenever you install, upgrade, or remove packages using nix-env. User environments are essentially packages themselves, stored in the Nix store with a unique cryptographic hash as the directory name.

User environments are linked to user profiles through symlinks, allowing different users to have different configurations without interference.


A generation is a snapshot of your user environment (the set of packages you have installed) at a particular point in time. Whenever you install, upgrade, or remove packages using nix-env, a new generation is created.

Each generation is stored in the /nix/store directory with a unique cryptographic hash name (e.g. /nix/store/3aw2pdyx2jfc...-user-env). There are symlinks in /nix/var/nix/profiles (e.g. default-2-link, default-3-link) that point to these generation directories.

The key advantage of generations is that they allow you to easily roll back or switch between different package configurations using commands like nix-env --rollback and nix-env --switch-generation commands.

To put this in simple words:

  • Profiles are collections of packages and configurations for users or the system.
  • User environments are specific sets of installed packages stored in the Nix store.
  • Generations are snapshots of user environments, created whenever packages are installed, upgraded, or removed, allowing for easy rollbacks and switches between configurations.

This approach promotes system reliability, reproducibility, and the ability to safely experiment with different package configurations without risking the current setup.

How to List Generations

To view the list of available generations run:

$ nix-env --list-generations

This command lists all available generations for your current profile. The output will show the generation numbers, their paths in the Nix store, and indicate which generation is currently active.

Example Output:

   1   2024-04-01 15:06:49   
   2   2024-04-01 15:17:14   
   3   2024-04-01 15:56:46   (current)

Rollback to Previous Generation

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

$ nix-env --rollback

This command reverts your current user environment to the previous generation. For example, if you're currently on generation 3 and run nix-env --rollback, it will switch to generation 2.

Switch Generations

We can switch between different generations using command:

$ nix-env --switch-generations <no>

This command allows you to switch to a specific generation. For example, nix-env --switch-generation 2 will switch your user environment to generation 2, even if that's not the immediately previous generation.

In summary, the generations provide a way to manage and rollback package configurations. The --list-generations shows you what generations are available, nix-env --rollback command is a convenient way to undo your last change, and --switch-generation lets you pick a specific generation.

This generation management approach promotes system reliability by allowing you to easily revert bad changes or test new configurations without affecting your current setup.

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 (Garbage Collection)

In Nix, when you update (-u) or remove (-e) programs using nix-env, the system doesn't actually delete the old versions from the Nix Store (i.e /nix/store/ directory). Instead, it makes a new setup for your profile without links to the removed programs. In other words, only the symlinks will be removed from your profile.

However, because we can't keep adding new programs without running out of disk space, NixOS has a way to clean up unused programs called garbage collection. The garbage collector gets rid of programs that no profiles or their versions (generations) need anymore.

But, if any old version of your setup still needs a program, it won't be removed. This feature is crucial for allowing you to go back to previous setups if needed.

To help the garbage collector do its job, it's a good idea to clear out old versions you don't use anymore. Here's how you can do it:

Delete all old versions except the current one in your profile:

$ nix-env --delete-generations old

Remove specific versions you no longer need, for example, versions 10, 11, and 14:

$ nix-env --delete-generations 10 11 14

Delete versions that are more than 30 days old:

$ nix-env --delete-generations 30d

After cleaning up old versions, run the garbage collector to remove unused programs:

$ nix-store --gc

You can check which files the garbage collector will delete before actually running it:

$ nix-store --gc --print-dead

And to see which files will remain:

$ nix-store --gc --print-live

There's also a handy tool for cleaning, nix-collect-garbage, which can remove all the old versions from every profile at once. Use it with the -d option:

$ nix-collect-garbage -d

This command is an easy way to tidy up your system by getting rid of old versions and then running the garbage collector to clear out unneeded programs.

Example 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

This will actually remove uninstalled packages.

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.

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.


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.


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