zaphyra's git: nixfiles

zaphyra's nixfiles

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 
{
  machineConfig,
  config,
  lib,
  ...
}:
let
  inherit (lib) types;
  cfg = config.common.configure.primaryNetworkInterface;

in
{

  options.common.configure.primaryNetworkInterface = {
    enable = lib.mkEnableOption "configuration of the primary network interface";
    interfaceName = lib.mkOption {
      type = types.str;
      default = machineConfig.networking.primaryInterface;
    };
    dnsServers = lib.mkOption {
      type = with types; listOf str;
      default = [
        "1.1.1.1"
        "8.8.8.8"
        "9.9.9.9"
      ];
    };
    useDHCP = lib.mkOption {
      type = types.bool;
      default = if (cfg.ip4Address != null) then false else true;
    };
    acceptRouterAdvertisements = lib.mkOption {
      type = types.bool;
      default = if (cfg.ip6Address != null) then false else true;
    };
    ip4Address = lib.mkOption {
      type = with types; nullOr str;
      default =
        if (machineConfig.networking ? ip4Address && machineConfig.networking ? ip4PrefixLength) then
          "${machineConfig.networking.ip4Address}/${toString machineConfig.networking.ip4PrefixLength}"
        else
          null;
    };
    ip4DefaultGateway = lib.mkOption {
      type = with types; nullOr str;
      default =
        if machineConfig.networking ? defaultGateway4 then
          machineConfig.networking.defaultGateway4
        else
          null;
    };
    ip6Address = lib.mkOption {
      type = with types; nullOr str;
      default =
        if (machineConfig.networking ? ip6Address && machineConfig.networking ? ip6PrefixLength) then
          "${machineConfig.networking.ip6Address}/${toString machineConfig.networking.ip6PrefixLength}"
        else
          null;
    };
    ip6DefaultGateway = lib.mkOption {
      type = with types; nullOr str;
      default =
        if machineConfig.networking ? defaultGateway6 then
          machineConfig.networking.defaultGateway6
        else
          null;
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.network = {
      networks = {
        "5-primaryInterface" = {
          enable = true;
          name = cfg.interfaceName;
          networkConfig = {
            DHCP = cfg.useDHCP;
            IPv6AcceptRA = cfg.acceptRouterAdvertisements;
          };
          dns = cfg.dnsServers;
          gateway = [
            (lib.mkIf (cfg.ip4DefaultGateway != null) cfg.ip4DefaultGateway)
            (lib.mkIf (cfg.ip6DefaultGateway != null) cfg.ip6DefaultGateway)
          ];
          address = [
            (lib.mkIf (cfg.ip4Address != null) cfg.ip4Address)
            (lib.mkIf (cfg.ip6Address != null) cfg.ip6Address)
          ];
        };
      };
    };
  };

}