# NetworkManager hook to make MozillaVPN work nicely with other overlay networks like netmaker/tailscale/etc
{ pkgs
, vpn_interface
, vpn_network
, ...
}: (pkgs.writeText "${vpn_interface}-route" ''
  #!${pkgs.fish}/bin/fish
  set MOZ_INTERFACE "moz0"
  set VPN_INTERFACE "${vpn_interface}"
  set VPN_NETWORK "${vpn_network}"

  sleep 1 # give mozillavpn time to create the rules we want to override
  logger "NetworkManager dispatcher event: $argv[1], $argv[2]"

  # When MOZ interface comes up, check and add the Tailscale route
  if test "$argv[1]" = "$MOZ_INTERFACE" -a "$argv[2]" = "up"
    # Re-creating forces high priority in case the route already exists
    ip route del $VPN_NETWORK dev $VPN_INTERFACE
    ip route add $VPN_NETWORK dev $VPN_INTERFACE
    logger "Added Tailscale route for $VPN_NETWORK via $VPN_INTERFACE"
  end
'')