Compile to Raspberry pi

Hello, nixos noob here.
I’ve been googling a lot on how to compile Haskell programs to aarch64. Given issues with TemplateHaskell, I started looking for beefy aarch64 systems that could compile my code for me.
But then I looked at emulation, namely, with qemu and realised that I can just emulate an aarch64 system and build on it.
I also learnt that such emulation is baked into nix; namely with flakes. I saw GitHub - considerate/nixos-odroidhc4: NixOS configuration for the ODROID HC4 microcomputer.
Are there any pointers/docs on how to use flakes to target aarch64 while being on any arch?
Thanks.

Nothing to do with flakes, actually. On any NixOS machine, you can set up support for running aarch64 builds with boot.binfmt.emulatedSystems = [ "aarch64-linux" ]; and a reboot. After that you can build things with --system aarch64-linux or just select the aarch64 outputs of flakes explicitly. Either way, it’ll automatically use qemu for the build.

2 Likes

This is not an accurate explanation. 1) It has nothing to do with flakes. 2) The only part that’s baked into Nix is the ability to set the extra-platforms setting so that it will attempt to do builds for different platforms.

The way it actually works is quite simple though. On NixOS, you just set boot.binfmt.emulatedSystems = ["aarch64-linux"];. This setting enables your machine to transparently run binaries built for aarch64. It does this with binfmt (see: man binformat.d), so that when an aarch64 binary is executed, the associated emulator is automatically launched to run it. boot.binfmt.emulatedSystems also configures the extra-platforms Nix setting so that Nix will attempt to build as an aarch64-system despite not natively supporting it, and then the binfmt magically makes it work.

TL;DR: All you have to do is add boot.binfmt.emulatedSystems = ["aarch64-linux"]; to your NixOS config; no need for flakes.

1 Like

Thanks for replying. Nix is just great.
I’m just not sure what the role of binformat is here, but I guess that needs more reading.

Thanks. That makes it quite clear to me. I’ll read up more to understand the role of binformat. The manpage entry isn’t found, but I can read the docs online.
Thanks again for clarifying.

Hosted by Flying Circus.