1{ config
2, lib
3, pkgs
4, ...
5}:
6let
7 cfg = config.programs.nh;
8in
9{
10 meta.maintainers = [ lib.maintainers.viperML ];
11
12 options.programs.nh = {
13 enable = lib.mkEnableOption "nh, yet another Nix CLI helper";
14
15 package = lib.mkPackageOption pkgs "nh" { };
16
17 flake = lib.mkOption {
18 type = lib.types.nullOr lib.types.path;
19 default = null;
20 description = ''
21 The path that will be used for the `FLAKE` environment variable.
22
23 `FLAKE` is used by nh as the default flake for performing actions, like `nh os switch`.
24 '';
25 };
26
27 clean = {
28 enable = lib.mkEnableOption "periodic garbage collection with nh clean all";
29
30 dates = lib.mkOption {
31 type = lib.types.singleLineStr;
32 default = "weekly";
33 description = ''
34 How often cleanup is performed. Passed to systemd.time
35
36 The format is described in
37 {manpage}`systemd.time(7)`.
38 '';
39 };
40
41 extraArgs = lib.mkOption {
42 type = lib.types.singleLineStr;
43 default = "";
44 example = "--keep 5 --keep-since 3d";
45 description = ''
46 Options given to nh clean when the service is run automatically.
47
48 See `nh clean all --help` for more information.
49 '';
50 };
51 };
52 };
53
54 config = {
55 warnings =
56 if (!(cfg.clean.enable -> !config.nix.gc.automatic)) then [
57 "programs.nh.clean.enable and nix.gc.automatic are both enabled. Please use one or the other to avoid conflict."
58 ] else [ ];
59
60 assertions = [
61 # Not strictly required but probably a good assertion to have
62 {
63 assertion = cfg.clean.enable -> cfg.enable;
64 message = "programs.nh.clean.enable requires programs.nh.enable";
65 }
66
67 {
68 assertion = (cfg.flake != null) -> !(lib.hasSuffix ".nix" cfg.flake);
69 message = "nh.flake must be a directory, not a nix file";
70 }
71 ];
72
73 environment = lib.mkIf cfg.enable {
74 systemPackages = [ cfg.package ];
75 variables = lib.mkIf (cfg.flake != null) {
76 FLAKE = cfg.flake;
77 };
78 };
79
80 systemd = lib.mkIf cfg.clean.enable {
81 services.nh-clean = {
82 description = "Nh clean";
83 script = "exec ${lib.getExe cfg.package} clean all ${cfg.clean.extraArgs}";
84 startAt = cfg.clean.dates;
85 path = [ config.nix.package ];
86 serviceConfig.Type = "oneshot";
87 };
88
89 timers.nh-clean = {
90 timerConfig = {
91 Persistent = true;
92 };
93 };
94 };
95 };
96}