Skip to content

Unexpected dependencies when building docker image #155924

@torgeirsh

Description

@torgeirsh

I have the following default.nix:

let
    nixpkgs = import (fetchTarball https://github.com/nixos/nixpkgs/archive/21.11.tar.gz) {};
in
    nixpkgs.haskellPackages.callCabal2nix "test" ./. {}

And test.cabal looks like this:

cabal-version: 1.12

name: test
version: 0.1.0.0

library lib
    exposed-modules: Lib
    build-depends: base

executable test
    main-is: Main.hs
    build-depends: base, lib

Then I use docker.nix to build a docker image:

let
    nixpkgs = import (fetchTarball https://github.com/nixos/nixpkgs/archive/21.11.tar.gz) {};
in nixpkgs.dockerTools.buildLayeredImage {
    name = "test";
    contents = import ./default.nix;
}

The resulting docker image is almost 400 MB, and contains ghc, gcc, documentation, and so on. However, if I remove the library from the cabal file, the resulting docker image is only 14 MB, and doesn't contain anything superfluous. I guess that when the cabal file contains a library, it becomes part of the build result, and since a library isn't useful on its own, it depends on ghc? However, I don't want to make the library available, it's only for building the executable. How can I get a lean docker image without a build environment, while keeping the library structure of the cabal project?

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: questionRequests for a specific question to be answered6.topic: closure sizeThe final size of a derivation, including its dependencies6.topic: haskellGeneral-purpose, statically typed, purely functional programming language

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions