Verifying musl static builds for a Rust binary with C deps, locally on Apple Silicon, via messense/rust-musl-cross
RecipeVerified 30 Sep 2026Holds anywhere
Recipe. When to use it, the steps, and what showed that they work.
When to use#
A Rust workspace with C dependencies (libgit2 via git2 with vendored-openssl, tree-sitter grammars, libz-sys) needs statically linked Linux release binaries, and you want to verify the musl build locally before pushing a release workflow.
Steps#
- Build in the standard cross image, caching toolchain and registry in named volumes (mount /root/.rustup and /root/.cargo/registry separately — mounting over all of /root/.cargo shadows rustup's shims): docker run --rm -v $PWD:/src -v musl-reg:/root/.cargo/registry -v musl-rustup:/root/.rustup -v musl-target:/src/target -w /src messense/rust-musl-cross:aarch64-musl sh -c '...'.
- The image's rustc is often older than the workspace rust-version: run
rustup toolchain install <ver> --profile minimal && rustup default <ver> && rustup target add <musl-target>inside the container; the volume keeps it for later runs. - libz-sys builds zlib from source for the target, so libgit2-sys gets its zlib — no system musl zlib needed. openssl-src and tree-sitter compile for musl out of the box. Result:
filereports statically linked / static-pie,lddsays 'not a dynamic executable'. - musl runs natively on Apple Silicon docker for aarch64-musl; the x86_64-musl image runs under QEMU (slow but works, ~2x a native build).
- Run and test the binary inside the same container (arm64 linux binaries do not run on macOS).
Evidence#
rgit (rayandrew/rgit) release build verified 2026-09-30: both x86_64 and aarch64 musl binaries static, rgit --human diff and diff -W byte-identical to modern git output in /tmp/wtest. Needed a musl fallback in userdiff.rs (below).
Also found by searching
- docker run messense/rust-musl-cross:aarch64-musl cargo build -> error: package requires rustc 1.85 or newer
- I want fully static Linux binaries of my Rust tool with libgit2 and openssl, tested locally on my M-series Mac before release
- how do I build and verify x86_64 and aarch64 musl static Rust binaries with C dependencies using docker on macOS?
- cross-compile git2 vendored-openssl tree-sitter to x86_64-unknown-linux-musl, mounting /root/.cargo breaks rustup shims
- cargo build --target x86_64-unknown-linux-musl fails linking openssl libgit2 on my Mac
- how can I test static musl release binaries for my Rust project locally on an M1 Mac?
- need statically linked Linux binary for rust with git2 and tree-sitter, build in docker
- cross compile rust to musl with C dependencies, rust-musl-cross image, rustc too old for rust-version