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

feat: modularize link renaming

This commit is contained in:
oddlama 2023-05-11 01:28:31 +02:00
parent e8f50ab906
commit 14ef8ef877
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
10 changed files with 98 additions and 70 deletions

View file

@ -0,0 +1,47 @@
{
config,
extraLib,
lib,
pkgs,
...
}: let
inherit
(lib)
attrValues
concatStringsSep
mapAttrsToList
mkIf
mkOption
types
;
cfg = config.extra.networking.renameInterfacesByMac;
interfaceNamesUdevRules = pkgs.writeTextFile {
name = "interface-names-udev-rules";
text = concatStringsSep "\n" (mapAttrsToList
(name: mac: ''SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${mac}", NAME:="${name}"'')
cfg);
destination = "/etc/udev/rules.d/01-interface-names.rules";
};
in {
options.extra.networking.renameInterfacesByMac = mkOption {
default = {};
example = {lan = "11:22:33:44:55:66";};
description = "Allows naming of network interfaces based on their physical address";
type = types.attrsOf types.str;
};
config = {
assertions = let
duplicateMacs = extraLib.duplicates (attrValues cfg);
in [
{
assertion = duplicateMacs == [];
message = "Duplicate mac addresses found in network interface name assignment: ${concatStringsSep ", " duplicateMacs}";
}
];
services.udev.packages = lib.mkIf (cfg != {}) [interfaceNamesUdevRules];
};
}

View file

@ -72,6 +72,21 @@
];
};
extra.networking.renameInterfacesByMac.${vmCfg.linkName} = vmCfg.mac;
systemd.network.networks = {
"10-${vmCfg.linkName}" = {
matchConfig.Name = vmCfg.linkName;
DHCP = "yes";
networkConfig = {
IPv6PrivacyExtensions = "kernel";
ConfigureWithoutCarrier = true;
};
dhcpV4Config.RouteMetric = 20;
dhcpV6Config.RouteMetric = 20;
};
};
# TODO change once microvms are compatible with stage-1 systemd
boot.initrd.systemd.enable = mkForce false;
};
@ -90,6 +105,12 @@ in {
description = mdDoc "Whether this VM should be started automatically with the host";
};
linkName = mkOption {
type = types.str;
default = "wan";
description = mdDoc "The main ethernet link name inside of the VM";
};
mac = mkOption {
type = config.lib.net.types.mac;
description = mdDoc "The MAC address to assign to this VM";