1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 dataDir = "/var/lib/tedicross";
7 cfg = config.services.tedicross;
8 configJSON = pkgs.writeText "tedicross-settings.json" (builtins.toJSON cfg.config);
9 configYAML = pkgs.runCommand "tedicross-settings.yaml" { preferLocalBuild = true; } ''
10 ${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out
11 '';
12
13in {
14 options = {
15 services.tedicross = {
16 enable = mkEnableOption (lib.mdDoc "the TediCross Telegram-Discord bridge service");
17
18 config = mkOption {
19 type = types.attrs;
20 # from https://github.com/TediCross/TediCross/blob/master/example.settings.yaml
21 example = literalExpression ''
22 {
23 telegram = {
24 useFirstNameInsteadOfUsername = false;
25 colonAfterSenderName = false;
26 skipOldMessages = true;
27 sendEmojiWithStickers = true;
28 };
29 discord = {
30 useNickname = false;
31 skipOldMessages = true;
32 displayTelegramReplies = "embed";
33 replyLength = 100;
34 };
35 bridges = [
36 {
37 name = "Default bridge";
38 direction = "both";
39 telegram = {
40 chatId = -123456789;
41 relayJoinMessages = true;
42 relayLeaveMessages = true;
43 sendUsernames = true;
44 ignoreCommands = true;
45 };
46 discord = {
47 serverId = "DISCORD_SERVER_ID";
48 channelId = "DISCORD_CHANNEL_ID";
49 relayJoinMessages = true;
50 relayLeaveMessages = true;
51 sendUsernames = true;
52 crossDeleteOnTelegram = true;
53 };
54 }
55 ];
56
57 debug = false;
58 }
59 '';
60 description = lib.mdDoc ''
61 {file}`settings.yaml` configuration as a Nix attribute set.
62 Secret tokens should be specified using {option}`environmentFile`
63 instead of this world-readable file.
64 '';
65 };
66
67 environmentFile = mkOption {
68 type = types.nullOr types.path;
69 default = null;
70 description = lib.mdDoc ''
71 File containing environment variables to be passed to the TediCross service,
72 in which secret tokens can be specified securely using the
73 `TELEGRAM_BOT_TOKEN` and `DISCORD_BOT_TOKEN`
74 keys.
75 '';
76 };
77 };
78 };
79
80 config = mkIf cfg.enable {
81 # from https://github.com/TediCross/TediCross/blob/master/guides/autostart/Linux.md
82 systemd.services.tedicross = {
83 description = "TediCross Telegram-Discord bridge service";
84 wantedBy = [ "multi-user.target" ];
85 wants = [ "network-online.target" ];
86 after = [ "network-online.target" ];
87 serviceConfig = {
88 Type = "simple";
89 ExecStart = "${pkgs.nodePackages.tedicross}/bin/tedicross --config='${configYAML}' --data-dir='${dataDir}'";
90 Restart = "always";
91 DynamicUser = true;
92 StateDirectory = baseNameOf dataDir;
93 EnvironmentFile = cfg.environmentFile;
94 };
95 };
96 };
97
98 meta.maintainers = with maintainers; [ pacien ];
99}
100