1{ lib, pkgs, config, ... }:
2
3with lib;
4
5let
6 pkg = pkgs.tox-node;
7 cfg = config.services.tox-node;
8 homeDir = "/var/lib/tox-node";
9
10 configFile = let
11 # fetchurl should be switched to getting this file from tox-node.src once
12 # the dpkg directory is in a release
13 src = pkgs.fetchurl {
14 url = "https://raw.githubusercontent.com/tox-rs/tox-node/master/dpkg/config.yml";
15 sha256 = "1431wzpzm786mcvyzk1rp7ar418n45dr75hdggxvlm7pkpam31xa";
16 };
17 confJSON = pkgs.writeText "config.json" (
18 builtins.toJSON {
19 log-type = cfg.logType;
20 keys-file = cfg.keysFile;
21 udp-address = cfg.udpAddress;
22 tcp-addresses = cfg.tcpAddresses;
23 tcp-connections-limit = cfg.tcpConnectionLimit;
24 lan-discovery = cfg.lanDiscovery;
25 threads = cfg.threads;
26 motd = cfg.motd;
27 }
28 );
29 in with pkgs; runCommand "config.yml" {} ''
30 ${remarshal}/bin/remarshal -if yaml -of json ${src} -o src.json
31 ${jq}/bin/jq -s '(.[0] | with_entries( select(.key == "bootstrap-nodes"))) * .[1]' src.json ${confJSON} > $out
32 '';
33
34in {
35 options.services.tox-node = {
36 enable = mkEnableOption "Tox Node service";
37
38 logType = mkOption {
39 type = types.enum [ "Stderr" "Stdout" "Syslog" "None" ];
40 default = "Stderr";
41 description = "Logging implementation.";
42 };
43 keysFile = mkOption {
44 type = types.str;
45 default = "${homeDir}/keys";
46 description = "Path to the file where DHT keys are stored.";
47 };
48 udpAddress = mkOption {
49 type = types.str;
50 default = "0.0.0.0:33445";
51 description = "UDP address to run DHT node.";
52 };
53 tcpAddresses = mkOption {
54 type = types.listOf types.str;
55 default = [ "0.0.0.0:33445" ];
56 description = "TCP addresses to run TCP relay.";
57 };
58 tcpConnectionLimit = mkOption {
59 type = types.int;
60 default = 8192;
61 description = "Maximum number of active TCP connections relay can hold";
62 };
63 lanDiscovery = mkOption {
64 type = types.bool;
65 default = true;
66 description = "Enable local network discovery.";
67 };
68 threads = mkOption {
69 type = types.int;
70 default = 1;
71 description = "Number of threads for execution";
72 };
73 motd = mkOption {
74 type = types.str;
75 default = "Hi from tox-rs! I'm up {{uptime}}. TCP: incoming {{tcp_packets_in}}, outgoing {{tcp_packets_out}}, UDP: incoming {{udp_packets_in}}, outgoing {{udp_packets_out}}";
76 description = "Message of the day";
77 };
78 };
79
80 config = lib.mkIf cfg.enable {
81 systemd.services.tox-node = {
82 description = "Tox Node";
83
84 after = [ "network.target" ];
85 wantedBy = [ "multi-user.target" ];
86
87 serviceConfig = {
88 ExecStart = "${pkg}/bin/tox-node config ${configFile}";
89 StateDirectory = "tox-node";
90 DynamicUser = true;
91 Restart = "always";
92 };
93 };
94 };
95}