6. Nix Integration¶
Nix is a package manager popular with some Haskell developers due to its focus on reliability and reproducibility.
cabal now has the ability to integrate with Nix for dependency management during local package development.
6.1. Enabling Nix Integration¶
To enable Nix integration, simply pass the
--enable-nix global option when you call
cabal. To use this option everywhere, edit your
$HOME/.cabal/config file to include:
If the package (which must be locally unpacked) provides a
default.nix file, this flag will cause
cabal to run most commands through
nix-shell. If both expressions are present,
shell.nix is preferred. The following commands are affected:
cabal install(only if installing into a sandbox)
If the package does not provide an expression,
cabal runs normally.
6.2. Creating Nix Expressions¶
The Nix package manager is based on a lazy, pure, functional programming language; packages are defined by expressions in this language. The fastest way to create a Nix expression for a Cabal package is with the cabal2nix tool. To create a
shell.nix expression for the package in the current directory, run this command:
$ cabal2nix --shell ./. >shell.nix
6.3. Nix Expression Evaluation¶
(This section describes for advanced users how Nix expressions are evaluated.)
First, the Nix expression (
default.nix) is instantiated with
--indirect options are used to create an indirect root in the Cabal build directory, preventing Nix from garbage collecting the derivation while in use. The
IN_NIX_SHELL environment variable is set so that
builtins.getEnv works as it would in
Next, the commands above are run through
nix-shell using the instantiated derivation. Again,
--indirect are used to prevent Nix from garbage collecting the packages in the environment. The child
cabal process reads the
CABAL_IN_NIX_SHELL environment variable to prevent it from spawning additional child shells.