1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 concatAndSort =
9 name: files:
10 pkgs.runCommand name { } ''
11 awk 1 ${lib.escapeShellArgs files} | sed '{ /^\s*$/d; s/^\s\+//; s/\s\+$// }' | sort | uniq > $out
12 '';
13in
14{
15 options = {
16 environment.wordlist = {
17 enable = lib.mkEnableOption "environment variables for lists of words";
18
19 lists = lib.mkOption {
20 type = lib.types.attrsOf (lib.types.nonEmptyListOf lib.types.path);
21
22 default = {
23 WORDLIST = [ "${pkgs.scowl}/share/dict/words.txt" ];
24 };
25
26 defaultText = lib.literalExpression ''
27 {
28 WORDLIST = [ "''${pkgs.scowl}/share/dict/words.txt" ];
29 }
30 '';
31
32 description = ''
33 A set with the key names being the environment variable you'd like to
34 set and the values being a list of paths to text documents containing
35 lists of words. The various files will be merged, sorted, duplicates
36 removed, and extraneous spacing removed.
37
38 If you have a handful of words that you want to add to an already
39 existing wordlist, you may find `builtins.toFile` useful for this
40 task.
41 '';
42
43 example = lib.literalExpression ''
44 {
45 WORDLIST = [ "''${pkgs.scowl}/share/dict/words.txt" ];
46 AUGMENTED_WORDLIST = [
47 "''${pkgs.scowl}/share/dict/words.txt"
48 "''${pkgs.scowl}/share/dict/words.variants.txt"
49 (builtins.toFile "extra-words" '''
50 desynchonization
51 oobleck''')
52 ];
53 }
54 '';
55 };
56 };
57 };
58
59 config = lib.mkIf config.environment.wordlist.enable {
60 environment.variables = lib.mapAttrs (
61 name: value: "${concatAndSort "wordlist-${name}" value}"
62 ) config.environment.wordlist.lists;
63 };
64}