Are `cabal2nix` or `haskell.nix` necessary, or can I just use `ghc` with `make` or a shell script?

Hi everyone,

I’m a nix user who’s interested in working a bit more with Haskell. I’ve used stack in the past, but now that I’ve switched to NixOS, I’d rather use nix to manage my projects and dependencies than any language-specific tool.

To my understanding, cabal2nix and haskell.nix exist for helping users of cabal and stack integrate with nix. If I’m migrating an existing project, that seems awesome, but is there any reason to use those programs if I’m starting with nix from scratch? I’m tempted to put the following code in shell.nix like so:

let 
  customGhc = pkgs.haskellPackages.ghcWithPackages (pkgs: with pkgs; [
    haskell-say
  ]);
in
pkgs.mkShell {
  buildInputs = [
    customGhc
    # Other dependencies here
  ];
}

From there, I would just use a script to invoke ghc from the command line. Why is this a bad idea? I would appreciate any advice.

3 Likes

What you’ve got here will work for a simple code base or a single Haskell file, but it would likely get annoying with a big project, multiple modules, multiple libraries, etc.

Most Haskell developers like to do day-to-day development with tools like cabal, stack, ghcid, HLS, etc.

In Super-Simple Haskell Development with Nix - #3 by cdepillabout I outline a couple different approaches for Haskell+Nix that you might want to checkout if you start to hit the limits of calling GHC directly.

1 Like

Yea, it’s easy enough for simple cases. But as soon as you start having different derivations that depend on each other, you’re going to essentially find yourself re-implementing Cabal in Nix (which, granted, sounds like an interesting project idea).

1 Like