nlewo / nix2container

An archive-less dockerTools.buildImage implementation
Apache License 2.0
501 stars 45 forks source link

Could `foldImageLayers` be added as lib function? #108

Open jashandeep-sohi opened 8 months ago

jashandeep-sohi commented 8 months ago

Appreciate the pointer in the readme on how to avoid duplicate files in layers. But could the foldImageLayers utility function mentioned in that post be included in this flake (or something similar)?

  foldImageLayers = let
    mergeToLayer = priorLayers: component:
      assert builtins.isList priorLayers;
      assert builtins.isAttrs component; let
        layer = nix2container.buildLayer (component
          // {
            layers = priorLayers;
          });
      in
        priorLayers ++ [layer];
  in
    layers: lib.foldl mergeToLayer [] layers;

I find myself duplicating it in multiple projects. Would be easier if it was available upstream.

mikepurvis commented 7 months ago

I agree that this would be great to add and document— as it stands, buildLayer is a bit of a footgun where you can easily end up re-shipping the same store paths if you're not careful to fold things together in the right way.

As a further enhancement, it would be nice if the layers were nameable/addressable somehow. Basically I have a project with multiple large images that intersect significantly with each other, and I want to define a hierarchy of shared layers upfront (say, A <- B <- C <- D <- E), and then have some of the images include all five, where others include just the first three or four. I know with a list of just a few elements like this I can use numerical slicing, but I'm thinking it will be more like 10-15 in my case, which could get fragile. It would be great to handle this with an API for defining, basically, a "layer set" that then another layer set can build upon.

nlewo commented 6 months ago

But could the foldImageLayers utility function mentioned in that post be included in this flake

Yes, a PR would be welcome ;)