about summary refs log tree commit diff
path: root/nixos/modules/services/networking/ifplugd.nix
blob: 20bfca8f8723a3bf81341764362c4c63dcb9f2cf (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
{ config, lib, pkgs, ... }:

with lib;

let

  inherit (pkgs) ifplugd;

  cfg = config.networking.interfaceMonitor;

  # The ifplugd action script, which is called whenever the link
  # status changes (i.e., a cable is plugged in or unplugged).
  plugScript = pkgs.writeScript "ifplugd.action"
    ''
      #! ${pkgs.stdenv.shell}
      iface="$1"
      status="$2"
      ${cfg.commands}
    '';

in

{

  ###### interface

  options = {

    networking.interfaceMonitor.enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        If <literal>true</literal>, monitor Ethernet interfaces for
        cables being plugged in or unplugged.  When this occurs, the
        commands specified in
        <option>networking.interfaceMonitor.commands</option> are
        executed.
      '';
    };

    networking.interfaceMonitor.beep = mkOption {
      type = types.bool;
      default = false;
      description = ''
        If <literal>true</literal>, beep when an Ethernet cable is
        plugged in or unplugged.
      '';
    };

    networking.interfaceMonitor.commands = mkOption {
      type = types.lines;
      default = "";
      description = ''
        Shell commands to be executed when the link status of an
        interface changes.  On invocation, the shell variable
        <varname>iface</varname> contains the name of the interface,
        while the variable <varname>status</varname> contains either
        <literal>up</literal> or <literal>down</literal> to indicate
        the new status.
      '';
    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    jobs.ifplugd =
      { description = "Network interface connectivity monitor";

        startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        exec =
          ''
            ${ifplugd}/sbin/ifplugd --no-daemon --no-startup --no-shutdown \
              ${if config.networking.interfaceMonitor.beep then "" else "--no-beep"} \
              --run ${plugScript}
          '';
      };

    environment.systemPackages = [ ifplugd ];

  };

}