nh2 / static-haskell-nix

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

Fails to link against tinfo (ncurses5) #14

Closed nomeata closed 3 years ago

nomeata commented 5 years ago

I am trying to build https://travis-ci.org/entropia/tip-toi-reveng/ statically using these instructions, and I made good progress by pinning nixpkgs to version https://github.com/NixOS/nixpkgs/commit/2c07921cff84dfb0b9e0f6c2d10ee2bfee6a85ac (which is the one mentioned in the README) and then buliding pkgs.pkgsMusl.haskell.packages.ghc843.tttool.

And indeed, as long as I stub out uses of TemplateHaskell in my program (it it expected that this is needed?), it builds… almost: It fails linking

Linking dist/build/tttool/tttool ...
/nix/store/3a74d03pq6d0cg3crkfw965k5s58hbn4-binutils-2.30/bin/ld: cannot find -ltinfo
collect2: error: ld returned 1 exit status
`cc' failed in phase `Linker'. (Exit code: 1)
builder for '/nix/store/4z8hp5bslaxlgasq69k4njxl2z1hnwz2-tttool-1.8.1.drv' failed with exit code 1
error: build of '/nix/store/4z8hp5bslaxlgasq69k4njxl2z1hnwz2-tttool-1.8.1.drv' failed

Blindly cargo-culting the example scotty app, I tried adding the following to my project.nix

  configureFlags = pkgs.lib.optional static [
     "--ghc-option=-optl=-static"
     "--extra-lib-dirs=${pkgs.gmp6.override { withStatic = true; }}/lib"
     "--extra-lib-dirs=${pkgs.zlib.static}/lib"
     "--extra-lib-dirs=${pkgs.ncurses5.override { enableStatic = true; }}/lib"
  ];

but it did not make a difference (and there was already a --extra-lib-dirs=/nix/store/1xwbnc903gymfi99zpvj8mdp5601yqz6-ncurses-6.1/lib commde line argument passed automatically somehow).

Is it maybe dubious that https://github.com/NixOS/nixpkgs/blob/2c07921cff84dfb0b9e0f6c2d10ee2bfee6a85ac/pkgs/development/libraries/ncurses/default.nix starts with

, enableStatic ? stdenv.hostPlatform.useAndroidPrebuilt
nomeata commented 5 years ago

A work-around is to compile the haskeline package with -f-readline; I am not sure which features are lost this way.

nh2 commented 5 years ago

Check out commit 76d2cd3c46fabb55419de0fdb7cb7af5ac687ca8. All that was needed was https://github.com/nh2/static-haskell-nix/blob/76d2cd3c46fabb55419de0fdb7cb7af5ac687ca8/survey/default.nix#L629-L634

I could then build it with

NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/88ae8f7d.tar.gz nix-build --no-link survey/default.nix -A haskellPackages.tttool

Since I have commited this, you can now build it the same way, and I have also uploaded it to the cachix.

(If you add tttool to Stackage, you will get that for free without an entry as I have committed now.)

For clarity, it is also possible to do this without adding code to static-haskell-nix, by importing it instead; example: https://github.com/nh2/static-haskell-nix/blob/76d2cd3c46fabb55419de0fdb7cb7af5ac687ca8/static-stack/default.nix#L38-L42

nh2 commented 5 years ago

I've also uploaded the static result: https://github.com/nh2/static-haskell-nix/releases/tag/tttool-static

7.7 MB uncompressed.

nh2 commented 5 years ago

In other words, because you use pkgs.pkgsMusl instead of my overridden haskellPackages, you likely don't have any of my fixes to build stuff statically, including the one for tinfo.

nomeata commented 5 years ago

Thanks for the fix and explanation!

I also managed to build it statically using haskell.nix, and since that also gives me windows builds, I will probably use that for now. But using plain nixpks eventually (with your work) would be nice!

srid commented 3 years ago

@nomeata Can plain nixpkgs not give Windows builds? Must one switch to haskell.nix for that?

nomeata commented 3 years ago

Haven't tried recently, back then they had a fair amount of patches for cross compilation onto Windows that were not in upstream nixpkgs yet.

nh2 commented 3 years ago

I believe that all tinfo / ncurses issues should be fixed with PR #98.