Can I trigger a systemd service when radios are turned on?

I use the following systemd service to restart openvpn after a suspend:

  systemd.services.openvpn-reconnect = {
    enable = true;
    description = "Restart OpenVPN after suspend";
    script = "${pkgs.procps}/bin/pkill --signal SIGHUP --exact openvpn";
    wantedBy = [ "sleep.target" ];
  };

It works great.
However, if I put my laptop into airplane mode by hitting the airplane mode button (which I presume turns off all radios), when I hit the airplane mode button again to turn the radios back on, I need to manually restart openvpn using the above command.
Is there a way to have the above systemd service run after re-enabling the radios? Is this the correct approach?

I think that what you really want is for the vpn to reconnect when access to its endpoint becomes available. Why endpoint access went away should be irrelevant. But I do not know systemd well enough to find a suitable target.

Does systemd send a notification when a network link goes up or down? How about when an interface gets an IP address? These would be useful triggers.

networking.networkmanager.dispatcherScripts runs a script when link status changes. If you use NetworkManager, maybe you can use such a script to signal openvpn-reconnect.

Hopefully someone else has a better solution. I’ve wanted to solve a similar problem with Nebula VPN but I’ve never taking the time to really dig into it.

Just some out of the box thinking, does pressing this button cause acpi event?

You maybe able to get nixos to do a openvpn systemd.restart when the button is disables or re enables wifi.

Populating /etc/acpi/events/ with configuration to launch a script. There is a example here of someone doing for headphones.

https://sourcegraph.com/github.com/chris-martin/home/-/blob/nix/os/renzo.nix?L116:3&subtree=true

just an idea, there is might be a way to for systemd to pickup acpi event also, but i’m not sure how to do that.

The non-nixos way for doing it is here.

Good luck and let us know how you get on.