A couple of weeks ago I saw mozgan’s thread on freebsd-pkgbase about pkg-be, a pkg plugin that snapshots a ZFS boot environment before upgrades. It finally got me off my butt to fix something I had been putting off in Daemonless: our FreeBSD container images were huge, and the only cleanup we had was a pile of rm -rf lines scattered across Containerfiles.

Post-install rm -rf is a bad habit. Delete headers or docs after installing packages and pkg check -s immediately complains about missing files. Run pkg upgrade later and everything comes right back. Worse, most of the weight sits inside compiler packages that the app still depends on at runtime.

Where the Size Actually Comes From

The postgres binary itself is 9.4 MB. Yet installing postgresql18-server on top of a 195 MB FreeBSD base image produced a 2.63 GB container (703 MB compressed download).

FreeBSD binary packages are built with general-purpose default options. Because Postgres enables LLVM JIT, pkg pulls in the entire llvm19 package, even though Postgres only links one shared library (libLLVM.so.19.1) at runtime:

Before pkg-slim 2.63 GB uncompressed (703 MB download)
23%
dev 43%
toolchain 30%
After pkg-slim 586 MB uncompressed (185 MB download)
586 MB
-77.7% removed at pkg extraction time
Kept 586 MB FreeBSD base, Postgres, ICU, and libLLVM.so.19.1 (118 MB) for runtime JIT
dev tier 1,090 MB 946 MB static .a archives, 130 MB Python test/ and idlelib, 14 MB headers and pkgconfig
toolchain tier 762 MB 412 MB clang/lld/opt binaries, 350 MB libclang/MLIR/LLDB and LLVM headers
docs tier 82 MB share/doc, man pages, info pages, and examples

The same fan-out hits media and web apps: ffmpeg and libvips pull in mesa-libs and llvm19, plus openblas, gcc14, and GNU binutils. That single dependency chain is why ONLYOFFICE started as a 2.8 GB download and Emby was 2.2 GB.

Why I Stopped Writing a pkg Plugin

With the pkg-be thread fresh in my head, I started scoping a C pkg plugin to filter files during extraction. Halfway through sketching the hooks, I stopped.

pkg already does this. Setting FILES_IGNORE_GLOB in pkg.conf tells pkg to skip matching paths at extraction time. Because pkg knows those files were intentionally excluded, pkg check -s -a stays clean, the files never land in an OCI layer, and the rules survive future upgrades. Even better, pkg.conf is UCL and merges drop-in files natively:

AUTOCLEAN = true;
.include(try=true, glob=true, duplicate=merge) "/usr/local/etc/pkg/daemonless-slim.d/*.conf"

Writing a C plugin meant coupling to libpkg’s ABI and building binaries for every FreeBSD release and architecture. A small POSIX shell wrapper around UCL drop-ins did the exact same job with none of that overhead.

Meet pkg-slim

pkg-slim is a small package baked into base-core. It ships three tier templates under /usr/local/etc/pkg/daemonless-slim.available/:

  • docs (10-docs.conf): /usr/local/share/doc, man pages, examples, and info pages.
  • dev (20-dev.conf): /usr/local/include, pkgconfig files, static .a archives, Python’s test/, idlelib/, and turtledemo/, gir-1.0, and OpenJDK dev files (jmods, src.zip, ct.sym, demo, include).
  • toolchain (30-toolchain.conf): LLVM/Clang, GCC (including libgccjit), and GNU binutils (ld, as, objdump, libopcodes, libbfd), while keeping runtime shared libraries like libLLVM*.so* and /usr/local/lib/gcc*/lib*.so*.

base-core enables only the docs tier by default so multi-stage builder stages keep their compilers and headers intact. Runtime images opt into all three tiers with one line:

RUN pkg-slim enable && \
    pkg update && \
    pkg install -y ${PACKAGES} && \
    pkg clean -ay && \
    rm -rf /var/cache/pkg/* /var/db/pkg/repos/*

For app-specific weight that does not belong in a generic tier, --ignore and --ignore-dir write rules into a managed 90-local.conf. In ONLYOFFICE, skipping the in-app HTML help files alone saved 566 MB:

RUN pkg-slim enable \
        --ignore '/usr/local/www/onlyoffice/documentserver/web-apps/apps/*/main/resources/help/*' && \
    pkg update && \
    pkg install -y ${PACKAGES}

If a downstream image builds FROM a slimmed image (like cnpg-postgres or immich-postgres compiling extensions from source), getting headers back is two lines:

RUN pkg-slim disable && \
    pkg update && \
    pkg install -fy postgresql17-client postgresql17-server gettext-runtime icu llvm19

The -f matters: pkg already considers those packages installed, so -fy tells it to re-extract the skipped headers and static libraries.

The Results

Postgres 18 was the first canary. With pgvector and forced LLVM JIT (jit_above_cost = 0) passing, pkg check -s -a clean, and a full /usr/local ELF ldd scan showing zero missing libraries:

  • Compressed download: 703 MB -> 185 MB (-73.7%)
  • Uncompressed image: 2.63 GB -> 586 MB (-77.7%)
  • On ZFS (du -sh /): 1.1 GB -> 282 MB (-74%)

Before (703 MB download, 2.63 GB uncompressed):

Install PostgreSQL in fjord showing 703 MB download before pkg-slim

After pkg-slim (185 MB download, 586 MB uncompressed):

Install PostgreSQL in fjord showing 185 MB download after pkg-slim

Across the first 20 images pushed to GHCR, pkg-slim cut 7.63 GB of compressed downloads (exact amd64 layer sums via skopeo inspect --raw):

ImageBeforeAfterSaved
onlyoffice2,800 MB1,254 MB-1,546 MB (-55.2%)
nocodb1,756 MB905 MB-850 MB (-48.4%)
dispatcharr1,382 MB541 MB-841 MB (-60.8%)
readmeabook1,401 MB613 MB-789 MB (-56.3%)
qbittorrent945 MB340 MB-605 MB (-64.1%)
postgres703 MB185 MB-518 MB (-73.7%)
ffmpeg649 MB287 MB-362 MB (-55.7%)
grimmory880 MB519 MB-361 MB (-41.0%)
bazarr658 MB298 MB-360 MB (-54.7%)
audiobookshelf652 MB297 MB-355 MB (-54.4%)
smokeping283 MB167 MB-116 MB (-40.9%)
sabnzbd190 MB130 MB-60 MB (-31.7%)

Downstream cnpg-postgres picked up the reduction automatically on rebuild and dropped to a 187 MB download.

What’s Next

Our image audit tool (dbuild analyze) now reads active pkg-slim drop-ins and measures actual on-disk compiler payloads instead of flagging libLLVM.so as bloat. Next up is rolling pkg-slim across the rest of the fleet and cleaning up per-image layer duplication (like COPY followed by chown -R doubling node_modules) that became obvious once the compiler fat was gone.

If you build FreeBSD containers or jails and want to take a look, the whole thing is a couple hundred lines of POSIX sh and UCL at github.com/daemonless/pkg-slim.