Dhcpd hotplugging?

Hello,

I found the very useful module nixos-router to connect a Raspberry Pi to WiFi through a laptop (rpi --[ethernet]--> laptop --[wifi]--> LAN/WAN).

However, my laptop will hang on boot, waiting for dhcpd to connect the raspberry pi over ethernet (possible relevant NixOS code). I want dhcpd to automatically configure ethernet when connected instead of relying on ethernet always being connected. I’ve heard this is called “hotplugging.”

How can I go about doing this on NixOS? Or, where should I look for more information?

I’d ask in a non-NixOS-specific forum, but almost everything I can find online expects imperative commands and/or a Debian /etc/network/interfaces. I also think this question could be helpful for other NixOS users.

Thanks!

2 Likes

I have a WIP solution but I still need more time to test it. If it works, I’ll post it here.

1 Like

It works beautifully!

For me, the ethernet device is available from my laptop via 10.42.0.186.

let
  bridgeName = "rpi";
  wifiDevice = "wlp2s0";
  ethernetDevice = "ens8u1";
in { 
  networking.nat.enable = true;
  networking.nat.externalInterface = wifiDevice;
  networking.nat.internalInterfaces = [ ethernetDevice ];

  # don't require ethernet to be connected when booting
  systemd.services = {
    "network-link-${ethernetDevice}".wantedBy = lib.mkForce [];
    "network-addresses-${ethernetDevice}".wantedBy = lib.mkForce [];
    "${bridgeName}-netdev".wantedBy = lib.mkForce [];
  };
}

EDIT: I forgot a few attrributes

let
  bridgeName = "rpi";
  wifiDevice = "wlp2s0";
  ethernetDevice = "ens8u1";
in { 
  networking.nat.enable = true;
  networking.nat.externalInterface = wifiDevice;
  networking.nat.internalInterfaces = [ ethernetDevice ];

  networking.bridges."${bridgeName}" = {
    interfaces = [ ethernetDevice wifiDevice ];
    rstp = true;
  };

  # don't require ethernet to be connected when booting
  systemd.services = {
    "network-link-${ethernetDevice}".wantedBy = lib.mkForce [];
    "network-addresses-${ethernetDevice}".wantedBy = lib.mkForce [];
    "${bridgeName}-netdev".wantedBy = lib.mkForce [];
  };
}
1 Like

I found this incredibly useful (thanks!) but for a different use-case:

I use NixOS on my laptop, which I often have connected to ethernet via the USB dock on my desk. When connected, I want dhcpcd to connect on boot, but when I’m on the road, I don’t want to sit around waiting for dhcpcd to timeout before being allowed to login to my laptop.

I’m not sure if this is the “proper” solution for my problem, but I’ve been pulling my hair out and this seems to fix it perfectly. Kudos

2 Likes