diff --git a/system/options.nix b/system/options.nix index 81e4ec9efbf..bc694312231 100644 --- a/system/options.nix +++ b/system/options.nix @@ -310,6 +310,29 @@ } + { + name = ["networking" "interfaceMonitor" "enable"]; + default = false; + description = " + If true, monitor Ethernet interfaces for + cables being plugged in or unplugged. When this occurs, the + dhclient service is restarted to + automatically obtain a new IP address. This is useful for + roaming users (laptops). + "; + } + + + { + name = ["networking" "interfaceMonitor" "beep"]; + default = false; + description = " + If true, beep when an Ethernet cable is + plugged in or unplugged. + "; + } + + { name = ["fileSystems"]; default = []; diff --git a/upstart-jobs/default.nix b/upstart-jobs/default.nix index f1e0a2e959f..f74fedcce1e 100644 --- a/upstart-jobs/default.nix +++ b/upstart-jobs/default.nix @@ -119,6 +119,13 @@ import ../upstart-jobs/gather.nix { interfaces = config.get ["networking" "interfaces"]; }) + # ifplugd daemon for monitoring Ethernet cables. + ++ optional ["networking" "interfaceMonitor" "enable"] + (import ../upstart-jobs/ifplugd.nix { + inherit (pkgs) ifplugd writeScript bash; + inherit config; + }) + # DHCP server. ++ optional ["services" "dhcpd" "enable"] (import ../upstart-jobs/dhcpd.nix { diff --git a/upstart-jobs/dhclient.nix b/upstart-jobs/dhclient.nix index f2ebc39190d..3f198d44d2d 100644 --- a/upstart-jobs/dhclient.nix +++ b/upstart-jobs/dhclient.nix @@ -12,6 +12,8 @@ in { name = "dhclient"; + + extraPath = [dhcp]; job = " description \"DHCP client\" diff --git a/upstart-jobs/ifplugd.nix b/upstart-jobs/ifplugd.nix new file mode 100644 index 00000000000..b3f4b811c33 --- /dev/null +++ b/upstart-jobs/ifplugd.nix @@ -0,0 +1,34 @@ +{ifplugd, config, writeScript, bash}: + +let + + # The ifplugd action script, which is called whenever the link + # status changes (i.e., a cable is plugged in or unplugged). We do + # nothing when a cable is unplugged. When a cable is plugged in, we + # restart dhclient, which will hopefully give us a new IP address + # if appropriate. + plugScript = writeScript "ifplugd.action" "#! ${bash}/bin/sh + if test \"$2\" = up; then + initctl stop dhclient + initctl start dhclient + fi + "; + +in + +{ + name = "ifplugd"; + + extraPath = [ifplugd]; + + job = " +description \"Network interface connectivity monitor\" + +start on network-interfaces/started +stop on network-interfaces/stop + +respawn ${ifplugd}/sbin/ifplugd --no-daemon --no-startup --no-shutdown \\ + ${if config.get ["networking" "interfaceMonitor" "beep"] then "" else "--no-beep"} \\ + --run ${plugScript}"; + +}