nixos/README.md: relax the requirement of providing option defaults (#334509)
[NixPkgs.git] / nixos / modules / services / networking / networkd-dispatcher.nix
blob49d5cd545656a5e4bd41a7f3d385b9ca412ee47d
2   config,
3   lib,
4   pkgs,
5   ...
6 }:
8 with lib;
10 let
12   cfg = config.services.networkd-dispatcher;
17   options = {
18     services.networkd-dispatcher = {
20       enable = mkEnableOption ''
21         Networkd-dispatcher service for systemd-networkd connection status
22         change. See [upstream instructions](https://gitlab.com/craftyguy/networkd-dispatcher)
23         for usage
24       '';
26       rules = mkOption {
27         default = { };
28         example = lib.literalExpression ''
29           { "restart-tor" = {
30               onState = ["routable" "off"];
31               script = '''
32                 #!''${pkgs.runtimeShell}
33                 if [[ $IFACE == "wlan0" && $AdministrativeState == "configured" ]]; then
34                   echo "Restarting Tor ..."
35                   systemctl restart tor
36                 fi
37                 exit 0
38               ''';
39             };
40           };
41         '';
42         description = ''
43           Declarative configuration of networkd-dispatcher rules. See
44           [upstream instructions](https://gitlab.com/craftyguy/networkd-dispatcher)
45           for an introduction and example scripts.
46         '';
47         type = types.attrsOf (
48           types.submodule {
49             options = {
50               onState = mkOption {
51                 type = types.listOf (
52                   types.enum [
53                     "routable"
54                     "dormant"
55                     "no-carrier"
56                     "off"
57                     "carrier"
58                     "degraded"
59                     "configuring"
60                     "configured"
61                   ]
62                 );
63                 default = null;
64                 description = ''
65                   List of names of the systemd-networkd operational states which
66                   should trigger the script. See <https://www.freedesktop.org/software/systemd/man/networkctl.html>
67                   for a description of the specific state type.
68                 '';
69               };
70               script = mkOption {
71                 type = types.lines;
72                 description = ''
73                   Shell commands executed on specified operational states.
74                 '';
75               };
76             };
77           }
78         );
79       };
81       extraArgs = mkOption {
82         type = types.listOf types.str;
83         default = [ ];
84         description = ''
85           Extra arguments to pass to the networkd-dispatcher command.
86         '';
87         apply = escapeShellArgs;
88       };
90     };
91   };
93   config = mkIf cfg.enable {
95     systemd = {
96       packages = [ pkgs.networkd-dispatcher ];
97       services.networkd-dispatcher = {
98         wantedBy = [ "multi-user.target" ];
99         environment.networkd_dispatcher_args = cfg.extraArgs;
100       };
101     };
103     services.networkd-dispatcher.extraArgs =
104       let
105         scriptDir = pkgs.symlinkJoin {
106           name = "networkd-dispatcher-script-dir";
107           paths = lib.mapAttrsToList (
108             name: cfg:
109             (map (
110               state:
111               pkgs.writeTextFile {
112                 inherit name;
113                 text = cfg.script;
114                 destination = "/${state}.d/${name}";
115                 executable = true;
116               }
117             ) cfg.onState)
118           ) cfg.rules;
119         };
120       in
121       [
122         "--verbose"
123         "--script-dir"
124         "${scriptDir}"
125       ];
127   };