nh2 / static-haskell-nix

easily build most Haskell programs into fully static Linux executables
388 stars 36 forks source link

[WIP] build musl-native instead of cross #3

Closed dtzWill closed 6 years ago

dtzWill commented 6 years ago

Started out wanting to show you how to use nixpkgs + musl, but.....

Anyway I'm new to the nixpkgs haskell packaging infrastructure and I know some of this is going about things the slightly wrong way but seems to work for a few packages at least.

Not all listed here work, but wanted to show you where i ended up!

Feel free to pick+choose bits that you like, definitely don't need to actually "pull" these :).

Hope this helps, GLHF!

dtzWill commented 6 years ago

Oh, I think some of the problems remaining is for packages that have libraries and executables both, and using wrong linker flags for creating the libraries. Maybe. Heading out for the evening but that might be worth checking-- with justStaticExecutables or so... But not in the overrides since they might be deps. Anyway.

nh2 commented 6 years ago

This is so good I am super happy!

Thanks a lot for putting in this effort, this saves me so much time experimenting.

nh2 commented 6 years ago

One of the packages that don't build yet, the stack build gives me this (I used stack = appendConfigureFlag (statify super.stack) [ "--ghc-option=-j1" ]; to get deterministic output of what module fails):

[ 44 of 125] Compiling Stack.Constants  ( src/Stack/Constants.hs, dist/build/Stack/Constants.o )
/nix/store/3a74d03pq6d0cg3crkfw965k5s58hbn4-binutils-2.30/bin/ld: /nix/store/rd8w17sgz9wr47nd0s7xvarapib0h9jm-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/nix/store/3a74d03pq6d0cg3crkfw965k5s58hbn4-binutils-2.30/bin/ld: /nix/store/rd8w17sgz9wr47nd0s7xvarapib0h9jm-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-musl/7.3.0/crtend.o: relocation R_X86_64_32 against `.ctors' can not be used when making a shared object; recompile with -fPIC
/nix/store/3a74d03pq6d0cg3crkfw965k5s58hbn4-binutils-2.30/bin/ld: final link failed: Nonrepresentable section on output
nh2 commented 6 years ago

@dtzWill I have squashed the commits, moved the whole thing to the survey directory so we can continue working in there, and added a call for contributions in the README, in commit f6acf62.

nh2 commented 6 years ago

@dtzWill

I finally figured out one of the remaining big problem (crtbeginT.o from https://github.com/nh2/static-haskell-nix/pull/3#issuecomment-404991877 above). The problem is where you pass --ghc-option=-optl=-static. It works for many use cases but not for some (e.g. stack or intray-web-server). This is because you intend to pass that flag only to the final gcc/ld invocation that links the exe.

But --ghc-option=-optl=-static will tell GHC to use it for any linker invocation, even those for intermediate files like when TemplateHaskell is used. The solution is to pass that flag only in ghc-options of the executable section in the cabal file instead of as a general GHC option.

And I succeeded building stack 1.7.1 statically this way.

To do this conveniently from nix instead of having to change the package's cabal file, we would need a cabal configure-time flag that does this, e.g. named --enable-static-executables.

Which looks like this cabal feature request: https://github.com/haskell/cabal/issues/391