1{
2 config,
3 pkgs,
4 lib,
5 ...
6}:
7
8with lib;
9
10{
11 options = {
12 services.toxvpn = {
13 enable = mkEnableOption "toxvpn running on startup";
14
15 localip = mkOption {
16 type = types.str;
17 default = "10.123.123.1";
18 description = "your ip on the vpn";
19 };
20
21 port = mkOption {
22 type = types.port;
23 default = 33445;
24 description = "udp port for toxcore, port-forward to help with connectivity if you run many nodes behind one NAT";
25 };
26
27 auto_add_peers = mkOption {
28 type = types.listOf types.str;
29 default = [ ];
30 example = [
31 "toxid1"
32 "toxid2"
33 ];
34 description = "peers to automatically connect to on startup";
35 };
36 };
37 };
38
39 config = mkIf config.services.toxvpn.enable {
40 systemd.services.toxvpn = {
41 description = "toxvpn daemon";
42
43 wantedBy = [ "multi-user.target" ];
44 after = [ "network.target" ];
45
46 preStart = ''
47 mkdir -p /run/toxvpn || true
48 chown toxvpn /run/toxvpn
49 '';
50
51 path = [ pkgs.toxvpn ];
52
53 script = ''
54 exec toxvpn -i ${config.services.toxvpn.localip} -l /run/toxvpn/control -u toxvpn -p ${toString config.services.toxvpn.port} ${
55 lib.concatMapStringsSep " " (x: "-a ${x}") config.services.toxvpn.auto_add_peers
56 }
57 '';
58
59 serviceConfig = {
60 KillMode = "process";
61 Restart = "on-success";
62 Type = "notify";
63 };
64
65 restartIfChanged = false; # Likely to be used for remote admin
66 };
67
68 environment.systemPackages = [ pkgs.toxvpn ];
69
70 users.users = {
71 toxvpn = {
72 isSystemUser = true;
73 group = "toxvpn";
74 home = "/var/lib/toxvpn";
75 createHome = true;
76 };
77 };
78 users.groups.toxvpn = { };
79 };
80}