I’m setting up a NixOS machine running the GNOME3 desktop for an art installation. The installation requires communication with a network of nodes over Ethernet, each with their own static IP address known ahead of time.
I’d like to be able to add something to my nix configuration which will automatically add a Wired network profile with the known static IPV4 address and netmask and set it as the default wired network profile.
Ideally I would be able to run
sudo nixos-rebuild switch and then the network would be automatically configured as necessary so that when I plug in an Ethernet cable, we’re ready to go.
I just came across the NetworkManager options. I’m aware that GNOME uses NetworkManager under the hood of its Settings > Network interface, and was hoping there might be a way to declare a list of profiles here, but the options appear to be pretty limited.
It looks like there is an
extraConfig option which allows to append extra configuration to
NetworkManager.conf. There is some info on available options here. I’ll do some digging into this to see if it’s possible to achieve what I’m after using this.
In the mean-time, if anyone has any advice or ready-to-do examples of achieving this, that would be greatly appreciated! Otherwise I’ll report back soon, hopefully with a solution.
Is using Network Manager a strict requirement? the default
networking bindings are also available to easily create a static network profile.
Thanks for the suggestion!
My initial hesitation with using these options was that I thought it might result in some sort of conflict with the default
NetworkManager profile. My intuition was that it might be safer to try and add an explicit NetworkManager connection profile somehow.
However, it looks like using
networking.interfaces.eno2.ipv4.addresses to add a static IP and prefix and running
sudo nixos-rebuild switch has worked like a charm! The GNOME Settings > Network GUI also seems to have picked up the configuration nicely. Thanks!
Should it matter if I use
eno2 (the network interface name for the Ethernet port I’m using as reported by
ip a) or if I use
eth0 as in the nixos manual example? I tried changing it to
networking.interfaces.eth0.ipv4.addresses and it appears to be working just the same, but for some reason IPV6 also appears to be automatically enabled now, whereas it wasn’t when I used
One thing I liked about the idea of adding a NetworkManager Wired connection profile was that I might not need to specify the name of the specific interface, but rather have the profile be the default for whatever Ethernet interface happened to be used on the device. It looks like I might be getting that behaviour by using the
networking.interfaces nix configuration option anyway, but it’s not clear to me why it works like this. E.g. this leads me to questions like:
- What if I have two separate Ethernet interfaces that I want to default to different profiles?
- Or what if I want to declare several different connection profiles and have them show up in my network settings so that I can easily switch between them at runtime?
My understanding is that you should use what
ip a reports, as
NixOS uses the interface name to configure it appropriately. There is another option
usePredictableInterfaceNames that can change how the interfaces get reported. I suspect when you used
eth0, it fell back to DHCP configuration for
What if I have two separate Ethernet interfaces that I want to default to different profiles?
This should be as simple as setting
networking.interfaces.<other interface>.ipv4.addresses to a different configuration.
Or what if I want to declare several different connection profiles and have them show up in my network settings so that I can easily switch between them at runtime?
I don’t believe this is possible, but have never needed to try it. I think the WiFi functionality can pull this off as it is more likely to need different profiles (SSIDs), but haven’t needed to set this up for a wired network, sorry.
An untested possibility to continue using network manager:
1/ on a test system, create the required connection with the gui
2/ locate the corresponding file in /etc/NetworkManager/system-connections
3/ move it to /etc/nixos
4/ in configuration.nix:
environment.etc."NetworkManager/system-connections/foo.nmconnection".source = ./foo.nmconnection;
Note that this makes the file world readable, including the secrets it contains (like wifi passphrases for wifi connections).