Install Haskell package from GitHub

My situation is the following: I have a simple Haskell project (using cabal) hosted on GitHub that I would like to be able to install on my loval NixOS machine. What kind of .nix files do I need to add to the repository and/or my config to achieve that?

I’ve been experimenting with the following

{ lib, buildHaskellPackage, fetchgit, stdenv, haskellPackages }:

buildHaskellPackage rec {
  pname = "my-project";             # Package name
  version = "1.0";                  # Package version

  src = fetchgit {
    url = "https://github.com/yourusername/your-repo.git";  # GitHub repository URL
    rev = "commit-or-branch-hash";   # Specific commit or branch to fetch
    sha256 = "sha256-hash";          # SHA-256 hash of the fetched source
  };

  nativeBuildInputs = [ haskellPackages.cabal-install ];

  meta = with lib; {
    description = "My Haskell Project";  # Description
    license = licenses.mit;               # License (e.g., MIT)
  };
}

Then I put in my configuration.nix the following:

  environment.systemPackages = with pkgs; [ (import ./my-project.nix) ];

This throws the following error:

error: anonymous function at /nix/store/w0826yzx885j3f8z14c1d8gkc7qk1xxa-source/my-project.nix:1:1 called without required argument 'lib'

Any ideas what I’m doing wrong?

Instead of import ./my-project.nix, try callPackage ./my-project.nix {}.

Thanks for the suggestion. Unfortunately I get this error message:

error: evaluation aborted with the following error message: 'Function called without required argument "buildHaskellPackage" at /nix/store/hr2bqhsmpccbjhxf7sxpaq7bj5j5bl95-source/my-project.nix:1'

I’ve also tried using pkgs.haskellPackages.callPackage ./my-project.nix {} but got this error message:

error: anonymous function at /nix/store/mlrik90barv8nm5xbdh5rnp1a45inxjy-source/my-project.nix:1:1 called without required argument 'buildHaskellPackage'

       at /nix/store/rpj98mcvj2zkhv8x3xri1laf8arqs130-source/pkgs/development/haskell-modules/make-package-set.nix:97:40:

           96|       # this wraps the `drv` function to add `scope` and `overrideScope` to the result.
           97|       drvScope = allArgs: ensureAttrs (drv allArgs) // {
             |                                        ^
           98|         inherit scope;

Also, do I need any other .nix file? Like a default.nix in the repository?

buildHaskellPackage is nothing that exists anywhere in nixpkgs. Haskell derivations use haskellPackages.mkDerivation. You can generate expressions using that builder using cabal2nix by running cabal2nix /path/to/my/haskell/project > my-project.nix.

Thank you! After tweaking a bit the output from cabal2nix, I came up with this my-project.nix file:

{ lib, fetchFromGitHub, haskellPackages }:
haskellPackages.mkDerivation {
  pname = "my-project";
  version = "0.1.0.0";
  src = fetchFromGitHub {
    owner = "user-name";
    repo = "repo-name";
    rev = "some-hash";
    sha256 = "some-hash";
  };
  isLibrary = false;
  isExecutable = true;
  executableHaskellDepends = [ haskellPackages.base ];
  description = "My Haskell Project";
  license = lib.licenses.gpl3Plus;
  mainProgram = "my-project";
}

And in my configuration.nix I have

    (callPackage ./my-project.nix {})

And now it works fine.