Installation Guide
Learn how to install Nix package manager on a non-NixOS machine
Installing the Nix package manager on a non‑NixOS machine is a straightforward process that opens up a powerful, reproducible package ecosystem. Below is a step‑by‑step guide that covers prerequisites, installation, verification, basic usage, and advanced configuration options, all written in a technical yet approachable style.
Prerequisites
- 64‑bit Linux (kernel 4.15+) or macOS (10.15+)
- At least 1 GB of free disk space for the Nix store
- On macOS, install Xcode command line tools (
xcode-select --install) and ensurelibiconvandlibffiare available (e.g., via Homebrew). - On Linux, install build essentials (
gcc,make,libc6-dev) if you plan to compile packages.
Step 1 – Run the Installersh <(curl -L https://nixos.org/nix/install)
The script auto‑detects your OS, downloads the correct binary, and creates the Nix store under /nix/store on both Linux and macOS. It also writes a user‑specific configuration file at ~/.config/nix/nix.conf and updates your shell profile (~/.bashrc, ~/.zshrc, etc.) to add /nix/var/nix/profiles/default/bin to your PATH.
Step 2 – Verify the Installationnix-env --versionnix-env -qaP hello
The first command shows the Nix version; the second lists available packages, confirming that the store is reachable.
Step 3 – Install a Packagenix-env -iA nixpkgs.hellonix-env manages user‑specific packages. For system‑wide packages, use a dedicated profile:nix-env --profile /nix/var/nix/profiles/system -iA nixpkgs.hello
Alternatively, create a configuration.nix and run nix-env --switch-profile.
Step 4 – Configure Nix
Edit ~/.config/nix/nix.conf to enable features and set defaults. Common entries:
experimental-features = nix-command flakes
trusted-users = root @users
sandbox = true
experimental-featuresunlocks the newnixcommand syntax and flakes support.trusted-usersallows non‑root users to perform privileged operations.sandboxenforces isolation during builds.
For automated maintenance, enable the garbage‑collector daemon and set retention policies:
gc-keep-older-than = 7d
Step 5 – Explore Flakes
Create a flake.nix in any project directory:
{
description = "Example flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }: {
devShell.x86_64-linux = nixpkgs.mkShell {
buildInputs = [ nixpkgs.hello ];
};
};
}
Run nix develop . to spawn a development shell with the specified dependencies. Use nix flake show to inspect outputs and nix flake update to refresh inputs.
Step 6 – Advanced System Configuration
Even on non‑NixOS systems, you can use nix-env for package installation and nix-shell for isolated environments. For declarative system configuration, create a configuration.nix and apply it with nixos-rebuild switch (available on NixOS) or use nix-env --switch-profile on other systems.
Summary
- Run the installer script.
- Add Nix to your
PATH. - Verify with
nix-env. - Install packages with
nix-env -iA. - Configure via
~/.config/nix/nix.conf. - Embrace flakes for reproducible development environments.
With these steps, you’ll have a fully functional Nix installation on any modern Linux or macOS machine, ready to manage packages, build reproducible environments, and experiment with the powerful flakes system.