From b5c9cecbccc4e6183369fe2fd8bb072cc598204a Mon Sep 17 00:00:00 2001 From: Todd Brown Date: Sun, 16 Mar 2025 02:56:31 -0700 Subject: [PATCH] nixos/docker: add auto prune randomizedDelaySec and persistent options (#301547) docker: add auto prune randomizedDelaySec and persistent options Options mirror nix garbage collection --- nixos/modules/virtualisation/docker.nix | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/nixos/modules/virtualisation/docker.nix b/nixos/modules/virtualisation/docker.nix index 76f91c71fa59..00a99b6fcd7d 100644 --- a/nixos/modules/virtualisation/docker.nix +++ b/nixos/modules/virtualisation/docker.nix @@ -159,6 +159,34 @@ in which the prune will occur. ''; }; + + randomizedDelaySec = mkOption { + default = "0"; + type = types.singleLineStr; + example = "45min"; + description = '' + Add a randomized delay before each auto prune. + The delay will be chosen between zero and this value. + This value must be a time span in the format specified by + {manpage}`systemd.time(7)` + ''; + }; + + persistent = mkOption { + default = true; + type = types.bool; + example = false; + description = '' + Takes a boolean argument. If true, the time when the service + unit was last triggered is stored on disk. When the timer is + activated, the service unit is triggered immediately if it + would have been triggered at least once during the time when + the timer was inactive. Such triggering is nonetheless + subject to the delay imposed by RandomizedDelaySec=. This is + useful to catch up on missed runs of the service when the + system was powered down. + ''; + }; }; package = mkPackageOption pkgs "docker" { }; @@ -254,6 +282,13 @@ in requires = [ "docker.service" ]; }; + systemd.timers.docker-prune = mkIf cfg.autoPrune.enable { + timerConfig = { + RandomizedDelaySec = cfg.autoPrune.randomizedDelaySec; + Persistent = cfg.autoPrune.persistent; + }; + }; + assertions = [ { assertion = cfg.enableNvidia && pkgs.stdenv.hostPlatform.isx86_64 -> config.hardware.graphics.enable32Bit or false; message = "Option enableNvidia on x86_64 requires 32-bit support libraries";