2024-01-28 05:11:23 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.nix.gc;
|
2024-03-17 10:54:44 +01:00
|
|
|
darwinIntervals =
|
|
|
|
[ "hourly" "daily" "weekly" "monthly" "semiannually" "annually" ];
|
2024-01-28 05:11:23 +01:00
|
|
|
|
|
|
|
mkCalendarInterval = frequency:
|
|
|
|
let
|
|
|
|
freq = {
|
|
|
|
"hourly" = [{ Minute = 0; }];
|
2024-03-15 13:36:03 +01:00
|
|
|
"daily" = [{
|
|
|
|
Hour = 0;
|
|
|
|
Minute = 0;
|
|
|
|
}];
|
2024-01-28 05:11:23 +01:00
|
|
|
"weekly" = [{
|
|
|
|
Weekday = 1;
|
|
|
|
Hour = 0;
|
|
|
|
Minute = 0;
|
|
|
|
}];
|
|
|
|
"monthly" = [{
|
|
|
|
Day = 1;
|
|
|
|
Hour = 0;
|
|
|
|
Minute = 0;
|
|
|
|
}];
|
|
|
|
"semiannually" = [
|
|
|
|
{
|
|
|
|
Month = 1;
|
|
|
|
Day = 1;
|
|
|
|
Hour = 0;
|
|
|
|
Minute = 0;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Month = 7;
|
|
|
|
Day = 1;
|
|
|
|
Hour = 0;
|
|
|
|
Minute = 0;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
"annually" = [{
|
|
|
|
Month = 1;
|
|
|
|
Day = 1;
|
|
|
|
Hour = 0;
|
|
|
|
Minute = 0;
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
in freq.${frequency};
|
|
|
|
|
|
|
|
nixPackage = if config.nix.enable && config.nix.package != null then
|
|
|
|
config.nix.package
|
|
|
|
else
|
|
|
|
pkgs.nix;
|
|
|
|
in {
|
|
|
|
meta.maintainers = [ maintainers.shivaraj-bh ];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
nix.gc = {
|
|
|
|
automatic = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Automatically run the garbage collector at a specific time.
|
|
|
|
|
|
|
|
Note: This will only garbage collect the current user's profiles.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
frequency = mkOption {
|
2024-03-17 10:54:44 +01:00
|
|
|
type = types.str;
|
2024-01-28 05:11:23 +01:00
|
|
|
default = "weekly";
|
2024-03-17 10:54:44 +01:00
|
|
|
example = "03:15";
|
2024-01-28 05:11:23 +01:00
|
|
|
description = ''
|
2024-03-17 10:54:44 +01:00
|
|
|
When to run the Nix garbage collector.
|
|
|
|
|
|
|
|
On Linux this is a string as defined by {manpage}`systemd.time(7)`.
|
2024-01-28 05:11:23 +01:00
|
|
|
|
2024-03-17 10:54:44 +01:00
|
|
|
On Darwin it must be one of: ${toString darwinIntervals}, which are
|
|
|
|
implemented as defined in the manual page above.
|
2024-01-28 05:11:23 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-10-21 12:29:57 +02:00
|
|
|
randomizedDelaySec = lib.mkOption {
|
|
|
|
default = "0";
|
|
|
|
type = lib.types.singleLineStr;
|
|
|
|
example = "45min";
|
|
|
|
description = ''
|
|
|
|
Add a randomized delay before each garbage collection.
|
|
|
|
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)`
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-01-28 05:11:23 +01:00
|
|
|
options = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "--max-freed $((64 * 1024**3))";
|
|
|
|
description = ''
|
|
|
|
Options given to {file}`nix-collect-garbage` when the
|
|
|
|
garbage collector is run automatically.
|
|
|
|
'';
|
|
|
|
};
|
2024-06-04 06:20:47 +02:00
|
|
|
|
|
|
|
persistent = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = ''
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
2024-01-28 05:11:23 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.automatic (mkMerge [
|
|
|
|
(mkIf pkgs.stdenv.isLinux {
|
|
|
|
systemd.user.services.nix-gc = {
|
|
|
|
Unit = { Description = "Nix Garbage Collector"; };
|
|
|
|
Service = {
|
2024-07-29 22:45:50 +02:00
|
|
|
Type = "oneshot";
|
2024-07-31 08:27:17 +02:00
|
|
|
ExecStart = toString (pkgs.writeShellScript "nix-gc"
|
|
|
|
"exec ${nixPackage}/bin/nix-collect-garbage ${
|
2024-01-28 05:11:23 +01:00
|
|
|
lib.optionalString (cfg.options != null) cfg.options
|
2024-07-31 08:27:17 +02:00
|
|
|
}");
|
2024-01-28 05:11:23 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.user.timers.nix-gc = {
|
|
|
|
Unit = { Description = "Nix Garbage Collector"; };
|
|
|
|
Timer = {
|
|
|
|
OnCalendar = "${cfg.frequency}";
|
2024-10-21 12:29:57 +02:00
|
|
|
RandomizedDelaySec = cfg.randomizedDelaySec;
|
2024-06-04 06:20:47 +02:00
|
|
|
Persistent = cfg.persistent;
|
2024-01-28 05:11:23 +01:00
|
|
|
Unit = "nix-gc.service";
|
|
|
|
};
|
|
|
|
Install = { WantedBy = [ "timers.target" ]; };
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf pkgs.stdenv.isDarwin {
|
2024-03-17 10:54:44 +01:00
|
|
|
assertions = [{
|
|
|
|
assertion = elem cfg.frequency darwinIntervals;
|
|
|
|
message = "On Darwin nix.gc.frequency must be one of: ${
|
|
|
|
toString darwinIntervals
|
|
|
|
}.";
|
|
|
|
}];
|
|
|
|
|
2024-01-28 05:11:23 +01:00
|
|
|
launchd.agents.nix-gc = {
|
|
|
|
enable = true;
|
|
|
|
config = {
|
|
|
|
ProgramArguments = [ "${nixPackage}/bin/nix-collect-garbage" ]
|
|
|
|
++ lib.optional (cfg.options != null) cfg.options;
|
|
|
|
StartCalendarInterval = mkCalendarInterval cfg.frequency;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}
|