NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.34k stars 14.3k forks source link

Building an oci container image with babashka results in > 600 megabyte image #352635

Closed Ramblurr closed 3 weeks ago

Ramblurr commented 3 weeks ago

Describe the bug

Building an OCI container image that includes babashka results in an unexpectedly large image size of over 600MB (the .tar.gz).

The image contains many unnecessary dependencies like openjdk 21, GTK3, systemd, cups, and ICU that aren't required for babashka's core functionality.

Steps To Reproduce

  1. Create a minimal nix file with this content:

    { pkgs ? import <nixpkgs> { } }:
    pkgs.dockerTools.buildLayeredImage {
      name = "babashka-test";
      tag = "latest";
      fromImage = null;
      contents = [
        pkgs.dockerTools.usrBinEnv
        pkgs.dockerTools.binSh
        pkgs.babashka
      ];
    }
  2. Build the image: nix-build

  3. ls -alh $(readlink result) -> the resulting tar.gz iz over 600 M

  4. Load and analyze with dive: sudo docker load < result; sudo dive babashka-test:latest

Expected behavior

The container image should only include babashka and its essential runtime dependencies, resulting in a much smaller image size (likely <100MB).

I can download and extract babashka from https://github.com/babashka/babashka/releases (the babashka-1.12.194-linux-amd64-static.tar.gz) and get a self contained binary that is 65M.

Screenshots

Using dive, the final layer shows many unnecessary dependencies:

🖼️ Screenshot: Largest packages from Just the final layer (click) ![image](https://github.com/user-attachments/assets/bd28eba4-f070-4904-9231-8b69edbe1ed4)
🖼️ Screenshot: The largest packages from all the layers (click) ![image](https://github.com/user-attachments/assets/70ee5f86-7dc3-45cf-84f2-b1efae0616e7)

Notify maintainers

@bandresen @bhougland @DerGuteMoritz @jlesquembre @thiagokokada

jlesquembre commented 3 weeks ago

could you try with babashka-unwrapped instead on babashka?

Ramblurr commented 3 weeks ago

Wow. babashka-unwrapped produces a tarball of 46M, compared to babashka which produces 624M.

What is the difference? I thought the wrapped version just provided readline functionality?

jlesquembre commented 3 weeks ago

The :deps entry is managed by deps.clj and requires a java installation to resolve and download dependencies.

The JDK. Babashka requires a java installation to resolve and download dependencies

Ramblurr commented 3 weeks ago

The JDK. Babashka requires a java installation to resolve and download dependencies

Hm ok, that makes sense, I was wondering why the JDK was being dragged along.

Thanks for your responses your time :pray: !

jlesquembre commented 3 weeks ago

Yep, copy/paste error, too many things in parallel. I updated the comment with what I wanted to paste