-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.nix
150 lines (145 loc) · 4.12 KB
/
default.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{
config,
lib,
pkgs,
hostName,
...
}: let
cfg = config.vyxos;
inherit (lib) types mkDefault mkOption mkMerge mkIf;
inherit (lib) splitString elemAt id toInt;
hostData = (lib.importTOML ../../hosts.toml).${hostName};
splitting = attrs: key: delim: index: parse:
if attrs ? ${key}
then let
parts = splitString delim attrs.${key};
part = elemAt parts index;
in
parse part
else null;
in {
options.vyxos.net = mkOption {
readOnly = true;
type = types.submodule {
options = {
dev = mkOption {
type = types.str;
example = "eno1";
readOnly = true;
};
ipv4a = mkOption {
type = types.nullOr types.str;
example = "64.71.162.164";
readOnly = true;
};
ipv4p = mkOption {
type = types.nullOr (types.ints.between 8 32);
example = 26;
readOnly = true;
};
gwv4a = mkOption {
type = types.nullOr types.str;
example = "172.31.1.1";
readOnly = true;
};
gwv4p = mkOption {
type = types.nullOr (types.ints.between 8 32);
example = 32;
readOnly = true;
};
ipv6a = mkOption {
type = types.nullOr types.str;
example = "2402:1f00:8201:3e3:49::2";
readOnly = true;
};
ipv6p = mkOption {
type = types.nullOr (types.ints.between 16 128);
example = 64;
readOnly = true;
};
gwv6a = mkOption {
type = types.nullOr types.str;
example = "2402:1f00:8201:3ff:ff:ff:ff:ff";
readOnly = true;
};
gwv6i = mkOption {
type = types.nullOr types.str;
example = "eno1";
readOnly = true;
};
};
};
default = {
dev = hostData.net.dev;
ipv4a = splitting hostData.net "ipv4" "/" 0 id;
ipv4p = splitting hostData.net "ipv4" "/" 1 toInt;
gwv4a = splitting hostData.net "gwv4" "/" 0 id;
gwv4p = splitting hostData.net "gwv4" "/" 1 toInt;
ipv6a = splitting hostData.net "ipv6" "/" 0 id;
ipv6p = splitting hostData.net "ipv6" "/" 1 toInt;
gwv6a = splitting hostData.net "gwv6" "#" 0 id;
gwv6i = splitting hostData.net "gwv6" "#" 1 id;
};
};
config = {
boot.kernel.sysctl = {
"net.ipv4.ip_forward" = "1"; # for tailscale exit node
"net.ipv6.conf.all.forwarding" = "1"; # for tailscale exit node
};
systemd.services.vyxos-transport-layer-offloads = {
# See https://tailscale.com/kb/1320/performance-best-practices#ethtool-configuration.
description = "VyxOS: better performance for exit nodes";
after = ["network.target"];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.ethtool}/sbin/ethtool -K ${cfg.net.dev} rx-udp-gro-forwarding on rx-gro-list off";
};
wantedBy = ["default.target"];
};
networking = mkMerge [
{
inherit (cfg) hostId;
nameservers = ["1.1.1.1" "1.0.0.1"];
firewall = {
enable = mkDefault true;
allowedTCPPorts = [];
allowedUDPPorts = [config.services.tailscale.port];
trustedInterfaces = ["tailscale0"];
};
}
(mkIf (cfg.net.ipv4a != null) {
useDHCP = false;
networkmanager.enable = false;
interfaces.${cfg.net.dev}.ipv4 = {
addresses = [
{
address = cfg.net.ipv4a;
prefixLength = cfg.net.ipv4p;
}
];
routes = [
{
address = cfg.net.gwv4a;
prefixLength = cfg.net.gwv4p;
}
];
};
defaultGateway = cfg.net.gwv4a;
})
(mkIf (cfg.net.ipv6a != null) {
useDHCP = false;
networkmanager.enable = false;
interfaces.${cfg.net.dev}.ipv6.addresses = [
{
address = cfg.net.ipv6a;
prefixLength = cfg.net.ipv6p;
}
];
defaultGateway6 = {
address = cfg.net.gwv6a;
interface = cfg.net.gwv6i;
};
})
];
};
}