Home NixOS How To Install Nix Package Manager In Linux

How To Install Nix Package Manager In Linux

Nix Package Manager Installation Guide: Effortless Software Management for Linux

By sk
Published: Updated: 2.3K views

Nix is a powerful package manager for Linux that offers features like reliable installations and easy rollbacks. In this Step-by-Step tutorial, we will learn how to install Nix Package Manager in Linux.

With Nix, you can effortlessly manage software installations, dependencies, and configurations. Its declarative approach ensures stability and reproducibility, making it a suitable choice for developers and system administrators.

Nix ensures that packages are isolated from each other, reducing conflicts and making it easier to manage dependencies.

You can install Nix on most Linux distributions to manage your software alongside your existing system's package manager.

This guide will show you the straightforward steps to get Nix up and running on your Linux machine.

For the demonstration purpose, I am going to show you the steps to install Nix in a Ubuntu system. Please note that the following installation steps are same for all Linux distributions.

Let us get started!

Install Nix Package Manager

Installing the Nix package manager on Linux is a simple and straightforward process.

Nix developers recommend multi-user installation if your platform supports.

1. First, download and run the Nix installation script from their website.

$ curl -L https://nixos.org/nix/install | sh

This will run the default installation type for your platform.

  1. Multi-user installation - Linux with systemd and without SELinux and macOS.
  2. Single-user installation - Linux without systemd and with SELinux.

Based on your platform type, one of the installations will start. Since my system is Ubuntu 22.04 LTS, the installer automatically switched to the multi-user installation type.

You can also explicitly specify the installation type as well. For example, to explicitly instruct the installer to perform a single-user installation on your system, run:

$ curl -L https://nixos.org/nix/install | sh -s -- --no-daemon

To explicitly instruct the installer to perform a multi-user installation on your system:

$ curl -L https://nixos.org/nix/install | sh -s -- --daemon

Keep an eye on your screen and follow the instructions on your screen to proceed further.

2. If multi-user installation is started, you will see the following output. If you'd like to see the detailed list of what Nix will do on your system, type y and hit ENTER key. If you don't want to view the list, simply type n and press ENTER.

Sample Output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:05 --:--:--     0
100  4052  100  4052    0     0    637      0  0:00:06  0:00:06 --:--:-- 16273
downloading Nix 2.21.0 binary tarball for x86_64-linux from 'https://releases.nixos.org/nix/nix-2.21.0/nix-2.21.0-x86_64-linux.tar.xz' to '/tmp/nix-binary-tarball-unpack.SpybifSJck'...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21.8M  100 21.8M    0     0  1998k      0  0:00:11  0:00:11 --:--:-- 1746k
Note: a multi-user installation is possible. See https://nixos.org/manual/nix/stable/installation/installing-binary.html#multi-user-installation
Switching to the Multi-user Installer
Welcome to the Multi-User Nix Installation

This installation tool will set up your computer with the Nix package
manager. This will happen in a few stages:

1. Make sure your computer doesn't already have Nix. If it does, I
   will show you instructions on how to clean up your old install.

2. Show you what I am going to install and where. Then I will ask
   if you are ready to continue.

3. Create the system users (uids [30001..30032]) and groups (gid 30000)
   that the Nix daemon uses to run builds. To create system users
   in a different range, exit and run this tool again with
   NIX_FIRST_BUILD_UID set.

4. Perform the basic installation of the Nix files daemon.

5. Configure your shell to import special Nix Profile files, so you
   can use Nix.

6. Start the Nix daemon.

Would you like to see a more detailed list of what I will do?
[y/n]
Welcome to the Multi-User Nix Installation
Welcome to the Multi-User Nix Installation

3. I entered y to see what the installer would do on my system. The installer script provided the output below. If you're okay with the changes it plans to make, simply type y to continue.

