NixOS Series #3: Install and Remove Packages in NixOS

The packaging system in NixOS is its strongest point. The Nix package manager uses a vastly different syntax than apt, dnf and other package managers.

In this guide, I will share two ways to install and remove packages on NixOS:

  • Using the Nix package manager
  • Using configuration.nix config file
โš ๏ธ
Using the Nix package manager, you can only install packages but not services like SSH or Plex server. For the installation of services, you’d have to use a Nix configuration file.

To install any package, it is necessary to know its exact name and for that purpose, I will start with how you can search for packages in NixOS.

Search packages

To look for packages, you can use its web search using your preferred browser.

You can utilize its web search using the given steps:

  1. Enter the name of the package in the search bar
  2. Select the appropriate package (decide from the given description)
  3. Click on nix-env option
  4. And copy the command for NixOS (first one)

For example, if I want librewolf package, I will perform the following:

NixOS Series #3: Install and Remove Packages in NixOS
Searching for the required package (Click to expand the image)

You can do the same through the terminal.

To search packages using the terminal, you can follow the given command syntax:

nix-env -qaP --description [package_name]

For example, here, I searched for the librewolf:

NixOS Series #3: Install and Remove Packages in NixOS

You will have to copy the first line of the output as that is the name for the package you need to install.

For me, it was nixos.librewolf.

Yes, it may not sound as convenient as the package names when using APT or DNF. But, it is not too bad, I think.

Some compromises for some benefits, I guess?

Suggested Read ๐Ÿ“–

How to Install RPM Files on Fedora Linux [Beginnerโ€™s Tutorial]
This beginner article explains how to install RPM packages on Fedora and Red Hat Linux. It also shows you how to remove those RPM packages afterwards. When you start using Fedora Linux in the Red Hat domain, sooner or later, youโ€™ll come across .rpm files. Like .exe files in
NixOS Series #3: Install and Remove Packages in NixOS

Install a package in NixOS

To install a package, all you have to do is use the following command syntax:

nix-env -iA [package_name]

And if you use the web search to look for the package, you will already have the exact command you need for the installation.

So let’s say I want to install librewolf, so I will be using the following command:

nix-env -iA nixos.librewolf

And if you want to perform a system-wide install (make this package available for every user), execute the installation command with sudo:

sudo nix-env -iA nixos.librewolf

That’s it! You will have your favorite package installed in no time.

Uninstall a Package in NixOS

To remove a package, you can refer to the given command syntax:

nix-env --uninstall [package_name]

So if I have to remove the librewolf package, I have to use the following command:

nix-env --uninstall librewolf

If you notice closely, I have used librewolf instead of nixos.librewolf what I used for the installation.

This means you will have to skip the nixos part during removal of the package, which makes things easy and quick.

Install services in NixOS

As I mentioned earlier, you can not use the nix package manager to install services like OpenSSH, Plex server, Flatpak, etc.

From searching for the service to the installation process, it differs from what you saw above.

So let me start with how you can search for a service:

  1. To search for the service, head over to the web page for the Nix package search.
  2. Select NixOS options (3rd option in the top-menu row of the page).
  3. Enter the name of the service you are looking for.
  4. Copy the name of the service.

For example, here, I’m searching for OpenSSH service:

NixOS Series #3: Install and Remove Packages in NixOS
Searching for a service (Click to enlarge the image)

Once you have the name, open the configuration.nix file using the following command:

sudo nano /etc/nixos/configuration.nix

And add the name of the service at the end of the line (before }) in the following manner:

[service_name] = true;

As I want to enable OpenSSH, I will be adding the following:

services.openssh.enable = true;
NixOS Series #3: Install and Remove Packages in NixOS

Once you are done adding the service to the config file, save the changes and exit from the nano text editor.

To enable the service, rebuild the config file and switch to the changes using the following command:

sudo nixos-rebuild switch

That’s it! You have the service enabled.

Uninstall services from NixOS

To uninstall a service, all you have to do is remove or comment out the line for that service from configuration.nix file.

So first, open the config file using the following command:

sudo nano /etc/nixos/configuration.nix

Look for the service and remove the line or comment it out with #:

NixOS Series #3: Install and Remove Packages in NixOS

With the added comment #, I am ignoring the OpenSSH service to load up as I no longer want it on my system.

Once done, save the change and exit from the text editor.

And finally, rebuild the config file and make the switch:

sudo nixos-rebuild switch

Install packages using Nix config file

The configuration file lets you easily manage packages in one go.

To install a package using the Nix config file, you have to enter the package’s name in the config file, rebuild, and switch to the config file, and that’s it.

First, open the configuration.nix file:

sudo nano /etc/nixos/configuration.nix

If you want to install a package for a specific logged-in user, add the package’s name to the user’s profile.

The user profile looks like this:

users.users.sagar = {
    isNormalUser = true;
    description = "Sagar";
    extraGroups = [ "networkmanager" "wheel" ];
    packages = with pkgs; [
      firefox
    ];
  };

Sure, it will show your username instead of sagar.

And you are supposed to add the name of the package using the syntax ย packages = with pkgs; [package_name];

So let’s suppose I want to install Thunderbird as well, then I will add its name as shown below:

NixOS Series #3: Install and Remove Packages in NixOS

You must add all the package names inside the square bracket without commas. It has to be on a new line as the screenshot describes.

But if you want to install this package system-wide, then you will have to add the package name under environment.systemPackages like:

environment.systemPackages = with pkgs; [package_name];

NixOS Series #3: Install and Remove Packages in NixOS

Once you are done adding the name of the required package in the system profile or user profile, or even both, you will have to follow the same command to complete the installation:

sudo nixos-rebuild switch

And you have it!

Remove packages using the Nix config file

To remove the package, all you have to do is follow the given simple steps:

  1. Open the Nix config file
  2. Remove or comment out the name of the package
  3. Rebuild the config and make a switch

So let’s start with the first step (opening the config file):

sudo nano /etc/nixos/configuration.nix

Next, comment out the name of the packet from the user profile or system profile:

NixOS Series #3: Install and Remove Packages in NixOS

Save changes and exit from the config file.

And finally, rebuild the config and make a switch to remove the package:

sudo nixos-rebuild switch

That’s it!

๐Ÿ“‹
Currently, there are no official GUI tools to help you with installing/removing packages. You might find some projects like nix-gui and nix42b developed by the community, but they are no longer maintained or simply in their early stages of development.

Next Up…

I hope you enjoy reading the NixOS series as much as I do writing it.

In the next part, I will highlight some important things you need to do right after installing NixOS.

๐Ÿ’ฌ If you think I’m missing out on something or have any other suggestions, please let me know in the comments.

Leave a Comment