nix-git/modules/nix-gc.nix

55 lines
1.5 KiB
Nix
Raw Normal View History

2024-02-12 18:08:41 +01:00
{ config, pkgs, ... }:
{
2024-11-11 11:48:49 +01:00
# Delete all system generations except the 7 most recent ones.
# Then delete all unreachable store objects.
2024-08-23 14:38:42 +02:00
#
2024-02-12 18:08:41 +01:00
# Based on https://github.com/NixOS/nixpkgs/blob/nixos-23.11/nixos/modules/services/misc/nix-gc.nix
2024-11-11 11:48:49 +01:00
# Alternative: Delete generations older than 7 days and then delete unreachable store objects
2024-08-23 14:38:42 +02:00
# https://nixos.wiki/wiki/Storage_optimization#Automation
#nix.gc = {
# automatic = true;
# dates = "weekly";
# options = "--delete-older-than 7d";
#};
2024-02-12 18:08:41 +01:00
assertions = [{
assertion = config.nix.enable;
message = ''nix-gc-7 requires nix.enable'';
}];
systemd.timers."nix-gc-7" = {
wantedBy = [ "timers.target" ];
partOf = [ "nix-gc-7.service" ];
timerConfig = {
OnCalendar = "weekly";
Persistent = true;
AccuracySec = "2d";
RandomizedDelaySec = "1d";
};
};
2024-11-11 11:48:49 +01:00
# View generated systemd service config with:
# cat "$(systemctl show -P FragmentPath nix-gc-7.service)" | grep ExecStart
2024-02-12 18:08:41 +01:00
systemd.services."nix-gc-7" = {
2024-11-11 11:48:49 +01:00
description = "rm old system generations and unreachable store objects";
2024-02-12 18:08:41 +01:00
conflicts = [ "shutdown.target" "sleep.target" ];
before = [ "shutdown.target" "sleep.target" ];
serviceConfig = {
Type = "oneshot";
PrivateTmp = true;
Nice = 19;
IOSchedulingClass = "idle";
};
script = ''
${config.nix.package.out}/bin/nix-env --delete-generations +7 --profile /nix/var/nix/profiles/system
2024-11-11 11:48:49 +01:00
${config.nix.package.out}/bin/nix-collect-garbage
2024-02-12 18:08:41 +01:00
'';
};
}