forked from viperML/nh
-
Notifications
You must be signed in to change notification settings - Fork 4
/
darwin-module.nix
135 lines (119 loc) · 4.15 KB
/
darwin-module.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Notice: this file will only exist until this pr is merged https://github.com/LnL7/nix-darwin/pull/942
self:
{
config,
lib,
pkgs,
...
}:
let
cfg = config.programs.nh;
in
{
meta.maintainers = [ lib.maintainers.ToyVo ];
options.programs.nh = {
enable = lib.mkEnableOption "nh, yet another Nix CLI helper";
package = lib.mkPackageOption pkgs "nh" { } // {
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
};
flake = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
The path that will be used for the `NH_FLAKE` environment variable.
`NH_FLAKE` is used by nh as the default flake for performing actions on NixOS/nix-darwin, like `nh os switch`.
'';
};
os.flake = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
The path that will be used for the `NH_OS_FLAKE` environment variable.
`NH_OS_FLAKE` is used by nh as the default flake for performing actions on NixOS/nix-darwin, like `nh os switch`.
'';
};
home.flake = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
The path that will be used for the `NH_HOME_FLAKE` environment variable.
`NH_HOME_FLAKE` is used by nh as the default flake for performing actions on home-manager, like `nh home switch`.
'';
};
clean = {
enable = lib.mkEnableOption "periodic garbage collection with nh clean all";
# Not in NixOS module
user = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "User that runs the garbage collector.";
};
interval = lib.mkOption {
type = lib.types.attrs;
default = {
Weekday = 0;
};
description = ''
How often cleanup is performed. Passed to launchd.StartCalendarInterval
The format is described in
{manpage}`crontab(5)`.
'';
};
extraArgs = lib.mkOption {
type = lib.types.singleLineStr;
default = "";
example = "--keep 5 --keep-since 3d";
description = ''
Options given to nh clean when the service is run automatically.
See `nh clean all --help` for more information.
'';
};
};
};
config = {
warnings =
if (!(cfg.clean.enable -> !config.nix.gc.automatic)) then
[
"programs.nh.clean.enable and nix.gc.automatic are both enabled. Please use one or the other to avoid conflict."
]
else
[ ];
assertions = [
# Not strictly required but probably a good assertion to have
{
assertion = cfg.clean.enable -> cfg.enable;
message = "programs.nh.clean.enable requires programs.nh.enable";
}
{
assertion = (cfg.flake != null) -> !(lib.hasSuffix ".nix" cfg.flake);
message = "nh.flake must be a directory, not a nix file";
}
{
assertion = (cfg.os.flake != null) -> !(lib.hasSuffix ".nix" cfg.os.flake);
message = "nh.os.flake must be a directory, not a nix file";
}
{
assertion = (cfg.home.flake != null) -> !(lib.hasSuffix ".nix" cfg.home.flake);
message = "nh.home.flake must be a directory, not a nix file";
}
];
nixpkgs.overlays = [ self.overlays.default ];
environment = lib.mkIf cfg.enable {
systemPackages = [ cfg.package ];
variables = lib.mkMerge [
(lib.mkIf (cfg.flake != null) { NH_FLAKE = cfg.flake; })
(lib.mkIf (cfg.os.flake != null) { NH_OS_FLAKE = cfg.os.flake; })
(lib.mkIf (cfg.home.flake != null) { NH_HOME_FLAKE = cfg.home.flake; })
];
};
launchd = lib.mkIf cfg.clean.enable {
daemons.nh-clean = {
command = "exec ${lib.getExe cfg.package} clean all ${cfg.clean.extraArgs}";
environment.NIX_REMOTE = lib.optionalString config.nix.useDaemon "daemon";
serviceConfig.RunAtLoad = false;
serviceConfig.StartCalendarInterval = [ cfg.clean.interval ];
serviceConfig.UserName = cfg.clean.user;
};
};
};
}