1{ config, lib, pkgs, options }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.exporters.imap-mailstat;
7 valueToString = value:
8 if (builtins.typeOf value == "string") then "\"${value}\""
9 else (
10 if (builtins.typeOf value == "int") then "${toString value}"
11 else (
12 if (builtins.typeOf value == "bool") then (if value then "true" else "false")
13 else "XXX ${toString value}"
14 )
15 );
16 createConfigFile = accounts:
17 # unfortunately on toTOML yet
18 # https://github.com/NixOS/nix/issues/3929
19 pkgs.writeText "imap-mailstat-exporter.conf" ''
20 ${concatStrings (attrValues (mapAttrs (name: config: "[[Accounts]]\nname = \"${name}\"\n${concatStrings (attrValues (mapAttrs (k: v: "${k} = ${valueToString v}\n") config))}") accounts))}
21 '';
22 mkOpt = type: description: mkOption {
23 type = types.nullOr type;
24 default = null;
25 description = lib.mdDoc description;
26 };
27 accountOptions.options = {
28 mailaddress = mkOpt types.str "Your email address (at the moment used as login name)";
29 username = mkOpt types.str "If empty string mailaddress value is used";
30 password = mkOpt types.str "";
31 serveraddress = mkOpt types.str "mailserver name or address";
32 serverport = mkOpt types.int "imap port number (at the moment only tls connection is supported)";
33 starttls = mkOpt types.bool "set to true for using STARTTLS to start a TLS connection";
34 };
35in
36{
37 port = 8081;
38 extraOpts = {
39 oldestUnseenDate = mkOption {
40 type = types.bool;
41 default = false;
42 description = lib.mdDoc ''
43 Enable metric with timestamp of oldest unseen mail
44 '';
45 };
46 accounts = mkOption {
47 type = types.attrsOf (types.submodule accountOptions);
48 default = {};
49 description = lib.mdDoc ''
50 Accounts to monitor
51 '';
52 };
53 configurationFile = mkOption {
54 type = types.path;
55 example = "/path/to/config-file";
56 description = lib.mdDoc ''
57 File containing the configuration
58 '';
59 };
60 };
61 serviceOpts = {
62 serviceConfig = {
63 ExecStart = ''
64 ${pkgs.prometheus-imap-mailstat-exporter}/bin/imap-mailstat-exporter \
65 -config ${createConfigFile cfg.accounts} \
66 ${optionalString cfg.oldestUnseenDate "-oldestunseendate"} \
67 ${concatStringsSep " \\\n " cfg.extraFlags}
68 '';
69 };
70 };
71}