title: How to compile a static musl binary of a Rust project with native dependencies?
body: <p>I have a project with dependencies on Hyper and Diesel, and because of that, on native libraries OpenSSL and libpq. The project builds on nightly Rust because it uses compiler plugins.</p>
  
  <p>My current attempt is to build on a Docker container. I have the MUSL libc and the libraries <code>make</code>'d and installed with prefix <code>/usr/local/musl</code>. I run <code>cargo</code> with the following command: (Not sure if some of the options are redundant, I'm not too well-versed with the compiler chain, and not even sure if they end up to the linker, but I have to try, right.)</p>
  
  <pre class="lang-none prettyprint-override"><code>LDFLAGS="-static -L/usr/local/musl/lib" \
  LD_LIBRARY_PATH=/usr/local/musl/lib:$LD_LIBRARY_PATH \
  CFLAGS="-I/usr/local/musl/include" \
  PKG_CONFIG_PATH=/usr/local/musl/lib/pkgconfig \
  cargo build --release --target=x86_64-unknown-linux-musl
  </code></pre>
  
  <p>When I <code>ldd</code> the resulting file, it reveals this:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ ldd server
  linux-vdso.so.1 (0x00007fffb878e000)
  libpq.so.5 =&gt; /usr/local/musl/lib/libpq.so.5 (0x00007f4d730e7000)
  libssl.so.1.0.0 =&gt; /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f4d72e82000)
  libcrypto.so.1.0.0 =&gt; /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f4d72a85000)
  libc.so =&gt; /usr/local/musl/lib/libc.so (0x00007f4d727f6000)
  libdl.so.2 =&gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4d725f2000)
  libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4d72246000)
  /lib/ld64.so.1 =&gt; /lib64/ld-linux-x86-64.so.2 (0x000055e2124a2000)
  </code></pre>
  
  <p>There's all that dynamically linked stuff, and some even with the "x86_64-linux-gnu" chain! What went wrong?</p>
  
  <p>I can make statically linked, simple pure-Rust projects without problems. <code>ldd</code> says that they are statically linked, and they run without problems, unlike the executable I have problems with.</p>
  
  <p>When I used <code>--verbose</code> with Cargo, I got the following <code>rustc</code> command that actually builds the executable: <a href="http://pastebin.com/ywv0zNBK" rel="noreferrer">http://pastebin.com/ywv0zNBK</a> (Oops, that one had a custom <code>outdir</code> and <code>-Z print-link-args</code>, added by me)
  Adding the <code>print-link-args</code> flag, I got the following linker command: <a href="http://pastebin.com/Aw43qd7h" rel="noreferrer">http://pastebin.com/Aw43qd7h</a></p>
  
  <p>How do I get <code>cargo</code> or <code>rustc</code> to believe that I want a static binary?</p>