2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.ejabberd;
|
|
|
|
|
|
|
|
ctlcfg = pkgs.writeText "ejabberdctl.cfg" ''
|
|
|
|
ERL_EPMD_ADDRESS=127.0.0.1
|
|
|
|
${cfg.ctlConfig}
|
|
|
|
'';
|
|
|
|
|
|
|
|
ectl = ''${cfg.package}/bin/ejabberdctl ${optionalString (cfg.configFile != null) "--config ${cfg.configFile}"} --ctl-config "${ctlcfg}" --spool "${cfg.spoolDir}" --logs "${cfg.logsDir}"'';
|
|
|
|
|
|
|
|
dumps = lib.escapeShellArgs cfg.loadDumps;
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.ejabberd = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether to enable ejabberd server";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "ejabberd" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "ejabberd";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "User under which ejabberd is ran";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "ejabberd";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Group under which ejabberd is ran";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
spoolDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/ejabberd";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Location of the spooldir of ejabberd";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
logsDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/log/ejabberd";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Location of the logfile directory of ejabberd";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Configuration file for ejabberd in YAML format";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ctlConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Configuration of ejabberdctl";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
loadDumps = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
default = [];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Configuration dumps that should be loaded on the first startup";
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression "[ ./myejabberd.dump ]";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
imagemagick = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Add ImageMagick to server's path; allows for image thumbnailing";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
|
|
|
users.users = optionalAttrs (cfg.user == "ejabberd") {
|
|
|
|
ejabberd = {
|
|
|
|
group = cfg.group;
|
|
|
|
home = cfg.spoolDir;
|
|
|
|
createHome = true;
|
|
|
|
uid = config.ids.uids.ejabberd;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == "ejabberd") {
|
|
|
|
ejabberd.gid = config.ids.gids.ejabberd;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.ejabberd = {
|
|
|
|
description = "ejabberd server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
path = [ pkgs.findutils pkgs.coreutils ] ++ lib.optional cfg.imagemagick pkgs.imagemagick;
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
ExecStart = "${ectl} foreground";
|
|
|
|
ExecStop = "${ectl} stop";
|
|
|
|
ExecReload = "${ectl} reload_config";
|
|
|
|
};
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
if [ -z "$(ls -A '${cfg.spoolDir}')" ]; then
|
|
|
|
touch "${cfg.spoolDir}/.firstRun"
|
|
|
|
fi
|
2024-01-02 11:29:13 +00:00
|
|
|
|
|
|
|
if ! test -e ${cfg.spoolDir}/.erlang.cookie; then
|
|
|
|
touch ${cfg.spoolDir}/.erlang.cookie
|
|
|
|
chmod 600 ${cfg.spoolDir}/.erlang.cookie
|
|
|
|
dd if=/dev/random bs=16 count=1 | base64 > ${cfg.spoolDir}/.erlang.cookie
|
|
|
|
fi
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
postStart = ''
|
|
|
|
while ! ${ectl} status >/dev/null 2>&1; do
|
|
|
|
if ! kill -0 "$MAINPID"; then exit 1; fi
|
|
|
|
sleep 0.1
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -e "${cfg.spoolDir}/.firstRun" ]; then
|
|
|
|
rm "${cfg.spoolDir}/.firstRun"
|
|
|
|
for src in ${dumps}; do
|
|
|
|
find "$src" -type f | while read dump; do
|
|
|
|
echo "Loading configuration dump at $dump"
|
|
|
|
${ectl} load "$dump"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.logsDir}' 0750 ${cfg.user} ${cfg.group} -"
|
|
|
|
"d '${cfg.spoolDir}' 0700 ${cfg.user} ${cfg.group} -"
|
|
|
|
];
|
|
|
|
|
|
|
|
security.pam.services.ejabberd = {};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|