I will:

 - make sure your computer doesn't already have Nix files
   (if it does, I will tell you how to clean them up.)
 - create local users (see the list above for the users I'll make)
 - create a local group (nixbld)
 - install Nix in to /nix
 - create a configuration file in /etc/nix
 - set up the "default profile" by creating some Nix-related files in
   /root
 - back up /etc/bash.bashrc to /etc/bash.bashrc.backup-before-nix
 - update /etc/bash.bashrc to include some Nix configuration
 - back up /etc/zsh/zshrc to /etc/zsh/zshrc.backup-before-nix
 - update /etc/zsh/zshrc to include some Nix configuration
 - load and start a service (at /etc/systemd/system/nix-daemon.service
   and /etc/systemd/system/nix-daemon.socket) for nix-daemon

Ready to continue?
[y/n]
Nix System Changes
Nix System Changes

4. Now the installer will prompt if it can use sudo . Type y and hit ENTER key to allow it to use sudo and continue.

---- let's talk about sudo -----------------------------------------------------
This script is going to call sudo a lot. Every time I do, it'll
output exactly what it'll do, and why.

Just like this:

---- sudo execution ------------------------------------------------------------
I am executing:

    $ sudo echo

to demonstrate how our sudo prompts look


This might look scary, but everything can be undone by running just a
few commands. I used to ask you to confirm each time sudo ran, but it
was too many times. Instead, I'll just ask you this one time:

Can I use sudo?
[y/n]
Allow Nix to Use Sudo
Allow Nix to Use Sudo

5. Nix installer will check if there is any existing installation available on your system. Once again, type y to continue.

~~> Checking for artifacts of previous installs
Before I try to install, I'll check for signs Nix already is or has
been installed on this system.

---- Nix config report ---------------------------------------------------------
        Temp Dir:    /tmp/tmp.Q9lNrzTTJm
        Nix Root:    /nix
     Build Users:    32
  Build Group ID:    30000
Build Group Name:    nixbld

build users:
    Username:    UID
     nixbld1:    30001
     nixbld2:    30002
     nixbld3:    30003
     nixbld4:    30004
     nixbld5:    30005
     nixbld6:    30006
     nixbld7:    30007
     nixbld8:    30008
     nixbld9:    30009
     nixbld10:    30010
     nixbld11:    30011
     nixbld12:    30012
     nixbld13:    30013
     nixbld14:    30014
     nixbld15:    30015
     nixbld16:    30016
     nixbld17:    30017
     nixbld18:    30018
     nixbld19:    30019
     nixbld20:    30020
     nixbld21:    30021
     nixbld22:    30022
     nixbld23:    30023
     nixbld24:    30024
     nixbld25:    30025
     nixbld26:    30026
     nixbld27:    30027
     nixbld28:    30028
     nixbld29:    30029
     nixbld30:    30030
     nixbld31:    30031
     nixbld32:    30032

Ready to continue?
[y/n]
Check for Existing Nix Installation
Check for Existing Nix Installation

6. Enter your sudo password to continue:

~~> Setting up the build group nixbld

---- sudo execution ------------------------------------------------------------
I am executing:

    $ sudo groupadd -g 30000 --system nixbld

Create the Nix build group, nixbld

[sudo] password for ostechnix: 

7. Upon successful Nix installation, you will see the following message:

Alright! We're done!
Try it! Open a new terminal, and type:

  $ nix-shell -p nix-info --run "nix-info -m"

Thank you for using this installer. If you have any feedback or need
help, don't hesitate:

You can open an issue at
https://github.com/NixOS/nix/issues/new?labels=installer&template=installer.md

Or get in touch with the community: https://nixos.org/community

---- Reminders -----------------------------------------------------------------
[ 1 ]
Nix won't work in active shell sessions until you restart them.

Press enter/return to acknowledge.
Nix Installation is Successful
Nix Installation is Successful

Congratulations! Nix package manager is successfully installed on Ubuntu. Log out from your current session and log back in.

If single-user installation mode is started, you will not be asked any questions. The installer will automatically choose the optimal settings and complete the installation automatically.

Upon successful installation, you will be prompted to run the following command to ensure that the necessary environment variables are set:

$ . /home/ostechnix/.nix-profile/etc/profile.d/nix.sh
Single User Nix Installation
Single User Nix Installation

Alternatively, you can log out from the current session and log back in.

You may want to place this file directly in your ~/.bashrc or a similar location, so that the Nix package manager is available in any newly created shell.

source $HOME/.nix-profile/etc/profile.d/nix.sh

Verify Nix Installation

Open a Terminal window and type the following command to verify if Nix has been properly installed or not:

$ nix-shell -p nix-info --run "nix-info -m"

Sample Output:

[...]
 - system: `"x86_64-linux"`
 - host os: `Linux 6.2.0-39-generic, Ubuntu, 22.04.3 LTS (Jammy Jellyfish), nobuild`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.21.0`
 - channels(root): `"nixpkgs"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`
Verify Nix Installation
Verify Nix Installation

If you saw an output something like this, Nix package manager has been installed.

If you want to verify only the installed version, run:

$ nix --version
nix (Nix) 2.21.0

You can now use the nix-env command to manage your packages and environments.

Getting Started with Nix

Getting started with the Nix package manager is straightforward. Nix provides a declarative approach to building and managing software packages on Linux and Unix systems.

Refer the following guide to learn how to use Nix package manager to install and manage packages in Linux.

Uninstall Nix

To uninstall Nix package manager, follow these steps based on your installation type:

Single User:

Open your terminal and run the following command:

$ rm -rf /nix

Multi User - Linux with systemd:

Open your terminal and execute the following commands one by one:

$ sudo systemctl stop nix-daemon.service
$ sudo systemctl disable nix-daemon.socket nix-daemon.service
$ sudo systemctl daemon-reload

Remove files created by Nix:

$ sudo rm -rf /etc/nix /etc/profile.d/nix.sh /etc/tmpfiles.d/nix-daemon.conf /nix ~root/.nix-channels ~root/.nix-defexpr ~root/.nix-profile

Remove build users and their group:

for i in $(seq 1 32); do
  sudo userdel nixbld$i
done
sudo groupdel nixbld

Additionally, check for references to Nix in the following files and remove them if present:

/etc/bash.bashrc
/etc/bashrc
/etc/profile
/etc/zsh/zshrc
/etc/zshrc

These steps will completely uninstall Nix package manager from your system.

Conclusion

In this guide, we discussed how to install Nix package manager in Ubuntu Linux. Even though it is tested on a Ubuntu system, the Nix installation steps are same for other Linux systems.

With the Nix package manager installed on your Linux system, you can now take advantage of its powerful features.

You May Also Like

2 comments

Carlos March 29, 2024 - 2:37 pm

Great article, but the floating table of contents window in annoying af.

Reply
sk March 29, 2024 - 7:44 pm

Glad you find it useful. The ToC allows the users to quickly jump to different section of the article. We will find a way to make ToC less intrusive.

Reply

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