Skip to content

Commit 26e14e5

Browse files
committed
nixos/networkd-dispatcher: add rules option
1 parent 14b23e0 commit 26e14e5

1 file changed

Lines changed: 61 additions & 26 deletions

File tree

nixos/modules/services/networking/networkd-dispatcher.nix

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
with lib;
44

55
let
6+
67
cfg = config.services.networkd-dispatcher;
8+
79
in {
10+
811
options = {
912
services.networkd-dispatcher = {
1013

@@ -14,14 +17,49 @@ in {
1417
for usage.
1518
'');
1619

17-
scriptDir = mkOption {
18-
type = types.path;
19-
default = "/var/lib/networkd-dispatcher";
20-
description = mdDoc ''
21-
This directory is used for keeping various scripts read and run by
22-
networkd-dispatcher. See [https://gitlab.com/craftyguy/networkd-dispatcher](upstream instructions)
23-
for directory structure and script usage.
20+
rules = mkOption {
21+
default = {};
22+
example = lib.literalExpression ''
23+
{ "restart-tor" = {
24+
onState = ["routable" "off"];
25+
script = '''
26+
#!''${pkgs.runtimeShell}
27+
if [[ $IFACE == "wlan0" && $AdministrativeState == "configured" ]]; then
28+
echo "Restarting Tor ..."
29+
systemctl restart tor
30+
fi
31+
exit 0
32+
''';
33+
};
34+
};
35+
'';
36+
description = lib.mdDoc ''
37+
Declarative configuration of networkd-dispatcher rules. See
38+
[https://gitlab.com/craftyguy/networkd-dispatcher](upstream instructions)
39+
for an introduction and example scripts.
2440
'';
41+
type = types.attrsOf (types.submodule {
42+
options = {
43+
onState = mkOption {
44+
type = types.listOf (types.enum [
45+
"routable" "dormant" "no-carrier" "off" "carrier" "degraded"
46+
"configuring" "configured"
47+
]);
48+
default = null;
49+
description = lib.mdDoc ''
50+
List of names of the systemd-networkd operational states which
51+
should trigger the script. See <https://www.freedesktop.org/software/systemd/man/networkctl.html>
52+
for a description of the specific state type.
53+
'';
54+
};
55+
script = mkOption {
56+
type = types.lines;
57+
description = lib.mdDoc ''
58+
Shell commands executed on specified operational states.
59+
'';
60+
};
61+
};
62+
});
2563
};
2664

2765
};
@@ -30,34 +68,31 @@ in {
3068
config = mkIf cfg.enable {
3169

3270
systemd = {
33-
3471
packages = [ pkgs.networkd-dispatcher ];
3572
services.networkd-dispatcher = {
3673
wantedBy = [ "multi-user.target" ];
3774
# Override existing ExecStart definition
38-
serviceConfig.ExecStart = [
75+
serviceConfig.ExecStart = let
76+
scriptDir = pkgs.symlinkJoin {
77+
name = "networkd-dispatcher-script-dir";
78+
paths = lib.mapAttrsToList (name: cfg:
79+
(map(state:
80+
pkgs.writeTextFile {
81+
inherit name;
82+
text = cfg.script;
83+
destination = "/${state}.d/${name}";
84+
executable = true;
85+
}
86+
) cfg.onState)
87+
) cfg.rules;
88+
};
89+
in [
3990
""
40-
"${pkgs.networkd-dispatcher}/bin/networkd-dispatcher -v --script-dir ${cfg.scriptDir} $networkd_dispatcher_args"
91+
"${pkgs.networkd-dispatcher}/bin/networkd-dispatcher -v --script-dir ${scriptDir} $networkd_dispatcher_args"
4192
];
4293
};
43-
44-
# Directory structure required according to upstream instructions
45-
# https://gitlab.com/craftyguy/networkd-dispatcher
46-
tmpfiles.rules = [
47-
"d '${cfg.scriptDir}' 0750 root root - -"
48-
"d '${cfg.scriptDir}/routable.d' 0750 root root - -"
49-
"d '${cfg.scriptDir}/dormant.d' 0750 root root - -"
50-
"d '${cfg.scriptDir}/no-carrier.d' 0750 root root - -"
51-
"d '${cfg.scriptDir}/off.d' 0750 root root - -"
52-
"d '${cfg.scriptDir}/carrier.d' 0750 root root - -"
53-
"d '${cfg.scriptDir}/degraded.d' 0750 root root - -"
54-
"d '${cfg.scriptDir}/configuring.d' 0750 root root - -"
55-
"d '${cfg.scriptDir}/configured.d' 0750 root root - -"
56-
];
57-
5894
};
5995

60-
6196
};
6297
}
6398

0 commit comments

Comments
 (0)