summaryrefslogtreecommitdiffstats
path: root/nixos/modules/misc/wordlist.nix
blob: 0bd1072baa2283895f24c3e194ce223efc4795aa (plain)
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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  concatAndSort =
    name: files:
    pkgs.runCommand name { } ''
      awk 1 ${lib.escapeShellArgs files} | sed '{ /^\s*$/d; s/^\s\+//; s/\s\+$// }' | sort | uniq > $out
    '';
in
{
  options = {
    environment.wordlist = {
      enable = lib.mkEnableOption "environment variables for lists of words";

      lists = lib.mkOption {
        type = lib.types.attrsOf (lib.types.nonEmptyListOf lib.types.path);

        default = {
          WORDLIST = [ "${pkgs.scowl}/share/dict/words.txt" ];
        };

        defaultText = lib.literalExpression ''
          {
            WORDLIST = [ "''${pkgs.scowl}/share/dict/words.txt" ];
          }
        '';

        description = ''
          A set with the key names being the environment variable you'd like to
          set and the values being a list of paths to text documents containing
          lists of words. The various files will be merged, sorted, duplicates
          removed, and extraneous spacing removed.

          If you have a handful of words that you want to add to an already
          existing wordlist, you may find `builtins.toFile` useful for this
          task.
        '';

        example = lib.literalExpression ''
          {
            WORDLIST = [ "''${pkgs.scowl}/share/dict/words.txt" ];
            AUGMENTED_WORDLIST = [
              "''${pkgs.scowl}/share/dict/words.txt"
              "''${pkgs.scowl}/share/dict/words.variants.txt"
              (builtins.toFile "extra-words" '''
                desynchonization
                oobleck''')
            ];
          }
        '';
      };
    };
  };

  config = lib.mkIf config.environment.wordlist.enable {
    environment.variables = lib.mapAttrs (
      name: value: "${concatAndSort "wordlist-${name}" value}"
    ) config.environment.wordlist.lists;
  };
}