all repos — nixfiles @ 042e38e627e15c6a392de76f2b463fff5660e0b3

System and user configuration, managed by nix and home-manager

system/roles/router.nix (view raw)

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
{
  config,
  pkgs,
  lib,
  ...
}:
let
  wan = config.networking.nat.externalInterface;
  lans = config.networking.nat.internalInterfaces;
  lan = builtins.head lans;
  tailnet = "fin-marlin.ts.net";

  inherit (config.networking) domain hostName;

  wanPrefixLen = 56;

  modem = {
    hostname = "modem";

    deviceAddr = "192.168.100.1";
    ourAddr = "192.168.100.10";
  };

  nameservers = [
    "2620:fe::fe"
    "2620:fe::9"
    "9.9.9.9"
    "149.112.112.112"
  ];
  nsTLSHostname = "dns.quad9.net";
in
{
  imports = [
    ../settings/services/adguard-home.nix
  ];

  networking = {
    useDHCP = false;
    hosts = {
      "${modem.deviceAddr}" = [
        "${modem.hostname}.${domain} ${modem.hostname}"
      ];
    };
    nameservers = lib.mkForce [ "::1" ];

    nat = {
      # TODO upstream changes somehow
      enable = false; # NixOS module is a bit limiting
    };
    firewall = {
      enable = true;
      trustedInterfaces = lans;
      filterForward = true;
      extraForwardRules = ''
        iifname "${lan}"  oifname "${wan}" accept comment "from internal interfaces"
      '';
      interfaces = {
        "${lan}" = {
          allowedTCPPorts = [ 22 ];
        };
      };
    };
    nftables = {
      enable = true;
      tables = {
        nat = {
          family = "ip";
          content = ''
            chain pre {
              type nat hook prerouting priority dstnat; policy accept;
            }

            chain post {
              type nat hook postrouting priority srcnat; policy accept;
              iifname "${lan}" oifname "${wan}" ip daddr ${modem.deviceAddr}/24 snat to ${modem.ourAddr} comment "half-bridge modem access";
              iifname "${lan}" oifname "${wan}" masquerade comment "from internal interfaces"
            }

            chain out {
              type nat hook output priority mangle; policy accept;
            }
          '';
        };
      };
    };
  };

  systemd.network = {
    enable = true;
    config = {
      networkConfig = {
        IPv6Forwarding = true;
        IPv6PrivacyExtensions = false;
        SpeedMeter = true;
      };
    };
    networks = {
      "40-${lan}" = rec {
        matchConfig.Name = lan;
        address = [
          "fd12:d04f:65d:42::1/56"
          "10.0.0.1/16"
        ];
        addresses = [
          {
            Address = "fe80::1/64";
            Scope = "link";
          }
        ];
        networkConfig = {
          IPv6AcceptRA = false;
          DHCPPrefixDelegation = true;
          ConfigureWithoutCarrier = true;
          MulticastDNS = true;
          Domains = [ config.networking.domain ];
          LinkLocalAddressing = "ipv6"; # required for networkd RA despite fe80::1 static assignment
          DNS = map (a: builtins.head (lib.strings.splitString "/" a)) address;
          DNSDefaultRoute = false;
          IPv6SendRA = true;
        };
        dhcpPrefixDelegationConfig = {
          UplinkInterface = wan;
          SubnetId = "42";
          Assign = true;
          Announce = true;
          Token = "::1";
        };
        ipv6SendRAConfig = {
          Managed = true;
          OtherInformation = true;
          DNS = [ "_link_local" ];
        };
        ipv6Prefixes = [
          {
            Prefix = "fd12:d04f:65d:42::/56";
            AddressAutoconfiguration = false;
            Assign = false;
          }
        ];
      };
      "40-${wan}" = {
        matchConfig.Name = wan;
        networkConfig = {
          DHCP = true; # this enables DHCPv6
          IPv6AcceptRA = true;
          IPv4Forwarding = true;
          MulticastDNS = false;
          DNSDefaultRoute = true;
          DNSOverTLS = true;
          DNS = "::1";
        };
        dhcpV4Config = {
          UseDNS = false;
          SendHostname = false;
          UseHostname = false;
          Label = "${wan}:1";
        };
        dhcpV6Config = {
          UseDNS = false;
          UseAddress = true; # necessary for dynamic DNS
          SendHostname = false;
          PrefixDelegationHint = "::/${toString wanPrefixLen}";
        };
        dhcpPrefixDelegationConfig = {
          UplinkInterface = ":self";
        };
        ipv6AcceptRAConfig = {
          UseDNS = false;
        };
        addresses = [
          {
            Address = "${modem.ourAddr}/24";
            Label = "${wan}:0";
            Scope = "link";
          }
        ];
        routes = [
          {
            Gateway = "_dhcp4";
            InitialCongestionWindow = 30;
            InitialAdvertisedReceiveWindow = 30;
          }
          {
            Gateway = "_ipv6ra";
            InitialCongestionWindow = 30;
            InitialAdvertisedReceiveWindow = 30;
          }
        ];
        cakeConfig = {
          Bandwidth = "20M";
          OverheadBytes = 18;
          MPUBytes = 64;
          CompensationMode = "none";
          NAT = true;
          RTTSec = "100ms";
          AckFilter = true;
          PriorityQueueingPreset = "diffserv8";
        };
      };
    };
  };

  services.adguardhome.settings = {
    dns = {
      upstream_dns = [ "tls://${nsTLSHostname}" ];
      bootstrap_dns = nameservers;
      bootstrap_prefer_ipv6 = true;
    };
  };

  services.resolved = {
    enable = true;
    settings.Resolve = {
      DNS = [ "" ];
      FallbackDNS = map (ns: "${ns}#${nsTLSHostname}") nameservers;
      LLMNR = false;
      MulticastDNS = true;
      DNSOverTLS = "opportunistic";
    };
  };
  networking.resolvconf.enable = false;

  services.tailscale.extraSetFlags = [ "--accept-dns=false" ];
  environment.etc."resolv.conf".text = lib.mkForce ''
    nameserver ::1
    search ${domain} ${tailnet}
    options edns0 trust-ad
  '';

  boot.kernelModules = [
    "tcp_lp"
  ];
  boot.kernel.sysctl = {
    "net.ipv4.tcp_slow_start_after_idle" = 0;
    "net.ipv4.tcp_ecn" = 1;
    "net.ipv4.tcp_fastopen" = "0x3";
    "net.core.default_qdisc" = "fq";

    "net.ipv6.conf.all.accept_ra" = 0;
    "net.ipv6.conf.all.autoconf" = 0;
    "net.ipv6.conf.default.autoconf" = 0;
    "net.ipv6.conf.${wan}.autoconf" = 0;
  };

  networking.tempAddresses = "disabled"; # use_tempaddr = 0

  services.ddclient = {
    usev4 = "disabled";
    usev6 = "ifv6, ifv6=${wan}";
  };

  environment.systemPackages = [
    config.services.dnsmasq.package
  ];
  services.dnsmasq = {
    enable = true;
    alwaysKeepRunning = true;
    resolveLocalQueries = false;
    settings = {
      inherit domain;
      interface = [
        lan
      ];
      except-interface = [ "lo" ];
      bind-interfaces = true;
      enable-ra = false;

      dhcp-fqdn = true;
      dhcp-authoritative = true;
      dhcp-rapid-commit = true;
      dhcp-range = [
        "10.0.1.10,10.0.250.250,48h"
        "fd12:d04f:65d:42::10,fd12:d04f:65d:42::ffff,64,48h"
      ];

      no-resolv = true;
      server = [ "::1" ];
      dns-loop-detect = true;

      # these next few settings imply trust of the upstream resolver and the path to it
      query-port = 0; # use single OS-allocated port: faster and more efficient
      proxy-dnssec = true; # resolved handles dnssec and is caching
      cache-size = 0; # let resolved do it

      # resolved may know about single-label names
      # particularly those it synthesises, such as "_gateway" (see systemd-resolved(8))
      domain-needed = false;
      stop-dns-rebind = false; # necessary for above

      # do not forward reverse lookups for private addresses, respond nxdomain
      # (resolved will not know more than dnsmasq in such cases)
      bogus-priv = true;

      quiet-ra = true;

      # resolved will look there anyway
      no-hosts = true;

      localise-queries = true;
      local = [
        "/${domain}/"
      ];
      local-ttl = 1; # defaults to 0 (client should not cache), but 1s shouldn't hurt
      interface-name = [
        "${hostName},${lan}" # prevents returning 127.0.0.2
        "${hostName}.${domain},${lan}" # prevents returning 127.0.0.2
        "wan.${domain},${wan}/6"
      ];
    };
  };
}