Network interface names in NixOS configuration

What is the purpose of the attributes networking.interfaces.<name>.name in configuration.nix?

My understanding is that the <name> part in the configuration of a network interface is the actual interface name as a string, so that you have, for example, something like networking.interfaces."eth0" = { ... }; in configuration.nix. However, what is the name attribute for then? Should I have something like networking.interfaces."eth0" = "eth0";?

The actual values used for the configuration is the part after <name>, so if you had networking.interfaces.foo.name = bar, the interface name would be bar. foo would be the attribute key name, it only used to refer to that interface within Nix.

If I understand it correctly, this part:

makes the attribute key name, the default value of the actual interface name. If you don’t explicitly define the interface name, it will be equal to the attribute key name.

2 Likes

This is a somewhat common pattern in general, I believe this is used for ease of programmatic generation of attrsets, as well as for if you’d like the real value to be different from the key you use.

To spell that out, no, you don’t need that, it is redundant. The name attribute is for if you’d like two different strings there for some reason :slight_smile:

2 Likes

Slightly related: how do I predict the network interface name of a machine, particular a newly provisioned vm where can’t assume the provider? I have the problem that I want to build a VM image that I can deploy on a host with pre-defined network configuration, e.g. a static IP. What do I do here?

1 Like

Linux - or rather systemd/udev - provides “predictable” interface names, in that it will assign names to interfaces that map to their physical hardware location. If you know the network device configuration, in theory you can use those names.

When I just want any interface I often find myself disabling that feature and using the venerable eth0 though (link to the freedesktop docs on how to predict predictable names inside as well): NixOS Search

1 Like

Thank you, I think the networking.usePredictableInterfaceNames is probably my best bet. I agree with your putting the “predictable” in quotes, I’d say it provides “stable” network interface names. They are still pretty hard to predict especially with VMs or some form of provisioning on varying target systems in mind.

@Infinisil tipped me off, that systemd.network has some kind of interface matching and can configure networking that way. Unfortunately my internet crapped out during. If I choose to go down that path, I’ll document my findings here.

Here’s my config relevant to this:

This allowed me to predictably set the interface for iphone tethering to iphone and enable DHCP for it, no matter which USB port it’s plugged in

4 Likes