[Query]: Statically compile haskell script in nix environment

Hi All,

I am trying to compile a haskell script (shake build script) as static (standalone) executable using ghc installed with packages shake. Normal build works but if I use -optl-pthread -optl-static, the build fails where ld is unable to find dependencies.

I am sure I need to create nix-shell with some options and packages to ensure that ld loads those. Some direction would be helpful.

The error log is as follows:

#+begin_src sh

ghc -static -optl-pthread -optl-static -o shakeExample shakeExample.hs
Linking shakeExample …
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lm
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lrt
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lutil
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -ldl
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lpthread
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lgmp
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lm
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lrt
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -ldl
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lffi
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lpthread
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lpthread
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
cc' failed in phase Linker’. (Exit code: 1)

#+end_src

Thanks,

Rohit

for glibc, you need to add glibc.static to buildInputs to get the statically linked libraries.

1 Like