Home

Awesome

Nix and Haskell in production

This guide documents how I use Nix for Haskell development. Feel free to open issues or pull requests if you would like to contribute or suggest improvements

The purpose of this project is to support two Haskell workflows:

The emphasis of this guide is to be as robust as possible and gracefully handle writing Haskell projects at scale. Some of the suggestions in this guide might be overkill for a small Haskell project but are essential when managing multiple private Haskell projects across a team of developers.

This guide is based partly on the Haskell section of the nixpkgs manual and partly on experience using Nix and Haskell in production at Awake Security.

Background

Nix is not a cabal replacement and Nix actually complements cabal quite well. Nix is much more analogous to a stack replacement. stack does provide some support for Nix integration, but this document does not cover that. Instead, this document describes how to use Nix in conjunction with cabal for Haskell development

The main benefits of using Nix over stack are:

The main disadvantage of using Nix over stack are:

Both Nix and stack use curated package sets instead of version bounds for dependency management. stack calls these package sets "resolvers" whereas Nix calls these package sets "channels". Nix provides stable channels with names like NixOS-18.09 (analogous to stack's LTS releases) and then an unstable channel named nixpkgs-unstable (analogous to stack's nightly releases)

Related guides

Related tools

Before continuing, I'd like to mention some other tools for mixing Haskell with Nix:

Setup

Before you begin, you must install Nix if you haven't already:

$ curl -L https://nixos.org/nix/install | sh

You must also install cabal2nix and nix-prefetch-git:

$ nix-env --install cabal2nix
$ nix-env --install nix-prefetch-git

You also need to install cabal if you haven't done so already. You can either use your installed cabal or you can use nix to install cabal for you:

$ nix-env --install cabal-install

Make sure that you have a fairly recent version of cabal installed since these examples will use GHC 8 which requires version 1.24 or later of cabal. You can check what version you have installed by running:

$ cabal --version

Finally, run cabal update if you haven't done so already

Organization

This tutorial is split into several tutorial projects in the project*/ subdirectories. Read the README.md file in each subdirectory in order to follow the tutorial: