1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.errbot;
9 pluginEnv =
10 plugins:
11 pkgs.buildEnv {
12 name = "errbot-plugins";
13 paths = plugins;
14 };
15 mkConfigDir =
16 instanceCfg: dataDir:
17 pkgs.writeTextDir "config.py" ''
18 import logging
19 BACKEND = '${instanceCfg.backend}'
20 BOT_DATA_DIR = '${dataDir}'
21 BOT_EXTRA_PLUGIN_DIR = '${pluginEnv instanceCfg.plugins}'
22
23 BOT_LOG_LEVEL = logging.${instanceCfg.logLevel}
24 BOT_LOG_FILE = False
25
26 BOT_ADMINS = (${lib.concatMapStringsSep "," (name: "'${name}'") instanceCfg.admins})
27
28 BOT_IDENTITY = ${builtins.toJSON instanceCfg.identity}
29
30 ${instanceCfg.extraConfig}
31 '';
32in
33{
34 options = {
35 services.errbot.instances = lib.mkOption {
36 default = { };
37 description = "Errbot instance configs";
38 type = lib.types.attrsOf (
39 lib.types.submodule {
40 options = {
41 dataDir = lib.mkOption {
42 type = lib.types.nullOr lib.types.path;
43 default = null;
44 description = "Data directory for errbot instance.";
45 };
46
47 plugins = lib.mkOption {
48 type = lib.types.listOf lib.types.package;
49 default = [ ];
50 description = "List of errbot plugin derivations.";
51 };
52
53 logLevel = lib.mkOption {
54 type = lib.types.str;
55 default = "INFO";
56 description = "Errbot log level";
57 };
58
59 admins = lib.mkOption {
60 type = lib.types.listOf lib.types.str;
61 default = [ ];
62 description = "List of identifiers of errbot admins.";
63 };
64
65 backend = lib.mkOption {
66 type = lib.types.str;
67 default = "XMPP";
68 description = "Errbot backend name.";
69 };
70
71 identity = lib.mkOption {
72 type = lib.types.attrs;
73 description = "Errbot identity configuration";
74 };
75
76 extraConfig = lib.mkOption {
77 type = lib.types.lines;
78 default = "";
79 description = "String to be appended to the config verbatim";
80 };
81 };
82 }
83 );
84 };
85 };
86
87 config = lib.mkIf (cfg.instances != { }) {
88 users.users.errbot = {
89 group = "errbot";
90 isSystemUser = true;
91 };
92 users.groups.errbot = { };
93
94 systemd.services = lib.mapAttrs' (
95 name: instanceCfg:
96 lib.nameValuePair "errbot-${name}" (
97 let
98 dataDir = if instanceCfg.dataDir != null then instanceCfg.dataDir else "/var/lib/errbot/${name}";
99 in
100 {
101 after = [ "network-online.target" ];
102 wantedBy = [ "multi-user.target" ];
103 preStart = ''
104 mkdir -p ${dataDir}
105 chown -R errbot:errbot ${dataDir}
106 '';
107 serviceConfig = {
108 User = "errbot";
109 Restart = "on-failure";
110 ExecStart = "${pkgs.errbot}/bin/errbot -c ${mkConfigDir instanceCfg dataDir}/config.py";
111 PermissionsStartOnly = true;
112 };
113 }
114 )
115 ) cfg.instances;
116 };
117}