about summary refs log tree commit diff
path: root/modules/common/networking.nix
blob: 468bd8b649af3e6084fa6b685c88bc6341be808c (plain) (blame)
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
{
  config,
  lib,
  pkgs,
  this,
  ...
}:
let
  cfg = config.nixfiles.modules.common.networking;
in
{
  options.nixfiles.modules.common.networking.onlyDefault = lib.mkEnableOption "custom networking settings";

  config = lib.mkIf (!cfg.onlyDefault) {
    ark.directories =
      with config.networking;
      lib.optional networkmanager.enable "/etc/NetworkManager/system-connections"
      ++ lib.optional wireless.iwd.enable "/var/lib/iwd";

    networking = lib.mkMerge [
      {
        domain = lib.my.domain.shire;

        hostName = this.hostname;
        hostId = lib.substring 0 8 (builtins.hashString "md5" this.hostname);

        # Remove default hostname mappings. This is required at least by the
        # current implementation of the monitoring module.
        hosts = {
          "127.0.0.2" = lib.mkForce [ ];
          "::1" = lib.mkForce [ ];
        };

        # There's no way[1] to configure DNS server priority in
        # systemd-resolved. The only solution for dealing with a broken VPN
        # connection is to delete /etc/systemd/resolved.conf and restart the
        # systemd-resolved service. Otherwise I'll just end up with a random
        # server from the list most of the time because systemd-resolved
        # "conveniently" will manage server priority for me...
        #
        # [1]: https://askubuntu.com/questions/1116732/how-do-i-list-dns-server-order-in-systemd-resolve
        # [2]: https://github.com/systemd/systemd/issues/6076
        nameservers = with lib.my.configurations.manwe.wireguard; [
          ipv6.address
          ipv4.address
        ];

        useDHCP = false;

        nftables.enable = true;

        firewall = {
          enable = true;

          rejectPackets = false;

          allowPing = true;
          pingLimit = "1/minute burst 5 packets";

          logRefusedConnections = false;
          logRefusedPackets = false;
          logRefusedUnicastsOnly = false;
          logReversePathDrops = false;
        };
      }
      (
        let
          interface = "eth0"; # This assumes `usePredictableInterfaceNames` is false.
        in
        lib.mkIf (lib.hasAttr "ipv4" this && lib.hasAttr "ipv6" this) {
          usePredictableInterfaceNames = false; # NOTE This can break something!
          interfaces.${interface} = {
            ipv4.addresses =
              with this.ipv4;
              lib.optional (lib.isString address && lib.isInt prefixLength) {
                inherit address prefixLength;
              };

            ipv6.addresses =
              with this.ipv6;
              lib.optional (lib.isString address && lib.isInt prefixLength) {
                inherit address prefixLength;
              };
          };
          defaultGateway =
            with this.ipv4;
            lib.mkIf (lib.isString gatewayAddress) {
              inherit interface;
              address = gatewayAddress;
            };
          defaultGateway6 =
            with this.ipv6;
            lib.mkIf (lib.isString gatewayAddress) {
              inherit interface;
              address = gatewayAddress;
            };
        }
      )
      (lib.mkIf this.isHeadful {
        networkmanager = {
          enable = true;
          plugins = lib.mkForce [ ];
          wifi.backend = "iwd";
        };

        wireless = {
          enable = false;
          iwd.enable = lib.mkDefault true;
          userControlled.enable = true;
          allowAuxiliaryImperativeNetworks = true;
        };
      })
    ];

    services.resolved = {
      llmnr = "false";
      dnsovertls = "opportunistic";
      dnssec = "allow-downgrade";
      fallbackDns = lib.dns.mkDoT lib.dns.const.quad9.ecs;
    };

    environment = {
      shellAliases = lib.listToAttrs (
        map ({ name, value }: lib.nameValuePair name "${pkgs.iproute2}/bin/${value}") [
          {
            name = "bridge";
            value = "bridge -color=always";
          }
          {
            name = "ip";
            value = "ip -color=always";
          }
          {
            name = "tc";
            value = "tc -color=always";
          }
        ]
      );

      systemPackages = with pkgs; [
        ethtool
        myip
        nethogs
      ];
    };
  };
}

Consider giving Nix/NixOS a try! <3