Cross compiling Rust Linux Musl binaries on Mac OSX

Thanks to a brew package by FiloSuttile, and a post by Timryan, it’s actually pretty easy.

# Install the necessary C cross-compiler
# This takes a while
brew install FiloSottile/musl-cross/musl-cross

# Install the rust target you want
rustup target add x86_64-unknown-linux-musl

Now you need to change your Cargo configuration to use your newly installed musl linker

# ~/.cargo/config
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

Now you should be able to compile your binary

# Build your app for that target
cargo build --release --target x86_64-unknown-linux-musl

# Your build is in /target/x86_64-unknown-linux-musl/release/your-gizmo