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