summaryrefslogtreecommitdiffstats
path: root/configs/networkd.nix
blob: a5feb9fb94b780d9cfacf80a7cb05f082be1ca5c (plain)
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
{ config, lib, mylib, ... }: {
  services.resolved.settings.Resolve.DNSSEC = "allow-downgrade";

  # LLMNR answers come from anyone on the local network (spoofable), and
  # mDNS covers local names.
  services.resolved.settings.Resolve.LLMNR = "false";

  # Not systemd's built-in fallback, which includes Google's DNS.
  services.resolved.settings.Resolve.FallbackDNS = [
    "1.1.1.1#cloudflare-dns.com"
    "1.0.0.1#cloudflare-dns.com"
    "2606:4700:4700::1111#cloudflare-dns.com"
    "2606:4700:4700::1001#cloudflare-dns.com"
  ];

  # often hangs
  systemd.network.wait-online.enable = lib.mkDefault false;

  networking.useNetworkd = true;
  networking.useDHCP = false;

  # Do not take the network down for too long.
  # From: https://github.com/numtide/srvos/commit/a99a47d
  systemd.services.systemd-networkd.stopIfChanged = false;
  systemd.services.systemd-resolved.stopIfChanged = false;

  # The networks are called 40-NAME like the ones from
  # <nixpkgs/nixos/modules/tasks/network-interfaces-systemd.nix>, so the
  # settings merge into them: systemd uses only the first file that matches
  # an interface, so with 00-NAME it would stay degraded (configured), and
  # 99-NAME would be ignored.
  systemd.network.networks = lib.mkMerge [

    # Don't use DNS servers received from DHCP servers.
    (mylib.genAttrs'
      (builtins.attrNames config.networking.interfaces)
      (name: lib.nameValuePair "40-${name}" {
        dhcpV4Config.UseDNS = lib.mkDefault false;
        dhcpV6Config.UseDNS = lib.mkDefault false;
      }))

    # Prefer ethernet: its DHCP routes get metric 512 instead of 1024.
    (mylib.genAttrs'
      (builtins.filter
        (name:
          config.hrm.network.interfaces.${name}.medium == "ethernet" &&
          config.networking.interfaces ? ${name})
        (builtins.attrNames config.hrm.network.interfaces))
      (name: lib.nameValuePair "40-${name}" {
        inherit name;
        dhcpV4Config.RouteMetric = 512;
      }))
  ];
}