Version Conflicts in Haskell Dependencies

In the previous chapter we have created a trivial Haskell project with no dependency other than base. Let's look at what happen when our Haskell project have some dependencies, which happen to conflict with the dependencies available in nixpkgs.

We first define a new Haskell project, haskell-project-v2:

cabal-version:       2.4
name:                haskell-project
license:             ISC
build-type:          Simple

executable hello
  main-is:             Main.hs
  build-depends:       base >=4.13
                     , yaml ==
  default-language:    Haskell2010

We add a new dependency yaml and explicitly requiring version At the time of writing, the latest version of yaml available on Hackage is However let's pretend there are breaking changes in, and we only support


Let's try to follow our previous approach to define our Haskell derivation using callCabal2nix. If we do that and try to build it, we would run into an error:

$ nix-build 05-package-management/haskell-project-v2/nix/01-nixpkgs-conflict/
building '/nix/store/3gab06pwqjc16wdqhj5akxk21g1z0qnx-cabal2nix-haskell-project.drv'...
these derivations will be built:
building '/nix/store/242x69pl2la3lb201qd57rghisrwclpy-haskell-project-'...
Setup: Encountered missing or private dependencies:
yaml ==

builder for '/nix/store/242x69pl2la3lb201qd57rghisrwclpy-haskell-project-' failed with exit code 1
error: build of '/nix/store/242x69pl2la3lb201qd57rghisrwclpy-haskell-project-' failed

Why is that so?

If we try to enter Nix shell, it will still succeed. But if we try to build our Haskell project in Nix shell, we will find out that the package yaml has to be explicitly built by cabal:

$ nix-shell 05-package-management/haskell-project-v2/nix/01-nixpkgs-conflict/shell.nix

[nix-shell]$ cd 05-package-management/haskell-project-v2/haskell/

[nix-shell]$ cabal --dry-run build all
Resolving dependencies...
Build profile: -w ghc-8.10.2 -O1
In order, the following would be built (use -v for more details):
 - yaml- (lib) (requires build)
 - haskell-project- (exe:hello) (first run)

Problem with Mono-versioning

If we look into nixpkgs source code, we can in fact see that the version of yaml available in nixpkgs is the latest,

As discussed earlier, with the mono-versioning approach by nixpkgs, there is exactly one version of each package available. Mono-versioning conflicts can happen when we need packages that are either older or newer than the version provided by nixpkgs.

In theory we could switch to a version of nixpkgs that has yaml-, however we would then have to buy into the versions of other Haskell packages available at that time.

Overriding Versions

Nixpkgs provides a workaround for mono-versioning conflicts, by using the override pattern. We can override the version of yaml to the one we want as follows:

  sources = import ../sources.nix {};
  nixpkgs = import sources.nixpkgs {};

  hsLib = nixpkgs.haskell.lib;
  hsPkgs-original = nixpkgs.haskell.packages.ghc8102;

  hsPkgs = hsPkgs-original.override {
    overrides = hsPkgs-old: hsPkgs-new: {
      yaml = hsPkgs-new.callHackage
        "yaml" "" {};

  src = builtins.path {
    name = "haskell-project-src";
    path = ../../haskell;
    filter = path: type:
        basePath = builtins.baseNameOf path;
      basePath != "dist-newstyle"

  project = hsPkgs.callCabal2nix "haskell-project" src;
hsPkgs.callPackage project {}

Essentially, we refer to the original haskell package set provided as hsPkgs-original, and we call hsPkgs-original.override to produce a new package set hsPkgs with yaml overridden to

Using callHackage, we can fetch the version of yaml from the Hackage snapshot in nixpkgs. With that we can just provide the string "" to specify the version that we want. Note however that this only works if the version can be found in the given Hackage snapshot, which may be outdated over time.

An issue with overriding dependencies this way is that the override affects the entire Haskell package set. This means that all other Haskell packages that depend on yaml will also get instead of As a result, this may have the unintended ripple effect of breaking other Haskell packages that we depends on.

Building Overridden Package

If we try to build our Haskell derivation with overridden yaml, it would work this time:

$ nix-build 05-package-management/haskell-project-v2/nix/02-nixpkgs-override/
these derivations will be built:

We can also enter the Nix shell to verify that this time, cabal will not try to build yaml for us:

$ nix-shell 05-package-management/haskell-project-v2/nix/02-nixpkgs-override/shell.nix

[nix-shell]$ cd 05-package-management/haskell-project-v2/haskell/

[nix-shell]$ cabal --dry-run build all
Resolving dependencies...
Build profile: -w ghc-8.10.2 -O1
In order, the following would be built (use -v for more details):
 - haskell-project- (exe:hello) (first run)


In comparison with the mono-versioned nixpkgs, Haskell.nix is much more flexible in allowing any version of Haskell packages that are supported by cabal. So we can leave the Nix project unchanged and still build it successfully:

$ nix-build 05-package-management/haskell-project-v2/nix/03-haskell.nix/
trace: No index state specified, using the latest index state that we know about (2020-12-04T00:00:00Z)!
building '/nix/store/v4pf9jffq0dh6xang25qviwb77947s7s-plan-to-nix-pkgs.drv'...
Using index-state 2020-12-04T00:00:00Z
Warning: The package list for '' is
18603 days old.
Run 'cabal update' to get the latest list of available packages.
Warning: Requested index-state2020-12-04T00:00:00Z is newer than
''! Falling back to older state
Resolving dependencies...
Build profile: -w ghc-8.10.2 -O1
In order, the following would be built (use -v for more details):
 - base-compat-0.11.2 (lib) (requires download & build)
 - base-orphans-0.8.3 (lib) (requires download & build)
 - aeson- (lib) (requires download & build)
 - yaml- (lib) (requires download & build)
 - haskell-project- (exe:hello) (first run)
these derivations will be built:
these derivations will be built:

$ /nix/store/phm2jk6xnvxsgp640r66cwgipc62kbc5-haskell-project-exe-hello-
Hello, Haskell!