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.
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
If you use nix-darwin, I recently put together a rosetta-builder module that can replace (or run alongside) nix-darwin’s nix.linux-builder. It uses Rosetta 2 (within the VM) and it supports aarch64-linux and x86_64-linux builds simultaneously.
If someone has a non-nix-darwin use case that they’re interested I’d love to hear the details to see if it could help with that too.