Overriding dependencies
Haskell libraries ultimately come from Hackage, and nixpkgs contains most of these. Adding a library to your project involves modifying the .cabal
file and restarting the nix shell. The process is typically as follows:
- Identify the package name from Hackage. Let's say you want to use
ema
- Add the package,
ema
, to the.cabal
file under thebuild-depends
section. - Exit and restart the nix shell (
nix develop
).
Step (3) above will try to fetch the package from the Haskell package set in nixpkgs (pkgs.haskellPackages
by default). For various reasons, this package may be either missing or marked as "broken". In such cases, you will have to override the package locally in the project (see the next section).
Overriding a Haskell package source
In Nix, it is possible to use an exact package built from an arbitrary source - which can be a Git repo, local directory or a Hackage version.
Using a Git repo
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:srid/ema";
ema.flake = false;
};
} - Build it using
callCabal2nix
and assign it to theema
name in the Haskell package set by adding it to thepackages
argument of yourflake.nix
that is using haskell-flake:{
perSystem = { self', config, pkgs, ... }: {
haskellProjects.default = {
packages = {
ema.source = inputs.ema;
};
};
};
} - Re-run the nix shell (
nix develop
).
Using a Hackage version
packages.<name>.source
also supports Hackage versions. So the following works to pull ema 0.8.2.0:
{
perSystem = { self', config, pkgs, ... }: {
haskellProjects.default = {
packages = {
ema.source = "0.8.2.0";
};
};
};
}
Using a nixpkgs version
haskellProjects.default = {
settings = {
fourmolu = { super, ...}: { custom = _: super.fourmolu_0_13_1_0; };
};
};
Overriding a Haskell package settings
haskellProjects.default = {
settings = {
ema = { # This module can take `{self, super, ...}` args, optionally.
# Disable running tests
check = false;
# Disable building haddock (documentation)
haddock = false;
# Ignore Cabal version constraints
jailbreak = true;
# Extra non-Haskell dependencies
extraBuildDepends = [ pkgs.stork ];
# Source patches
patches = [ ./patches/ema-bug-fix.patch ];
# Enable/disable Cabal flags
cabalFlags.with-generics = true;
};
};
};
nixpkgs functions
- The
pkgs.haskell.lib
module provides various utility functions that you can use to override Haskell packages. The canonical place to find documentation on these is the source. haskell-flake provides asettings
submodule for convienience. For eg., thedontCheck
function translates tosettings.<name>.check
; the full list of options can be seen here.
Sharing settings
Project modules export both packages
and settings
options for reuse in downstream Haskell projects.
Examples
- Emanote overrides: also demonstrates how to add a new setting option (
removeReferencesTo
).