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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# The common module that contains Hjem's per-user options. To ensure Hjem remains
# somewhat compliant with cross-platform paradigms (e.g. NixOS or Darwin.) Platform
# specific options such as nixpkgs module system or nix-darwin module system should
# be avoided here.
{
config,
hjem-lib,
lib,
name,
options,
pkgs,
...
}: let
inherit (hjem-lib) envVarType listOrSingletonOf toEnv;
inherit (lib.attrsets) attrValues mapAttrs mapAttrsToList;
inherit (lib.lists) any;
inherit (lib.modules) mkIf;
inherit (lib.options) literalExpression mkEnableOption mkOption;
inherit (lib.strings) concatLines concatStringsSep;
inherit (lib.trivial) id;
inherit (lib.types) attrsOf attrsWith bool listOf package passwdEntry path str strMatching;
cfg = config;
fileTypeRelativeTo' = rootDir:
attrsWith {
elemType = hjem-lib.fileTypeRelativeTo {
inherit rootDir;
clobberDefault = cfg.clobberFiles;
clobberDefaultText = literalExpression "config.hjem.users.${name}.clobberFiles";
};
placeholder = "path";
};
in {
_class = "hjem";
imports = [
# Makes "assertions" option available without having to duplicate the work
# already done in the Nixpkgs module.
(pkgs.path + "/nixos/modules/misc/assertions.nix")
];
options = {
enable =
mkEnableOption "home management for this user"
// {
default = true;
example = false;
};
user = mkOption {
type = strMatching "[a-zA-Z0-9_.][a-zA-Z0-9_.-]*";
description = "The owner of a given home directory.";
};
directory = mkOption {
type = passwdEntry path;
description = ''
The home directory for the user, to which files configured in
{option}`hjem.users.<username>.files` will be relative to by default.
'';
};
clobberFiles = mkOption {
type = bool;
example = true;
description = ''
The default override behaviour for files managed by Hjem for a
particular user.
A top level option exists under the Hjem module option
{option}`hjem.clobberByDefault`. Per-file behaviour can be modified
with {option}`hjem.users.<username>.files.<path>.clobber`.
'';
};
files = mkOption {
default = {};
type = fileTypeRelativeTo' cfg.directory;
example = {".config/foo.txt".source = "Hello World";};
description = "Hjem-managed files.";
};
xdg = {
cache = {
directory = mkOption {
type = path;
default = "${cfg.directory}/.cache";
defaultText = "$HOME/.cache";
description = ''
The XDG cache directory for the user, to which files configured in
{option}`hjem.users.<username>.xdg.cache.files` will be relative to by default.
Adds {env}`XDG_CACHE_HOME` to {option}`environment.sessionVariables` for
this user if changed.
'';
};
files = mkOption {
default = {};
type = fileTypeRelativeTo' cfg.xdg.cache.directory;
example = {"foo.txt".source = "Hello World";};
description = "Hjem-managed cache files.";
};
};
config = {
directory = mkOption {
type = path;
default = "${cfg.directory}/.config";
defaultText = "$HOME/.config";
description = ''
The XDG config directory for the user, to which files configured in
{option}`hjem.users.<username>.xdg.config.files` will be relative to by default.
Adds {env}`XDG_CONFIG_HOME` to {option}`environment.sessionVariables` for
this user if changed.
'';
};
files = mkOption {
default = {};
type = fileTypeRelativeTo' cfg.xdg.config.directory;
example = {"foo.txt".source = "Hello World";};
description = "Hjem-managed config files.";
};
};
data = {
directory = mkOption {
type = path;
default = "${cfg.directory}/.local/share";
defaultText = "$HOME/.local/share";
description = ''
The XDG data directory for the user, to which files configured in
{option}`hjem.users.<username>.xdg.data.files` will be relative to by default.
Adds {env}`XDG_DATA_HOME` to {option}`environment.sessionVariables` for
this user if changed.
'';
};
files = mkOption {
default = {};
type = fileTypeRelativeTo' cfg.xdg.data.directory;
example = {"foo.txt".source = "Hello World";};
description = "Hjem-managed data files.";
};
};
state = {
directory = mkOption {
type = path;
default = "${cfg.directory}/.local/state";
defaultText = "$HOME/.local/state";
description = ''
The XDG state directory for the user, to which files configured in
{option}`hjem.users.<username>.xdg.state.files` will be relative to by default.
Adds {env}`XDG_STATE_HOME` to {option}`environment.sessionVariables` for
this user if changed.
'';
};
files = mkOption {
default = {};
type = fileTypeRelativeTo' cfg.xdg.state.directory;
example = {"foo.txt".source = "Hello World";};
description = "Hjem-managed state files.";
};
};
mime-apps = {
added-associations = mkOption {
type = attrsOf (listOrSingletonOf str);
default = {};
example = {
mimetype1 = ["foo1.desktop" "foo2.desktop" "foo3.desktop"];
mimetype2 = "foo4.desktop";
};
description = ''
Defines additional [associations] of applications with mimetypes, as
if the `.desktop` file was listing this mimetype in the first place.
[associations]: https://specifications.freedesktop.org/mime-apps/latest/associations.html
'';
};
removed-associations = mkOption {
type = attrsOf (listOrSingletonOf str);
default = {};
example = {
mimetype1 = "foo5.desktop";
};
description = ''
Removes [associations] of applications with mimetypes, as if the
`.desktop` file was NOT listing this mimetype in the first place.
[associations]: https://specifications.freedesktop.org/mime-apps/latest/associations.html
'';
};
default-applications = mkOption {
type = attrsOf (listOrSingletonOf str);
default = {};
example = {
mimetype1 = ["default1.desktop" "default2.desktop"];
};
description = ''
Indicates the [default application] to be used for a given mimetype.
[default application]: https://specifications.freedesktop.org/mime-apps/latest/default.html
'';
};
};
};
packages = mkOption {
type = listOf package;
default = [];
example = literalExpression "[pkgs.hello]";
description = "Packages to install for this user.";
};
extraDependencies = mkOption {
type = listOf package;
default = [];
description = ''
A list of paths/packages that should be included in the system closure
but generally not visible to users.
On NixOS these paths are forwarded to {option}`system.extraDependencies`.
On nix-darwin, which has no equivalent host option, the paths are pinned
to the system closure by listing them in `/etc/hjem/extra-dependencies`.
'';
};
environment = {
loadEnv = mkOption {
type = path;
readOnly = true;
description = ''
A POSIX compliant shell script containing the user session variables needed to bootstrap the session.
As there is no reliable and agnostic way of setting session variables, Hjem's
environment module does nothing by itself. Rather, it provides a POSIX compliant shell script
that needs to be sourced where needed.
'';
};
sessionVariables = mkOption {
type = envVarType;
default = {};
example = {
EDITOR = "nvim";
VISUAL = "nvim";
};
description = ''
A set of environment variables used in the user environment.
If a list of strings is used, they will be concatenated with colon
characters.
'';
};
};
};
config = {
# for docs
_module.args.name = lib.mkDefault "‹username›";
environment = {
sessionVariables = {
XDG_CACHE_HOME = mkIf (cfg.xdg.cache.directory != options.xdg.cache.directory.default) cfg.xdg.cache.directory;
XDG_CONFIG_HOME = mkIf (cfg.xdg.config.directory != options.xdg.config.directory.default) cfg.xdg.config.directory;
XDG_DATA_HOME = mkIf (cfg.xdg.data.directory != options.xdg.data.directory.default) cfg.xdg.data.directory;
XDG_STATE_HOME = mkIf (cfg.xdg.state.directory != options.xdg.state.directory.default) cfg.xdg.state.directory;
};
loadEnv = lib.pipe cfg.environment.sessionVariables [
(mapAttrsToList (name: value: "export ${name}=\"${toEnv value}\""))
concatLines
(pkgs.writeShellScript "load-env")
];
};
xdg.config.files."mimeapps.list" = let
nonDefault = {
added = cfg.xdg.mime-apps.added-associations != options.xdg.mime-apps.added-associations.default;
removed = cfg.xdg.mime-apps.removed-associations != options.xdg.mime-apps.removed-associations.default;
default = cfg.xdg.mime-apps.default-applications != options.xdg.mime-apps.default-applications.default;
};
in
mkIf (any id (attrValues nonDefault)) {
generator = (pkgs.formats.ini {listToValue = concatStringsSep ";";}).generate "mimeapps.list";
value = {
"Added Associations" = mkIf nonDefault.added cfg.xdg.mime-apps.added-associations;
"Removed Associations" = mkIf nonDefault.removed cfg.xdg.mime-apps.removed-associations;
"Default Applications" = mkIf nonDefault.default cfg.xdg.mime-apps.default-applications;
};
};
};
}
|