static-haskell-nix icon indicating copy to clipboard operation
static-haskell-nix copied to clipboard

Cross-compiling from MacOS

Open aidangilmore opened this issue 6 years ago • 5 comments

Is there a way that I could build a statically-linked Linux from Nix on MacOS?

Thanks

aidangilmore avatar Oct 30 '19 16:10 aidangilmore

Hey,

not yet, because to my knowledge GHC and Cabal do not have the necessary cross-compiling support for that yet.

But @Ericson2314 is working on that so it may work in the not-too-far future.

nh2 avatar Oct 31 '19 01:10 nh2

Does

nix-build '<nixpkgs>' --arg crossSystem '{ config = "x86_64-unknown-linux-musl"; useLLVM = true; }' -A hello

work? I don't remember any Haskell-specific issues, but could easily be forgetting things. Maybe @angerman knows?

Ericson2314 avatar Oct 31 '19 02:10 Ericson2314

I can confirm that cross-compiling hello on Darwin works as expected. How would I tweak my default.nix to use cross compiling with static-stack2nix-builder?

aidangilmore avatar Nov 01 '19 16:11 aidangilmore

I'm not sure!

If anybody knows, please chime in. I haven't tried to cross-compile static Haskell exes myself yet, so any contributions or insights would be appreciated.

I gave a super quick shot at passing config = "x86_64-unknown-linux-musl"; useLLVM = true; directly to nixpkgs imports, but that currently gets an infinite recursion problem.

nh2 avatar Nov 08 '19 00:11 nh2

static muslc is a special case on Linux. The special part is that you can natively run the muslc executable. This makes issues like TH and others (e.g. you can have a proper stage2 compiler) go away. If you want macOS as a build host you now need some form of executing haskell Linux binaries at compile time (if you want comprehensive compilation capabilities). We can do this for windows with WINE. For macOS there are a few projects that promise a Linux execution layer (e.g. the noah project). But last I checked (for macOS -> Linux cross compilation), it needed substantial patching and even then didn’t support enough Linux to make the programs run.

Even if they run, making sure they behave sufficiently similar is another issue.

What you could do is use vbox to start up a virtual Linux machine and delegate all cl pipe time requirements to that. Or use the splice dumping/loading approach that zeroTH/evil-splicer and obsidian.systems do to get macOS -> Linux cross compilation. Is it worth it? I can’t tell. Would it be cool to have? Absolutely!

angerman avatar Nov 08 '19 04:11 angerman