{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.fudo.garbage-collector;

in {

  options.fudo.garbage-collector = {
    enable = mkEnableOption "Enable periodic NixOS garbage collection";

    timing = mkOption {
      type = types.str;
      default = "weekly";
      description = "Period (systemd format) at which to run garbage collector.";
    };

    age = mkOption {
      type = types.str;
      default = "30d";
      description = "Age of garbage to collect (eg. 30d).";
    };
  };

  config = mkIf cfg.enable {
    systemd = {
      timers.fudo-garbage-collector = {
        enable = true;
        description = "Collect NixOS garbage older than ${cfg.age}";
        partOf = [ "fudo-garbage-collector.service" ];
        wantedBy = [ "timers.target" ];
        timerConfig = {
          OnCalendar = cfg.timing;
        };
      };

      services.fudo-garbage-collector = {
        enable = true;
        serviceConfig = {
          Type = "oneshot";
          StandardOutput = "journal";
        };
        script = ''
          ${pkgs.nix}/bin/nix-collect-garbage --delete-older-than ${cfg.age}
        '';
      };
    };
  };
}