at 25.11-pre 2.9 kB view raw
1{ config, lib, ... }: 2 3let 4 cfg = config.nix.gc; 5in 6 7{ 8 9 ###### interface 10 11 options = { 12 13 nix.gc = { 14 15 automatic = lib.mkOption { 16 default = false; 17 type = lib.types.bool; 18 description = "Automatically run the garbage collector at a specific time."; 19 }; 20 21 dates = lib.mkOption { 22 type = lib.types.singleLineStr; 23 default = "03:15"; 24 example = "weekly"; 25 description = '' 26 How often or when garbage collection is performed. For most desktop and server systems 27 a sufficient garbage collection is once a week. 28 29 This value must be a calendar event in the format specified by 30 {manpage}`systemd.time(7)`. 31 ''; 32 }; 33 34 randomizedDelaySec = lib.mkOption { 35 default = "0"; 36 type = lib.types.singleLineStr; 37 example = "45min"; 38 description = '' 39 Add a randomized delay before each garbage collection. 40 The delay will be chosen between zero and this value. 41 This value must be a time span in the format specified by 42 {manpage}`systemd.time(7)` 43 ''; 44 }; 45 46 persistent = lib.mkOption { 47 default = true; 48 type = lib.types.bool; 49 example = false; 50 description = '' 51 Takes a boolean argument. If true, the time when the service 52 unit was last triggered is stored on disk. When the timer is 53 activated, the service unit is triggered immediately if it 54 would have been triggered at least once during the time when 55 the timer was inactive. Such triggering is nonetheless 56 subject to the delay imposed by RandomizedDelaySec=. This is 57 useful to catch up on missed runs of the service when the 58 system was powered down. 59 ''; 60 }; 61 62 options = lib.mkOption { 63 default = ""; 64 example = "--max-freed $((64 * 1024**3))"; 65 type = lib.types.singleLineStr; 66 description = '' 67 Options given to [`nix-collect-garbage`](https://nixos.org/manual/nix/stable/command-ref/nix-collect-garbage) when the garbage collector is run automatically. 68 ''; 69 }; 70 71 }; 72 73 }; 74 75 ###### implementation 76 77 config = { 78 assertions = [ 79 { 80 assertion = cfg.automatic -> config.nix.enable; 81 message = ''nix.gc.automatic requires nix.enable''; 82 } 83 ]; 84 85 systemd.services.nix-gc = lib.mkIf config.nix.enable { 86 description = "Nix Garbage Collector"; 87 script = "exec ${config.nix.package.out}/bin/nix-collect-garbage ${cfg.options}"; 88 serviceConfig.Type = "oneshot"; 89 startAt = lib.optional cfg.automatic cfg.dates; 90 }; 91 92 systemd.timers.nix-gc = lib.mkIf cfg.automatic { 93 timerConfig = { 94 RandomizedDelaySec = cfg.randomizedDelaySec; 95 Persistent = cfg.persistent; 96 }; 97 }; 98 99 }; 100 101}