DEV Community

John  Ajera
John Ajera

Posted on

Installing Nix on Ubuntu

Installing Nix on Ubuntu

This guide walks you through installing Nix on Ubuntu using the official supported method. Nix is a powerful package manager that enables reproducible development environments and declarative system configuration.


1. Overview

Installation Method:

Multi-user installation with daemon mode (recommended for Ubuntu)

What Nix Provides:

  • Reproducible Environments: Consistent toolchains across machines
  • Declarative Configuration: Define environments as code
  • Isolated Packages: No conflicts between package versions
  • Cross-Platform: Same configuration works on Linux, macOS, and WSL

Key Features:

  • System-wide installation in /nix
  • Dedicated build users for security
  • Flakes support for modern workflows
  • Compatible with all Ubuntu versions

2. Prerequisites

Before starting, ensure you have:

  • Ubuntu (any recent version)
  • sudo access (required for multi-user installation)
  • Internet connection (for downloading Nix installer)
  • curl (will be installed if missing)
  • Basic familiarity with Linux command line

3. Update Your System

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

4. Install Required Dependency

Nix requires curl to download the installer. Install it if it's not already present:

sudo apt install curl -y
Enter fullscreen mode Exit fullscreen mode

5. Run the Official Nix Installer (Multi-User Mode)

This is the recommended installation method for Ubuntu. The multi-user installation provides better security and isolation.

sh <(curl -L https://nixos.org/nix/install) --daemon
Enter fullscreen mode Exit fullscreen mode

What this does:

  • Creates dedicated build users (nixbld1, nixbld2, etc.) for secure package building
  • Installs Nix into /nix (system-wide location)
  • Configures Nix daemon for multi-user access
  • Sets up proper permissions and security isolation

You may be prompted for sudo access during installation. This is normal and required for the multi-user setup.

Installation Time: The installation typically takes 2-5 minutes depending on your internet connection.


6. Load Nix into Your Shell

After installation finishes, you need to load Nix into your current shell session. Either restart your terminal or run:

. /etc/profile.d/nix.sh
Enter fullscreen mode Exit fullscreen mode

This command sources the Nix profile script, making Nix commands available in your current shell.

Note: After restarting your terminal or opening a new session, Nix will be automatically available. You don't need to run this command every time.


7. Verify Nix Installation

Verify that Nix is installed correctly:

nix --version
Enter fullscreen mode Exit fullscreen mode

Expected output:

nix (Nix) 2.x.x
Enter fullscreen mode Exit fullscreen mode

The version number will vary depending on the latest release. If you see a version number, Nix is installed correctly.


8. Test Nix by Running a Package

Test Nix by running a simple package. The hello package is a classic test:

nix-shell -p hello
hello
Enter fullscreen mode Exit fullscreen mode

Expected output:

Hello, world!
Enter fullscreen mode Exit fullscreen mode

This confirms that Nix can download, build, and run packages correctly.

Note: The nix-shell -p hello command creates a temporary shell environment with the hello package available. Type exit to leave the shell when done.


9. Enable Flakes (Recommended)

Flakes are the modern way to use Nix, providing better reproducibility and easier sharing of configurations. Enable them by creating the Nix configuration directory and adding the experimental features:

Step 1: Create the config directory

sudo mkdir -p /etc/nix
Enter fullscreen mode Exit fullscreen mode

Step 2: Enable flakes and the new CLI

echo "experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf
Enter fullscreen mode Exit fullscreen mode

Step 3: Restart your shell

Open a new terminal session or restart your current one for the changes to take effect.

Verify flakes are enabled:

nix flake --version
Enter fullscreen mode Exit fullscreen mode

You should see output indicating flakes are available.


10. Quick Example: Using Flakes

Now that flakes are enabled, try a simple example:

mkdir -p ~/nix-test && cd ~/nix-test

cat > flake.nix <<'EOF'
{
  description = "Simple Nix flake example";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in {
      devShells.${system}.default = pkgs.mkShell {
        packages = [ pkgs.hello ];
      };
    };
}
EOF
Enter fullscreen mode Exit fullscreen mode

Enter the development shell:

nix develop
Enter fullscreen mode Exit fullscreen mode

Then run:

hello
Enter fullscreen mode Exit fullscreen mode

You should see "Hello, world!" confirming flakes are working correctly.


11. Best Practices Summary

  • Use Multi-User Installation: Always use --daemon flag for better security and isolation
  • Enable Flakes: Flakes are the future of Nix and provide better reproducibility
  • Keep System Updated: Regularly update your Ubuntu system before installing Nix
  • Use Flake-Based Projects: Prefer flake.nix over shell.nix for new projects
  • Version Control Configurations: Commit your flake.nix files to share environments across teams
  • Use Nix Develop: Prefer nix develop over nix-shell for flake-based projects

12. Troubleshooting

Issue: Nix commands not found after installation

Solution: Restart your terminal or run . /etc/profile.d/nix.sh

Issue: Permission denied errors

Solution: Ensure you have sudo access and the Nix daemon is running:

sudo systemctl status nix-daemon
Enter fullscreen mode Exit fullscreen mode

Issue: Flakes not working

Solution: Verify the configuration was added correctly:

cat /etc/nix/nix.conf
Enter fullscreen mode Exit fullscreen mode

Make sure you see experimental-features = nix-command flakes in the output.


13. References

Top comments (0)