1. 13
    1. 6

      You don’t actually need either Docker or a musl-based distro on the build host. Many GNU/Linux distros include musl and GCC wrapper for building with it in their repositories, and the OPAM repository provides virtual packages for building the compiler with musl instead of the system glibc and with static linking option enabled.

      For example, on Fedora you can do this (assuming the latest “oldstable” OCaml 4.14.1):

      $ sudo dnf install musl-libc-static musl-devel musl-gcc
      
      $ opam switch create 4.14.1-musl ocaml-variants.4.14.1+options \
         ocaml-option-musl ocaml-option-static
      

      The command format is opam switch create <arbitrary name> <base package> <option packages>.

      Then do opam switch 4.14.1-musl and you are ready to build static executables. There are more virtual packages for building the compiler with non-default options, such as ocaml-option-32bit.

      There’s one thing I want to see improved, though: I wish dune had a way to pass -cclib -static from the command line instead of adding it to the file. Maybe there is a way but I can’t see it? I’ve been using the musl option to build release binaries of soupault for Linux, but the procedure is to uncomment a line in src/dune. It’s workable but could be much simpler and friendlier to automated workflows.