system/modules/nixos/services/collect-garbage.nix

55 lines
1.3 KiB
Nix
Raw Permalink Normal View History

2024-04-16 02:51:46 +03:00
{ config, lib, ... }:
let
cfg = config.local.services.collect-garbage;
in
{
options.local.services.collect-garbage = with lib; {
enable = mkEnableOption "collect nix and docker garbage";
nix = {
enable = mkOption {
description = "collect unused nix packages";
type = types.bool;
default = true;
};
nixShellProtection = mkOption {
description = ''
Protect nix-shell against garbage collection.
Source: https://github.com/nix-community/nix-direnv#installation
'';
type = types.bool;
default = false;
};
};
docker.enable = mkOption {
description = "collect unused docker images";
type = types.bool;
default = config.virtualisation.docker.enable;
};
};
config = lib.mkIf cfg.enable (lib.mkMerge [
(lib.mkIf cfg.nix.enable {
nix.gc = {
automatic = true;
dates = "weekly";
options = "--delete-older-than 14d";
};
})
(lib.mkIf (cfg.nix.enable && cfg.nix.nixShellProtection) {
nix.settings = {
keep-derivations = true;
keep-outputs = true;
};
})
(lib.mkIf cfg.docker.enable {
virtualisation.docker.autoPrune = {
enable = true;
dates = "weekly";
};
})
]);
}