Hey @yellowbean, I’ve been quite bothered by this issue as well.
In the nix community it seems that it’s typical to always use a shell for the particular project you are building, and thus the advice I’ve found online for this is always to just start the shell to build that project.
That said, I’d prefer to have a working global environment (declaratively configured in my NixOS/nix-darwin config) for building Haskell projects and GHC itself by default.
I’ve fixed this just today actually, the key lines you need I believe are replicating what nix-shell is doing behind the scenes:
home.sessionVariables = {
# Commonly needed in the env for building haskell pkgs
# ncurses, gmp, zlib
PKG_CONFIG_PATH = "${pkgs.zlib.dev}/lib/pkgconfig:${pkgs.gmp.dev}/lib/pkgconfig:${pkgs.ncurses.dev}/lib/pkgconfig";
C_INCLUDE_PATH = "${pkgs.zlib.dev}/include:${pkgs.gmp.dev}/include:${pkgs.ncurses.dev}/include";
LIBRARY_PATH = "${pkgs.zlib}/lib:${pkgs.gmp}/lib:${pkgs.ncurses}/lib";
};
For what it’s worth, my global package set looks like this:
let
hs-comp = pkgs.haskell.compiler.ghc910;
hs-pkgs = pkgs.haskell.packages.ghc910;
in
{
...
home.packages = with pkgs; [
fzf ripgrep
hs-comp
hs-pkgs.cabal-install
hs-pkgs.haskell-language-server
nixos-rebuild # to deploy to remote nixos machines directly
# For (building) GHC
hs-pkgs.alex hs-pkgs.happy autoconf automake python3
# Make this available by default
pkgs.pkg-config
];
home.sessionVariables = ... # as in the first snippet
}
Hope that’s helpful! I would have loved to find this solution earlier .
PS: If you are not using home-manager, you should be able to do the same thing by setting the variables in environment.variables and packages in environment.systemPackages.
It’s because on NixOS there’s no /usr/lib or whatever, and the design of Nix means that it only knows to search specific paths in the store. So there’s no default search dir to pick up things like zlib.
Because zlib is so common, I sometimes use haskellPackages.ghcWithPackages(p: [p.zlib]), which ensures that the Hackage zlib package is available, which pulls in the C zlib and makes it available. But most of the time I set up a development environment for the package, since this is what Nix is best at.
If I just need to hack on someone else’s package, I can often do that with the following shell.nix: