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