aboutsummaryrefslogtreecommitdiff
path: root/systems/eldiron/ejabberd/default.nix
blob: 463d25511afbfc5cad177846ad0bda31f480768f (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
{ lib, pkgs, config, mypackages-lib, ... }:
let
  cfg = config.myServices.ejabberd;
in
{
  options.myServices = {
    ejabberd.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Whether to enable ejabberd service.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    myServices.dns.zones."immae.fr" = with config.myServices.dns.helpers;
      lib.mkMerge [
        {
          extraConfig = ''
            notify yes;
          '';
          slaves = [ "raito" ];
          emailPolicies."".receive = true;
        }
        zoneHeader
        mailMX
        (mailCommon "immae.fr" true)
        (ips servers.eldiron.ips.main)
        {
          ns = [ "immae" "raito" ];
          CAA = letsencrypt;
          subdomains.www = ips servers.eldiron.ips.production;
          subdomains.im = ips servers.eldiron.ips.main;
          subdomains.conference = ips servers.eldiron.ips.main;
          subdomains.pubsub = ips servers.eldiron.ips.main;
          subdomains.proxy = ips servers.eldiron.ips.main;
          subdomains.upload = ips servers.eldiron.ips.main;
          subdomains._xmppconnect.TXT = [
            "_xmpp-client-xbosh=https://im.immae.fr/bosh"
            "_xmpp-client-websocket=wss://im.immae.fr/ws"
          ];
        }
      ];

    security.acme.certs = {
      "ejabberd" = {
        group = "ejabberd";
        domain = "eldiron.immae.eu";
        keyType = "rsa4096";
        postRun = ''
          systemctl restart ejabberd.service
          '';
        extraDomainNames = [ "immae.fr" "conference.immae.fr" "proxy.immae.fr" "pubsub.immae.fr" "upload.immae.fr" ];
      };
    };
    networking.firewall.allowedTCPPorts = [ 5222 5269 ];
    myServices.websites.tools.im.enable = true;
    systemd.services.ejabberd.postStop = ''
      rm /var/log/ejabberd/erl_crash*.dump
      '';
    secrets.keys = {
      "ejabberd/psql.yml" = {
        permissions = "0400";
        user = "ejabberd";
        group = "ejabberd";
        text = ''
          sql_type: pgsql
          sql_server: "localhost"
          sql_database: "${config.myEnv.jabber.postgresql.database}"
          sql_username: "${config.myEnv.jabber.postgresql.user}"
          sql_password: "${config.myEnv.jabber.postgresql.password}"
          '';
      };
      "ejabberd/host.yml" = {
        permissions = "0400";
        user = "ejabberd";
        group = "ejabberd";
        text = ''
          host_config:
            "immae.fr":
              domain_certfile: "${config.security.acme.certs.ejabberd.directory}/full.pem"
              auth_method: [ldap]
              ldap_servers: ["${config.myEnv.jabber.ldap.host}"]
              ldap_encrypt: tls
              ldap_rootdn: "${config.myEnv.jabber.ldap.dn}"
              ldap_password: "${config.myEnv.jabber.ldap.password}"
              ldap_base: "${config.myEnv.jabber.ldap.base}"
              ldap_uids:
                uid: "%u"
                immaeXmppUid: "%u"
              ldap_filter: "${config.myEnv.jabber.ldap.filter}"
          '';
      };
    };
    users.users.ejabberd.extraGroups = [ "keys" ];
    services.ejabberd = {
      package = pkgs.ejabberd.override { withPgsql = true; };
      imagemagick = true;
      enable = true;
      ctlConfig = ''
        ERLANG_NODE=ejabberd@localhost
      '';
      configFile = pkgs.runCommand "ejabberd.yml" {
        certificatePrivateKeyAndFullChain = "${config.security.acme.certs.ejabberd.directory}/full.pem";
        certificateCA = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
        sql_config_file = config.secrets.fullPaths."ejabberd/psql.yml";
        host_config_file = config.secrets.fullPaths."ejabberd/host.yml";
      } ''
        substituteAll ${./ejabberd.yml} $out
        '';
    };
    secrets.keys."postfix/scripts/ejabberd-env" = {
      user = "postfixscripts";
      group = "root";
      permissions = "0400";
      text = builtins.toJSON {
        jid = "notify_bot@immae.fr";
        password = "{{ .xmpp.notify_bot }}";
      };
    };
    services.postfix.extraAliases = let
      nixpkgs = builtins.fetchTarball {
        url = "https://github.com/NixOS/nixpkgs/archive/840c782d507d60aaa49aa9e3f6d0b0e780912742.tar.gz";
        sha256 = "14q3kvnmgz19pgwyq52gxx0cs90ddf24pnplmq33pdddbb6c51zn";
      };
      pkgs' = import nixpkgs { inherit (pkgs) system; overlays = []; };
      warn_xmpp_email = scriptEnv: pkgs'.runCommand "warn_xmpp_email" {
        inherit scriptEnv;
        pythonEnv = pkgs'.python3.withPackages (ps: [
          ps.unidecode ps.slixmpp
        ]);
      } ''
        substituteAll ${./warn_xmpp_email.py} $out
        chmod a+x $out
      '';
    in ''
      ejabberd: "|${mypackages-lib.postfixScript pkgs "ejabberd" (warn_xmpp_email config.secrets.fullPaths."postfix/scripts/ejabberd-env")}"
    '';
  };
}