{ 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) ]; }; }; }; }; }