Getting your Haskell executable statically linked with Nix

https://cs-syd.eu/posts/2024-04-20_static-linking-haskell-nix

16 Likes

Nice! I like to use file to tell if a binary is executable, viz.

$ file $(which ja)
/home/vanessa/.local/bin/ja: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=224fa312cf92297cbd4ed8fe488f77155434ac6f, stripped
``

I’ve also had success using the Cabal flag --enable-executable-static in place of --ghc-option=-optl=-static.

According to the docs, it passes -optl=-static and -static to GHC.

1 Like

That used to be broken on install (as opposed to build).

It was fixed here: Apply local configuration to install targets by alt-romes · Pull Request #9697 · haskell/cabal · GitHub

But I don’t know if that made it into a release yet.

You can use the gchup cabal prerelease 3.11.0.0.2024.4.19: ghcup-metadata/ghcup-prereleases-0.0.8.yaml at develop · haskell/ghcup-metadata · GitHub

ghcup config add-release-channel https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.8.yaml
ghcup install cabal latest-prerelease
1 Like

--enable-executable-static is not working in GHC 9.12.2

/nix/store/xxx2585fx95n2n716a5hg44iv8nmaa6x-binutils-2.44/bin/ld: cannot find -lz: No such file or directory
/nix/store/xxx2585fx95n2n716a5hg44iv8nmaa6x-binutils-2.44/bin/ld: have you installed the static version of the z library ?
/nix/store/xxx2585fx95n2n716a5hg44iv8nmaa6x-binutils-2.44/bin/ld: cannot find -lgmp: No such file or directory
/nix/store/xxx2585fx95n2n716a5hg44iv8nmaa6x-binutils-2.44/bin/ld: have you installed the static version of the gmp library ?
/nix/store/xxx2585fx95n2n716a5hg44iv8nmaa6x-binutils-2.44/bin/ld: cannot find -lffi: No such file or directory
/nix/store/xxx2585fx95n2n716a5hg44iv8nmaa6x-binutils-2.44/bin/ld: have you installed the static version of the ffi

It breaks cabal build on its own and even together with --ghc-option=-optl=-static!

I was able to setup static linking with nix in git-phoenix

It looks like you don’t have the static versions of the c libraries.
Edit: the error message says that on second glance

1 Like