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