Cross-compiling Elixir project from Apple Silicon host fails to use rebar3 even in Rosetta enabled x86-64 VM

Hey,

I’m trying to cross-compile elixir project from my Apple Silicon host which is running a rosetta2 enabled builder.

The same build works perfectly when I’m building it remotely on the target host (by adding --build-host rusty.

$ nixos-rebuild switch --fast --flake .#rusty --target-host rusty --use-remote-sudo --override-input phoenix-service ./services/phoenix_demo/

building the system configuration...
• Updated input 'phoenix-service':
    'path:./services/phoenix_demo/?lastModified=1&narHash=sha256-EgrL6Ayn24ENrbijIapE/qmC10Tr20OojubTcVN0rM8%3D' (1970-01-01)
  → 'git+file:///Users/onnimonni/Projects/nixos-server?dir=services/phoenix_demo' (2025-02-03)
error: build of '/nix/store/gz5rn28b4xd2kyn548l5ccq3hmsgv2i9-phoenix-web-development.drv' on 'ssh-ng://rosetta-builder' failed: builder for '/nix/store/gz5rn28b4xd2kyn548l5ccq3hmsgv2i9-phoenix-web-development.drv' failed with exit code 1;
       last 18 log lines:
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/7k3bizcc1v0ncl5jgzhyvkx5k5h6lsc9-phoenix_demo
       > source root is phoenix_demo
       > Running phase: patchPhase
       > Running phase: updateAutotoolsGnuConfigScriptsPhase
       > Running phase: configurePhase
       > ==> decimal
       > Compiling 4 files (.ex)
       > Generated decimal app
       > ==> mime
       > Compiling 1 file (.ex)
       > Generated mime app
       > ==> nimble_options
       > Compiling 3 files (.ex)
       > Generated nimble_options app
       > ==> phoenix_demo
       > escript: Failed to open file: /nix/store/vp4i43lkk6simgyysx496a75bwfxlybf-erlang-27.2.1/lib/erlang/bin/escript.escript
       > ** (Mix) Could not compile dependency :telemetry, "/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3 bare compile --paths /build/phoenix_demo/_build/prod/lib/*/ebin" command failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile telemetry --force", update it with "mix deps.update telemetry" or clean it with "mix deps.clean telemetry"

Before this I needed to add ERL_FLAGS = "+JPperf true"; into mixRelease to allow cross-compiling elixir because it was failing for:

Error while loading project :mime at /build/deps/mime
** (ArgumentError) could not call Module.put_attribute/3 because the module PhoenixDemo.MixProject is already compiled

Has anyone been able to cross-compile Elixir or Erlang from Apple Silicon to x86-64-linux?

I guess the rebar3 my system tries to use is somehow incompatible with the apple silicon rosetta VM.

I tried to copy it into the target machine and it works just fine:

$ scp /nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3 rusty:~/
$ ssh rusty "./rebar3 -v"
rebar 3.24.0 on Erlang/OTP 27 Erts 15.2.1

Also when I just try to get the version in a mixRelease:

preConfigure = ''
    /nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3 -v
  '';

It still throws the same error:

> escript: Failed to open file: /nix/store/vp4i43lkk6simgyysx496a75bwfxlybf-erlang-27.2.1/lib/erlang/bin/escript.escript

And when I try to use it locally in my machine:

$ file /nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: a /nix/store/vp4i43lkk6simgyysx496a75bwfxlybf-erlang-27.2.1/bin/escript script executable (binary data)

$ /nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 2: fg: no job control
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 3: fg: no job control
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 14: ��: command not found
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 4: �����]�[vg��Ywvʭ��w�����3x��w�Bx&4����Ŗf����i�g���s�8�����Q�
                                                                                                                                          3H�X�D�6=��: Illegal byte sequence
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: command substitution: line 6: syntax error near unexpected token `)'
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: command substitution: line 6: `�4z�G�@�p�=Ķ��d�
                                                                                                                     ��ѫr�{��
                                                                                                                             �h,5�gC��u�V��Gbb4)HZ$&�]f35'
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 14: ��̒ƕ,���w
                                                                                       v�k8��DR�iaNs�h: command not found
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 4: P!�-�z��bbmustache/ebin/bbmustache.appUT: No such file or directory
ڃ: command not found0y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 14: 0�N�X,�k4z
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 14: ,
                                                                                �A٩�Z����,�: command not found
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: command substitution: line 15: unexpected EOF while looking for matching `''
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: command substitution: line 16: syntax error: unexpected end of file
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 14: ����: Illegal byte sequence
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 14: �: command not found
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 15: �
                                                                                t�a�3lȦ�Mwظ���lg.RK�: command not found
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 27: syntax error near unexpected token `('
/nix/store/0g85mlwk20y0v2sv3fcbiqsj08pmzw0j-rebar3-3.24.0/bin/rebar3: line 27: `���Ƕf���R�6-mI�i��17�X	�R���fX-ZF��eh�D�qڥ�aQg�F}��1jn�l�r��s�S�i�F����sa*f[�糪����b�y�������Hs1�[�ج\�ĥ�"N�!���u�ABt�(��p�NCc��##���0]'

But again if I try to use rebar3 locally on my Mac it shows a different rebar3:

$ nix-shell -p rebar3
$ which rebar3
/nix/store/2xldc0q1085cl350n9qsc2g7rwkhb8hp-rebar3-3.24.0/bin/rebar3

$ rebar3 -v
rebar 3.24.0 on Erlang/OTP 27 Erts 15.2.1

How can I validate if the instance of rebar3 my code is trying to use is actually compatible for the VM?

are you trying to compile a Linux binary on Darwin? This may not be a feasible course of action.

Also note that nixpkgs erlang may be missing some cross compilation support. erlang: cross compile support by IslandUsurper · Pull Request #222407 · NixOS/nixpkgs · GitHub

Thanks for the answer. I’m compiling it in a Linux VM running on Darwin. I have assumed this would still be supported?

1 Like

If you’re using a Rosetta VM then you shouldn’t need to cross compile at all. Does the .#rusty. nixos configuration have a system of x86?

Alternatively you can avoid doing any building locally and have the target host build by adding --build-host <target> to your nixos rebuild command.

So the Rosetta VM is running on my laptop.

If you’re using a Rosetta VM then you shouldn’t need to cross compile at all. Does the .#rusty. nixos configuration have a system of x86?
Yes indeed I wouldn’t need to “cross-compile” but the compilation just fails and x86 version of rebar3 doesn’t seem to work in the Rosetta VM.

Rusty configuration is for a remote server in Hetzner. If I use rusty to compile its new configuration it uses 100% of all of the limited cores available for few minutes. During this time the server won’t work for my users. This is why I would want to compile directly from my own machine and just upload results there instead.

Alternatively you can avoid doing any building locally and have the target host build by adding --build-host <target> to your nixos rebuild command.

Yes I know this and already mentioned in the first post that this is currently the only way for me to deploy new changes.

EDIT: I noticed that I didn’t answer your questions and edited my post :bowing_man: