1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 cfg = config.services.angrr;
10in
11{
12 meta.maintainers = pkgs.angrr.meta.maintainers;
13 options = {
14 services.angrr = {
15 enable = lib.mkEnableOption "angrr";
16 package = lib.mkPackageOption pkgs "angrr" { };
17 period = lib.mkOption {
18 type = lib.types.str;
19 default = "7d";
20 example = "2weeks";
21 description = ''
22 The retention period of auto GC roots.
23 '';
24 };
25 removeRoot = lib.mkOption {
26 type = lib.types.bool;
27 default = false;
28 description = ''
29 Whether to pass the `--remove-root` option to angrr.
30 '';
31 };
32 ownedOnly = lib.mkOption {
33 type = lib.types.bool;
34 default = false;
35 description = ''
36 Control the `--remove-root=<true|false>` option of angrr.
37 '';
38 apply = b: if b then "true" else "false";
39 };
40 logLevel = lib.mkOption {
41 type =
42 with lib.types;
43 enum [
44 "off"
45 "error"
46 "warn"
47 "info"
48 "debug"
49 "trace"
50 ];
51 default = "info";
52 description = ''
53 Set the log level of angrr.
54 '';
55 };
56 extraArgs = lib.mkOption {
57 type = with lib.types; listOf str;
58 default = [ ];
59 description = ''
60 Extra command-line arguments pass to angrr.
61 '';
62 };
63 enableNixGcIntegration = lib.mkOption {
64 type = lib.types.bool;
65 description = ''
66 Whether to enable nix-gc.service integration
67 '';
68 };
69 timer = {
70 enable = lib.mkEnableOption "angrr timer";
71 dates = lib.mkOption {
72 type = lib.types.str;
73 default = "03:00";
74 description = ''
75 How often or when the retention policy is performed.
76 '';
77 };
78 };
79 };
80 };
81
82 config = lib.mkIf cfg.enable (
83 lib.mkMerge [
84 {
85 assertions = [
86 {
87 assertion = cfg.enableNixGcIntegration -> config.nix.gc.automatic;
88 message = "angrr nix-gc.service integration requires `nix.gc.automatic = true`";
89 }
90 ];
91 services.angrr.enableNixGcIntegration = lib.mkDefault config.nix.gc.automatic;
92 }
93
94 {
95 systemd.services.angrr = {
96 description = "Auto Nix GC Roots Retention";
97 script = ''
98 ${lib.getExe cfg.package} run \
99 --log-level "${cfg.logLevel}" \
100 --period "${cfg.period}" \
101 ${lib.optionalString cfg.removeRoot "--remove-root"} \
102 --owned-only="${cfg.ownedOnly}" \
103 --no-prompt ${lib.escapeShellArgs cfg.extraArgs}
104 '';
105 serviceConfig = {
106 Type = "oneshot";
107 };
108 };
109 }
110
111 (lib.mkIf cfg.timer.enable {
112 systemd.timers.angrr = {
113 timerConfig = {
114 OnCalendar = cfg.timer.dates;
115 };
116 wantedBy = [ "timers.target" ];
117 };
118 })
119
120 (lib.mkIf cfg.enableNixGcIntegration {
121 systemd.services.angrr = {
122 wantedBy = [ "nix-gc.service" ];
123 before = [ "nix-gc.service" ];
124 };
125 })
126 ]
127 );
128}