Super-Simple Haskell Development with Nix

I have written a tutorial on what I think might be the simplest, most beginner-friendly approach to developing Haskell under Nix. Feedback and suggestions are appreciated. If people find this useful, I’ll look into contributing it to the Nix manual.

@cdepillabout I think you’re an expert on pkgs.haskellPackages.developPackage, so if you have any suggestions, let me know.

3 Likes

Also, @Skyfold this was largely based on your excellent tutorial Nix Haskell Development (2020).

3 Likes

the simplest, most beginner-friendly approach to developing Haskell under Nix

I think what you have here probably is the easiest approach to getting started with Nix + Haskell.


For other people reading this, there are a couple other approaches to developing Haskell under Nix that you may want to consider:

  • Use Nix to get GHC, cabal, and system deps. Then use cabal to download and build all Haskell dependencies:

    $ nix-shell -p haskell.compiler.ghc8104 -p cabal-instal -p zlib # zlib here is the system library, not the Haskell package
    $ cabal build # this is within the nix-shell
    

    The big disadvantage here is that you’re not building any of your Haskell dependencies with Nix, so you don’t get reproducbility, caching, etc.

  • Use stack with Nix-integration.

    Like the previous method, the big disadvantage here is that you’re not building any of your Haskell deps with Nix, so you don’t get caching, etc. This also only works for people using Stack.

  • Use shellFor from nixpkgs:

    nixpkgs/make-package-set.nix at db809100db91e97c118a70c3879307afdbf1dbb7 · NixOS/nixpkgs · GitHub

    This is similar to developPackage, but slightly more complicated. This gives you the ability to develop multiple Haskell packages at the same time (similar to cabal.project).

  • Use haskell.nix.

    haskell.nix is quite a big step-up in complexity, but it allows things like generating your own Haskell package set based on a stack.yaml file, or using the Cabal solver to generate a package set. You then compile this Haskell package set with Nix.

    If you’re working in a large environment where some people want to use stack, some people want to use cabal, and some people want to use Nix, then haskell.nix is a pretty flexible option.

    If you want a skeleton repo with bells and whistles for getting started with haskell.nix, I recommend something like GitHub - jonascarpay/template-haskell: batteries-included haskell.nix project template. If you Google for it, you may be able to find other templates that better fit your needs.


As for my own personal recommendations, if you just want to get started with something as quick as possible, I’d recommend you use either Stack’s Nix integration, or something like nix-shell -p cabal-install -p haskell.compiler.ghc8104.

If you want to actually build your Haskell dependencies with Nix and you have a simple project (no cabal.project file), then developPackage is a nice simple choice.

If you want to build your Haskell dependencies with Nix, you have a complicated project (multiple Haskell libraries), and you’re okay with the inflexibility of the Nixpkgs Haskell package set, then the Nixpkgs Haskell shellFor function is a good choice.

If you want ultimate flexibility (and you’re willing to pay for it with some additional complexity), or you’re working in a large heterogeneous team, then haskell.nix has you covered.

6 Likes