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.
The first and main goal is to get to ~28 EUR/month to buy a cheap Hetzner dedicated build server for fast CI and pushing to Cachix. It will also allow anybody to download almost any executable on Stackage pre-built as a static binary, so that people can try out Haskell programs easily without having to install lots of dependencies.
Because the server is so cheap, already 1 or 2 EUR/month will bring us to that goal quickly.
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:
HerculesCI: Builds with pinned nixpkgs.
Publicly visible, but requires free sign-in. Click the most recent job to which 100s of binaries we build.
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:
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):
working -- build all exes known to be working
notWorking -- build all exes known to be not working (help welcome to make them work)
haskellPackages.somePackage -- build a specific package from our overridden package set
If you are a nix user, you can easily import this functionality and add an override to add your own packages.
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.
FAQ
I get cannot find section .dynamic. Is this an error?
No, this is an informational message printed by patchelf. If your final looks like
then /nix/store/dax3wjbjfrcwj6r3mafxj5fx6wcg5zbp-stack-2.3.0.1 is your final output store path whose /bin directory contains your static executable.
I get stack2nix: user error (No such package mypackage-1.2.3 in the cabal database. Did you run cabal update?).
You most likely have to bump the date like hackageSnapshot = "2019-05-08T00:00:00Z"; to a newer date (past the time that package-version was added to Hackage).
Can I build Stack projects with resolvers that are too old to be supported by Stack >= 2?
No. For that you need need to use an old static-haskell-nix version: The one before this PR was merged.
I get some other error. Can I just file an issue and have you help me with it?
Yes. If possible (especially if your project is open source), please push some code so that your issue can be easily reproduced.
请发表评论