RPi-Distro / raspberrypi-sys-mods

A collection of Raspberry Pi-sourced system configuration files and associated scripts
99 stars 36 forks source link

Wifi pre-config: Write nmconnection file instead of using timer-service #83

Closed cillian64 closed 11 months ago

cillian64 commented 11 months ago

Background: Raspberry Pi Imager lets you pre-configure a wifi network - useful for headless systems you'll be accessing over the network. In the Bookworm release we move to NetworkManager for managing wifi connections. This has a downside that we can't easily save a wifi network, instead we just tell NM to connect to the network. But if NM can't see the network then it won't save it.

If you pre-configured a wifi network in Imager but your Pi didn't see that network on first boot, the network details would be forgotten and your Pi will never connect to the network.

After this commit we will keep trying to connect to the wifi network until NM remembers the network (because it actually saw the SSID). So if the network isn't present at first boot, we will still connect to it if we see it some time later. As soon as NM stores the network we stop trying to connect and just let NM handle it.

cillian64 commented 11 months ago

I've tested this in various scenarios on my Pi 5 with bookworm

XECDesign commented 11 months ago
  • f you preconfigure a network A which we can't see and then manually connect to network B which we can see, set-wlan attempting to connect to A doesn't interrupt the connection to B

Are you sure this is the desired behaviour? I would think that if any valid connection has been made, then there's no point trying to connect to whatever was pre-configured.

I'm thinking most of the time it will be people making a typo in the SSID, in which case the service will just keep running and spamming the logs even after they've connected manually.

I don't mind either way, but just wanted to double check.

cillian64 commented 11 months ago

Working on doing this using .nmconnection files instead. I have a prototype mostly working. I'm on holiday for a few days but should have this ready next week.

cillian64 commented 11 months ago

I've updated the PR to write an nmconnection file directly. This works fine in my testing, including if the wifi network isn't present on first boot and only appear later. I've tested with ', ", $, and @ in the SSID and PSK and these all work (although the PSK is easy because rpi-imager hashes it)

I haven't tested open networks because you can't actually configure these at the moment in rpi-imager. I haven't tested with WPA3 because I don't have an AP which can do this as far as I know.

cillian64 commented 11 months ago

One slight regression in this - even though rpi-imager forces you to enter a wifi PSK, if the SSID is of an open network then currently it works fine if you just enter a dummy PSK. This PR breaks that workaround. I don't think that's a big issue given it's a weird undocumented behaviour, but I'll try to add a proper open wifi option to rpi-imager ASAP.

XECDesign commented 11 months ago

Thanks! Looks like a huge improvement over what we have now.