Adding library dependencies

There are several libraries from Hackage that you can use in your Haskell project created off haskell-template. The general steps to do this are:

  • Identify the package name from Hackage. Let’s say you want to use ema
  • Add the package, ema, to the .cabal file under the build-depends section.
  • Exit and restart the nix shell (nix develop) and restart VSCode.

Step (3) above will try to fetch the package from the Haskell package set in nixpkgs (the one that is pinned in flake.lock), and this package set (which is ultimately derived from Stackage sets) sometimes may not have the package you are looking for. A common reason is that it is marked as “broken” or it simply doesn’t exist. In such cases, you will have to override the package in the overrides argument of haskell-flake (see the next section).

Overriding a Haskell package in Nix

In Nix, it is possible to use an exact package built from an arbitrary source (Git repo or local directory). If you want to use the master branch of the ema library, for instance, you can do it as follows:

  • Add a flake input pointing to the ema Git repo in flake.nix:
    {
      inputs = {
        ema = {
          url = "github:EmaApps/ema";
          flake = false;
        };
      };
    }
  • Build it using callCabal2nix and assign it to the ema name in the Haskell package set by adding it to the overrides argument of your flake.nix that is using haskell-flake:
    {
      perSystem = { self', config, pkgs, ... }: {
        haskellProjects.default = {
          overrides = self: super: with pkgs.haskell.lib; {
            ema = dontCheck (self.callCabal2nix "ema" inputs.ema {}); 
          };
        };
      };
    }
    We use dontCheck here to disable running tests. You can also use source-overrides instead of overrides.
  • Re-run the nix shell (nix develop) and restart VSCode.

pkgs.haskell.lib functions

  • Write about this.

See also

Links to this page