summary refs log tree commit diff
path: root/nixos/modules/services/networking/ejabberd.nix
blob: 28b8e234a5cf5695d885fdb5ad233c0291762a50 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.ejabberd;

in

{

  ###### interface

  options = {

    services.ejabberd = {

      enable = mkOption {
        default = false;
        description = "Whether to enable ejabberd server";
      };

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

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

      confDir = mkOption {
        default = "/var/ejabberd";
        description = "Location of the config directory of ejabberd";
      };

      virtualHosts = mkOption {
        default = "\"localhost\"";
        description = "Virtualhosts that ejabberd should host. Hostnames are surrounded with doublequotes and separated by commas";
      };

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

  };


  ###### implementation

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

    jobs.ejabberd =
      { description = "EJabberd server";

        startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        environment = {
          PATH = "$PATH:${pkgs.ejabberd}/sbin:${pkgs.ejabberd}/bin:${pkgs.coreutils}/bin:${pkgs.bash}/bin:${pkgs.gnused}/bin";
        };

        preStart =
          ''
            PATH="$PATH:${pkgs.ejabberd}/sbin:${pkgs.ejabberd}/bin:${pkgs.coreutils}/bin:${pkgs.bash}/bin:${pkgs.gnused}/bin";
	    
            # Initialise state data
            mkdir -p ${cfg.logsDir}

            if ! test -d ${cfg.spoolDir}
            then
                initialize=1
                cp -av ${pkgs.ejabberd}/var/lib/ejabberd /var/lib
            fi

            if ! test -d ${cfg.confDir}
            then
                mkdir -p ${cfg.confDir}
                cp ${pkgs.ejabberd}/etc/ejabberd/* ${cfg.confDir}
                sed -e 's|{hosts, \["localhost"\]}.|{hosts, \[${cfg.virtualHosts}\]}.|' ${pkgs.ejabberd}/etc/ejabberd/ejabberd.cfg > ${cfg.confDir}/ejabberd.cfg
            fi

            ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} start

            ${if cfg.loadDumps == [] then "" else
              ''
                if [ "$initialize" = "1" ]
                then
                    # Wait until the ejabberd server is available for use
                    count=0
                    while ! ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} status
                    do
                        if [ $count -eq 30 ]
                        then
                            echo "Tried 30 times, giving up..."
                            exit 1
                        fi

                        echo "Ejabberd daemon not yet started. Waiting for 1 second..."
                        count=$((count++))
                        sleep 1
                    done

                    ${concatMapStrings (dump:
                      ''
                        echo "Importing dump: ${dump}"

                        if [ -f ${dump} ]
                        then
                            ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} load ${dump}
                        elif [ -d ${dump} ]
                        then
                            for i in ${dump}/ejabberd-dump/*
                            do
                                ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} load $i
                            done
                        fi
                      '') cfg.loadDumps}
                fi
              ''}
          '';

        postStop =
          ''
            ejabberdctl --config-dir ${cfg.confDir} --logs ${cfg.logsDir} --spool ${cfg.spoolDir} stop
          '';
      };

    security.pam.services.ejabberd = {};

  };

}