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
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:
- Enter the name of the package in the search bar
- Select the appropriate package (decide from the given description)
- Click on
nix-env
option - And copy the command for
NixOS
(first one)
For example, if I want librewolf
package, I will perform the following:
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
:
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 ๐
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:
- To search for the service, head over to the web page for the Nix package search.
- Select
NixOS options
(3rd option in the top-menu row of the page). - Enter the name of the service you are looking for.
- Copy the name of the service.
For example, here, I’m searching for OpenSSH service:
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;
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 #
:
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:
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]
;
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:
- Open the Nix config file
- Remove or comment out the name of the package
- 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:
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!
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.