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 = lib.mdDoc "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 = lib.mdDoc "Data directory for errbot instance.";
37 };
38
39 plugins = mkOption {
40 type = types.listOf types.package;
41 default = [];
42 description = lib.mdDoc "List of errbot plugin derivations.";
43 };
44
45 logLevel = mkOption {
46 type = types.str;
47 default = "INFO";
48 description = lib.mdDoc "Errbot log level";
49 };
50
51 admins = mkOption {
52 type = types.listOf types.str;
53 default = [];
54 description = lib.mdDoc "List of identifiers of errbot admins.";
55 };
56
57 backend = mkOption {
58 type = types.str;
59 default = "XMPP";
60 description = lib.mdDoc "Errbot backend name.";
61 };
62
63 identity = mkOption {
64 type = types.attrs;
65 description = lib.mdDoc "Errbot identity configuration";
66 };
67
68 extraConfig = mkOption {
69 type = types.lines;
70 default = "";
71 description = lib.mdDoc "String to be appended to the config verbatim";
72 };
73 };
74 });
75 };
76 };
77
78 config = mkIf (cfg.instances != {}) {
79 users.users.errbot = {
80 group = "errbot";
81 isSystemUser = true;
82 };
83 users.groups.errbot = {};
84
85 systemd.services = mapAttrs' (name: instanceCfg: nameValuePair "errbot-${name}" (
86 let
87 dataDir = if instanceCfg.dataDir != null then instanceCfg.dataDir else
88 "/var/lib/errbot/${name}";
89 in {
90 after = [ "network-online.target" ];
91 wantedBy = [ "multi-user.target" ];
92 preStart = ''
93 mkdir -p ${dataDir}
94 chown -R errbot:errbot ${dataDir}
95 '';
96 serviceConfig = {
97 User = "errbot";
98 Restart = "on-failure";
99 ExecStart = "${pkgs.errbot}/bin/errbot -c ${mkConfigDir instanceCfg dataDir}/config.py";
100 PermissionsStartOnly = true;
101 };
102 })) cfg.instances;
103 };
104}