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