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.
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.
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 {};
};The key idea in both cases is the same:
- Override
haskellPackageswith a customoverridesfunction. - Use
callCabal2nixto build a Haskell package from a source (whether fetched from GitHub or a local path). - 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.