|
| 1 | +Cross compilation of Haskell projects involves building a version of |
| 2 | +GHC that outputs code for the target platform, and providing builds of |
| 3 | +all library dependencies for that platform. |
| 4 | + |
| 5 | +First, understand how to cross-compile a normal package from |
| 6 | +Nixpkgs. Matthew Bauer's [Beginners' guide to cross compilation in |
| 7 | +Nixpkgs][bauer] is a useful resource. |
| 8 | + |
| 9 | +[bauer]: https://matthewbauer.us/blog/beginners-guide-to-cross.html |
| 10 | + |
| 11 | + |
| 12 | +Using an example from the guide, this builds GNU Hello for a Raspberry |
| 13 | +Pi: |
| 14 | + |
| 15 | + nix build -f '<nixpkgs>' pkgsCross.raspberryPi.hello |
| 16 | + |
| 17 | +We will use the same principle in [Haskell.nix][] — replacing the normal |
| 18 | +package set `pkgs` with a cross-compiling package set |
| 19 | +`pkgsCross.raspberryPi`. |
| 20 | + |
| 21 | +### Raspberry Pi example |
| 22 | + |
| 23 | +This is an example of using [Haskell.nix][] to build the [Bench][] |
| 24 | +command-line utility. |
| 25 | + |
| 26 | +```nix |
| 27 | +{ pkgs ? import <nixpkgs> {} }: |
| 28 | +let |
| 29 | + haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz); |
| 30 | + native = haskellNix { inherit pkgs; }; |
| 31 | +in |
| 32 | + native.haskellPackages.bench.components.exes.bench |
| 33 | +``` |
| 34 | + |
| 35 | +Now switch the package set as in the previous example: |
| 36 | + |
| 37 | +```nix |
| 38 | +{ pkgs ? import <nixpkgs> {} }: |
| 39 | +let |
| 40 | + haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz); |
| 41 | + raspberryPi = haskellNix { pkgs = pkgs.pkgsCross.raspberryPi; }; |
| 42 | +in |
| 43 | + raspberryPi.haskellPackages.bench.components.exes.bench |
| 44 | +``` |
| 45 | + |
| 46 | +You should be prepared for a long wait because it first needs to build |
| 47 | +GHC, before building all the Haskell dependencies of [Bench][]. If all |
| 48 | +of these dependencies compiled successfully, I would be very surprised! |
| 49 | + |
| 50 | +To fix the build problems, you must add extra configuration to the |
| 51 | +package set. Your project will have a [`mkStackPkgSet`](../reference/library.md#mkstackpkgset) or |
| 52 | +[`mkCabalProjectPkgSet`](../reference/library.md#mkcabalprojectpkgset). It is there where you must add module |
| 53 | +[options](options.md) for setting compiler flags and so on. |
| 54 | + |
| 55 | +### Static executables with Musl libc |
| 56 | + |
| 57 | +Another application of cross-compiling is to produce fully static |
| 58 | +binaries for Linux. For information about how to do that with the |
| 59 | +[Nixpkgs Haskell infrastructure][nixpkgs] (not [Haskell.nix][]), see |
| 60 | +[nh2/static‑haskell‑nix][nh2]. Vaibhav Sagar's linked [blog |
| 61 | +post][vaibhav] is also very informative. |
| 62 | + |
| 63 | + |
| 64 | +```nix |
| 65 | +{ pkgs ? import <nixpkgs> {} }: |
| 66 | +let |
| 67 | + haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz); |
| 68 | + musl64 = haskellNix { pkgs = pkgs.pkgsCross.musl64; }; |
| 69 | +in |
| 70 | + musl64.haskellPackages.bench.components.exes.bench |
| 71 | +``` |
| 72 | + |
| 73 | +This example will build [Bench][] linked against Musl libc. However |
| 74 | +the executable will still be dynamically linked. To get fully static |
| 75 | +executables you must add package overrides to: |
| 76 | + |
| 77 | +1. Disable dynamic linking |
| 78 | +2. Provide static versions of system libraries. (For more details, see |
| 79 | + [Vaibhav's article][vaibhav]). |
| 80 | + |
| 81 | +```nix |
| 82 | +{ |
| 83 | + packages.bench.components.exes.bench.configureFlags = |
| 84 | + stdenv.lib.optionals stdenv.hostPlatform.isMusl [ |
| 85 | + "--disable-executable-dynamic" |
| 86 | + "--disable-shared" |
| 87 | + "--ghc-option=-optl=-pthread" |
| 88 | + "--ghc-option=-optl=-static" |
| 89 | + "--ghc-option=-optl=-L${gmp6.override { withStatic = true; }}/lib" |
| 90 | + "--ghc-option=-optl=-L${zlib.static}/lib" |
| 91 | + ]; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +!!! note "Licensing" |
| 96 | + Note that if copyleft licensing your program is a problem for you, |
| 97 | + then you need to statically link with `integer-simple` rather than |
| 98 | + `integer-gmp`. However, at present, [Haskell.nix][] does not provide |
| 99 | + an option for this. |
| 100 | + |
| 101 | +[nh2]: https://github.com/nh2/static-haskell-nix |
| 102 | +[vaibhav]: https://vaibhavsagar.com/blog/2018/01/03/static-haskell-nix/ |
| 103 | +[haskell.nix]: https://github.com/input-output-hk/haskell.nix |
| 104 | +[bench]: https://hackage.haskell.org/package/bench |
| 105 | +[nixpkgs]: https://nixos.org/nixpkgs/manual/#users-guide-to-the-haskell-infrastructure |
0 commit comments