1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.ejabberd;
8
9 ctlcfg = pkgs.writeText "ejabberdctl.cfg" ''
10 ERL_EPMD_ADDRESS=127.0.0.1
11 ${cfg.ctlConfig}
12 '';
13
14 ectl = ''${cfg.package}/bin/ejabberdctl ${optionalString (cfg.configFile != null) "--config ${cfg.configFile}"} --ctl-config "${ctlcfg}" --spool "${cfg.spoolDir}" --logs "${cfg.logsDir}"'';
15
16 dumps = lib.escapeShellArgs cfg.loadDumps;
17
18in {
19
20 ###### interface
21
22 options = {
23
24 services.ejabberd = {
25
26 enable = mkOption {
27 type = types.bool;
28 default = false;
29 description = "Whether to enable ejabberd server";
30 };
31
32 package = mkPackageOption pkgs "ejabberd" { };
33
34 user = mkOption {
35 type = types.str;
36 default = "ejabberd";
37 description = "User under which ejabberd is ran";
38 };
39
40 group = mkOption {
41 type = types.str;
42 default = "ejabberd";
43 description = "Group under which ejabberd is ran";
44 };
45
46 spoolDir = mkOption {
47 type = types.path;
48 default = "/var/lib/ejabberd";
49 description = "Location of the spooldir of ejabberd";
50 };
51
52 logsDir = mkOption {
53 type = types.path;
54 default = "/var/log/ejabberd";
55 description = "Location of the logfile directory of ejabberd";
56 };
57
58 configFile = mkOption {
59 type = types.nullOr types.path;
60 description = "Configuration file for ejabberd in YAML format";
61 default = null;
62 };
63
64 ctlConfig = mkOption {
65 type = types.lines;
66 default = "";
67 description = "Configuration of ejabberdctl";
68 };
69
70 loadDumps = mkOption {
71 type = types.listOf types.path;
72 default = [];
73 description = "Configuration dumps that should be loaded on the first startup";
74 example = literalExpression "[ ./myejabberd.dump ]";
75 };
76
77 imagemagick = mkOption {
78 type = types.bool;
79 default = false;
80 description = "Add ImageMagick to server's path; allows for image thumbnailing";
81 };
82 };
83
84 };
85
86
87 ###### implementation
88
89 config = mkIf cfg.enable {
90 environment.systemPackages = [ cfg.package ];
91
92 users.users = optionalAttrs (cfg.user == "ejabberd") {
93 ejabberd = {
94 group = cfg.group;
95 home = cfg.spoolDir;
96 createHome = true;
97 uid = config.ids.uids.ejabberd;
98 };
99 };
100
101 users.groups = optionalAttrs (cfg.group == "ejabberd") {
102 ejabberd.gid = config.ids.gids.ejabberd;
103 };
104
105 systemd.services.ejabberd = {
106 description = "ejabberd server";
107 wantedBy = [ "multi-user.target" ];
108 after = [ "network.target" ];
109 path = [ pkgs.findutils pkgs.coreutils ] ++ lib.optional cfg.imagemagick pkgs.imagemagick;
110
111 serviceConfig = {
112 User = cfg.user;
113 Group = cfg.group;
114 ExecStart = "${ectl} foreground";
115 ExecStop = "${ectl} stop";
116 ExecReload = "${ectl} reload_config";
117 };
118
119 preStart = ''
120 if [ -z "$(ls -A '${cfg.spoolDir}')" ]; then
121 touch "${cfg.spoolDir}/.firstRun"
122 fi
123
124 if ! test -e ${cfg.spoolDir}/.erlang.cookie; then
125 touch ${cfg.spoolDir}/.erlang.cookie
126 chmod 600 ${cfg.spoolDir}/.erlang.cookie
127 dd if=/dev/random bs=16 count=1 | base64 > ${cfg.spoolDir}/.erlang.cookie
128 fi
129 '';
130
131 postStart = ''
132 while ! ${ectl} status >/dev/null 2>&1; do
133 if ! kill -0 "$MAINPID"; then exit 1; fi
134 sleep 0.1
135 done
136
137 if [ -e "${cfg.spoolDir}/.firstRun" ]; then
138 rm "${cfg.spoolDir}/.firstRun"
139 for src in ${dumps}; do
140 find "$src" -type f | while read dump; do
141 echo "Loading configuration dump at $dump"
142 ${ectl} load "$dump"
143 done
144 done
145 fi
146 '';
147 };
148
149 systemd.tmpfiles.rules = [
150 "d '${cfg.logsDir}' 0750 ${cfg.user} ${cfg.group} -"
151 "d '${cfg.spoolDir}' 0700 ${cfg.user} ${cfg.group} -"
152 ];
153
154 security.pam.services.ejabberd = {};
155
156 };
157
158}