Introduction to Package Installation on NixOS
Introduction
NixOS (Nix Operating System) is a Linux distribution with a powerful configuration and package manager. It comprises the Nix package management utility that runs on top of the NixOS operating system.
The main command for installing and managing packages is nix-env
. It allows root and non-root users to install, upgrade and uninstall packages. It also allows users to view installed packages, roll back and switch between different generations.
Packages installed by root users are available to everyone. However, packages installed by non-root users are only available in their profiles and not accessible by others.
This article guides you on how to install, upgrade and remove packages. It also shows you how to switch between different configurations and how to delete unused packages and generations completely.
Prerequisites
- A local or virtual computer running NixOS
- Root access
Step 1. Installing Packages
Log in to your computer as root. If using a virtual computer, connect with SSH as a root user.
To install a package, use the following syntax.
# nix-env -iA [package_location.package_name]
For example, to install the Hello package from the nixos
repository, run
# nix-env -iA nixos.hello
Adding the A
ensures that the nix-env
command evaluates only the attributes you enter, hence a faster and more resource-efficient execution. For most Nixos packages, the default package_location
is nixos
, but there are other locations, such as github
, and so on.
Installing Multiple Packages
You can install multiple packages with one command using the syntax:
# nix-env -iA nixos.[package1] nixos.[package2] nixos.[package3] nixos.[package4]
For example, to install git, zip, file, and nodejs, run:
# nix-env -iA nixos.git nixos.python nixos.zip nixos.file nixos.nodejs
Install Different Versions of a Package
Syntax
# nix-env -iA nixos.[package_name&version]
For example, to install Python3, run;
# nix-env -iA nixos.python3
View Installed Packages and Versions
To view the list of all the installed packages and their versions, run:
# nix-env --query --installed or $ `nix-env --query "*"
For example,
# nix-env --query --installed
Output
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18
python-3.9.13
zip-3.0
Step 2. Updating and Upgrading Existing Packages
Update the NixOS channel first and then upgrade the installed packages.
# nix-channel --update
To upgrade the packages, run:
# nix-env --upgrade
Where applicable, the command upgrades all packages with the newest versions unless specified. To upgrade a specific package, use the syntax:
# nix-env --upgrade -A nixos.[package_name]
For example, to upgrade the python package only, run:
# nix-env --upgrade -A nixos.python
Step 3. Deleting Packages from NixOS
To remove a package, use the syntax:
# nix-env -e [package_name]
For example, to remove the zip package, run:
# nix-env -e zip
Output
uninstalling 'zip-3.0'
building '/nix/store/fac48nwh31h7lwnpw3xsb7ba7zmvb85h-user-environment.drv'...`
The above command removes the specified package. Similarly, you can uninstall a specific version of a package when you have multiple versions installed.
To find out if you have multiple versions of a particular package, run the query command.
# nix-env --query --installed
output
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18
python3-3.8.13`
The output lists the packages and their versions. For example, the above list shows that the system has python versions 2 and 3. To remove version 3, run
# nix-env -e python3
Remove Multiple Packages
If you want to remove multiple selected packages, use the syntax:
#nix-env -e [package1_name] [package2_name] [package3_name]
For example, run the following command to remove git, nodejs, and python.
# nix-env -e git nodejs python
You can also remove all the packages by using the wildcard.
#$ nix-env -e "*"
Step 4. Roll Back Generations
Upgrading a package to a newer version does not overwrite the files for the older version. The process retains the old files and makes them available in case there is a need for a rollback. If the upgrade does not work, you can revert to the older version using the rollback command. Similarly, if you delete a package, you can restore it using the rollback feature.
# nix-env --rollback
The rollback reverses the changes and switches the configuration to an earlier state, provided you have not performed the garbage collection.
For example, if you uninstall zip and install Python3, you can roll back and undo the changes. The following shows the installed packages.
# nix-env --query --installed
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18
python-3.8.13
Rolling back the uninstall zip/install python3 operations requires two steps.
1st rollback
# nix-env --rollback
Output
switching profile from version 7 to 6
The rollback removes python3, which is the most recently installed package.
Listing the installed packages returns the output below.
# nix-env --query --installed
Output
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18`
Please note that python3 is missing from the list.
2nd rollback
# nix-env --rollback
output
switching profile from version 6 to 5
The command restores the Zip package removed before installing Python3.
To confirm the restoration, list the installed packages again.
# nix-env --query --installed
Output
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18
zip-3.0
Switch between Different NixOS Generations
While rollback allows you to change the configuration to older states, you can use the switch
command to move backward or forward to a specific generation. For example, the last rollback switched to generation 5. To move back to generation 8, the most recent, use the command.
You can find the available generations by running:
# nix-env --list-generations
Output
1 2022-08-30 13:29:14
2 2022-08-30 13:29:35
3 2022-08-30 13:32:07
4 2022-08-30 13:32:46
5 2022-08-30 13:35:20 (current)
6 2022-08-30 13:36:35
7 2022-08-30 13:44:12
8 2022-08-30 13:59:25
To switch to a different configuration, use the syntax:
# nix-env --switch-generation [generation_number]
For example, to switch to generation 8, run:
# nix-env --switch-generation 8
Completely Delete Files
Uninstalling a package does not delete the files from storage. It retains the files for other user profiles or a rollback if the newer version malfunctions. The files can occupy a significant part of the storage device, and you may want to completely and safely remove them if you do not foresee the need for a rollback.
Run the garbage collector to completely remove all uninstalled packages, files, and generations not in use by any running application or user profile.
Garbage collection
To safely remove the unused generations, packages, and files for all users, use the garbage collection command.
# nix-collect-garbage -d
You cannot roll back configurations or switch to another generation if you perform garbage collection.
Conclusion
NixOS is a lightweight package manager that allows users to install, uninstall and manage packages with and without root access. To learn more about its features and capabilities, check the NixOS online manual.