1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.ejabberd;
8
9in
10
11{
12
13 ###### interface
14
15 options = {
16
17 services.ejabberd = {
18
19 enable = mkOption {
20 default = false;
21 description = "Whether to enable ejabberd server";
22 };
23
24 spoolDir = mkOption {
25 default = "/var/lib/ejabberd";
26 description = "Location of the spooldir of ejabberd";
27 };
28
29 logsDir = mkOption {
30 default = "/var/log/ejabberd";
31 description = "Location of the logfile directory of ejabberd";
32 };
33
34 confDir = mkOption {
35 default = "/var/ejabberd";
36 description = "Location of the config directory of ejabberd";
37 };
38
39 virtualHosts = mkOption {
40 default = "\"localhost\"";
41 description = "Virtualhosts that ejabberd should host. Hostnames are surrounded with doublequotes and separated by commas";
42 };
43
44 loadDumps = mkOption {
45 default = [];
46 description = "Configuration dump that should be loaded on the first startup";
47 example = literalExample "[ ./myejabberd.dump ]";
48 };
49 };
50
51 };
52
53
54 ###### implementation
55
56 config = mkIf cfg.enable {
57 environment.systemPackages = [ pkgs.ejabberd ];
58
59 jobs.ejabberd =
60 { description = "EJabberd server";
61
62 startOn = "started network-interfaces";
63 stopOn = "stopping network-interfaces";
64
65 environment = {
66 PATH = "$PATH:${pkgs.ejabberd}/sbin:${pkgs.ejabberd}/bin:${pkgs.coreutils}/bin:${pkgs.bash}/bin:${pkgs.gnused}/bin";
67 };
68
69 preStart =
70 ''
71 PATH="$PATH:${pkgs.ejabberd}/sbin:${pkgs.ejabberd}/bin:${pkgs.coreutils}/bin:${pkgs.bash}/bin:${pkgs.gnused}/bin";
72
73 # Initialise state data
74 mkdir -p ${cfg.logsDir}
75
76 if ! test -d ${cfg.spoolDir}
77 then
78 initialize=1
79 cp -av ${pkgs.ejabberd}/var/lib/ejabberd /var/lib
80 fi
81
82 if ! test -d ${cfg.confDir}
83 then
84 mkdir -p ${cfg.confDir}
85 cp ${pkgs.ejabberd}/etc/ejabberd/* ${cfg.confDir}
86 sed -e 's|{hosts, \["localhost"\]}.|{hosts, \[${cfg.virtualHosts}\]}.|' ${pkgs.ejabberd}/etc/ejabberd/ejabberd.cfg > ${cfg.confDir}/ejabberd.cfg
87 fi
88
89 ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} start
90
91 ${if cfg.loadDumps == [] then "" else
92 ''
93 if [ "$initialize" = "1" ]
94 then
95 # Wait until the ejabberd server is available for use
96 count=0
97 while ! ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} status
98 do
99 if [ $count -eq 30 ]
100 then
101 echo "Tried 30 times, giving up..."
102 exit 1
103 fi
104
105 echo "Ejabberd daemon not yet started. Waiting for 1 second..."
106 count=$((count++))
107 sleep 1
108 done
109
110 ${concatMapStrings (dump:
111 ''
112 echo "Importing dump: ${dump}"
113
114 if [ -f ${dump} ]
115 then
116 ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} load ${dump}
117 elif [ -d ${dump} ]
118 then
119 for i in ${dump}/ejabberd-dump/*
120 do
121 ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} load $i
122 done
123 fi
124 '') cfg.loadDumps}
125 fi
126 ''}
127 '';
128
129 postStop =
130 ''
131 ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} stop
132 '';
133 };
134
135 security.pam.services.ejabberd = {};
136
137 };
138
139}