From 5e0230dcdd416b5d1865256ac09edb1acb648d37 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 20 Jul 2024 16:11:42 +0100 Subject: [PATCH] Added dmenu-wifi to dwm --- home/common/optional/desktop/dwm/default.nix | 1 + .../desktop/dwm/scripts/dmenu-wifi.nix | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 home/common/optional/desktop/dwm/scripts/dmenu-wifi.nix diff --git a/home/common/optional/desktop/dwm/default.nix b/home/common/optional/desktop/dwm/default.nix index 9f217e7..88996b6 100644 --- a/home/common/optional/desktop/dwm/default.nix +++ b/home/common/optional/desktop/dwm/default.nix @@ -28,6 +28,7 @@ ./scripts/get-focused-monitor.nix ./scripts/git-commit-ai.nix ./scripts/aichat-wrapper.nix + ./scripts/dmenu-wifi.nix ]; home.packages = [ diff --git a/home/common/optional/desktop/dwm/scripts/dmenu-wifi.nix b/home/common/optional/desktop/dwm/scripts/dmenu-wifi.nix new file mode 100644 index 0000000..06a82df --- /dev/null +++ b/home/common/optional/desktop/dwm/scripts/dmenu-wifi.nix @@ -0,0 +1,51 @@ +{ pkgs, ... }: +{ + home.packages = with pkgs; [ + (writeShellScriptBin "dmenu-wifi" '' + nmcli dev wifi rescan + ssid_list=$(nmcli -f in-use,bssid,ssid,mode,chan,freq,rate,signal,bars,security dev wifi) + available_connections=$(echo "$ssid_list" | sed '/--.*Infra/d') + + connection=$( echo "$available_connections" | dmenu -l 20) + bssid=$(echo "$connection" | sed 's/^.\s*//;s/\s\s.*$//') + ssid=$(echo "$connection" | sed 's/^.\s*[0-9;A-Z;:]*\s\s//;s/\s*Infra\s*[0-9].*$//') + [[ "$connection" = "" ]] && notify-send -t 5000 "Wifi Connect" "Cancelled" && exit 0 + [[ $( echo "$connection" | grep "IN-USE" -o) = "IN-USE" ]] && notify-send -t 5000 "Wifi Connect" "Please select valid network" && exit 0 + [[ -n "$(echo "$connection" | grep '\*')" ]] && notify-send -t 5000 "Wifi Connect" "Already Connected to: $(echo "$ssid")" && exit 0 + + notify-send -t 5000 "Network Manager" "Attempting to connect to $ssid..." + nmcli connection modify "$ssid" 802-11-wireless.bssid "$bssid" + nmcli device wifi connect "$bssid" + return_code=$? + if [ $return_code == 4 ]; + then + notify-send -t 5000 "Wifi Connect" "Please enter password for '$ssid'..." + prompt="Enter Password for '$ssid'" + pwd=$(echo "" | dmenu -p "$prompt") + nmcli device wifi connect "$bssid" password "$pwd" + return_code=$? + fi + case "$return_code" in + 0) notify-send -t 5000 "Wifi Connect" "Successfully connected to: $ssid!" + pkill -RTMIN+12 dwmblocks + exit 0 + ;; + 3) notify-send -t 5000 "Wifi Connect" "Connection to $ssid failed. Timeout expired?" + pkill -RTMIN+12 dwmblocks + exit 1 + ;; + 4) + notify-send -t 5000 "Wifi Connect" "Connection to $ssid failed. Possibly wrong password?" + nmcli connection delete id "$ssid" + pkill -RTMIN+12 dwmblocks + exit 1 + ;; + *) notify-send -t 5000 "Wifi Connect" "Connection to $ssid failed. Error code $?" + echo "Failed. Exiting" + pkill -RTMIN+12 dwmblocks + exit 1 + esac + '') + ]; +} +