Best way to compile x86_64-linux from Apple silicon?

Hey,

I read this amazing blog post while ago and understood that it’s easy to compile aarch64-linux from apple silicon host but not to x86_64-linux targets:

How are the users of M1 machines currently deploying to remote machines which use different CPU architecture?

Theoretically, it ought to be possible to cross-compile Linux from Darwin but I don’t think Nixpkgs supports that yet and cross is generally not at a point where most packages can be expected to “just work” yet.

Your next option is to simply let that target machine build its NixOS generation itself. If all you need is the few trivial builds typical for a NixOS generation, pretty much any machine is capable of that. nixos-rebuild supports this via the --build-host flag.

If you need non-trivial builds and the remote you’re deploying to is not capable of reasonably building them itself, you can make use of remote builders. With these, nix transparently lets another machine do the builds and then copies the results back to your local machine which you can then use to copy the paths to the target machine. It’s also possible to have the target machine use the builder directly.
You either need your own machine capable of doing such builds or rent one from i.e. https://nixbuild.net/.

There’s one further option I just thought of with an apple silicon mac specifically: You can use rosetta2 inside of a VM. I haven’t tried this one in my time with a mac and I heard it’s quite a bit slower than Rosetta on macOS but it should at least work.

1 Like
  1. Download https://getutm.app/ and install aarch64 NixOS (enable apple virtualization)
  2. Set virtualisation.rosetta.enable = true;
  3. Build and run your x86 Linux apps
2 Likes

The answer above definitely pointed me to right direction but it was quite vague for a Nix beginner like me so I published a more thorough example here on how to get UTM VM up and running: GitHub - onnimonni/nixos-utm-vm-example.

Now I can finally build x86_64-linux targets easily from my Macbook :sunny:

1 Like