Is this the correct way to tie udev add/remove to a systemd service?

The network controller I’m testing out with hostapd is in a thunderbolt enclosure. I wanted to figure out how to set it up for hotplug. Eventually, I came up with:

services.udev.extraRules = ''
  ACTION=="add|move", SUBSYSTEM=="net", KERNEL=="wlx000a5207f695", TAG+="systemd", ENV{SYSTEMD_WANTS}="hostapd.service"
  ACTION=="remove", SUBSYSTEM=="net", KERNEL=="wlx000a5207f695", RUN+="${pkgs.systemd}/bin/systemctl --no-block stop hostapd"
'';

Is this the right way to do it? I tried a number of different approaches, and this was the only one I got to work. Just want to check I’m not overlooking a simpler way.

I’m still using it. Thought I had an issue, but it was a simple error on my part. In the case of hostapd this would be used with:

  systemd.services.hostapd.wantedBy = lib.mkForce [];

I notice that the hostapd service already has a bindsTo (and an after) set up for the "sys-subsystem-net-devices-wlxNNN.device" unit, based on what I specify for the interface. So, I think, it would be possible for just the above line to suffice. Is NixOS itself missing something in this regard? Maybe in the way that it sets up udev?

I’ve finally got it figured out! Clearing out wantedBy and relying on the bindsTo is not enough.

The following works without any extra udev rules:

systemd.services.hostapd.wantedBy = lib.mkForce [ "sys-subsystem-net-devices-${ifName}.device" ];