This builds an example executable (originally from https://github.com/vaibhavsagar/experiments) with nix,
- creating a fully static executable (
ldd
saysnot a dynamic executable
) - to make that possible, it and all dependencies (including ghc) are built against
musl
instead of glibc
Originally inspired by this comment.
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/2c07921cff84dfb0b9e0f6c2d10ee2bfee6a85ac.tar.gz nix-build --no-out-link
This prints a path that contains the fully linked static executable in the bin
subdirectory.
You can use the binary cache shown in here to not have to build lots of native dependencies against musl
,
and you can use my binary nix closure mentioned here to not have to build GHC.
Install cachix and run cachix use static-haskell-nix
before your nix-build
.
The static-stack
directory shows how to build a fully static stack
executable (a Haskell package with many dependencies), and makes it reasonably easy to build other packages as well.
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=https://github.com/nh2/nixpkgs/archive/50677e464359947f2a71cfd497c4022e3cdf8c7d.tar.gz 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 workingnotWorking
-- 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