Hey
I develop a Haskell program for scientific calculations, that I want to link fully statically. It is supposed to run on a HPC cluster at some point. The cluster runs a very old CentOS version, the firewall blocks access to the outside, and there will probably never be Nix. Thus I basically only see the option to build a statically linked executable.
My expression is inspired by the static-haskell-nix example from @nh2
I have a nix like this (interesting parts only, full file is here)
{ mkDerivation, base, binary, hpack # and many more Haskell deps
# C dependencies
, gmp6, libffi
# Linking flags
, staticLinking ? false
}:
in mkDerivation {
pname = "spicy";
version = "0.0.1";
prePatch = "hpack";
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [ base, binary ]; # And many more ...
libraryToolDepends = [ hpack ];
executableHaskellDepends = [ base, binary ];
testHaskellDepends = [ base, binary ];
benchmarkHaskellDepends = [ base, binary ];
# Create a fully static executable. Usually requires a GHC linked against Musl instead of Glibc.
enableSharedExecutables = !staticLinking;
enableSharedLibraries = !staticLinking;
enableStaticLibraries = staticLinking;
configureFlags = [
"--ghc-option=-optl=-static"
"--extra-lib-dirs=${gmp6.override { withStatic = true; }}/lib"
"--extra-lib-dirs=${libffi.overrideAttrs (old: { dontDisableStatic = true; })}/lib"
];
}
add it to the set of Haskell packages via an overlay (see here) and then call it with the pkgsMusl
set like this
spicyStatic = pkgsMusl.haskell.packages.ghc921.spicy.override {
staticLinking = true;
};
The corresponding flake is here (nix build git+https://gitlab.com/theoretical-chemistry-jena/quantum-chemistry/Spicy?ref=feature/static-builds#spicyStatic
)
Everything works fine until the linking phase, which fails to link against libffi
.
spicy> [2 of 2] Compiling Paths_spicy ( dist/build/calculations/autogen/Paths_spicy.hs, dist/build/calculations/calculations-tmp/Paths_spicy.o, dist/build/calculations/calculations-tmp/Paths_spicy.dyn_o )
spicy> Linking dist/build/calculations/calculations ...
spicy> /nix/store/y1mackrlqz0cwff5grlv18v1srk9mdrf-binutils-2.35.2/bin/ld: cannot find -lffi
spicy> collect2: error: ld returned 1 exit status
spicy> `cc' failed in phase `Linker'. (Exit code: 1)
Is there something I am missing that static-haskell-nix does, that I am not doing? Any idea?
Best wishes
Phillip