-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.nix
59 lines (56 loc) · 1.95 KB
/
module.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{ config, lib, pkgs, ... }: {
options.environment.image-lock = with lib; {
lockFile = mkOption {
type = types.path;
};
containers = mkOption {
type = types.submodule {
freeformType = types.attrsOf types.str;
};
};
prune = mkEnableOption "";
};
config = let
data = (builtins.fromJSON (builtins.readFile cfg.lockFile));
cfg = config.environment.image-lock;
in {
# if there are no containers defined, it'll throw an error without this
environment.image-lock.containers = {};
# define images for all containers
virtualisation.oci-containers.containers = builtins.mapAttrs (name: value:
if (lib.attrsets.hasAttr "digest" data."${value}") then {
image = "${value}@${data."${value}".digest}";
} else {
image = data."${value}".finalImageName;
imageFile = pkgs.dockerTools.pullImage data."${value}";
}
) cfg.containers;
# pruning
systemd.services =
let backend = config.virtualisation.oci-containers.backend;
in lib.mkIf cfg.prune (lib.attrsets.mapAttrs' (name: value:
lib.attrsets.nameValuePair "${backend}-${name}-prune" (let
digest = if (lib.attrsets.hasAttr "digest" data."${value}") then data."${value}".digest else data."${value}".imageDigest;
in {
description = "Prune old images of ${value}";
requiredBy = [ "${backend}-${name}.service" ];
before = [ "${backend}-${name}.service" ];
path = config.systemd.services."${backend}-${name}".path; # get ${backend} in the path
serviceConfig = {
ExecStart = "${pkgs.writeShellApplication {
name = "${backend}-${name}-prune";
runtimeInputs = with pkgs; [ gawk ];
text = ''
images="$(${backend} images --digests | { grep '^${value} ' || true; } | { grep -v '${digest}' || true; } | awk '{print $4}')"
if [[ "$images" != "" ]]; then
# shellcheck disable=SC2086
${backend} rmi $images
fi
'';
}}/bin/${backend}-${name}-prune";
Type = "oneshot";
};
})
) cfg.containers);
};
}