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