Nix /cabal build weird behaviour ( can't not find -lz)

Deal ,all

The haskell project compiles well in MacOS, when I switched to NixOS

cabal build

/nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld.gold: 
>>> output:
error: cannot find -lz
collect2: error: ld returned 1 exit status
`cc' failed in phase `Linker'. (Exit code: 1)
Error: [Cabal-7125]

The installed nix pkgs(nix config file), shows that it should have zlib installed:

gcc
libgcc
binutils
zlib.dev
haskellPackages.zlib
haskellPackages.cabal2nix
haskellPackages.cabal-install
haskellPackages.ghc
ghc
ghcid
haskell-language-server
pkg-config

But with nix-shell to include 5 packages below, the project builds successfully

$ nix-shell -p zlib haskellPackages.ghc haskellPackages.cabal-install pkg-config binutils
cabal build
>>> no error ,build successfully

Any step I’m missing here to debug ?
Thank you for your all help & much appreciate !

1 Like

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 :slight_smile:.

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.

Cheers

3 Likes