diff --git a/nixos/modules/virtualisation/docker.nix b/nixos/modules/virtualisation/docker.nix
index c26cae06cd1d..25274c440e68 100644
--- a/nixos/modules/virtualisation/docker.nix
+++ b/nixos/modules/virtualisation/docker.nix
@@ -94,6 +94,37 @@ in
docker daemon.
'';
};
+
+ autoPrune = {
+ enable = mkOption {
+ type = types.bool;
+ default = false;
+ description = ''
+ Whether to periodically prune Docker resources. If enabled, a systemd timer will run
+ docker system prune -f once a day.
+ '';
+ };
+
+ flags = mkOption {
+ type = types.listOf types.str;
+ default = [];
+ example = [ "--all" ];
+ description = ''
+ Any additional flags passed to docker system prune.
+ '';
+ };
+
+ dates = mkOption {
+ default = "weekly";
+ type = types.str;
+ description = ''
+ Specification (in the format described by
+ systemd.time
+ 7) of the time at
+ which the prune will occur.
+ '';
+ };
+ };
};
###### implementation
@@ -137,6 +168,22 @@ in
SocketGroup = "docker";
};
};
+
+
+ systemd.services.docker-prune = {
+ description = "Prune docker resources";
+
+ restartIfChanged = false;
+ unitConfig.X-StopOnRemoval = false;
+
+ serviceConfig.Type = "oneshot";
+
+ script = ''
+ ${pkgs.docker}/bin/docker system prune -f ${toString cfg.autoPrune.flags}
+ '';
+
+ startAt = optional cfg.autoPrune.enable cfg.autoPrune.dates;
+ };
}
]);