nh2 / static-haskell-nix

easily build most Haskell programs into fully static Linux executables
389 stars 36 forks source link
haskell nix static-linking

Funding button Buildkite build status

static-haskell-nix

With this repository you can easily build most Haskell programs into fully static Linux executables.

static-haskell-nix can successfully build > 90% of Stackage executables, so chances are high it can build yours.

History

glibc encourages dynamic linking to the extent that correct functionality under static linking is somewhere between difficult and bug-ridden. For this reason, static linking, despite its many advantages (details here) has become less and less common.

Due to GHC's dependency on a libc, and many libraries depending on C libraries for which Linux distributions often do not include static library archive files, this situation has resulted in fully static Haskell programs being extremely hard to produce for the common Haskeller, even though the language is generally well-suited for static linking.

This project solves this.

It was inspired by a blog post by Vaibhav Sagar, and a comment by Will Dietz about musl.

Work on this so far was sponsored largely by my free time, FP Complete and their clients, and the contributors mentioned here.

By now we have a nixpkgs issue on Fully static Haskell executables (progress on which is currently this repo, with plans to later merge it into nixpkgs), and a merged nixpkgs overlay for static nixpkgs in general.

There's also nixpkgs's pkgsStatic package set, which can also build many Haskell packages statically with musl. Differences are:

In general, any contribution to static-haskell-nix or pkgsStatic benefits the respective other one.

A goal is to shrink static-haskell-nix over time, moving those parts into nixpkgs that do not slow down nixpkgs's fast pace.

Funding

You can support this project financially on OpenCollective. Goals:

Hercules CI Logo The storage infrastructure (Cachix) for downloading pre-built packages is sponsored by the awesome guys from Hercules CI. They are building a nix-based CI service you can safely run on your own infrastructure. static-haskell-nix also uses it.
If your company or project needs that, check Hercules CI out!

Testing

We have multiple CIs:

Building a minimal example (don't use this in practice)

default.nix builds an example executable (originally from https://github.com/vaibhavsagar/experiments). Run:

NIX_PATH=nixpkgs=nixpkgs nix-build --no-link

This prints a path that contains the fully linked static executable in the bin subdirectory.

This example is so that you get the general idea. In practice, you probably want to use one of the approaches from the "Building arbitrary packages" or "Building stack projects" sections below.

Binary caches for faster building (optional)

Install cachix and run cachix use static-haskell-nix before your nix-build.

If you get a warning during cachix use, read this.

If you don't want to install cachix for some reason or cachix use doesn't work, you should also be able to manually set up your nix.conf (e.g. in $HOME/.config/nix/nix.conf; you may have to create the file) to have contents like this:

extra-substituters = http://static-haskell-nix-ci.nh2.me:5000 https://cache.nixos.org https://static-haskell-nix.cachix.org
extra-trusted-public-keys = static-haskell-nix-ci-cache-key:Z7ZpqYFHVs467ctsqZADpjQ/XkSHx6pm5XBZ4KZW3/w= static-haskell-nix.cachix.org-1:Q17HawmAwaM1/BfIxaEDKAxwTOyRVhPG5Ji9K3+FvUU=

or append to command lines:

--option extra-substituters 'http://static-haskell-nix-ci.nh2.me:5000' --option extra-trusted-public-keys 'static-haskell-nix-ci-cache-key:Z7ZpqYFHVs467ctsqZADpjQ/XkSHx6pm5XBZ4KZW3/w='

Note that you may not get cached results if you use a different nix version than I used to produce the cache (I used 2.0.4 as of writing, which you can get from here).

Building arbitrary packages

The survey directory maintains a select set of Haskell executables that are known and not known to work with this approach; contributions are welcome to grow the set of working executables. Run for example:

NIX_PATH=nixpkgs=nixpkgs nix-build --no-link survey/default.nix -A working

There are multiple package sets available in the survey (select via -A):

If you are a nix user, you can easily import this functionality and add an override to add your own packages.

Building stack projects

The static-stack2nix-builder-example directory shows how to build any stack-based project statically.

Until Stack 2.3, the official static build of stack itself was built using this method (Stack >= 2.3 static builds are built in an Alpine Docker image after GHC on Alpine started working again, see here). The static-stack directory shows how Stack itself can be built statically with static-haskell-nix. stack is a big package with many dependencies, demonstrating that it works also for large projects.

Related important open issues

You can contribute to these to help static Haskell executables:

FAQ

Open questions