about summary refs log tree commit diff
path: root/nixos/modules/services/networking/networkd-dispatcher.nix
blob: 427835870e59f704809de4dc3fa5d19039de0ae7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.networkd-dispatcher;

in {

  options = {
    services.networkd-dispatcher = {

      enable = mkEnableOption ''
        Networkd-dispatcher service for systemd-networkd connection status
        change. See [https://gitlab.com/craftyguy/networkd-dispatcher](upstream instructions)
        for usage
      '';

      rules = mkOption {
        default = {};
        example = lib.literalExpression ''
          { "restart-tor" = {
              onState = ["routable" "off"];
              script = '''
                #!''${pkgs.runtimeShell}
                if [[ $IFACE == "wlan0" && $AdministrativeState == "configured" ]]; then
                  echo "Restarting Tor ..."
                  systemctl restart tor
                fi
                exit 0
              ''';
            };
          };
        '';
        description = ''
          Declarative configuration of networkd-dispatcher rules. See
          [https://gitlab.com/craftyguy/networkd-dispatcher](upstream instructions)
          for an introduction and example scripts.
        '';
        type = types.attrsOf (types.submodule {
          options = {
            onState = mkOption {
              type = types.listOf (types.enum [
                "routable" "dormant" "no-carrier" "off" "carrier" "degraded"
                "configuring" "configured"
              ]);
              default = null;
              description = ''
                List of names of the systemd-networkd operational states which
                should trigger the script. See <https://www.freedesktop.org/software/systemd/man/networkctl.html>
                for a description of the specific state type.
              '';
            };
            script = mkOption {
              type = types.lines;
              description = ''
                Shell commands executed on specified operational states.
              '';
            };
          };
        });
      };

    };
  };

  config = mkIf cfg.enable {

    systemd = {
      packages = [ pkgs.networkd-dispatcher ];
      services.networkd-dispatcher = {
        wantedBy = [ "multi-user.target" ];
        # Override existing ExecStart definition
        serviceConfig.ExecStart = let
          scriptDir = pkgs.symlinkJoin {
            name = "networkd-dispatcher-script-dir";
            paths = lib.mapAttrsToList (name: cfg:
              (map(state:
                pkgs.writeTextFile {
                  inherit name;
                  text = cfg.script;
                  destination = "/${state}.d/${name}";
                  executable = true;
                }
              ) cfg.onState)
            ) cfg.rules;
          };
        in [
          ""
          "${pkgs.networkd-dispatcher}/bin/networkd-dispatcher -v --script-dir ${scriptDir} $networkd_dispatcher_args"
        ];
      };
    };

  };
}