From e8ee73bdd9f86df77f7cdaab06608526449c7f6c Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Mon, 9 Oct 2023 12:06:15 +0200 Subject: [PATCH] fix: weekly docker prune --- modules/docker.nix | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/docker.nix b/modules/docker.nix index cb1e25a..337b259 100644 --- a/modules/docker.nix +++ b/modules/docker.nix @@ -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" ];