Keystone SystemsKS Systems

Installing Nix on Ubuntu

This guide covers installing Nix on Ubuntu and other Debian-based Linux distributions.

Prerequisites

  • Ubuntu 20.04+ (or similar Debian-based distribution)
  • curl installed: sudo apt install curl
  • sudo access

Installation Options

Multi-User Installation (Recommended)

Multi-user mode runs the Nix daemon as a system service, providing better security isolation:

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

This creates:

  • Nix store at /nix/store
  • System service nix-daemon
  • Build users nixbld1 through nixbld32

Single-User Installation

For personal machines where simplicity is preferred:

sh <(curl -L https://nixos.org/nix/install) --no-daemon

Simpler setup, but all builds run as your user.

Determinate Installer

Alternative installer with improved defaults:

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

Post-Installation

Log out and back in, or source the profile manually:

# Multi-user installation
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh

# Single-user installation
. ~/.nix-profile/etc/profile.d/nix.sh

Verify installation:

nix --version
nix run nixpkgs#hello

Enabling Flakes

Add to your Nix configuration:

mkdir -p ~/.config/nix
echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf

For system-wide configuration (multi-user):

echo "experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf
sudo systemctl restart nix-daemon

Service Management

The nix-daemon runs as a systemd service:

# Check status
sudo systemctl status nix-daemon

# Restart (useful after config changes)
sudo systemctl restart nix-daemon

# View logs
journalctl -u nix-daemon

Configuration

nix.conf Options

Create or edit /etc/nix/nix.conf (system) or ~/.config/nix/nix.conf (user):

# Enable flakes and new CLI
experimental-features = nix-command flakes

# Binary cache substituters
substituters = https://cache.nixos.org https://nix-community.cachix.org
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=

# Automatic garbage collection
auto-optimise-store = true

Trusted Users

To allow users to configure substituters:

echo "trusted-users = root $USER" | sudo tee -a /etc/nix/nix.conf
sudo systemctl restart nix-daemon

Using Nix

Run a Package

# Run directly
nix run nixpkgs#htop

# Open a shell with packages
nix shell nixpkgs#git nixpkgs#vim

Search Packages

nix search nixpkgs nodejs

Garbage Collection

# Remove unused packages
nix-collect-garbage

# Remove and delete old generations
nix-collect-garbage -d

Uninstallation

Multi-User

# Stop the daemon
sudo systemctl stop nix-daemon
sudo systemctl disable nix-daemon

# Remove files
sudo rm -rf /nix /etc/nix /etc/profile.d/nix.sh
sudo rm /etc/systemd/system/nix-daemon.service
sudo rm /etc/systemd/system/nix-daemon.socket

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

# Remove from shell profiles
# Edit ~/.bashrc, ~/.zshrc to remove nix sourcing

Single-User

rm -rf ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.local/state/nix ~/.cache/nix
sudo rm -rf /nix
# Edit ~/.bashrc, ~/.zshrc to remove nix sourcing

Next Steps