Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Created October 3, 2018 13:35
Show Gist options
  • Save adrianparvino/2f2e047f7e4aa9e329a7d00687ab2449 to your computer and use it in GitHub Desktop.
Save adrianparvino/2f2e047f7e4aa9e329a7d00687ab2449 to your computer and use it in GitHub Desktop.
wpaHook.enable = true;
wpaHook.disconnectHook = ''
${pkgs.iproute}/bin/ip addr flush dev wlp2s0
${pkgs.iproute}/bin/ip route flush dev wlp2s0
'';
wpaHook.connectHook = ''
case "$(${pkgs.wpa_supplicant}/bin/wpa_cli status | grep bssid | cut -d'=' -f2)" in
"ae:0d:1b:e7:e9:f9")
${pkgs.iproute}/bin/ip addr add 192.168.43.2/24 dev wlp2s0
${pkgs.iproute}/bin/ip route add default via 192.168.43.1
;;
"b0:6e:bf:9a:47:6c")
${pkgs.iproute}/bin/ip addr add 192.168.2.102/24 dev wlp2s0
${pkgs.iproute}/bin/ip route add default via 192.168.2.1
;;
*)
${pkgs.dhcpcd}/bin/dhcpcd -1 wlp2s0
;;
esac
'';
{ config, pkgs, lib, ... }:
with lib;
let
cfg = config.services.wpaHook;
hookScript = pkgs.writeScriptBin "wpaHook" ''
#!${pkgs.stdenv.shell}
case "$2" in
CONNECTED)
${cfg.connectHook}
;;
DISCONNECTED)
${cfg.disconnectHook}
;;
esac
'';
in
{
options = {
services.wpaHook.enable = mkOption {
type = with types; bool;
};
services.wpaHook.connectHook = mkOption {
type = with types; lines;
default = "";
};
services.wpaHook.disconnectHook = mkOption {
type = with types; lines;
default = "";
};
};
config = mkIf cfg.enable {
assertions = [{
assertion = config.networking.wireless.enable;
message = "wpa_suplicant must be enabled to enable wpaHook.";
}];
systemd.services.wpaHook = {
requires = [ "wpa_supplicant.service" ];
wantedBy = [ "wpa_supplicant.service" ];
path = with pkgs; [ wpa_supplicant ];
preStart = ''
wpa_cli -G 0 < /dev/null > /dev/null
wpa_cli status | grep wpa_state=COMPLETED && ${hookScript}/bin/wpaHook _ CONNECTED || true
'';
script = ''
wpa_cli -a ${hookScript}/bin/wpaHook
'';
preStop = ''
${hookScript}/bin/wpaHook _ DISCONNECTED
'';
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment