feat: add interface naming module
This commit is contained in:
parent
42374eff1f
commit
073a8ae3b3
3 changed files with 56 additions and 2 deletions
|
@ -1 +1,5 @@
|
|||
{}
|
||||
{
|
||||
imports = [
|
||||
./interface-naming.nix
|
||||
];
|
||||
}
|
||||
|
|
50
modules/interface-naming.nix
Normal file
50
modules/interface-naming.nix
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Provides an option to easily rename interfaces by their mac addresses.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
inherit
|
||||
(lib)
|
||||
attrValues
|
||||
concatStringsSep
|
||||
duplicates
|
||||
flip
|
||||
mapAttrsToList
|
||||
mkOption
|
||||
types
|
||||
;
|
||||
|
||||
cfg = config.networking.renameInterfacesByMac;
|
||||
|
||||
interfaceNamesUdevRules = pkgs.writeTextFile {
|
||||
name = "interface-names-udev-rules";
|
||||
text = concatStringsSep "\n" (
|
||||
flip mapAttrsToList cfg
|
||||
(name: mac: ''SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${mac}", NAME:="${name}"'')
|
||||
);
|
||||
destination = "/etc/udev/rules.d/01-interface-names.rules";
|
||||
};
|
||||
in {
|
||||
options.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 = lib.mkIf (cfg != {}) {
|
||||
assertions = let
|
||||
duplicateMacs = duplicates (attrValues cfg);
|
||||
in [
|
||||
{
|
||||
assertion = duplicateMacs == [];
|
||||
message = "Duplicate mac addresses found in network interface name assignment: ${concatStringsSep ", " duplicateMacs}";
|
||||
}
|
||||
];
|
||||
|
||||
services.udev.packages = [interfaceNamesUdevRules];
|
||||
boot.initrd.services.udev.packages = [interfaceNamesUdevRules];
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue