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 to 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.
A local or virtual computer running NixOS
Root access
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.helloAdding 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.
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 Node.js, run:
# nix-env -iA nixos.git nixos.python nixos.zip nixos.file nixos.nodejsSyntax
# nix-env -iA nixos.[package_name&version]For example, to install Python3, run;
# nix-env -iA nixos.python3To 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 --installedOutput
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18
python-3.9.13
zip-3.0Update the NixOS channel first and then upgrade the installed packages.
# nix-channel --updateTo upgrade the packages, run:
# nix-env --upgradeWhere applicable, the command upgrades all packages to 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.pythonTo remove a package, use the syntax:
# nix-env -e [package_name]For example, to remove the zip package, run:
# nix-env -e zipOutput
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 --installedoutput
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 python3If 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, Node.js, and Python.
# nix-env -e git nodejs pythonYou can also remove all the packages by using the wildcard.
#$ nix-env -e "*"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 --rollbackThe 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.13Rolling back the uninstall zip/install python3 operations requires two steps.
# nix-env --rollbackOutput
Switching profile from version 7 to 6The rollback removes Python 3, which is the most recently installed package.
Listing the installed packages returns the output below.
# nix-env --query --installedOutput
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18`Please note that Python 3 is missing from the list.
# nix-env --rollback
output
Switching profile from version 6 to 5The command restores the Zip package removed before installing Python 3.
To confirm the restoration, list the installed packages again.
# nix-env --query --installedOutput
file-5.41
git-2.36.0
hello-2.12
nodejs-16.16.0
python-2.7.18
zip-3.0While 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-generationsOutput
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:25To 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 8Uninstalling 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.
To safely remove the unused generations, packages, and files for all users, use the garbage collection command.
# nix-collect-garbage -dYou cannot roll back configurations or switch to another generation if you perform garbage collection.
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.