Remote cross-compiled builds for macOS (darwin) from NixOS remote

I have a nix workstation and a mac laptop. I’ve setup remote builds with the following /etc/nix/nix.conf

allowed-users = michael
trusted-users = michael
build-users-group = nixbld
builders-use-substitutes = true
builders = ssh://nix x86_64-darwin,x86_64-linux

For others looking to do a similar setup, the thing that was critical but unclear in the wiki was I needed to add the following /var/root/.ssh/config with a passwordless ssh key.

Host nix
    HostName ***
    User nix
    IdentityFile /var/root/.ssh/id_rsa
    IdentitiesOnly yes
    StrictHostKeyChecking accept-new
    ServerAliveInterval=15
    ServerAliveCountMax=3000
    Port 4920

However, when I attempt to build a package locally, I get the following error:

❯ nix-build '<nixpkgs>' --builders nix -j0 -A firefox
these derivations will be built:
  /nix/store/jpraymhpsqh48gps6c78z64sf2a4iwds-libstartup-notification-0.12.drv
  /nix/store/x4y2r22znsnrz885slg2b2c3bs3qdk66-firefox-unwrapped-72.0.2.drv
  /nix/store/nyv8yb5dhpi1wrjzaf0wqra4dqkb8b80-Firefox-72.0.2.drv
building '/nix/store/jpraymhpsqh48gps6c78z64sf2a4iwds-libstartup-notification-0.12.drv' on 'ssh://nix'...
error: build of '/nix/store/jpraymhpsqh48gps6c78z64sf2a4iwds-libstartup-notification-0.12.drv' on 'ssh://nix' failed: a 'x86_64-darwin' with features {} is required to build '/nix/store/jpraymhpsqh48gps6c78z64sf2a4iwds-libstartup-notification-0.12.drv', but I am a 'x86_64-linux' with features {benchmark, big-parallel, kvm, nixos-test}
builder for '/nix/store/jpraymhpsqh48gps6c78z64sf2a4iwds-libstartup-notification-0.12.drv' failed with exit code 1
cannot build derivation '/nix/store/x4y2r22znsnrz885slg2b2c3bs3qdk66-firefox-unwrapped-72.0.2.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/nyv8yb5dhpi1wrjzaf0wqra4dqkb8b80-Firefox-72.0.2.drv': 1 dependencies couldn't be built
error: build of '/nix/store/nyv8yb5dhpi1wrjzaf0wqra4dqkb8b80-Firefox-72.0.2.drv' failed

I was under the impression cross-compilation for darwin worked automatically, given building a derivation for x86_64-darwin on a Linux machine is explicitly mentioned in the nix manual. Is there a guide for enabling this?

This issue is partially relevant https://github.com/NixOS/nixpkgs/issues/60101

2 Likes

@mjlbach Did you end up fixing your issue? I’m trying to cross-compile from x86_64-linux to x86_64-darwin and I can’t make it work at all. Not even locally. Can’t find the answer if it is even possible.

Remote builds don’t do cross-compilation automatically. The system tuples (e. g. x86_64-darwin) are matched with the derivation’s system attribute which doesn’t tell you anything about where you can run the derivation, but rather where it is supposed to be built. Nix itself doesn’t know or care about the runtime of the produced binaries, cross-compilation is only supported and implemented via nixpkgs. Building an x86_64-darwin derivation on an x86_64-linux doesn’t magically work as a result.

Consequently, the machine complains:

a  'x86_64-darwin' with features {} is required to build '/nix/store/jpraymhpsqh48gps6c78z64sf2a4iwds-libstartup-notification-0.12.drv', but I am a 'x86_64-linux' with features {benchmark, big-parallel, kvm, nixos-test}

If you want to cross-compile something, use the package sets below pkgsCross, e. g. pkgsCross.aarch64-multiplatform.firefox would cross-compile firefox to aarch64-linux. However, it is currently not possible to cross compile to x86_64-darwin, as far as I am aware, since some parts of the stdenv are impure.