{ 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"; }; }) ]); }