1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 dataDir = "/var/lib/mx-puppet-discord";
7 registrationFile = "${dataDir}/discord-registration.yaml";
8 cfg = config.services.mx-puppet-discord;
9 settingsFormat = pkgs.formats.json {};
10 settingsFile = settingsFormat.generate "mx-puppet-discord-config.json" cfg.settings;
11
12in {
13 options = {
14 services.mx-puppet-discord = {
15 enable = mkEnableOption (lib.mdDoc ''
16 mx-puppet-discord is a discord puppeting bridge for matrix.
17 It handles bridging private and group DMs, as well as Guilds (servers)
18 '');
19
20 settings = mkOption rec {
21 apply = recursiveUpdate default;
22 inherit (settingsFormat) type;
23 default = {
24 bridge.port = 8434;
25 presence = {
26 enabled = true;
27 interval = 500;
28 };
29 provisioning.whitelist = [ ];
30 relay.whitelist = [ ];
31
32 # variables are preceded by a colon.
33 namePatterns = {
34 user = ":name";
35 userOverride = ":displayname";
36 room = ":name";
37 group = ":name";
38 };
39
40 #defaults to sqlite but can be configured to use postgresql with
41 #connstring
42 database.filename = "${dataDir}/database.db";
43 logging = {
44 console = "info";
45 lineDateFormat = "MMM-D HH:mm:ss.SSS";
46 };
47 };
48 example = literalExpression ''
49 {
50 bridge = {
51 bindAddress = "localhost";
52 domain = "example.com";
53 homeserverUrl = "https://example.com";
54 };
55
56 provisioning.whitelist = [ "@admin:example.com" ];
57 relay.whitelist = [ "@.*:example.com" ];
58 }
59 '';
60 description = lib.mdDoc ''
61 {file}`config.yaml` configuration as a Nix attribute set.
62 Configuration options should match those described in
63 [
64 sample.config.yaml](https://github.com/matrix-discord/mx-puppet-discord/blob/master/sample.config.yaml).
65 '';
66 };
67 serviceDependencies = mkOption {
68 type = with types; listOf str;
69 default = optional config.services.matrix-synapse.enable "matrix-synapse.service";
70 defaultText = literalExpression ''
71 optional config.services.matrix-synapse.enable "matrix-synapse.service"
72 '';
73 description = lib.mdDoc ''
74 List of Systemd services to require and wait for when starting the application service.
75 '';
76 };
77 };
78 };
79
80 config = mkIf cfg.enable {
81 systemd.services.mx-puppet-discord = {
82 description = "Matrix to Discord puppeting bridge";
83
84 wantedBy = [ "multi-user.target" ];
85 wants = [ "network-online.target" ] ++ cfg.serviceDependencies;
86 after = [ "network-online.target" ] ++ cfg.serviceDependencies;
87
88 preStart = ''
89 # generate the appservice's registration file if absent
90 if [ ! -f '${registrationFile}' ]; then
91 ${pkgs.mx-puppet-discord}/bin/mx-puppet-discord -r -c ${settingsFile} \
92 -f ${registrationFile}
93 fi
94 '';
95
96 serviceConfig = {
97 Type = "simple";
98 Restart = "always";
99
100 ProtectSystem = "strict";
101 ProtectHome = true;
102 ProtectKernelTunables = true;
103 ProtectKernelModules = true;
104 ProtectControlGroups = true;
105
106 DynamicUser = true;
107 PrivateTmp = true;
108 WorkingDirectory = pkgs.mx-puppet-discord;
109 StateDirectory = baseNameOf dataDir;
110 UMask = "0027";
111
112 ExecStart = ''
113 ${pkgs.mx-puppet-discord}/bin/mx-puppet-discord \
114 -c ${settingsFile} \
115 -f ${registrationFile}
116 '';
117 };
118 };
119 };
120
121 meta.maintainers = with maintainers; [ govanify ];
122}