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 ''
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}/mx-puppet-discord/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 = ''
61 <filename>config.yaml</filename> configuration as a Nix attribute set.
62 Configuration options should match those described in
63 <link xlink:href="https://github.com/matrix-discord/mx-puppet-discord/blob/master/sample.config.yaml">
64 sample.config.yaml</link>.
65 '';
66 };
67 serviceDependencies = mkOption {
68 type = with types; listOf str;
69 default = optional config.services.matrix-synapse.enable "matrix-synapse.service";
70 description = ''
71 List of Systemd services to require and wait for when starting the application service.
72 '';
73 };
74 };
75 };
76
77 config = mkIf cfg.enable {
78 systemd.services.mx-puppet-discord = {
79 description = ''
80 mx-puppet-discord is a discord puppeting bridge for matrix.
81 It handles bridging private and group DMs, as well as Guilds (servers).
82 '';
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 -c ${settingsFile}
114 '';
115 };
116 };
117 };
118
119 meta.maintainers = with maintainers; [ govanify ];
120}