@@ -6,7 +6,7 @@ First, understand how to cross-compile a normal package from
66Nixpkgs. Matthew Bauer's [Beginners' guide to cross compilation in
77Nixpkgs][bauer] is a useful resource.
88
9−[bauer]: https://matthewbauer.us/blog/beginners-guide-to-cross.html
9+[bauer]: https://matthewbauer.us/blog/beginners-guide-to-cross.html
1010
1111
1212Using an example from the guide, this builds GNU Hello for a Raspberry
@@ -47,18 +47,24 @@ You should be prepared for a long wait because it first needs to build
4747GHC, before building all the Haskell dependencies of [Bench][]. If all
4848of these dependencies compiled successfully, I would be very surprised!
4949
50+!!! hint
51+ The above example won't build, but you can try and see, if you like.
52+ It will fail on [clock-0.7.2](http://hackage.haskell.org/package/clock-0.7.2),
53+ which needs a patch to build.
54+
5055To fix the build problems, you must add extra configuration to the
5156package set. Your project will have a [`mkStackPkgSet`](../reference/library.md#mkstackpkgset) or
5257[`mkCabalProjectPkgSet`](../reference/library.md#mkcabalprojectpkgset). It is there where you must add
53−[module options](../reference/modules.md) for setting compiler flags and so on.
58+[module options](../reference/modules.md) for setting compiler flags, adding patches, and so on.
59+
5460
5561### Static executables with Musl libc
5662
5763Another application of cross-compiling is to produce fully static
5864binaries for Linux. For information about how to do that with the
5965[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.
66+[nh2/static‑haskell‑nix][nh2]. Vaibhav Sagar's linked
67+[blog post][vaibhav] is also very informative.
6268
6369
6470```nix
@@ -98,6 +104,76 @@ executables you must add package overrides to:
98104 `integer-gmp`. However, at present, [Haskell.nix][] does not provide
99105 an option for this.
100106
107+
108+### How to cross-compile your project
109+
110+Set up your project Haskell package set.
111+
112+```nix
113+# default.nix
114+{ pkgs ? import <nixpkgs> {}
115+let
116+ # Import the Haskell.nix library,
117+ haskell = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {
118+ inherit pkgs;
119+ };
120+
121+ # Instantiate a package set using the generated file.
122+ pkgSet = haskell.mkCabalProjectPkgSet {
123+ plan-pkgs = import ./pkgs.nix;
124+ pkg-def-extras = [];
125+ modules = [
126+ {
127+ # You will need to put build fixes here.
128+ }
129+ ];
130+ };
131+in
132+ pkgSet.config.hsPkgs
133+```
134+
135+Apply that package set to the Nixpkgs cross package sets that you are
136+interested in.
137+
138+We are going to expand the `pkgs.pkgsCross` shortcut to be more
139+explicit.
140+
141+```nix
142+let
143+ pkgs = import <nixpkgs> {}
144+in {
145+ shortcut = pkgs.pkgsCross.SYSTEM;
146+ actual = import <nixpkgs> { crossSystem = pkgs.lib.systems.examples.SYSTEM; };
147+}
148+```
149+
150+In the above example, for any `SYSTEM`, `shortcut` and `actual` are
151+the same package set.
152+
153+```nix
154+# release.nix
155+let
156+ myProject = import ./default.nix;
157+
158+ pkgsNative = import <nixpkgs> {};
159+ pkgsRaspberryPi = import <nixpkgs> {
160+ crossSystem = pkgsNative.lib.systems.examples.raspberryPi;
161+ };
162+
163+ native = myProject { pkgs = pkgsNative; };
164+ crossRaspberryPi = myProject { pkgs = pkgsRaspberryPi; };
165+
166+in {
167+ my-project-native = native.my-project.components.exes.my-project;
168+ my-project-raspberry-pi = crossRaspberryPi.my-project.components.exes.my-project;
169+}
170+```
171+
172+Try to build it, and apply fixes to the `modules` list, until there
173+are no errors left.
174+
175+
176+
101177[nh2]: https://github.com/nh2/static-haskell-nix
102178[vaibhav]: https://vaibhavsagar.com/blog/2018/01/03/static-haskell-nix/
103179[haskell.nix]: https://github.com/input-output-hk/haskell.nix