ArchWiki / Nix
ArchWiki / Nix
Nix is a purely functional package manager that aims to make package management reproducible, declarative and reliable.
Installation
Configuration
To have the Nix daemon launched at boot time, enable nix-daemon.service.
Add a channel and update it.
https://channels.nixos.org/nixpkgs-unstable
$ nix-channel --updateUsage
With the shell configured, the following should install hello into your updated PATH:
$ nix-env -iA nixpkgs.helloThe binary itself will be located at /nix/store/[hash]-hello-[version]/bin/hello.
Run hello and make sure it is in the right PATH. If it works, you can remove it:
$ nix-env --uninstall helloOr you can check the list of installed programs:
$ nix-env --queryYou can also list generations:
$ nix-env --list-generationsSee nix-env(1) for more detailed information.
Tips and tricks
Declarative management of software installation and configuration
Home Manager provides a system for managing a user environment with Nix.
It lets you
install software declaratively in your user profile, rather than using
nix-envmanage dotfiles in the home directory of your user.
See Home Manager Manual and the Nixos Wiki page for the installation and configuration guide.
Max jobs
By default, nix only uses one builder. The following will allow nix to use as many jobs as the number of CPUs:
max-jobs = auto/etc/nix/nix.confGraphical acceleration
To run OpenGL and Vulkan applications, use NixGL.
Desktop integration
For integrating Nix applications with your desktop environment, add the ~/.nix-profile/share directory to your $XDG_DATA_DIRS, for instance using export XDG_DATA_DIRS=$HOME/.nix-profile/share:$XDG_DATA_DIRS.
Zsh integration for nix-shell
nix-shell starts Bash by default. zsh-nix-shell lets you use Zsh as the default shell in a nix-shell environment. Some prompt plugins such as zsh-theme-powerlevel10k and zsh-pure-prompt provide a nix-shell indicator.
Command completion
Zsh
nix-zsh-completions provides Zsh completions for nix commands such as nix-env and nix-shell.
Troubleshooting
Too many open files
Some builds may run into an error such as:
error: opening directory '/nix/store/...': Too many open filesEdit nix-daemon.service and increase the file limit:
[Service]
LimitNOFILE=65536Warning message about root user channels
If you get this error while using Nix:
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoringThe root user will need to update their channels:
# nix-channel --updateSandbox build issues
Other sandbox issues
error: while setting up the build environment: mounting /proc: Operation not permitted
error: program '/usr/bin/nix-env' failed with exit code 1
1: package 'utils' in options("defaultPackages") was not found
2: package 'stats' in options("defaultPackages") was not found
Error: .onLoad failed in loadNamespace() for 'utils', details:
call: system(paste(which, shQuote(names[i])), intern = TRUE, ignore.stderr = TRUE)
error: cannot popen '/nix/store/fnkvlbls29d01jcx3wsdnhykyrl7087r-which-2.21/bin/which 'uname' 2>/dev/null', probable reason 'Cannot allocate memory'The issue is known upstream: #2311, #3000, and #4636.
It is generally not recommended to disable sandboxing as it pollutes the build environment and could possibly cause more build errors. Anything from nixpkgs expects to be built with the sandbox on.
The most common fix is to disable sandboxing in the configuration file:
# disable sandboxing
sandbox = false/etc/nix/nix.confThen restart the nix-daemon.service.
Locale warnings
The Nixos wiki recommends running export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive. Alternatively, export the environment variable LC_ALL=C.