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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
{
config,
lib,
pkgs,
...
}: let
cfg = config.programs.qutebrowser;
/*
Flattens an attribute set of key values to stringified keys and their values.
This is necessary, as qutebrowser only understands key value settings.
## Determining the keys and values
Currently, this function will recursively iterate through the `configAttrs`, whilst
adding up the attribute keys, until one of the following conditions apply:
- The value is not an attribute set.
- The value is “marked” as being a raw attribute set. This is done automatically via the
`apply` functions in the option that take attribute sets (i.e., dictionaries) as
values.
## Marking an attribute set as value
Currently, this function assumes that a attribute set is meant as value, if it's key is
`__raw`. For example:
```nix
{
bindings.command.__raw = {
normal.L = "close";
};
}
```
# Type
flattenSettings :: AttrSet -> [{key :: String; value :: Any}]
# Arguments
configAttrs
: The configuration attribute set to flatten.
# Examples
flattenSettings {aliases.__raw = {c = "close";}; colors.background = "0xffffff00";}
=> [
{key = "aliases"; value = {c = "close";};}
{key = "colors.background"; value = "0xffffff00";}
]
*/
flattenSettings = let
isValue = aS: (builtins.attrNames aS) == ["__raw"];
in
configAttrs:
lib.collect (aS: (builtins.attrNames aS) == ["key" "value"]) (
lib.attrsets.mapAttrsRecursiveCond (a: !(isValue a)) (
path: value:
assert builtins.isList path; {
key = lib.concatStringsSep "." path;
inherit value;
}
)
configAttrs
);
pythonize = identCount: v: let
nextIdentCount = identCount + 2;
prevIdentCount = identCount - 2;
ident = builtins.concatStringsSep "" (builtins.genList (_: " ") identCount);
prevIdent = builtins.concatStringsSep "" (builtins.genList (_: " ") prevIdentCount);
in
if v == null
then "None"
else if builtins.isBool v
then
(
if v
then "True"
else "False"
)
else if builtins.isString v
then ''"${lib.escape ["\"" "\\"] v}"''
else if builtins.isInt v
then builtins.toString v
else if builtins.isList v
then
(
let
items = builtins.map (pythonize nextIdentCount) v;
in
if (builtins.length items) < 2
then "[${builtins.concatStringsSep "," items}]"
else "[\n${ident}${builtins.concatStringsSep ",\n${ident}" items}\n${prevIdent}]"
)
else if builtins.isAttrs v
then
if (v ? __raw && v.__raw == null)
# Make it possible to skip values, as we would otherwise unconditional override the
# non-set default values with `{}`.
then null
else if (v ? __raw && builtins.isList v.__raw)
# The raw value is already pre-rendered (e.g., used by the bindings.commands to use
# the `config.bind` functions instead of setting the keys directly.)
then v.__raw
else
(
# {key = "value"; other = "other";} -> "{'key': 'value', 'other': 'other'}"
let
items =
lib.attrsets.mapAttrsToList
(key: value: ''${pythonize nextIdentCount key}: ${pythonize nextIdentCount value}'')
(v.__raw or v);
in
if (builtins.length items) == 0
then "{}"
else "{\n${ident}${builtins.concatStringsSep ",\n${ident}" items}\n${prevIdent}}"
)
else builtins.throw "Cannot serialize a '${builtins.typeOf v}' as python value.";
configSet = keyValue: maybe_url: let
value = pythonize 2 keyValue.value;
in
if value == null
then ""
else if builtins.isList value
then builtins.concatStringsSep "\n" value
else "config.set(${pythonize 2 keyValue.key}, ${value}${
if (maybe_url != null)
then ", ${maybe_url}"
else ""
})";
urlConfigSet = url: conf: builtins.map (c: configSet c url) (flattenSettings conf);
extraConfigSet = name: path_or_string: ''config.source("${
if (builtins.isPath path_or_string)
then path_or_string
else pkgs.writeText name path_or_string
}")'';
mkRaw = value: {__raw = value;};
formatQuickmarks = n: s: "${n} ${s}";
settingsType = lib.types.submodule {
freeformType = lib.types.attrsOf lib.types.anything;
options = {
# A list of config keys, that take dicts and need special handling:
# (See their configdata.yml for the source of this list)
# aliases
# bindings.commands
# bindings.default
# bindings.key_mappings
# content.headers.custom
# content.javascript.log
# hints.selectors
# statusbar.padding
# url.searchengines
aliases = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
};
bindings = let
keyBindType = lib.types.nullOr (lib.types.attrsOf (
lib.types.attrsOf (
lib.types.nullOr (
lib.types.separatedString " ;; "
)
)
));
in {
key_mappings = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
};
# This is special, as we need to use the `config.bind` command, instead of the
# normal one.
commands = lib.mkOption {
type = keyBindType;
default = null;
apply = v:
if v == null
then null
else {
__raw =
lib.lists.flatten
(lib.mapAttrsToList
(mode: mappings:
lib.mapAttrsToList
(key: value: "config.bind(${pythonize 0 key}, ${pythonize 0 value}, mode=${pythonize 0 mode})")
mappings)
v);
};
example = lib.literalExpression ''
{
normal = {
"<Ctrl-v>" = "spawn mpv {url}";
",p" = "spawn --userscript qute-pass";
",l" = '''config-cycle spellcheck.languages ["en-GB"] ["en-US"]''';
"<F1>" = mkMerge [
"config-cycle tabs.show never always"
"config-cycle statusbar.show in-mode always"
"config-cycle scrolling.bar never always"
];
};
prompt = {
"<Ctrl-y>" = "prompt-yes";
};
}
'';
};
default = lib.mkOption {
type = keyBindType;
default = null;
apply = mkRaw;
};
};
content = {
headers = {
custom = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
};
};
javascript = {
log = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
};
};
};
hints = {
selectors = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
};
};
qt = {
environ = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
};
};
url = {
searchengines = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
default = null;
apply = mkRaw;
example = lib.literalExpression ''
{
w = "https://en.wikipedia.org/wiki/Special:Search?search={}&go=Go&ns0=1";
aw = "https://wiki.archlinux.org/?search={}";
nw = "https://wiki.nixos.org/index.php?search={}";
g = "https://www.google.com/search?hl=en&q={}";
}
'';
};
};
statusbar = {
padding = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf lib.types.int);
default = null;
apply = mkRaw;
};
};
};
};
in {
options.programs.qutebrowser = {
enable = lib.mkEnableOption "qutebrowser";
package = lib.mkPackageOption pkgs "qutebrowser" {};
loadAutoconfig = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Load settings configured via the GUI.
'';
};
settings = lib.mkOption {
type = settingsType;
default = {};
description = ''
Options to add to qutebrowser {file}`config.py` file.
See <https://qutebrowser.org/doc/help/settings.html>
for options.
'';
example = lib.literalExpression ''
{
colors = {
hints = {
bg = "#000000";
fg = "#ffffff";
};
tabs.bar.bg = "#000000";
};
tabs.tabs_are_windows = true;
}
'';
};
quickmarks = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = {};
description = ''
Quickmarks to add to qutebrowser's {file}`quickmarks` file.
Note that when Home Manager manages your quickmarks, you cannot edit them at runtime.
'';
example = lib.literalExpression ''
{
nixpkgs = "https://github.com/NixOS/nixpkgs";
home-manager = "https://github.com/nix-community/home-manager";
}
'';
};
greasemonkey = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [];
example = lib.literalExpression ''
[
(pkgs.fetchurl {
url = "https://raw.githubusercontent.com/afreakk/greasemonkeyscripts/1d1be041a65c251692ee082eda64d2637edf6444/youtube_sponsorblock.js";
sha256 = "sha256-e3QgDPa3AOpPyzwvVjPQyEsSUC9goisjBUDMxLwg8ZE=";
})
(pkgs.writeText "some-script.js" '''
// ==UserScript==
// @name Some Greasemonkey script
// ==/UserScript==
''')
]
'';
description = ''
Greasemonkey userscripts to add to qutebrowser's {file}`greasemonkey`
directory.
'';
};
perUrlSettings = lib.mkOption {
type = lib.types.attrsOf settingsType;
default = {};
description = ''
Options to set, as in {option}`settings` but per url pattern.
See <https://qutebrowser.org/doc/help/settings.html> for options,
but beware, that not every option supports being set per-url.
'';
example = lib.literalExpression ''
{
"zoom.us" = {
content = {
autoplay = true;
media.audio_capture = true;
media.video_capture = true;
};
};
"github.com".colors.webpage.darkmode.enabled = false;
};
'';
};
extraConfig = lib.mkOption {
type = lib.types.attrsOf (lib.types.either lib.types.str lib.types.pathInStore);
default = {};
description = ''
Extra config snippets. The attribute name is their name, and the value their
value.
'';
example = lib.literalExpression ''
{
"redirects" = ' '
from PyQt5.QtCore import QUrl
from qutebrowser.api import interceptor
def intercept(info: interceptor.Request):
if info.request_url.host() == 'www.youtube.com':
new_url = QUrl(info.request_url)
new_url.setHost('www.invidio.us')
try:
info.redirect(new_url)
except interceptors.RedirectFailedException:
pass
interceptor.register(intercept)
' '
}
'';
};
};
config = let
qutebrowserConfig = lib.concatStringsSep "\n" (
[
(
if cfg.loadAutoconfig
then "config.load_autoconfig()"
else "config.load_autoconfig(False)"
)
]
++ builtins.map (c: configSet c null) (flattenSettings cfg.settings)
++ lib.lists.flatten (lib.mapAttrsToList urlConfigSet cfg.perUrlSettings)
++ lib.mapAttrsToList extraConfigSet cfg.extraConfig
);
quickmarksFile = lib.optionals (cfg.quickmarks != {}) lib.concatStringsSep "\n" (
lib.mapAttrsToList formatQuickmarks cfg.quickmarks
);
greasemonkeyDir =
lib.optionals (
cfg.greasemonkey != []
)
pkgs.linkFarmFromDrvs "greasemonkey-userscripts"
cfg.greasemonkey;
in
lib.mkIf cfg.enable {
home = {
packages = [cfg.package];
file = {
".qutebrowser/config.py" = lib.mkIf pkgs.stdenv.hostPlatform.isDarwin {
text = qutebrowserConfig;
};
".qutebrowser/quickmarks" =
lib.mkIf (cfg.quickmarks != {} && pkgs.stdenv.hostPlatform.isDarwin)
{
text = quickmarksFile;
};
".qutebrowser/greasemonkey" =
lib.mkIf (cfg.greasemonkey != [] && pkgs.stdenv.hostPlatform.isDarwin)
{
source = greasemonkeyDir;
};
};
};
xdg = {
configFile = {
"qutebrowser/config.py" = lib.mkIf pkgs.stdenv.hostPlatform.isLinux {
text = qutebrowserConfig;
onChange = ''
hash="$(echo -n "$USER" | md5sum | cut -d' ' -f1)"
socket="''${XDG_RUNTIME_DIR:-/run/user/$UID}/qutebrowser/ipc-$hash"
if [[ -S $socket ]]; then
command=${
lib.escapeShellArg (
builtins.toJSON {
args = [":config-source"];
target_arg = null;
protocol_version = 1;
}
)
}
echo "$command" | ${pkgs.socat}/bin/socat -lf /dev/null - UNIX-CONNECT:"$socket"
fi
unset hash socket command
'';
};
"qutebrowser/quickmarks" =
lib.mkIf (cfg.quickmarks != {} && pkgs.stdenv.hostPlatform.isLinux)
{
text = quickmarksFile;
};
"qutebrowser/greasemonkey" =
lib.mkIf (cfg.greasemonkey != [] && pkgs.stdenv.hostPlatform.isLinux)
{
source = greasemonkeyDir;
};
};
};
};
}
|