fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Gabriel Arazasrecoll: update option descriptions05649393

master
5.2 KiB184 linesraw
1{ config, options, lib, pkgs, ... }:
2
3with lib;
4
5# TODO: Fix the formatting of the resulting config.
6let
7  cfg = config.services.recoll;
8
9  # The key-value generator for Recoll config format. For future references,
10  # see the example configuration from the package (i.e.,
11  # `$out/share/recoll/examples/recoll.conf`).
12  mkRecollConfKeyValue = generators.mkKeyValueDefault {
13    mkValueString = let mkQuoted = v: ''"${escape [ ''"'' ] v}"'';
14    in v:
15    if v == true then
16      "1"
17    else if v == false then
18      "0"
19    else if isList v then
20      concatMapStringsSep " " mkQuoted v
21    else
22      generators.mkValueStringDefault { } v;
23  } " = ";
24
25  # A modified version of 'lib.generators.toINI' that also accepts top-level
26  # attributes as non-attrsets.
27  toRecollConf = { listsAsDuplicateKeys ? false }:
28    attr:
29    let
30      toKeyValue = generators.toKeyValue {
31        inherit listsAsDuplicateKeys;
32        mkKeyValue = mkRecollConfKeyValue;
33      };
34      mkSectionName = name: strings.escape [ "[" "]" ] name;
35      convert = k: v:
36        if isAttrs v then
37          ''
38            [${mkSectionName k}]
39          '' + toKeyValue v
40        else
41          toKeyValue { "${k}" = v; };
42
43      # TODO: Improve this chunk of code, pls.
44      # There's a possibility of attributes with attrsets overriding other
45      # top-level attributes with non-attrsets so we're forcing the attrsets to
46      # come last.
47      _config = mapAttrsToList convert (filterAttrs (k: v: !isAttrs v) attr);
48      _config' = mapAttrsToList convert (filterAttrs (k: v: isAttrs v) attr);
49      config = _config ++ _config';
50    in concatStringsSep "\n" config;
51
52  # A specific type for Recoll config format. Taken from `pkgs.formats`
53  # implementation from nixpkgs. See the 'Nix-representable formats' from the
54  # NixOS manual for more information.
55  recollConfFormat = { }: {
56    type = with types;
57      let
58        valueType = nullOr (oneOf [
59          bool
60          float
61          int
62          path
63          str
64          (attrsOf valueType)
65          (listOf valueType)
66        ]) // {
67          description = "Recoll config value";
68        };
69      in attrsOf valueType;
70
71    generate = name: value: pkgs.writeText name (toRecollConf { } value);
72  };
73
74  # The actual object we're going to use for this module. This is for the sake
75  # of consistency (and dogfooding the settings format implementation).
76  settingsFormat = recollConfFormat { };
77in {
78  meta.maintainers = [ maintainers.foo-dogsquared ];
79
80  options.services.recoll = {
81    enable = mkEnableOption "Recoll file index service";
82
83    package = mkOption {
84      type = types.package;
85      default = pkgs.recoll;
86      defaultText = literalExpression "pkgs.recoll";
87      description = ''
88        Package providing the {command}`recoll` binary.
89      '';
90      example = literalExpression "(pkgs.recoll.override { withGui = false; })";
91    };
92
93    startAt = mkOption {
94      type = types.str;
95      default = "hourly";
96      example = "00/2:00";
97      description = ''
98        When or how often the periodic update should run. Must be the format
99        described from {manpage}`systemd.time(7)`.
100      '';
101    };
102
103    settings = mkOption {
104      type = settingsFormat.type;
105      default = { };
106      description = ''
107        The configuration to be written at {file}`$RECOLL_CONFDIR/recoll.conf`.
108
109        See {manpage}`recoll.conf(5)` for more details about the configuration.
110      '';
111      example = literalExpression ''
112        {
113          nocjk = true;
114          loglevel = 5;
115          topdirs = [ "~/Downloads" "~/Documents" "~/projects" ];
116
117          "~/Downloads" = {
118            "skippedNames+" = [ "*.iso" ];
119          };
120
121          "~/projects" = {
122            "skippedNames+" = [ "node_modules" "target" "result" ];
123          };
124        }
125      '';
126    };
127
128    configDir = mkOption {
129      type = types.str;
130      default = "${config.home.homeDirectory}/.recoll";
131      defaultText = literalExpression "\${config.home.homeDirectory}/.recoll";
132      example = literalExpression "\${config.xdg.configHome}/recoll";
133      description = ''
134        The directory to contain Recoll configuration files. This will be set
135        as {env}`RECOLL_CONFDIR`.
136      '';
137    };
138  };
139
140  config = mkIf cfg.enable {
141    assertions = [
142      (lib.hm.assertions.assertPlatform "services.recoll" pkgs
143        lib.platforms.linux)
144    ];
145
146    home.packages = [ cfg.package ];
147
148    home.sessionVariables = { RECOLL_CONFDIR = cfg.configDir; };
149
150    home.file."${cfg.configDir}/recoll.conf".source =
151      settingsFormat.generate "recoll-conf-${config.home.username}"
152      cfg.settings;
153
154    systemd.user.services.recollindex = {
155      Unit = {
156        Description = "Recoll index update";
157        Documentation = [
158          "man:recoll"
159          "man:recollindex"
160          "https://www.lesbonscomptes.com/recoll/usermanual/"
161        ];
162      };
163
164      Service = {
165        ExecStart = "${cfg.package}/bin/recollindex";
166        Environment = [ "RECOLL_CONFDIR=${escapeShellArg cfg.configDir}" ];
167      };
168    };
169
170    systemd.user.timers.recollindex = {
171      Unit = {
172        Description = "Recoll index update";
173        PartOf = [ "default.target" ];
174      };
175
176      Timer = {
177        Persistent = true;
178        OnCalendar = cfg.startAt;
179      };
180
181      Install.WantedBy = [ "timers.target" ];
182    };
183  };
184}