Skip to content
View source of this page

Image Slimming (pkg-slim)

pkg-slim is an opt-in FILES_IGNORE_GLOB profile manager shipped in Daemonless base images (base-core, base-exec, base-rcd, and base). It lets Containerfile builds skip documentation, development headers, static archives, and unused compiler binaries during pkg install without deleting files after the fact.

Because FreeBSD pkg skips matching paths at extraction time instead of relying on post-install rm -rf:

  • pkg check -s -a stays clean with no missing-file errors.
  • Unneeded files are never written to an OCI layer and whiteout-deleted later.
  • Shared runtime libraries (such as libLLVM*.so* for PostgreSQL JIT and /usr/local/lib/gcc*/lib*.so* for GCC-linked ports) stay intact.

For example, enabling pkg-slim on ghcr.io/daemonless/postgres:18-pkg-latest reduces the image from ~2.63 GB to ~732 MB (-72%) while keeping pgvector and LLVM JIT working.

How It Works

Daemonless base images install /usr/local/etc/pkg.conf from pkg-slim:

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

Tier templates live in /usr/local/etc/pkg/daemonless-slim.available/. When you run pkg-slim enable, it symlinks the requested tiers into /usr/local/etc/pkg/daemonless-slim.d/, where pkg merges every FILES_IGNORE_GLOB array automatically. App-specific patterns are stored in /usr/local/etc/pkg/daemonless-slim.d/90-local.conf.

Base image default

base-core enables only the docs tier (10-docs.conf) by default. Headers, static archives, and compiler toolchains remain untouched in base images and multi-stage builder stages unless a Containerfile opts into dev or toolchain.

Tiers

Tier Drop-in What it skips What it keeps
docs 10-docs.conf /usr/local/share/doc/*, man pages (/usr/local/share/man/*, /usr/local/man/*, Perl man pages), /usr/local/share/examples/*, /usr/local/share/info/* Runtime data files under /usr/local/share/
dev 20-dev.conf /usr/local/include/*, /usr/local/libdata/pkgconfig/*, static archives (/usr/local/lib/*.a, /usr/local/llvm*/lib/*.a) Shared libraries (*.so*)
toolchain 30-toolchain.conf LLVM/Clang binaries, headers, CMake/Clang/MLIR/LLDB libs, GCC compilers and internal passes, GNU binutils (ld, as, ar, nm, objdump, strip, etc.) Runtime shared libraries (libLLVM*.so*, /usr/local/lib/gcc*/lib*.so*)

Commands

1
2
3
4
5
pkg-slim enable [tiers...] [--ignore-dir <dir>] [--ignore <glob>]
pkg-slim ignore-dir <dir>...
pkg-slim ignore <glob>...
pkg-slim disable
pkg-slim status
Command Behavior
pkg-slim enable Replaces the enabled tier set. With no tier names, enables all three tiers (docs dev toolchain). Preserves 90-local.conf and custom *.conf drop-ins.
pkg-slim ignore-dir <dir>... Appends <dir>/* to the managed 90-local.conf file.
pkg-slim ignore <glob>... Appends exact file or glob patterns to 90-local.conf.
pkg-slim disable Removes all *.conf files under /usr/local/etc/pkg/daemonless-slim.d/ to restore stock pkg extraction behavior.
pkg-slim status Prints active drop-ins and the merged FILES_IGNORE_GLOB patterns reported by pkg config. Exits non-zero if drop-ins exist but pkg reports 0 patterns.

Custom rules and path safety

90-local.conf is managed by pkg-slim and should not be edited by hand; put hand-written UCL rules in a separate *.conf file under /usr/local/etc/pkg/daemonless-slim.d/. Paths passed to --ignore-dir and --ignore must live under /usr/local/, be at least one directory below /usr/local/, and cannot start with a wildcard top-level component.

Containerfile Patterns

Standard Runtime Image

Enable all default tiers (docs dev toolchain) right before installing runtime packages:

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

Multi-Stage Builds

Leave the builder stage untouched so compilers, headers, and static libraries install normally during compilation, and enable pkg-slim only in the final production stage:

FROM ghcr.io/daemonless/base-exec:15-latest AS builder
RUN pkg update && \
    pkg install -y ... && \
    ...

FROM ghcr.io/daemonless/base-exec:15-latest
COPY --from=builder /out /tmp/out
RUN pkg-slim enable && \
    pkg update && \
    pkg install -y ${PACKAGES} && \
    pkg add /tmp/out/*.pkg && \
    rm -rf /tmp/out && \
    pkg clean -ay && \
    rm -rf /var/cache/pkg/* /var/db/pkg/repos/*

App-Specific Ignore Patterns

Add extra directories or globs inline with enable (or separately with pkg-slim ignore-dir / pkg-slim ignore):

1
2
3
4
5
6
7
RUN pkg-slim enable \
        --ignore-dir /usr/local/share/chromium/locales \
        --ignore '/usr/local/llvm*/lib/LLVMPolly.so' && \
    pkg update && \
    pkg install -y ${PACKAGES} && \
    pkg clean -ay && \
    rm -rf /var/cache/pkg/* /var/db/pkg/repos/*

Selective Tiers or Opting Out

If an application needs headers at runtime (for example, building native plugins on startup), enable only the safe tiers:

1
2
3
RUN pkg-slim enable docs toolchain && \
    pkg update && \
    pkg install -y ${PACKAGES}

If an image needs documentation or man pages (which base-core skips by default via 10-docs.conf), disable pkg-slim before installing packages:

1
2
3
RUN pkg-slim disable && \
    pkg update && \
    pkg install -y ${PACKAGES}

Building Downstream Images (FROM a Slimmed Image)

pkg-slim remains enabled in the final runtime image. If you write a downstream Containerfile FROM a slimmed image (for example, compiling a PostgreSQL extension FROM ghcr.io/daemonless/postgres) and need headers or compiler tools back:

  1. Run pkg-slim disable so pkg extracts headers and toolchains again.
  2. Reinstall the already-installed parent packages with pkg install -fy so pkg re-extracts the files that were skipped when the parent image was built.
# Example: builder stage compiling a PostgreSQL extension FROM daemonless/postgres
RUN if command -v pkg-slim >/dev/null 2>&1; then \
        pkg-slim disable && \
        pkg update && \
        pkg install -fy \
            postgresql${PG_VERSION}-client \
            postgresql${PG_VERSION}-server \
            gettext-runtime \
            icu \
            llvm19; \
    fi

Why -fy is required

Running pkg install -y without -f treats packages already installed in the parent image as up to date and will not re-extract their skipped headers or static libraries.

Verifying a Slimmed Image

After enabling pkg-slim in an image, verify both package integrity and runtime shared libraries before publishing:

  1. Check active profile:

    pkg-slim status
    
    All three default tiers (10-docs.conf, 20-dev.conf, 30-toolchain.conf) report 50 active patterns.

  2. Check package checksums:

    pkg check -s -a
    
    Confirm exit code 0 with no missing-file errors.

  3. Check ELF shared library links: Scan binaries and shared libraries under /usr/local with ldd to verify no runtime binary lost a required .so dependency.

  4. Run CIT and feature checks: Run dbuild test (or build-test.sh) and exercise runtime features that depend on preserved libraries (such as LLVM JIT queries in PostgreSQL or hardware transcoding in media apps).