Skip to content

Commit

Permalink
Add enableLayerDeduplication option
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Sep 5, 2024
1 parent 2c006b4 commit 5547092
Showing 1 changed file with 79 additions and 6 deletions.
85 changes: 79 additions & 6 deletions src/modules/containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,19 @@ let

maxLayers = cfg.maxLayers;

layers = cfg.layers;
layers =
if cfg.enableLayerDeduplication
then
builtins.foldl'
(layers: layer:
layers ++ [
(nix2container.nix2container.buildLayer (layer // { inherit layers; }))
]
)
[ ]
cfg.layers
else builtins.map (layer: nix2container.nix2container.buildLayer layer) cfg.layers
;

perms = [
{
Expand Down Expand Up @@ -223,13 +235,74 @@ let
};

maxLayers = lib.mkOption {
type = types.nullOr types.int;
description = "Maximum number of container layers created.";
type = types.int;
description = "the maximum number of layers to create.";
defaultText = lib.literalExpression 1;
default = 1;
};

enableLayerDeduplication = lib.mkOption {
type = types.bool;
description = "Enalbe layer deduplication using the approach described at https://blog.eigenvalue.net/2023-nix2container-everything-once/";
default = true;
};

layers = lib.mkOption {
type = types.listOf types.anything;
type = types.listOf (types.submoduleWith {
modules = [
{
options = {
deps = lib.mkOption {
type = types.listOf types.package;
description = "list of store paths to include in the layer.";
default = [ ];
};
copyToRoot = lib.mkOption {
type = types.listOf types.package;
description = "a list of derivations copied in the image root directory (store path prefixes `/nix/store/hash-path` are removed, in order to relocate them at the image `/`).";
default = [ ];
};
reproducible = lib.mkOption {
type = types.bool;
description = "whether the layer should be reproducible.";
default = true;
};
maxLayers = lib.mkOption {
type = types.int;
description = "the maximum number of layers to create.";
default = 1;
};
perms = lib.mkOption {
default = [ ];
type = types.listOf (types.submoduleWith {
modules = [
{
options = {
path = lib.mkOption {
type = types.pathInStore;
};
regex = lib.mkOption {
type = types.str;
example = ".*";
};
mode = lib.mkOption {
type = types.str;
example = "644";
};
};
}
];
});
};
ignore = lib.mkOption {
type = types.nullOr types.pathInStore;
default = null;
description = "a store path to ignore when building the layer. This is mainly useful to ignore the configuration file from the container layer.";
};
};
}
];
});
description = "the layers to create.";
default = [ ];
};
Expand Down Expand Up @@ -261,10 +334,10 @@ let
};
};
config.layers = [
(nix2container.nix2container.buildLayer {
{
perms = map mkPerm (mkMultiHome (homeRoots config));
copyToRoot = mkMultiHome (homeRoots config);
})
}
];

});
Expand Down

0 comments on commit 5547092

Please sign in to comment.