Build `x86_64-linux` on `aarch64-darwin`

Is there a way that’s reasonably performant to build x86_64-linux on aarch64-darwin? I used to use nix-docker on my Intel-based Mac, but I’ve found it to be unreliable and the performance to be middling on aarch64-darwin. I’ve tried using nix.linux-builder as well, but it uses QEMU and emulates instead of using Rosetta.

Does anyone have a preferred solution?

#!/usr/bin/env bash
set -euo pipefail

if ! command -v docker &> /dev/null
  echo "Please install docker first."
  exit 1

if ! docker container inspect nix-docker > /dev/null 2>&1; then
  docker create --platform linux/amd64 --privileged --name nix-docker -it -w /work -v $(git rev-parse --show-toplevel):/work nixos/nix
  docker start nix-docker > /dev/null
  docker exec nix-docker bash -c "git config --global --add /work"
  docker exec nix-docker bash -c "echo 'sandbox = true' >> /etc/nix/nix.conf"
  docker exec nix-docker bash -c "echo 'filter-syscalls = false' >> /etc/nix/nix.conf"
  docker exec nix-docker bash -c "echo 'max-jobs = auto' >> /etc/nix/nix.conf"
  docker exec nix-docker bash -c "echo 'experimental-features = nix-command flakes' >> /etc/nix/nix.conf"
  docker exec nix-docker bash -c "nix-env -iA nixpkgs.docker nixpkgs.jq nixpkgs.niv"

docker start nix-docker > /dev/null
docker exec -it nix-docker "$@"

I have this^ script inside my repo and use it like this $(git rev-parse --show-toplevel)/inside-docker nix build ...

You’ll need a recent Docker Desktop with “Use Rosetta …” enabled in the settings.

Not perfect but much faster than alternatives.