From baac242a1f9ea8f39e9bf00b484f77e490956d7e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 4 Jan 2013 14:04:41 +0100 Subject: [PATCH] Run the garbage collector as a systemd service Running it from systemd rather than cron has several advantages: systemd ensures that only one instance runs at a time; the GC can be manually started/stopped; and logging goes to the journal. We still need cron to start the service at the right time, but hopefully soon we can get rid of cron entirely (once systemd supports starting a unit at a specific time). --- modules/services/misc/nix-gc.nix | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/services/misc/nix-gc.nix b/modules/services/misc/nix-gc.nix index 942e7996da0..5322e1ff57d 100644 --- a/modules/services/misc/nix-gc.nix +++ b/modules/services/misc/nix-gc.nix @@ -16,7 +16,7 @@ in automatic = mkOption { default = false; - example = true; + type = types.bool; description = " Automatically run the garbage collector at specified dates. "; @@ -24,6 +24,7 @@ in dates = mkOption { default = "15 03 * * *"; + type = types.string; description = " Run the garbage collector at specified dates to avoid full hard-drives. @@ -33,6 +34,7 @@ in options = mkOption { default = ""; example = "--max-freed $((64 * 1024**3))"; + type = types.string; description = " Options given to nix-collect-garbage when the garbage collector is run automatically. @@ -45,10 +47,17 @@ in ###### implementation - config = mkIf cfg.automatic { - services.cron.systemCronJobs = [ - "${cfg.dates} root ${nix}/bin/nix-collect-garbage ${cfg.options} > /var/log/gc.log 2>&1" - ]; + config = { + + services.cron.systemCronJobs = mkIf cfg.automatic (singleton + "${cfg.dates} root ${config.system.build.systemd}/bin/systemctl start nix-gc.service"); + + boot.systemd.services."nix-gc" = + { description = "Nix Garbage Collector"; + serviceConfig.ExecStart = + "@${nix}/bin/nix-collect-garbage nix-collect-garbage ${cfg.options}"; + }; + }; }