fix: weekly docker prune

This commit is contained in:
Daniel Langbein 2023-10-09 12:06:15 +02:00
parent 20e590c1c1
commit e8ee73bdd9
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002

View File

@ -13,20 +13,38 @@
# As we use btrfs, we enable the according storageDriver option.
storageDriver = "btrfs";
# Run `docker system prune -f` every week.
# Run `docker system prune -f ${autoPrune.flags}` every week.
autoPrune.enable = true;
autoPrune.dates = "weekly";
# https://docs.docker.com/engine/reference/commandline/system_prune/#options
autoPrune.flags = [
"--all"
"--volumes"
# https://docs.docker.com/engine/reference/commandline/system_prune/#filter
# https://pkg.go.dev/maze.io/x/duration#ParseDuration
"--filter until=7d"
# `7d` could not be parsed, thus we use `168h`
"--filter until=168h"
# `--volumes` can't be used together with `--filter`.
# Thus we added our own service below.
#--volumes
];
};
};
# Prune docker volumes.
# This is a slightly modified copy of https://github.com/NixOS/nixpkgs/blob/5a237aecb57296f67276ac9ab296a41c23981f56/nixos/modules/virtualisation/docker.nix#L211C5-L226
systemd.services."docker-prune-volumes" = {
description = "Prune docker volumes";
restartIfChanged = false;
unitConfig.X-StopOnRemoval = false;
serviceConfig.Type = "oneshot";
script = ''
${pkgs.docker}/bin/docker system prune -f --all --volumes
'';
startAt = "weekly";
after = [ "docker.service" ];
requires = [ "docker.service" ];
};
# Monitor unhealthy Docker containers.
systemd.timers."docker-health" = {
wantedBy = [ "timers.target" ];