summaryrefslogtreecommitdiff
path: root/config/networking/default.nix
blob: 452a90ad4df9e3de996bec61144dcada36d34341 (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
# https://skogsbrus.xyz/blog/2022/06/12/router/
# https://blog.fraggod.net/2017/04/27/wifi-hostapd-configuration-for-80211ac-networks.html
{ config, lib, pkgs, secrets, ... }:

let cfg = config.personal.networking;
in {
  imports = [ ./nat.nix ./services ];

  options.personal.networking = {
    interfaces = lib.mkOption {
      type = with lib.types;
        attrsOf (submodule {
          interface = lib.mkOption {
            type = lib.types.str;
            description = "Name of the network interface.";
            example = "enp4s0";
          };
          subnet = lib.mkOption {
            type = lib.types.str;
            description = "IPv4 subnet of the network.";
            example = "192.168.1";
          };
          machines = lib.mkOption {
            type = with lib.types;
              attrsOf (submodule {
                address = lib.mkOption {
                  type = lib.types.str;
                  description = "IP address of this machine.";
                  example = "192.168.1.1";
                };
              });
            description = "Some machines connected to this network.";
          };
        });
      description = "Networks this device belongs to.";
    };
  };

  config = {
    personal.networking = {
      enable = true;
      ssh.enable = true;
      networks = {
        lan = {
          interface = "enp4s0";
          subnet = "192.168.1";
          machines = {
            livebox = { address = "192.168.1.1"; };
            self = { address = "192.168.1.2"; };
          };
        };
        wan = {
          interface = "wlp1s0";
          subnet = "192.168.2";
          machines = { self.address = "192.168.2.1"; };
        };
        iot = {
          interface = "wlp5s0";
          subnet = "192.168.3";
          machines = { self.address = "192.168.3.1"; };
        };
      };
    };

    networking = {
      hostName = "kerberos";
      domain = "local";
      nameserver = [ cfg.networks.lan.machines.livebox.address ];

      defaultGateway = with cfg.networks.lan; {
        inherit interface;
        inherit (machines.livebox) address;
      };

      dhcpcd.enable = false;
      interfaces = lib.concatMapAttrs (name: value: {
        "${value.interface}" = {
          useDHCP = false;
          ipv4.address = lib.optional (value.machines ? self) {
            inherit (value.machines) address;
            prefixLength = 24;
          };
        };
      }) cfg.networks;
    };
  };
}