summaryrefslogtreecommitdiff
path: root/modules/root/net-iface-labels.nix
blob: 4949659419b9bc6bb8631b821252f01c4ad2705d (plain)
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
{ config, lib, ... }: let
	validMac = str: builtins.match ''^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$'' str != null;
	macAddressType = lib.types.str // {
		check = validMac;
		description = "a mac address (xx:xx:xx:xx:xx:xx)";
	};
in {
	options.networking.interfaceLabels = {
		enable = lib.mkEnableOption "manually label network interfaces";
		interfaces = lib.mkOption {
			type = lib.types.attrsOf macAddressType;
			default = {};
			example = {
				"lan0" = "00:11:22:33:44:55";
				"lan1" = "66:77:88:99:AA:BB";
			};
			description = "label network interfaces by their mac address";
		};
	};

	config = lib.mkIf config.networking.interfaceLabels.enable {
		networking.usePredictableInterfaceNames = false;
		services.udev.extraRules = lib.concatStringsSep "\n" (
			lib.mapAttrsToList (name: mac: ''ACTION=="add", SUBSYSTEM=="net", ATTR{address}=="${mac}", NAME="${name}"'') config.networking.interfaceLabels.interfaces
		);
	};
}