summary refs log tree commit diff
path: root/nixos/modules/services/networking/ejabberd.nix
blob: 9868f303ab2b2fdef992518b3c22cc364bc68aa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{ 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 ${if cfg.configFile == null then "" else "--config ${cfg.configFile}"} --ctl-config "${ctlcfg}" --spool "${cfg.spoolDir}" --logs "${cfg.logsDir}"'';

  dumps = lib.concatMapStringsSep " " lib.escapeShellArg cfg.loadDumps;

in {

  ###### interface

  options = {

    services.ejabberd = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable ejabberd server";
      };

      package = mkOption {
        type = types.package;
        default = pkgs.ejabberd;
        defaultText = "pkgs.ejabberd";
        description = "ejabberd server package to use";
      };

      user = mkOption {
        type = types.str;
        default = "ejabberd";
        description = "User under which ejabberd is ran";
      };

      group = mkOption {
        type = types.str;
        default = "ejabberd";
        description = "Group under which ejabberd is ran";
      };

      spoolDir = mkOption {
        type = types.path;
        default = "/var/lib/ejabberd";
        description = "Location of the spooldir of ejabberd";
      };

      logsDir = mkOption {
        type = types.path;
        default = "/var/log/ejabberd";
        description = "Location of the logfile directory of ejabberd";
      };

      configFile = mkOption {
        type = types.nullOr types.path;
        description = "Configuration file for ejabberd in YAML format";
        default = null;
      };

      ctlConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Configuration of ejabberdctl";
      };

      loadDumps = mkOption {
        type = types.listOf types.path;
        default = [];
        description = "Configuration dumps that should be loaded on the first startup";
        example = literalExample "[ ./myejabberd.dump ]";
      };

      imagemagick = mkOption {
        type = types.bool;
        default = false;
        description = "Add ImageMagick to server's path; allows for image thumbnailing";
      };
    };

  };


  ###### implementation

  config = mkIf cfg.enable {
    environment.systemPackages = [ cfg.package ];

    users.extraUsers = optionalAttrs (cfg.user == "ejabberd") (singleton
      { name = "ejabberd";
        group = cfg.group;
        home = cfg.spoolDir;
        createHome = true;
        uid = config.ids.uids.ejabberd;
      });

    users.extraGroups = optionalAttrs (cfg.group == "ejabberd") (singleton
      { name = "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 = {
        Type = "forking";
        # FIXME: runit is used for `chpst` -- can we get rid of this?
        ExecStop = ''${pkgs.runit}/bin/chpst -u "${cfg.user}:${cfg.group}" ${ectl} stop'';
        ExecReload = ''${pkgs.runit}/bin/chpst -u "${cfg.user}:${cfg.group}" ${ectl} reload_config'';
        User = cfg.user;
        Group = cfg.group;
        PermissionsStartOnly = true;
      };

      preStart = ''
        mkdir -p -m750 "${cfg.logsDir}"
        chown "${cfg.user}:${cfg.group}" "${cfg.logsDir}"

        mkdir -p -m750 "/var/lock/ejabberdctl"
        chown "${cfg.user}:${cfg.group}" "/var/lock/ejabberdctl"

        mkdir -p -m750 "${cfg.spoolDir}"
        chown -R "${cfg.user}:${cfg.group}" "${cfg.spoolDir}"
      '';

      script = ''
        [ -z "$(ls -A '${cfg.spoolDir}')" ] && firstRun=1

        ${ectl} start

        count=0
        while ! ${ectl} status >/dev/null 2>&1; do
          if [ $count -eq 30 ]; then
            echo "ejabberd server hasn't started in 30 seconds, giving up"
            exit 1
          fi

          count=$((count++))
          sleep 1
        done

        if [ -n "$firstRun" ]; then
          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
      '';
    };

    security.pam.services.ejabberd = {};

  };

}