Skip to content

Instantly share code, notes, and snippets.

@instinctive
Created March 7, 2026 18:21
Show Gist options
  • Select an option

  • Save instinctive/dc86a2e2e2eda32a0eb7e183c12f5e44 to your computer and use it in GitHub Desktop.

Select an option

Save instinctive/dc86a2e2e2eda32a0eb7e183c12f5e44 to your computer and use it in GitHub Desktop.
Add custom Haskell packages to default.nix

Adding Custom Haskell Dependencies in Nix

When using developPackage in a default.nix, you may need to depend on Haskell libraries that aren't in nixpkgs — either from a personal GitHub repo or from your local filesystem. Both cases follow the same pattern: override haskellPackages with a custom overrides function and use callCabal2nix to build the package from source.

From a Personal GitHub Repository

Use fetchFromGitHub to pull the source and callCabal2nix to build it:

let
  pkgs = import <nixpkgs> {};

  myLibSrc = pkgs.fetchFromGitHub {
    owner = "your-github-username";
    repo = "your-repo-name";
    rev = "some-commit-sha-or-tag";
    sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
  };

  hsPkgs = pkgs.haskellPackages.override {
    overrides = self: super: {
      my-lib = self.callCabal2nix "my-lib" myLibSrc {};
    };
  };
in
  hsPkgs.developPackage {
    root = ./.;
    source-overrides = {};
    modifier = drv:
      pkgs.haskell.lib.addBuildTools drv (with hsPkgs; [
        cabal-install
        ghcid
        hlint
      ]);
  }

To get the correct sha256, you can temporarily set it to pkgs.lib.fakeSha256 (or an empty string). The build will fail and Nix will report the correct hash in the error output.

From a Local Path

Point callCabal2nix at a local directory instead of a fetched source. You can use an absolute path:

overrides = self: super: {
  my-lib = self.callCabal2nix "my-lib" /absolute/path/to/my-lib {};
};

Or a path relative to your project:

overrides = self: super: {
  my-lib = self.callCabal2nix "my-lib" ../my-lib {};
};

How It Works

The key idea in both cases is the same:

  1. Override haskellPackages with a custom overrides function.
  2. Use callCabal2nix to build a Haskell package from a source (whether fetched from GitHub or a local path).
  3. Use the modified package set for developPackage.

As long as your .cabal file lists my-lib as a dependency, Nix will wire everything together automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment