]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/defaults/predictable-interface-names.nix
mermet: fix shorewall
[sourcephile-nix.git] / nixos / defaults / predictable-interface-names.nix
1 # Use predictable interface names in stage-1 and stage-2.
2 # DOC: https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
3 #
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/*
7
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";
12 text = ''
13 SUBSYSTEM!="net", GOTO="net_setup_link_end"
14
15 IMPORT{builtin}="path_id"
16
17 ACTION!="add", GOTO="net_setup_link_end"
18
19 # Load net_setup_link to setup the ID_NET_NAME_* envvars
20 IMPORT{builtin}="net_setup_link"
21
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}"
30
31 LABEL="net_setup_link_end"
32 '';
33 };
34 in
35 {
36 networking = {
37 # Currently no-op.
38 # false would set boot.kernelParams = [ "net.ifnames=0" ];
39 # to disable NamePolicy= in *.link.
40 usePredictableInterfaceNames = true;
41 };
42
43 boot.initrd = {
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/
47
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/
51 '';
52 };
53
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.
60 udevNetSetupLinkRules
61 ];
62
63 /* Useless block, only here for explanations.
64
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 = ''
70 [Match]
71 OriginalName=*
72
73 [Link]
74 #NamePolicy=keep kernel database onboard slot path
75 NamePolicy=mac
76 MACAddressPolicy=persistent
77 '';
78 */
79 }