summaryrefslogtreecommitdiff
path: root/os/core/networking.nix
blob: b94b54073bc96cfc0409e49d065e253745d182dc (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
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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.os.core.network;
in
{
  options.os.core.network = {
    enable = lib.mkEnableOption "system-wide networking setup";
    enableFirewall = lib.mkEnableOption "integrated zero-trust nftables firewall layers";

    isVM = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Set to true if this configuration is running inside a guest VM. Set to false for the bare-metal host.";
    };

    ips = lib.mkOption {
      type = lib.types.attrsOf lib.types.str;
      default = rec {
        router = opnsense-vm;
        host = "10.0.0.2";
        opnsense-vm = "10.0.0.1";
        gateway-vm = "10.0.0.3";
        databse-vm = "10.0.0.4";
        monitor-vm = "10.0.0.5";
        media-vm = "10.0.0.6";
        sandbox-vm = "10.0.0.7";
        storage-vm = "10.0.0.8";
        web-vm = "10.0.0.9";
        mail-vm = "10.0.0.10";
        relay-vm = "10.0.0.11";
        gameserver-vm = "10.0.0.12";
      };
      description = "Central registry of static IP allocations for the cluster.";
    };

    profile = lib.mkOption {
      type = lib.types.enum [
        "client"
        "server"
      ];
      default = "client";
      description = "Which networking profile configuration to apply";
    };

    lan = lib.mkOption {
      description = "Physical Home Local Area Network configuration parameters.";
      type = lib.types.submodule {
        options = {
          ip = lib.mkOption {
            type = lib.types.str;
            default = "10.0.0.2";
            description = "The local static or leased IP assigned to this machine on the home network.";
          };
          range = lib.mkOption {
            type = lib.types.str;
            default = "10.0.0.0/24";
            description = "The broader subnet block representing the physical home network.";
          };
        };
      };
    };

    wg = lib.mkOption {
      description = "Standard WireGuard VPN tunnel configuration parameters.";
      type = lib.types.submodule {
        options = {
          ip = lib.mkOption {
            type = lib.types.str;
            default = "10.3.0.1";
            description = "The explicit tunnel IP address assigned to this machine's WireGuard interface.";
          };
          range = lib.mkOption {
            type = lib.types.str;
            default = "10.3.0.0/24";
            description = "The total addressable IP space assigned to the WireGuard network pool.";
          };
        };
      };
    };

    hs = lib.mkOption {
      description = "Headscale mesh overlay network configuration parameters.";
      type = lib.types.submodule {
        options = {
          ip = lib.mkOption {
            type = lib.types.str;
            default = "10.4.0.1";
            description = "The explicit mesh network IP address assigned to this machine via Headscale.";
          };
          range = lib.mkOption {
            type = lib.types.str;
            default = "10.4.0.0/24";
            description = "The full mesh overlay allocation subnet block.";
          };
        };
      };
    };
  };

  config = lib.mkIf cfg.enable (
    lib.mkMerge [
      {
        services.resolved.enable = true;
      }

      (lib.mkIf (cfg.profile == "client") {
        networking.networkmanager = {
          enable = true;
          dns = "systemd-resolved";
          wifi.macAddress = "random";
          wifi.backend = "iwd";
          ethernet.macAddress = "random";
        };

        systemd.services."NetworkManager-wait-online".enable = false;

        environment.systemPackages = [
          pkgs.impala
        ];
      })

      (lib.mkIf (cfg.profile == "client" && cfg.enableFirewall) {
        networking = {
          firewall.enable = true;
          nftables.enable = true;
        };
      })

      (lib.mkIf (cfg.profile == "server") {
        networking = {
          useNetworkd = true;
          useDHCP = false;
        };
        systemd.network = {
          enable = true;
          wait-online.enable = lib.mkIf (!cfg.isVM) false;

          netdevs = lib.mkIf (!cfg.isVM) {
            "10-br-srv" = {
              netdevConfig = {
                Name = "br-srv";
                Kind = "bridge";
              };
            };
          };
          networks."20-host-management" = {
            matchConfig.Name = if cfg.isVM then "eth0" else "br-srv";
            address = [ "${cfg.lan.ip}/24" ];
            gateway = [ cfg.ips.router ];
            networkConfig.LinkLocalAddressing = "no";
          };
        };
        boot.kernel.sysctl = {
          "net.ipv4.ip_nonlocal_bind" = 1;
          "net.ipv4.ip_forward" = 1;
        };
      })
      (lib.mkIf
        (
          cfg.profile == "server"
          && cfg.enableFirewall
          && cfg.isVM
          && config.networking.hostName != "vm2-gateway"
        )
        {
          networking.nftables.enable = true;
          networking.firewall = {
            enable = true;
            extraCommands = ''
              nft add table ip nat 2>/dev/null || true
              nft flush table ip nat
              nft add chain ip nat PREROUTING { type nat hook prerouting priority dstnat \; }
              nft add rule ip nat PREROUTING ip saddr ${cfg.ips.vm2-gateway} ip daddr ${cfg.lan.ip} redirect
            '';
          };
        }
      )
    ]
  );
}