Skip to content

Move to Zig for smaller static binary? #242

Open

Description

@blenessy

I was a big under-stimulated during X-mas break and decided to port tini to Zig (0.15.2).
The experience was very smooth (took me roughly 20h) to port and get all tests passing.

You can checkout my fork here and the following commit message regarding how to build it:
blenessy@b34e39d

Please let me know if you are open for a PR - then I'll invest more time in changing the rest
of the files (build system, scripts, README.md, etc.).

Why?

There are other advantages over C, but this is the biggest win:

  • static binary size went down to 44KiB.

Why not Rust ?

  • Nothing wrong with it, but I prefer simpler languages (python, zig, golang).

Activity

  1. polarathene commented on Jun 9, 2026

    @polarathene

    There are other advantages over C, but this is the biggest win:

    • static binary size went down to 44KiB.

    You know the static binary size for tini on this project is 42.8 KiB right? (0.19.0 release)

    Image

    You were likely looking at the tini-static-amd64 binary that is 850KB, as it attempts to statically link glibc (which can be problematic for some binaries or glibc releases to do properly). Even when it works, it's still "fat" by comparison to musl.

    Sadly, they did not publish static musl builds for other archs, so you'd need glibc (or the fat static glibc variants).


    I haven't been keeping up with Zig, so not sure if the static build you produced is using musl or their own libc (I think Zig had been making progress on something like that?), but regardless your current result is not smaller than what is already published here.

    If you did a port in Rust, it'd probably be lower if implemented with say eyra / rustix, effectively skipping libc. I assume Zig can do similar. For example in Rust I've produced a static "Hello World" that is only 456 bytes (outputs the text), with source code that isn't crazy low-level (eg: assembly focused), I couldn't figure out how to do that with Zig.

  2. cpuguy83 commented on Jun 19, 2026

    @cpuguy83

    Toolchains being available is far better than making a smaller binary here.

  3. polarathene commented on Jun 20, 2026

    @polarathene

    @cpuguy83 are you referring to availability of GCC/Clang toolchains compared to Zig or the language difference between Zig and C?

    You can still use C and build with Zig via zig cc:

    # Create the header file without depending upon cmake:
    echo -e '#define TINI_VERSION "0.19.0"\n#define TINI_GIT     " - git.369448a"' > src/tiniConfig.h
    
    # Build via `zig cc` from a x86_64 glibc host, 
    # while targeting aarch64 musl (No QEMU required):
    zig cc -target aarch64-linux-musl \
      -Oz -std=gnu99 -static \
      src/tini.c -o tini-static

    That produces a 53KB tini binary. If instead targeting x86_64-linux-musl it would be 46KB.

    By comparison (with standard GCC):

    • x86_64-linux-gnu is a 21KB (dynamic) or 797KB (static)
    • x86_64-linux-musl (via musl-gcc / musl-clang) static builds are 71KB / 85KB respectively
      • Build environment was Fedora 42 musl-libc-static package (musl 1.2.5)
      • No notable improvement when building with a checkout of the v0.19.0 release tag.
    • x86_64-linux-musl (via Alpine 3.24 container, musl 1.2.6) produces a static build of 60KB.
      • 48KB with Alpine 3.11 (Dec 2019, musl 1.1.24). In Aug 2020 musl 1.2.1 changed their allocator to mallocng, which might have contributed towards the size bump.

    zig cc works well for the task, it's simple to get into your local environment and is competitive on the size, no porting needed. It would also make publishing releases with musl for other arches more viable, as clearly that was a friction point with prior releases in the past?

    FWIW: I did experiment with a very minimal tini like program in Zig and Rust, they both achieved roughly 1.6KB (freestanding target, no libc), while the more practical variants that'd use libc along with std ranged from 30-150KB depending on feature set, DX, and UX expectations. Not that anyone is going to really care much for such aggressive optimization to save less than 1MB 😅

  4. polarathene commented on Jun 20, 2026

    @polarathene

    Just sharing this reference for building the project, demonstrating in further detail/comparison to how zig compares to gcc/clang, notably with cross-compilation to musl from glibc hosts when targeting multiple archs without paying QEMU tax 😝

    Basic compilation - 797KB (static glibc)

    The README has this build guidance:

    tini/README.md

    Lines 260 to 264 in 369448a

    Building Tini
    -------------
    If you'd rather not download the binary, you can build Tini by running
    `cmake . && make`.

    On a glibc host this will produce tini dynamically linked to glibc, and tini-static with statically linked glibc (rarely a good idea with glibc and awfully fat given the program):

    $ git clone --depth 1 https://github.com/krallin/tini /tmp/tini && cd /tmp/tini
    $ cmake . && make
    
    # 20.7KB (dynamic) vs 797KB (static):
    $ du --bytes tini tini-static
    20728   tini
    796664  tini-static

    That's only simple provided you've already got the toolchain / deps setup, the repo itself is providing a bunch of config via delegating to cmake + make to keep that simple.

    You can get similar benefits with modern toolchains such as Zig and Rust, both of which are ridiculously simple to acquire on your build system, modern distros often package these too. Arguably their equivalent supporting configuration files are far simpler to grok too if that is ever needed.

    Compile from x86_64 glibc to x86_64 musl (via musl-gcc) - 71KB (static musl)

    So if we want to build for musl on such a host, we need something like musl-gcc or musl-clang installed (for convenience), then we can do the following:

    $ CFLAGS='-DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37' \
      cmake . -DCMAKE_C_COMPILER=musl-gcc -DMINIMAL=1 \
      && make
    
    # 20KB (dynamic) vs 71KB (static):
    du --bytes tini tini-static
    20096   tini
    71184   tini-static

    Those are stripped builds, so I'm not quite sure why they're fatter than the GH release binaries? Something differs in the build environment since 2020 I guess? 🤷‍♂️ (for reference, this is built with Fedora 42 with Clang 20, GCC 15.2, musl 1.2.5)

    EDIT: Presumably due to changes in musl since April 2020? (There was a compiler fix in May 2025 to support musl 1.2.5, PR had been opened since April 2024)

    Built on Alpine 3.11 (Dec 2019), with musl 1.1.24, this produces a 48KB tini-static:

    $ docker run --rm -it --workdir /opt/tini alpine:3.11
    $ apk add git gcc musl-dev
    $ /lib/ld-musl-x86_64.so.1
    musl libc (x86_64)
    Version 1.1.24
    
    $ git clone --depth 1 https://github.com/krallin/tini .
    $ echo -e '#define TINI_VERSION "0.19.0"\n#define TINI_GIT     " - git.369448a"' > src/tiniConfig.h
    $ gcc -Os -std=gnu99 -static -Wl,--strip-all src/tini.c -o tini-static
    
    $ du -h tini-static
    48.0K   tini
    • Same size as results above if building with cmake . && make, or cloning from the v0.19.0 tag and building.
    • If building in a modern Alpine 3.24 (June 2026, musl 1.2.6) container it produces a 60KB binary.
    • For context mallocng was introduced with Musl 1.2.1 (Aug 2020), which may have contributed to increase in size since.

    Cross-compile from x86_64 glibc to aarch64 musl (via clang) - 85KB (static musl)

    That's all good, but now if you want to cross-compile for musl from say x86_64 to aarch64 on a glibc host... well that starts to veer into territory where zig cc is far more practical.

    Collapsed due to verbosity

    For Fedora we can install packages into a new sysroot for a different arch, we need the musl-libc-static package (alternatively you could acquire a musl sysroot for a foreign arch outside of your distro, but I'm not going there given the argument against a toolchain like zig):

    dnf install -yq clang lld
    
    # Minimal setup required for sysroot? (355 MB, depends on GCC files):
    dnf --forcearch=aarch64 --installroot=/opt/sysroot/aarch64 --use-host-config install -yq musl-libc-static gcc
    dnf --installroot /opt/sysroot/aarch64 clean all
    
    # NOTE: The `=` prefix with `-isystem`/`-L` args is syntax that `clang` parses as prepending the `--sysroot` path:
    $ clang -target aarch64-linux-musl -fuse-ld=lld -nostdinc \
      --sysroot /opt/sysroot/aarch64/usr \
      -isystem '=/aarch64-linux-musl/include' \
      -L '=/aarch64-linux-musl/lib64' \
      -static -Oz -std=gnu99 -Wl,-s \
      -DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37 -DTINI_MINIMAL=1 \
      -Isrc src/tini.c -o tini-static

    NOTE:

    • musl-clang is a shell script that does similar to the above, but as it doesn't split the compilation and linker flags separately, this project's use of -Werror will fail a build if you attempt to use with cmake like was shown previously with musl-gcc.

    • musl-gcc is also a wrapper script, but much simpler and invokes GCC to link musl via this:

      #!/usr/bin/sh
      exec "${REALGCC:-gcc}" "$@" -specs "/usr/x86_64-linux-musl/lib64/musl-gcc.specs"
    • Both musl-clang/musl-gcc have hard-coded the arch. I'm not familiar with GCC enough to know how compatible it'd be to just swap out the specs file to support linking to musl aarch64 via the /opt/sysroot/aarch64/ sysroot files.

    • LLD provides support to link to foreign archs, so -fuse-ld=lld is useful here.

    AFAIK, this isn't much nicer to configure cmake for (at least with clang):

    cmake . \
      -DCMAKE_C_COMPILER=clang \
      -DCMAKE_C_FLAGS='-target aarch64-linux-musl --sysroot=/opt/sysroot/aarch64 -nostdinc -isystem =/usr/aarch64-linux-musl/include -DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37' \
      -DCMAKE_EXE_LINKER_FLAGS='-target aarch64-linux-musl --sysroot=/opt/sysroot/aarch64 -fuse-ld=lld -L=/usr/aarch64-linux-musl/lib64' \
      -DMINIMAL=1 \
      && make
    
    # 17.7 KB (dynamic) vs 84.8KB (static)
    du --bytes tini tini-static
    17680   tini
    84760   tini-static
    
    $ file tini tini-static
    tini:        ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=c460cd40da8227577ed3da9bd8082b176e708ad0, stripped
    tini-static: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, BuildID[sha1]=f529d0bad5cbee874ebace75350816914a99d3a9, stripped

    Building without cmake + make (via zig cc) - 46KB (static musl)

    CMake is used to produce some dynamic build variables but you really don't need all of that in this case as it's two variables, the version and git ref (which unless you're building from the master branch instead of cloning the 0.19.0 tag from the April 2020 release, is going to be static anyway, or simple to manage directly given development pace).

    You can add the file to src/ (otherwise at project root and use -I ./):

    src/tiniConfig.h: (generated via cmake from this section)

    #define TINI_VERSION "0.19.0"
    #define TINI_GIT     " - git.369448a"

    The CMakeLists.txt file adds a bunch of compilation and linker flags, the bulk of which have little relevance for us to produce a static binary with musl as the libc - which when using zig cc (or Rust) with a *-musl target will default to static linking implicitly, but I suppose we can also be explicit.

    tini/CMakeLists.txt

    Lines 56 to 57 in 369448a

    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Werror -Wextra -Wall -pedantic-errors -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat")
    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-s")

    tini/CMakeLists.txt

    Lines 80 to 85 in 369448a

    include_directories ("${PROJECT_BINARY_DIR}")
    add_executable (tini src/tini.c)
    add_executable (tini-static src/tini.c)
    set_target_properties (tini-static PROPERTIES LINK_FLAGS "-Wl,--no-export-dynamic -static")

    So out of all that for a static build -std=gnu99 -static will be sufficient (but AFAIK should be fine without these?), you can add the others if there's some value to them to you 🤷‍♂️

    Then there's the opt-ins to customize the build if you'd like those:

    tini/CMakeLists.txt

    Lines 9 to 14 in 369448a

    # Build options
    option(MINIMAL "Disable argument parsing and verbose output" OFF)
    if(MINIMAL)
    add_definitions(-DTINI_MINIMAL=1)
    endif()

    tini/README.md

    Lines 266 to 273 in 369448a

    Before building, you probably also want to run:
    export CFLAGS="-DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37"
    This ensure that even if you're building on a system that has old Linux Kernel
    headers (< 3.4), Tini will be built with child subreaper support. This is
    usually what you want if you're going to use Tini with Docker (if your host
    Kernel supports Docker, it should also support child subreapers).

    # 53KB (46KB if building for x86_64 arch)
    zig cc -target aarch64-linux-musl \
      -DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37 \
      -Oz -std=gnu99 -static \
      src/tini.c -o tini-static

    That's vastly simpler than the clang approach for cross-compiling from a x86_64 glibc host to a aarch64 musl target. No sysroot providing the musl libc for that target arch required, Zig handles that for you.

    NOTE:

    • For the glibc *-gnu targets, you can append .2.34 as a suffix to the target to specifically dynamically link for that version of glibc, increasing it's compatibility (common problem for build environments when dynamic linking glibc). Static linking glibc is supported, but only via -target native, there's only stubs for dynamic linking and I don't think there's support for static linking a foreign arch with glibc via sysroot.
    • Converting that zig command to work with cmake will fail. zig cc itself needs to be wrapped since it's a subcommand to invoke, and some of the linker args (Zig forces it's own internal linker which doesn't support all the flags LLD does and ignores -fuse-ld to invoke a different linker). CMake can apparently configure the linker separately, but you can't call LLD directly with the format of the linker args the project adds, it'd have to be switched to something like musl-gcc/clang instead (clang if cross-compiling from glibc host to musl target).
  5. krallin commented on Jun 20, 2026

    @krallin
    Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions