Systemd/networkd/dispatcher: Difference between revisions

From NixOS Wiki
imported>Onny
Created page with "[https://gitlab.com/craftyguy/networkd-dispatcher Networkd-dispatcher] is a dispatcher service for systemd-networkd connection status changes. This daemon is similar to Networ..."
 
Klinger (talk | contribs)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Systemd/breadcrumb}}
[https://gitlab.com/craftyguy/networkd-dispatcher Networkd-dispatcher] is a dispatcher service for systemd-networkd connection status changes. This daemon is similar to NetworkManager-dispatcher, but is much more limited in the types of events it supports due to the limited nature of systemd-networkd.
[https://gitlab.com/craftyguy/networkd-dispatcher Networkd-dispatcher] is a dispatcher service for systemd-networkd connection status changes. This daemon is similar to NetworkManager-dispatcher, but is much more limited in the types of events it supports due to the limited nature of systemd-networkd.


{{Note|Parts of this instruction and module are not yet stable and will be available in the upcoming NixOS 23.05 release.}}
== Usage ==


== Installation ==
The following example triggers a script every time the networkd state <code>routable</code> or <code>off</code> is reached. This is the case when you connect to a new network or quit an existing connection as with [[OpenVPN]]. An additional check ensures that the affected interface corresponds to <code>wlan0</code> and that the uplink is <code>configured</code>. After that the [[Tor]] daemon gets restarted.
 
Add following line to your system configuration to install and enable Networkd-dispatcher


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
services.networkd-dispatcher.enable = true;
services.networkd-dispatcher = {
</syntaxhighlight>
  enable = true;
 
  rules."restart-tor" = {
== Configuration ==
    onState = ["routable" "off"];
 
    script = ''
In the following example we're going to create a script which will restart the [[Tor]] daemon everytime the interface <code>wlan0</code> establishes a new successfull connection to a wifi network.
      #!${pkgs.runtimeShell}
 
      if [[ $IFACE == "wlan0" && $AdministrativeState == "configured" ]]; then
{{file|/var/lib/networkd-dispatcher/routable.d/00-restart-tor.sh|bash|<nowiki>
        echo "Restarting Tor ..."
#!/usr/bin/env bash
        systemctl restart tor
if [[ $IFACE == "wlan0" && $AdministrativeState == "configured" ]]; then
      fi
    systemctl restart tor
      exit 0
fi
    '';
exit 0
  };
</nowiki>}}
};
 
Adjusting file permissions for the script to work
<syntaxhighlight lang="bash">
chmod 755 /var/lib/networkd-dispatcher/routable.d/00-restart-tor
chown root /var/lib/networkd-dispatcher/routable.d/00-restart-tor
</syntaxhighlight>
</syntaxhighlight>


Please refer [https://github.com/evilsocket/opensnitch/wiki/Rules upstream documentation] for configuration syntax and additional examples.
Please refer [https://gitlab.com/craftyguy/networkd-dispatcher upstream documentation] for available states and additional examples.


[[Category:Networking]]
[[Category:Networking]]
[[Category:systemd]]

Latest revision as of 21:33, 24 April 2024

Networkd-dispatcher is a dispatcher service for systemd-networkd connection status changes. This daemon is similar to NetworkManager-dispatcher, but is much more limited in the types of events it supports due to the limited nature of systemd-networkd.

Usage

The following example triggers a script every time the networkd state routable or off is reached. This is the case when you connect to a new network or quit an existing connection as with OpenVPN. An additional check ensures that the affected interface corresponds to wlan0 and that the uplink is configured. After that the Tor daemon gets restarted.

services.networkd-dispatcher = {
  enable = true;
  rules."restart-tor" = {
    onState = ["routable" "off"];
    script = ''
      #!${pkgs.runtimeShell}
      if [[ $IFACE == "wlan0" && $AdministrativeState == "configured" ]]; then
        echo "Restarting Tor ..."
        systemctl restart tor
      fi
      exit 0
    '';
  };
};

Please refer upstream documentation for available states and additional examples.