1 { config, lib, pkgs, ... }:
4 cfg = config.services.ejabberd;
6 ctlcfg = pkgs.writeText "ejabberdctl.cfg" ''
7 ERL_EPMD_ADDRESS=127.0.0.1
11 ectl = ''${cfg.package}/bin/ejabberdctl ${lib.optionalString (cfg.configFile != null) "--config ${cfg.configFile}"} --ctl-config "${ctlcfg}" --spool "${cfg.spoolDir}" --logs "${cfg.logsDir}"'';
13 dumps = lib.escapeShellArgs cfg.loadDumps;
23 enable = lib.mkOption {
24 type = lib.types.bool;
26 description = "Whether to enable ejabberd server";
29 package = lib.mkPackageOption pkgs "ejabberd" { };
34 description = "User under which ejabberd is ran";
37 group = lib.mkOption {
40 description = "Group under which ejabberd is ran";
43 spoolDir = lib.mkOption {
44 type = lib.types.path;
45 default = "/var/lib/ejabberd";
46 description = "Location of the spooldir of ejabberd";
49 logsDir = lib.mkOption {
50 type = lib.types.path;
51 default = "/var/log/ejabberd";
52 description = "Location of the logfile directory of ejabberd";
55 configFile = lib.mkOption {
56 type = lib.types.nullOr lib.types.path;
57 description = "Configuration file for ejabberd in YAML format";
61 ctlConfig = lib.mkOption {
62 type = lib.types.lines;
64 description = "Configuration of ejabberdctl";
67 loadDumps = lib.mkOption {
68 type = lib.types.listOf lib.types.path;
70 description = "Configuration dumps that should be loaded on the first startup";
71 example = lib.literalExpression "[ ./myejabberd.dump ]";
74 imagemagick = lib.mkOption {
75 type = lib.types.bool;
77 description = "Add ImageMagick to server's path; allows for image thumbnailing";
86 config = lib.mkIf cfg.enable {
87 environment.systemPackages = [ cfg.package ];
89 users.users = lib.optionalAttrs (cfg.user == "ejabberd") {
94 uid = config.ids.uids.ejabberd;
98 users.groups = lib.optionalAttrs (cfg.group == "ejabberd") {
99 ejabberd.gid = config.ids.gids.ejabberd;
102 systemd.services.ejabberd = {
103 description = "ejabberd server";
104 wantedBy = [ "multi-user.target" ];
105 after = [ "network.target" ];
106 path = [ pkgs.findutils pkgs.coreutils ] ++ lib.optional cfg.imagemagick pkgs.imagemagick;
111 ExecStart = "${ectl} foreground";
112 ExecStop = "${ectl} stop";
113 ExecReload = "${ectl} reload_config";
117 if [ -z "$(ls -A '${cfg.spoolDir}')" ]; then
118 touch "${cfg.spoolDir}/.firstRun"
121 if ! test -e ${cfg.spoolDir}/.erlang.cookie; then
122 touch ${cfg.spoolDir}/.erlang.cookie
123 chmod 600 ${cfg.spoolDir}/.erlang.cookie
124 dd if=/dev/random bs=16 count=1 | base64 > ${cfg.spoolDir}/.erlang.cookie
129 while ! ${ectl} status >/dev/null 2>&1; do
130 if ! kill -0 "$MAINPID"; then exit 1; fi
134 if [ -e "${cfg.spoolDir}/.firstRun" ]; then
135 rm "${cfg.spoolDir}/.firstRun"
136 for src in ${dumps}; do
137 find "$src" -type f | while read dump; do
138 echo "Loading configuration dump at $dump"
146 systemd.tmpfiles.rules = [
147 "d '${cfg.logsDir}' 0750 ${cfg.user} ${cfg.group} -"
148 "d '${cfg.spoolDir}' 0700 ${cfg.user} ${cfg.group} -"
151 security.pam.services.ejabberd = {};