diff --git a/topology/nixos/extractors/systemd.nix b/topology/nixos/extractors/systemd.nix index 13a69ef..b5859ee 100644 --- a/topology/nixos/extractors/systemd.nix +++ b/topology/nixos/extractors/systemd.nix @@ -5,43 +5,59 @@ }: let inherit (lib) + any + attrValues concatLists flip + mapAttrsToList mkDefault mkIf mkMerge optional ; in { - #config = mkIf config.systemd.network.enable { - # topology.interfaces = mkMerge ( - # # Create interfaces based on systemd.network.netdevs - # concatLists ( - # flip mapAttrsToList config.systemd.network.netdevs ( - # _unit: netdev: - # optional (netdev ? netdevConfig.Name) { - # ${netdev.netdevConfig.Name} = { - # physical = mkDefault false; - # }; - # } - # ) - # ) - # # Add interface configuration based on systemd.network.networks - # #++ concatLists ( - # # flip mapAttrsToList config.systemd.network.networks ( - # # _unit: network: - # # optional (network ? matchConfig.Name) { - # # ${network.networkConfig.Name} = { - # # }; - # # } - # # ) - # #) - # ); + config = mkIf config.systemd.network.enable { + topology.self.interfaces = mkMerge ( + # Create interfaces based on systemd.network.netdevs + concatLists ( + flip mapAttrsToList config.systemd.network.netdevs ( + _unit: netdev: + optional (netdev ? netdevConfig.Name) { + ${netdev.netdevConfig.Name} = { + virtual = mkDefault true; + }; + } + ) + ) + # Add interface configuration based on systemd.network.networks + ++ concatLists ( + flip mapAttrsToList config.systemd.network.networks ( + _unit: network: let + # FIXME: TODO renameInterfacesByMac is not a standard option! + nameFromMac = + optional (network ? matchConfig.MACAddress && config.networking.renameInterfacesByMac ? ${network.matchConfig.MACAddress}) + config.networking.renameInterfacesByMac.${network.matchConfig.MACAddress}; - # #self.interfaces = { - # #}; - # #networks.somenet = { - # # connections = []; - # #}; - #}; + nameFromNetdev = + optional ( + (network ? matchConfig.Name) + && flip any (attrValues config.systemd.network.netdevs) (x: + (x ? netdevConfig.Name) + && x.netdevConfig.Name == network.matchConfig.Name) + ) + network.matchConfig.Name; + + interfaceName = builtins.head (nameFromMac ++ nameFromNetdev ++ [null]); + in + optional (interfaceName != null) { + ${interfaceName} = { + mac = network.matchConfig.MACAddress or null; + addresses = network.address ++ (network.networkConfig.Address or []); + gateways = network.gateway ++ (network.networkConfig.Gateway or []); + }; + } + ) + ) + ); + }; } diff --git a/topology/nixos/module.nix b/topology/nixos/module.nix index 8885302..3e71c87 100644 --- a/topology/nixos/module.nix +++ b/topology/nixos/module.nix @@ -49,6 +49,6 @@ in { config.topology = { # Ensure a node exists for this host - nodes.${config.topology.id} = {}; + nodes.${config.topology.id}.type = "nixos"; }; } diff --git a/topology/options/interfaces.nix b/topology/options/interfaces.nix index 192d5e5..f0d1d92 100644 --- a/topology/options/interfaces.nix +++ b/topology/options/interfaces.nix @@ -31,6 +31,7 @@ in virtual = mkOption { description = "Whether this is a virtual interface."; type = types.bool; + default = false; }; mac = mkOption { @@ -39,16 +40,17 @@ in type = types.nullOr types.str; }; - #addresses = mkOption { - # description = "The configured address(es), or a descriptive string (like DHCP)."; - # type = types.listOf types.str; - #}; + addresses = mkOption { + description = "The configured address(es), or a descriptive string (like DHCP)."; + default = []; + type = types.listOf types.str; + }; - #gateway = mkOption { - # description = "The configured gateway, if any"; - # type = types.nullOr types.str; - # default = null; - #}; + gateways = mkOption { + description = "The configured gateways, if any."; + default = []; + type = types.listOf types.str; + }; network = mkOption { description = "The id of the network to which this interface belongs, if any.";