feat: add interface naming module

This commit is contained in:
oddlama 2023-12-22 01:47:12 +01:00
parent 42374eff1f
commit 073a8ae3b3
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
3 changed files with 56 additions and 2 deletions

View file

@ -25,7 +25,7 @@
...
}:
{
nixosModules.extra-modules = import ./modules nixpkgs;
nixosModules.extra-modules = import ./modules;
nixosModules.default = self.nixosModules.extra-modules;
}
// flake-utils.lib.eachDefaultSystem (system: rec {

View file

@ -1 +1,5 @@
{}
{
imports = [
./interface-naming.nix
];
}

View 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];
};
}