# Getting your Haskell executable statically linked with Nix

**URL:** <https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384>\
**Category:** Links\
**Created:** [April 20, 2024, 12:50pm UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384 "2024-04-20T12:50:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NorfairKing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/norfairking/32/514_2.png) [@NorfairKing](https://discourse.haskell.org/u/NorfairKing)\
**Post date:** [April 20, 2024, 12:50pm UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/1 "2024-04-20T12:50:21Z")

</div>

[https://cs-syd.eu/posts/2024-04-20\_static-linking-haskell-nix](https://cs-syd.eu/posts/2024-04-20_static-linking-haskell-nix)

---

<div class="post-metadata">

**Author:** ![vmchale](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/vmchale/32/86_2.png) [@vmchale](https://discourse.haskell.org/u/vmchale)\
**Post date:** [April 20, 2024, 11:41pm UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/2 "2024-04-20T23:41:00Z")

</div>

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

```haskell
$ 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
``
```

---

<div class="post-metadata">

**Author:** ![henrytill](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/henrytill/32/5383_2.png) [@henrytill](https://discourse.haskell.org/u/henrytill)\
**Post date:** [April 21, 2024, 5:52am UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/3 "2024-04-21T05:52:14Z")

</div>

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

According to the [docs](https://cabal.readthedocs.io/en/3.4/cabal-project.html#cfg-flag---enable-executable-static), it passes `-optl=-static` and `-static` to GHC.

---

<div class="post-metadata">

**Author:** ![hasufell](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/hasufell/32/1250_2.png) [@hasufell](https://discourse.haskell.org/u/hasufell)\
**Post date:** [April 21, 2024, 6:10am UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/4 "2024-04-21T06:10:13Z")

</div>

> [@henrytill](#):
>
> enable-executable-static

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](https://github.com/haskell/cabal/pull/9697)

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](https://github.com/haskell/ghcup-metadata/blob/develop/ghcup-prereleases-0.0.8.yaml)

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

```

---

<div class="post-metadata">

**Author:** ![dyaitskov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/dyaitskov/32/5142_2.png) [@dyaitskov](https://discourse.haskell.org/u/dyaitskov)\
**Post date:** [July 11, 2025, 2:52am UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/5 "2025-07-11T02:52:38Z")

</div>

> [@henrytill](#):
>
> I’ve also had success using the Cabal flag `--enable-executable-static` in place of `--ghc-option=-optl=-static`.

`--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](https://github.com/yaitskov/git-phoenix/)

---

<div class="post-metadata">

**Author:** ![TeofilC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/teofilc/32/3987_2.png) [@TeofilC](https://discourse.haskell.org/u/TeofilC)\
**Post date:** [July 11, 2025, 10:40am UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/6 "2025-07-11T10:40:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![henrytill](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/henrytill/32/5383_2.png) [@henrytill](https://discourse.haskell.org/u/henrytill)\
**Post date:** [October 20, 2025, 3:59am UTC](https://discourse.haskell.org/t/getting-your-haskell-executable-statically-linked-with-nix/9384/7 "2025-10-20T03:59:31Z")

</div>

Here’s a recent example of doing this with GHC 9.10.3 using Nix:

> <https://github.com/henrytill/hbt-hs/blob/01daf97311c972df03a6869c6a5afbfca5f10b70/flake.nix>

I had to build GHC with NUMA support disabled because I couldn’t find a way of statically linking to the `numactl` library.

Also this blog post by @athas was quite helpful in learning about the finer points of linking against `libdw`’s dependencies:

> **[Static linking on Nix with GHC 9.6](https://sigkill.dk/blog/2024-05-22-static-linking-on-nix-with-ghc96.html)**
>
> HACKING DEN
