From 22acfd0327e02a110bb975e1692aef6d8c5524e3 Mon Sep 17 00:00:00 2001 From: Pascal Bach Date: Wed, 19 Jul 2017 18:20:46 +0200 Subject: [PATCH] docker service: add option to do automatic pruning This allows to run the prune job periodically on a machine. By default the if enabled the job is run once a week. The structure is similar to how system.autoUpgrade works. --- nixos/modules/virtualisation/docker.nix | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/nixos/modules/virtualisation/docker.nix b/nixos/modules/virtualisation/docker.nix index c26cae06cd1..25274c440e6 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; + }; } ]);