1
1
Fork 1
mirror of https://github.com/oddlama/nix-config.git synced 2025-10-10 23:00:39 +02:00

feat(topology): add systemd network extractor

This commit is contained in:
oddlama 2024-03-16 03:32:21 +01:00
parent 65890181e9
commit 887115c96d
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
3 changed files with 58 additions and 40 deletions

View file

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

View file

@ -49,6 +49,6 @@ in {
config.topology = {
# Ensure a node exists for this host
nodes.${config.topology.id} = {};
nodes.${config.topology.id}.type = "nixos";
};
}

View file

@ -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.";