1 # Use predictable interface names in stage-1 and stage-2.
2 # DOC: https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
4 # Tip: names that can be given using ID_NET_NAME_* envvars
5 # can be checked before hand with:
6 # udevadm test-builtin net_id /sys/class/net/*
8 { pkgs, lib, config, ... }:
9 let udevNetSetupLinkRules = pkgs.writeTextFile {
10 name = "80-net-setup-link.rules";
11 destination = "/etc/udev/rules.d/80-net-setup-link.rules";
13 SUBSYSTEM!="net", GOTO="net_setup_link_end"
15 IMPORT{builtin}="path_id"
17 ACTION!="add", GOTO="net_setup_link_end"
19 # Load net_setup_link to setup the ID_NET_NAME_* envvars
20 IMPORT{builtin}="net_setup_link"
22 # Rename eth* using the "path" name policy (eg. enp1s0),
23 # Note that in stage-1 the envvar ID_NET_NAME is not set,
24 # hence not usable as in ''${pkgs.systemd}/lib/udev/rules.d/80-net-setup-link.rules
25 # Because in stage-1 there is no /etc/systemd/network/*.link
26 # nor **/systemd/network/99-default.link
27 # to set NamePolicy= which is responsible to set ID_NET_NAME.
28 # Not sure if ATTR{type}=="1" and KERNEL=="eth*" are equivalent or not.
29 ATTR{type}=="1", KERNEL=="eth*", NAME="$env{ID_NET_NAME_PATH}"
31 LABEL="net_setup_link_end"
38 # false would set boot.kernelParams = [ "net.ifnames=0" ];
39 # to disable NamePolicy= in *.link.
40 usePredictableInterfaceNames = true;
44 extraUdevRulesCommands = ''
45 # Query hwdb to set some more ID_* in case someone need them for their rules.
46 cp -v ${pkgs.systemd}/lib/udev/rules.d/75-net-description.rules $out/
48 # The name set here in stage-1 by 80-net-setup-link.rules
49 # will stay in stage-2 (at least until the device is removed/added).
50 cp -v ${udevNetSetupLinkRules}/etc/udev/rules.d/80-net-setup-link.rules $out/
54 services.udev.packages = [
55 # Only useful here in stage-2 if the device is removed and re-added
56 # (eg. the network module is rmmod-ed then modprobe-d).
57 # The stage-1 (or initrd) is only a pivot_root after all,
58 # it does not reload the kernel, hence passing to stage-2
59 # does not trigger ACTION=="add" for the net devices.
63 /* Useless block, only here for explanations.
65 # NixOS put this .link only in the root filesystem, not in the initrd
66 # hence it's only active in stage-2, not stage-1.
67 # And even in stage-2, the 80-net-setup-link.rules has priority.
68 # DOC: https://www.freedesktop.org/software/systemd/man/systemd.link.html
69 environment.etc."systemd/network/79-net-setup.link".text = ''
74 #NamePolicy=keep kernel database onboard slot path
76 MACAddressPolicy=persistent