-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.nix
110 lines (94 loc) · 3.79 KB
/
config.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{ config, pkgs, lib, ...}:
{
options.zerovpn = {
client = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
};
name = lib.mkOption {
type = lib.types.str;
default = config.networking.hostName;
};
};
server = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
};
staticClients = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
};
};
serverHost = lib.mkOption {
type = lib.types.str;
};
serverName = lib.mkOption {
type = lib.types.str;
};
key = lib.mkOption {
type = lib.types.path;
default = "/etc/nixos/zerovpn-master-key";
};
wireguardDevice = lib.mkOption {
type = lib.types.str;
default = "wg0";
};
wireguardPort = lib.mkOption {
type = lib.types.int;
default = 4242;
};
announcePort = lib.mkOption {
type = lib.types.int;
default = 4243;
};
announceInterval = lib.mkOption {
type = lib.types.int;
default = 10;
};
dnsPort = lib.mkOption {
type = lib.types.int;
default = 53;
};
};
config = lib.mkMerge [
(lib.mkIf (config.zerovpn.client.enable || config.zerovpn.server.enable) {
networking.wireguard.enable = true;
users.users.zerovpn = {
isSystemUser = true;
group = "zerovpn";
};
users.groups.zerovpn = { };
environment.systemPackages = [ pkgs.zerovpn ];
})
(lib.mkIf config.zerovpn.client.enable {
services.resolved.enable = true;
networking.networkmanager.dns = "systemd-resolved";
networking.networkmanager.enable = true;
systemd.services.zerovpnClient = {
enable = true;
wantedBy = [ "multi-user.target" ];
serviceConfig = { Restart = "always"; RestartSec = "1"; };
unitConfig = { StartLimitIntervalSec = 0; };
path = [ pkgs.wireguard-tools pkgs.netcat-openbsd pkgs.iproute2 pkgs.zerovpn pkgs.openresolv pkgs.hostname ];
script = "${pkgs.zerovpn}/bin/0vpn-client --wireguard-device ${config.zerovpn.wireguardDevice} --keyfile ${config.zerovpn.key} --server-name ${config.zerovpn.serverName} --server-hostname ${config.zerovpn.serverHost} --wireguard-port ${builtins.toString config.zerovpn.wireguardPort} --announce-port ${builtins.toString config.zerovpn.announcePort} --announce-interval ${builtins.toString config.zerovpn.announceInterval} --client-name ${config.zerovpn.client.name} --dns-port ${builtins.toString config.zerovpn.dnsPort}";
};
})
(lib.mkIf config.zerovpn.server.enable {
boot.kernel.sysctl = {
"net.ipv4.ip_forward" = 1;
"net.ipv6.conf.all.forwarding" = 1;
};
networking.firewall.allowedUDPPorts = [ config.zerovpn.wireguardPort config.zerovpn.announcePort config.zerovpn.dnsPort ];
systemd.services.zerovpnServer = {
enable = true;
wantedBy = [ "multi-user.target" ];
serviceConfig = { RuntimeMaxSec = 3600; Restart = "always"; RestartSec = "1"; };
unitConfig = { StartLimitIntervalSec = 0; };
path = [ pkgs.wireguard-tools pkgs.netcat-openbsd pkgs.iproute2 pkgs.zerovpn pkgs.openresolv pkgs.dnsmasq pkgs.hostname ];
script = "${pkgs.zerovpn}/bin/0vpn-server --wireguard-device ${config.zerovpn.wireguardDevice} --keyfile ${config.zerovpn.key} --server-name ${config.zerovpn.serverName} --server-hostname ${config.zerovpn.serverHost} --wireguard-port ${builtins.toString config.zerovpn.wireguardPort} --announce-port ${builtins.toString config.zerovpn.announcePort} --static-clients " + "\"" + (lib.concatStringsSep " " config.zerovpn.server.staticClients) + "\"" + " --dns-port ${builtins.toString config.zerovpn.dnsPort}";
};
})
];
}