From 086802ded0799e84c92da80dc93b45e951a3ccd6 Mon Sep 17 00:00:00 2001 From: Anish Lakhwara Date: Tue, 13 Dec 2022 00:13:53 +1000 Subject: [PATCH] updating a bunch of shit --- home/profiles/desktop/bin/bluetoothmenu | 317 ++++++++++++++++++++++++ home/profiles/desktop/bin/wifimenu | 115 +++++++++ home/profiles/desktop/default.nix | 2 + home/profiles/desktop/sxhkdrc | 6 + hosts/curve/configuration.nix | 7 - hosts/curve/hardware-configuration.nix | 2 +- hosts/profiles/gitea/default.nix | 1 - hosts/profiles/matrix/default.nix | 2 +- hosts/profiles/wifi/default.nix | 81 +++--- hosts/users/anish/default.nix | 2 +- 10 files changed, 488 insertions(+), 47 deletions(-) create mode 100644 home/profiles/desktop/bin/bluetoothmenu create mode 100644 home/profiles/desktop/bin/wifimenu diff --git a/home/profiles/desktop/bin/bluetoothmenu b/home/profiles/desktop/bin/bluetoothmenu new file mode 100644 index 00000000..73735a27 --- /dev/null +++ b/home/profiles/desktop/bin/bluetoothmenu @@ -0,0 +1,317 @@ +#!/usr/bin/env bash +# __ _ _ _ _ _ _ +# _ __ ___ / _(_) | |__ | |_ _ ___| |_ ___ ___ | |_| |__ +# | '__/ _ \| |_| |_____| '_ \| | | | |/ _ \ __/ _ \ / _ \| __| '_ \ +# | | | (_) | _| |_____| |_) | | |_| | __/ || (_) | (_) | |_| | | | +# |_| \___/|_| |_| |_.__/|_|\__,_|\___|\__\___/ \___/ \__|_| |_| +# +# Author: Nick Clyde (clydedroid) +# +# A script that generates a rofi menu that uses bluetoothctl to +# connect to bluetooth devices and display status info. +# +# Inspired by networkmanager-dmenu (https://github.com/firecat53/networkmanager-dmenu) +# Thanks to x70b1 (https://github.com/polybar/polybar-scripts/tree/master/polybar-scripts/system-bluetooth-bluetoothctl) +# +# Depends on: +# Arch repositories: rofi, bluez-utils (contains bluetoothctl) + +# Constants +divider="---------" +goback="Back" + +# Checks if bluetooth controller is powered on +power_on() { + if bluetoothctl show | grep -q "Powered: yes"; then + return 0 + else + return 1 + fi +} + +# Toggles power state +toggle_power() { + if power_on; then + bluetoothctl power off + show_menu + else + if rfkill list bluetooth | grep -q 'blocked: yes'; then + rfkill unblock bluetooth && sleep 3 + fi + bluetoothctl power on + show_menu + fi +} + +# Checks if controller is scanning for new devices +scan_on() { + if bluetoothctl show | grep -q "Discovering: yes"; then + echo "Scan: on" + return 0 + else + echo "Scan: off" + return 1 + fi +} + +# Toggles scanning state +toggle_scan() { + if scan_on; then + kill $(pgrep -f "bluetoothctl scan on") + bluetoothctl scan off + show_menu + else + bluetoothctl scan on & + echo "Scanning..." + sleep 5 + show_menu + fi +} + +# Checks if controller is able to pair to devices +pairable_on() { + if bluetoothctl show | grep -q "Pairable: yes"; then + echo "Pairable: on" + return 0 + else + echo "Pairable: off" + return 1 + fi +} + +# Toggles pairable state +toggle_pairable() { + if pairable_on; then + bluetoothctl pairable off + show_menu + else + bluetoothctl pairable on + show_menu + fi +} + +# Checks if controller is discoverable by other devices +discoverable_on() { + if bluetoothctl show | grep -q "Discoverable: yes"; then + echo "Discoverable: on" + return 0 + else + echo "Discoverable: off" + return 1 + fi +} + +# Toggles discoverable state +toggle_discoverable() { + if discoverable_on; then + bluetoothctl discoverable off + show_menu + else + bluetoothctl discoverable on + show_menu + fi +} + +# Checks if a device is connected +device_connected() { + device_info=$(bluetoothctl info "$1") + if echo "$device_info" | grep -q "Connected: yes"; then + return 0 + else + return 1 + fi +} + +# Toggles device connection +toggle_connection() { + if device_connected $1; then + bluetoothctl disconnect $1 + device_menu "$device" + else + bluetoothctl connect $1 + device_menu "$device" + fi +} + +# Checks if a device is paired +device_paired() { + device_info=$(bluetoothctl info "$1") + if echo "$device_info" | grep -q "Paired: yes"; then + echo "Paired: yes" + return 0 + else + echo "Paired: no" + return 1 + fi +} + +# Toggles device paired state +toggle_paired() { + if device_paired $1; then + bluetoothctl remove $1 + device_menu "$device" + else + bluetoothctl pair $1 + device_menu "$device" + fi +} + +# Checks if a device is trusted +device_trusted() { + device_info=$(bluetoothctl info "$1") + if echo "$device_info" | grep -q "Trusted: yes"; then + echo "Trusted: yes" + return 0 + else + echo "Trusted: no" + return 1 + fi +} + +# Toggles device connection +toggle_trust() { + if device_trusted $1; then + bluetoothctl untrust $1 + device_menu "$device" + else + bluetoothctl trust $1 + device_menu "$device" + fi +} + +# Prints a short string with the current bluetooth status +# Useful for status bars like polybar, etc. +print_status() { + if power_on; then + printf '' + + paired_devices_cmd="devices Paired" + # Check if an outdated version of bluetoothctl is used to preserve backwards compatibility + if (( $(echo "$(bluetoothctl version | cut -d ' ' -f 2) < 5.65" | bc -l) )); then + paired_devices_cmd="paired-devices" + fi + + mapfile -t paired_devices < <(bluetoothctl $paired_devices_cmd | grep Device | cut -d ' ' -f 2) + counter=0 + + for device in "${paired_devices[@]}"; do + if device_connected $device; then + device_alias=$(bluetoothctl info $device | grep "Alias" | cut -d ' ' -f 2-) + + if [ $counter -gt 0 ]; then + printf ", %s" "$device_alias" + else + printf " %s" "$device_alias" + fi + + ((counter++)) + fi + done + printf "\n" + else + echo "" + fi +} + +# A submenu for a specific device that allows connecting, pairing, and trusting +device_menu() { + device=$1 + + # Get device name and mac address + device_name=$(echo $device | cut -d ' ' -f 3-) + mac=$(echo $device | cut -d ' ' -f 2) + + # Build options + if device_connected $mac; then + connected="Connected: yes" + else + connected="Connected: no" + fi + paired=$(device_paired $mac) + trusted=$(device_trusted $mac) + options="$connected\n$paired\n$trusted\n$divider\n$goback\nExit" + + # Open rofi menu, read chosen option + chosen="$(echo -e "$options" | $rofi_command "$device_name")" + + # Match chosen option to command + case $chosen in + "" | $divider) + echo "No option chosen." + ;; + $connected) + toggle_connection $mac + ;; + $paired) + toggle_paired $mac + ;; + $trusted) + toggle_trust $mac + ;; + $goback) + show_menu + ;; + esac +} + +# Opens a rofi menu with current bluetooth status and options to connect +show_menu() { + # Get menu options + if power_on; then + power="Power: on" + + # Human-readable names of devices, one per line + # If scan is off, will only list paired devices + devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-) + + # Get controller flags + scan=$(scan_on) + pairable=$(pairable_on) + discoverable=$(discoverable_on) + + # Options passed to rofi + options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable\nExit" + else + power="Power: off" + options="$power\nExit" + fi + + # Open rofi menu, read chosen option + chosen="$(echo -e "$options" | $rofi_command "Bluetooth")" + + # Match chosen option to command + case $chosen in + "" | $divider) + echo "No option chosen." + ;; + $power) + toggle_power + ;; + $scan) + toggle_scan + ;; + $discoverable) + toggle_discoverable + ;; + $pairable) + toggle_pairable + ;; + *) + device=$(bluetoothctl devices | grep "$chosen") + # Open a submenu if a device is selected + if [[ $device ]]; then device_menu "$device"; fi + ;; + esac +} + +# Rofi command to pipe into, can add any options here +rofi_command="rofi -theme theme/passmenu.rasi -dmenu -no-fixed-num-lines -yoffset -100 -i -p" + +case "$1" in + --status) + print_status + ;; + *) + show_menu + ;; +esac diff --git a/home/profiles/desktop/bin/wifimenu b/home/profiles/desktop/bin/wifimenu new file mode 100644 index 00000000..c1d3ba24 --- /dev/null +++ b/home/profiles/desktop/bin/wifimenu @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +# rofi config without entry so to make select from available options only +rofiprompt=theme/passmenu.rasi +# normal rofi menu +rofidmenu=theme/passmenu.rasi +# rofi config for password +rofipass=theme/passmenu.rasi + +# Power Status +flag=$(cat /sys/class/net/wl*/flags) + +if [ "$flag" = "0x1003" ]; then + power="On" +else + power="Off" +fi + +# Connection Status +state=$(cat /sys/class/net/wl*/operstate) + +if [ "$state" = "up" ]; then + status=$(nmcli | grep "^wlp" | sed 's/\ connected\ to/Connected to/' | cut -d ':' -f2) +else + status="Disconnected" +fi + +# ┌─┐┌─┐┬ ┬┌─┐┬─┐ +# ├─┘│ ││││├┤ ├┬┘ +# ┴ └─┘└┴┘└─┘┴└─ + +powertoggle=$( [ "$flag" = "0x1003" ] && echo "Power Off" || echo "Power On" ) + +powerswitch() { +if [ "$powertoggle" = "Power Off" ]; then + nmcli radio wifi off +elif [ "$powertoggle" = "Power On" ]; then + nmcli radio wifi on +fi +} + +# ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐ +# │ │ │││││││├┤ │ │ +# └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ + +connections() { +list=$(nmcli --fields "SECURITY,SSID" device wifi list | tail -n +2 | sed "s/ */ /g" | sed -E "s/WPA*.?//g" | sed "s/^--//g" | sed "s/ //g") +networks=$(printf "$list" | rofi -theme $rofiprompt -dmenu -i -hover-select -p "Select Wifi Network +$status") +wifi=$( echo "$networks" | sed "s/ //g" | sed "s/ //g" | xargs) +[ -z "$networks" ] && exit +nopass="No Password" +pass=$(printf "$nopass\nCancel" | rofi -theme $rofipass -dmenu -i -hover-select -password -p "Wifi Password") + +if [ "$pass" = "" ]; then + exit +elif [ "$pass" = "$nopass" ]; then + nmcli dev wifi con "$wifi" && printf "exit" | rofi -theme $rofiprompt -dmenu -only-match -i -p "Successfully Connected to $wifi" && exit +elif [ "$pass" = "Cancel" ]; then + exit +else + nmcli dev wifi con "$wifi" password "$pass" && printf "exit" | rofi -theme $rofiprompt -dmenu -i -only-match -p "Successfully Connected to $wifi" && exit +fi + +printf "exit" | rofi -theme $rofiprompt -dmenu -i -p -only-match "Failed to Connect $wifi" && exit +} + + +# ┌┬┐┌─┐┌┐┌┬ ┬┌─┐┬ +# │││├─┤││││ │├─┤│ +# ┴ ┴┴ ┴┘└┘└─┘┴ ┴┴─┘ + +manual() { + +wifi=$(rofi -theme $rofidmenu -dmenu -i -p "Enter Wifi Name") + +[ -z "$wifi" ] && exit +nopass="Run Without Password" +pass=$(printf "$nopass\nCancel" | rofi -theme $rofipass -dmenu -i -password -p "Wifi Password") + +if [ "$pass" = "" ]; then + exit +elif [ "$pass" = "$nopass" ]; then + nmcli dev wifi con "$wifi" && printf "exit" | rofi -theme $rofiprompt -dmenu -i -only-match -p "Successfully Connected to $wifi" && exit +elif [ "$pass" = "Cancel" ]; then + exit +else + nmcli dev wifi con "$wifi" password "$pass" && printf "exit" | rofi -theme $rofiprompt -dmenu -i -only-match -p "Successfully Connected to $wifi" && exit +fi + +printf "exit" | rofi -theme $rofiprompt -dmenu -only-match -i -p "Failed to Connect $wifi" && exit +} + +# ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐ +# │ │├─┘ │ ││ ││││└─┐ +# └─┘┴ ┴ ┴└─┘┘└┘└─┘ + +options="$powertoggle\nConnections\nConnect Manually" + +rofiwifi=$(printf "$options" | rofi -theme $rofiprompt -dmenu -i -hover-select -p "Wifi +Powered: $power +Status: $status") + +case $rofiwifi in + $powertoggle) + powerswitch + ;; + Connections) + notify-send "Scanning" + connections + ;; + "Connect Manually") + manual +esac 2>/dev/null + diff --git a/home/profiles/desktop/default.nix b/home/profiles/desktop/default.nix index 0b00356b..6ba393b5 100644 --- a/home/profiles/desktop/default.nix +++ b/home/profiles/desktop/default.nix @@ -108,6 +108,8 @@ in ''; # TODO hardcoded ".local/bin/passmenu".source = ./bin/passmenu; + ".local/bin/bluetoothmenu".source = ./bin/bluetoothmenu; + ".local/bin/wifimenu".source = ./bin/wifimenu; ".local/bin/powermenu".source = ./bin/powermenu; ".local/bin/screenshot".source = ./bin/screenshot; ".local/bin/mpv-ify".source = ./bin/mpv-ify; diff --git a/home/profiles/desktop/sxhkdrc b/home/profiles/desktop/sxhkdrc index 21f3101b..18c90aec 100644 --- a/home/profiles/desktop/sxhkdrc +++ b/home/profiles/desktop/sxhkdrc @@ -36,6 +36,12 @@ super + s super + o screenshot +super + w + wifimenu + +super + b + bluetoothmenu + # super + Escape # pkill -USR1 -x sxhkd diff --git a/hosts/curve/configuration.nix b/hosts/curve/configuration.nix index 6cf0cbc7..82cb0450 100644 --- a/hosts/curve/configuration.nix +++ b/hosts/curve/configuration.nix @@ -30,13 +30,6 @@ time.timeZone = "Australia/Brisbane"; - networking.useDHCP = false; - networking.interfaces.enp0s31f6.useDHCP = true; - networking.interfaces.wlp3s0.useDHCP = true; - networking.wireless.enable = true; - networking.wireless.interfaces = [ "wlp3s0" ]; - networking.wireless.userControlled.enable = true; - # Select internationalisation properties. i18n.defaultLocale = "en_US.UTF-8"; # console = { diff --git a/hosts/curve/hardware-configuration.nix b/hosts/curve/hardware-configuration.nix index c92b7726..cabe79d9 100644 --- a/hosts/curve/hardware-configuration.nix +++ b/hosts/curve/hardware-configuration.nix @@ -41,7 +41,7 @@ # (the default) this is the recommended approach. When using systemd-networkd it's # still possible to use this option, but it's recommended to use it in conjunction # with explicit per-interface declarations with `networking.interfaces..useDHCP`. - networking.useDHCP = lib.mkDefault true; + # networking.useDHCP = lib.mkDefault true; # networking.interfaces.enp0s31f6.useDHCP = lib.mkDefault true; # networking.interfaces.wlp3s0.useDHCP = lib.mkDefault true; diff --git a/hosts/profiles/gitea/default.nix b/hosts/profiles/gitea/default.nix index a988a53f..6c51ce28 100644 --- a/hosts/profiles/gitea/default.nix +++ b/hosts/profiles/gitea/default.nix @@ -30,7 +30,6 @@ }; environment.systemPackages = [ pkgs.pandoc ]; - services.postgresql = { enable = true; # Ensure postgresql is enabled authentication = '' diff --git a/hosts/profiles/matrix/default.nix b/hosts/profiles/matrix/default.nix index a02feed4..444d7c0f 100644 --- a/hosts/profiles/matrix/default.nix +++ b/hosts/profiles/matrix/default.nix @@ -6,7 +6,7 @@ # ./mautrix-discord.nix # ./mautrix-whatsapp.nix # ./mautrix-slack.nix - # ./mautrix-signall.nix + # ./mautrix-signal.nix ./mjolnir.nix ./heisenbridge.nix ./compress-state-service.nix diff --git a/hosts/profiles/wifi/default.nix b/hosts/profiles/wifi/default.nix index 74528ac8..64d89c7f 100644 --- a/hosts/profiles/wifi/default.nix +++ b/hosts/profiles/wifi/default.nix @@ -3,40 +3,49 @@ { networking.dhcpcd.wait = "background"; networking.dhcpcd.extraConfig = "noarp"; - networking.wireless.userControlled.enable = true; - networking.wireless.networks = { - "Chester 11403" = { - pskRaw = "43fcb0bea43633899a9885865c53ea79d268b9bdfb8c3f4013718e43e6672e5e"; - }; - "HSBNEWiFi" = { - pskRaw = "0820d84980eeb47630f13f04804fc9add684a8feebb64ba914cafc48f569d801"; - }; - "TP-LINK_1D79" = { - pskRaw = "6d960910c33a59e94b151281cc3982863b4b112d8a4efd1b165e4f8e52e7dae8"; - }; - "nadir" = { - pskRaw = "92e0c1f0b3a1bada333964f0ee4ac04e0d9ba941aa3f29216cc3782595a41e5f"; - }; - "WiFi-399631" = { - pskRaw = "2e312c9721e470847f751d8b844d264dc5e2612361a579fc0bdebd2135b5a8ea"; - }; - "line" = { - pskRaw = "ec36a5a116224c12e305c90b7f3c5a0b7417abfaacf15828748f6a1e1e316c03"; - }; - "MEHRA" = { - pskRaw = "e1573bc9e30fd68ac8a805b7c5f9871322ffcdac909746831555905c2e156abb"; - }; - "Sandeep1" = { - pskRaw = "035972401640bf4dc2d8651c0b9df515a69f6bc2bffa07cc936183128a6ccf0a"; - }; - "chadpad" = { - pskRaw = "2594b3880a60817950bd60b2770c9793fd4cb784ed183f685a52502a9df2c7b1"; - }; - # "updog" = { - # pskRaw = "be28d9e50e4d9a065f1afccad52675d0d4fabe85526c0245648671721db606b1"; - # }; - "Vodafone-07F38" = { - pskRaw = "4ffd87de73ed31ef1aee1d0d178857490afbda3f0bb8453a0baaee7b2576f302"; - }; - }; + networking.networkmanager.enable = true; + + # networking.useDHCP = false; + # networking.interfaces.enp0s31f6.useDHCP = true; + # networking.interfaces.wlp3s0.useDHCP = true; + # networking.wireless.enable = true; + # networking.wireless.interfaces = [ "wlp3s0" ]; + # networking.wireless.userControlled.enable = true; + + # networking.wireless.userControlled.enable = true; + # networking.wireless.networks = { + # "Chester 11403" = { + # pskRaw = "43fcb0bea43633899a9885865c53ea79d268b9bdfb8c3f4013718e43e6672e5e"; + # }; + # "HSBNEWiFi" = { + # pskRaw = "0820d84980eeb47630f13f04804fc9add684a8feebb64ba914cafc48f569d801"; + # }; + # "TP-LINK_1D79" = { + # pskRaw = "6d960910c33a59e94b151281cc3982863b4b112d8a4efd1b165e4f8e52e7dae8"; + # }; + # "nadir" = { + # pskRaw = "92e0c1f0b3a1bada333964f0ee4ac04e0d9ba941aa3f29216cc3782595a41e5f"; + # }; + # "WiFi-399631" = { + # pskRaw = "2e312c9721e470847f751d8b844d264dc5e2612361a579fc0bdebd2135b5a8ea"; + # }; + # "line" = { + # pskRaw = "ec36a5a116224c12e305c90b7f3c5a0b7417abfaacf15828748f6a1e1e316c03"; + # }; + # "MEHRA" = { + # pskRaw = "e1573bc9e30fd68ac8a805b7c5f9871322ffcdac909746831555905c2e156abb"; + # }; + # "Sandeep1" = { + # pskRaw = "035972401640bf4dc2d8651c0b9df515a69f6bc2bffa07cc936183128a6ccf0a"; + # }; + # "chadpad" = { + # pskRaw = "2594b3880a60817950bd60b2770c9793fd4cb784ed183f685a52502a9df2c7b1"; + # }; + # # "updog" = { + # # pskRaw = "be28d9e50e4d9a065f1afccad52675d0d4fabe85526c0245648671721db606b1"; + # # }; + # "Vodafone-07F38" = { + # pskRaw = "4ffd87de73ed31ef1aee1d0d178857490afbda3f0bb8453a0baaee7b2576f302"; + # }; + # }; } diff --git a/hosts/users/anish/default.nix b/hosts/users/anish/default.nix index 26cbc741..3e9f15c9 100644 --- a/hosts/users/anish/default.nix +++ b/hosts/users/anish/default.nix @@ -6,7 +6,7 @@ hashedPassword = "$y$j9T$y3DPt/dWaPE.gRazQqw1w0$1RMH5sl/Nu8kW3ZMywYCPRniHD/jF5qRh0VKrdJ.bV2"; shell = pkgs.zsh; isNormalUser = true; - extraGroups = [ "wheel" "audio" ]; + extraGroups = [ "wheel" "audio" "networkmanager" ]; openssh.authorizedKeys.keys = [ # Curve "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDM0Zvei46x/yZl/IeBCq6+IYQQ0avulzVyBysF9cPigZMCybWRV7IEU+E3k9t6JrbdbdGfJkcZIWmsWDdKS8W8mBnZpVoT0ffLynu8JQ/TKdGm4Qv6bgUeKNrGsNv0ZPs2CDaGSLj0oJfRF7Ko10tcLP0vW+yujrh+y6TH/vVzJioaV4TGvtCUpn+wEQah9ROwPQLUUofsSWdnRsDJ/gp37zXWs4l5wyjSKtP3O9RZUP7kBekbSqEgSXiTk0oUQSVqIWl9NDiP6onk/gSOjXsR/JPqsSN/XI/c/yj6gyY0f51Ru2D7iBxuMJIJcWV+rU6coIj+ULcQWLzt/7TI8jq5AOOzI/ll4zbL24Eo84Rz+TP9tvMMhDZ0VaMN22AJ8qQEjc5P09tWKsX7Jg39XelyV1jHXncE4yvIE9F4RSCHzWCeKeXakizQNuzSaxTxIExRFYHjNW5bR6+3MTGwVrEIXU+qML+0yFTR86MT+tdY5AreAJQLwbog79O1NupeXJE= anish@curve"