Getting your Haskell executable statically linked with Nix

submited by
Style Pass
2024-04-20 13:30:08

I have been making my products statically linked over the past few days. This post why and how to statically link your Haskell executables and collects a mapping from obscure error to unexpected fixes.

This work would not have been possible without the many-year-long effort of people like nh2. This issue on GitHub is a good summary of what went into making this possible.

Statically linked executables start faster because they don't need to look through the file system for the libraries they link against.

If you don't know what this means, you have probably been (dynamically) linking your executables against glibc. Statically linking against glibc is discouraged, so we will have to statically link against musl instead. This includes building ghc against musl and involves using pkgs.pkgsMusl.

In order to statically link an executable that uses Template Haskell at compile-time, the GHC RTS has to be built in the same way as the code you are compiling. This means that we need to tell ghc to produce relocatedStaticLibs in order to use Template Haskell.

Leave a Comment