This post will not explain in detail how Nix can build haskell projects or set up development environments, but simply show one workflow that I use a lot. Please refer to this manual, Gabriel Gonzalez’s guide and various blog posts out there to find out more about Nix and Haskell.

Alright, so I often end up wanting to quickly experiment with some ideas in a ghci session or a standalone Haskell module. Sometimes I can get away by simply prototyping my idea with data types and functions from base, but sometimes (most of the time, really) I want (or need) to use other libraries. I’d like to have a magic command to which I would give a ghc version and a list of haskell packages, and I would end up with an environment with ghc/ghci and all the aforementionned packages pre-installed in that ghc’s package database, so that just running ghci would be enough for being able to use those packages. Similarly, standalone modules could be built just with ghc --make for example. No need for a cabal file, cabal-install or stack.

Well, this is possible with Nix. The nix-shell program allows us to enter some environment that possibly requires downloading, building and running several things. The precise provisioning process depends a lot on what the environment consists in. It turns out that the Haskell infrastructure in Nix provides some functions out of the box for describing environments that consist in a ghc install along with some Haskell packages from Hackage. Making use of them is as simple as:

This enters a shell with GHC 8.2.1 with aeson and dlist (and their transitive dependencies) installed in its package database. You might even fetch all those things from a binary cache with a little bit of luck!

The text between double quotes is a function call, in the Nix programming language. It calls a function, haskell.packages.ghc821.ghcWithPackages (. here is simply field/attribute access, like in many OO languages), provided by the standard Haskell infrastructure in nixpkgs, with an argument that’s itself a function: pkgs: [pkgs.aeson pkgs.dlist] is equivalent to (pseudo Haskell code) \pkgs -> [aeson pkgs, dlist pkgs] where aeson and dlist would be fields of a big record containing all Haskell packages. So this ghcWithPackages function lets us provision a ghc package database using a package set that it is providing us with, pkgs.

Note: a haskell package set in nix is basically any record that provides the packages you need, where packages must be declared under a very precise shape. The one we’re using here (haskell.packages.ghc821) is derived from the latest stackage LTS (9.x) but we could very well be calling thebestpackageset.ghcWithPackages instead, provided that we define thebestpackageset somewhere and that its definition is valid. For example, you could simply extend the latest LTS with a few packages of yours that nix should get from github, by simply using the record extension syntax of Nix. Or you could override just a few package versions from the LTS. Or you could even put together an entire package set yourself.

In that new shell, you can verify that you indeed get the GHC you asked for and that you can use the said packages:

Finally, you can define a very handy shell function for making it easier to spin up a new shell without having to remember the precise syntax for the nix-shell call above. Add this to your ~/.bash_profile, ~/.bashrc or what have you:

The with pkgs; bit simply allows us to say [aeson dlist] instead of [pkgs.aeson pkgs.dlist], it just “opens” the pkgs “namespace”, like RecordWildCards in Haskell, when you write SomeRecord{..} to bring all the fields of a record in scope, with the field selector name as variable name for each field.

Now entering our previous shell is even simpler!

Of course, if you need private dependencies or fancier environments, you do need to resort to writing a shell.nix that carefully puts everything together (not that it’s that complicated, but definitely not as simple as this nix-haskell command). However, for a quick hacking session or exploration of an idea, I’m not aware of a nicer solution.