How to slim down a haskell package docker image

I have a Haskell web server.
I wrote a dockerfile that does (simplified)

  • from nixos/nix
  • cabal2nix . > foo.nix
  • haskellPackage.callPackage ./foo.nix > default.nix
  • nix-build

Resulting image size is 5GB. How can I slim it down? I guess what I want is to leave only neccessary runtime for executable, and delete all the build tools.

You need to use justStaticExecutables from the hlib.

1 Like

Thank you for the tip! But how can I use it? Could you send me some documentation link?

@domenkozar I’m sorry. I thought I did what you said but it wasn’t. Could you please tell me how to use it?

I managed to use justStaticExecutables to reduce the size.
The official documentation was this nixpkgs/doc/language-frameworks/haskell.section.md.
I just had to wrap the output of callPackages with justStaticExecutables.
The other important thing was Docker image size does’nt get any smaller than it’s previous layer so I had to call nix-collect-garbage everytime I run any nix commands.
I could reduce from 4.5GB to 0.8GB!
Thanks a lot @domenkozar

1 Like