106 lines
3.4 KiB
Nix
106 lines
3.4 KiB
Nix
{ pkgs, ... }:
|
|
let
|
|
waybar-bluetooth = pkgs.writeShellScriptBin "waybar-bluetooth" ''
|
|
powered=$(${pkgs.bluez}/bin/bluetoothctl show | ${pkgs.gnugrep}/bin/grep "Powered:" | ${pkgs.gawk}/bin/awk '{print $2}')
|
|
if [ "$powered" = "yes" ]; then
|
|
connected_device=$(${pkgs.bluez}/bin/bluetoothctl devices Connected | head -1 | cut -d' ' -f3-)
|
|
if [ -n "$connected_device" ]; then
|
|
icon=""
|
|
text="$icon $connected_device"
|
|
class="connected"
|
|
tooltip="Connected: $connected_device"
|
|
else
|
|
icon=""
|
|
text="$icon"
|
|
class="on"
|
|
tooltip="Bluetooth on (no device connected)"
|
|
fi
|
|
else
|
|
icon=""
|
|
text="$icon"
|
|
class="off"
|
|
tooltip="Bluetooth off"
|
|
fi
|
|
|
|
${pkgs.jq}/bin/jq -cn \
|
|
--arg text "$text" \
|
|
--arg tooltip "$tooltip" \
|
|
--arg class "$class" \
|
|
'{text: $text, tooltip: $tooltip, class: $class}'
|
|
'';
|
|
|
|
bluetooth-toggle = pkgs.writeShellScriptBin "bluetooth-toggle" ''
|
|
powered=$(${pkgs.bluez}/bin/bluetoothctl show | ${pkgs.gnugrep}/bin/grep "Powered:" | ${pkgs.gawk}/bin/awk '{print $2}')
|
|
if [ "$powered" = "yes" ]; then
|
|
${pkgs.bluez}/bin/bluetoothctl power off
|
|
${pkgs.util-linux}/bin/rfkill block bluetooth
|
|
else
|
|
${pkgs.util-linux}/bin/rfkill unblock bluetooth
|
|
sleep 0.5
|
|
${pkgs.bluez}/bin/bluetoothctl power on
|
|
fi
|
|
'';
|
|
|
|
bluetooth-connect-menu = pkgs.writeShellScriptBin "bluetooth-connect-menu" ''
|
|
# Power on bluetooth if off
|
|
powered=$(${pkgs.bluez}/bin/bluetoothctl show | ${pkgs.gnugrep}/bin/grep "Powered:" | ${pkgs.gawk}/bin/awk '{print $2}')
|
|
if [ "$powered" != "yes" ]; then
|
|
${pkgs.util-linux}/bin/rfkill unblock bluetooth
|
|
sleep 0.5
|
|
${pkgs.bluez}/bin/bluetoothctl power on
|
|
sleep 1
|
|
fi
|
|
|
|
# Build list of paired devices with connection status
|
|
devices=""
|
|
while IFS= read -r line; do
|
|
mac=$(echo "$line" | ${pkgs.gawk}/bin/awk '{print $2}')
|
|
name=$(echo "$line" | cut -d' ' -f3-)
|
|
[ -z "$mac" ] && continue
|
|
|
|
# Check if connected
|
|
info=$(${pkgs.bluez}/bin/bluetoothctl info "$mac" 2>/dev/null)
|
|
connected=$(echo "$info" | ${pkgs.gnugrep}/bin/grep "Connected:" | ${pkgs.gawk}/bin/awk '{print $2}')
|
|
if [ "$connected" = "yes" ]; then
|
|
devices="''${devices}[connected] $name\n"
|
|
else
|
|
devices="''${devices}$name\n"
|
|
fi
|
|
done < <(${pkgs.bluez}/bin/bluetoothctl devices Paired)
|
|
|
|
devices="''${devices}Open Bluetooth Manager"
|
|
|
|
# Show fzf menu
|
|
choice=$(printf "%b" "$devices" | ${pkgs.fzf}/bin/fzf --prompt="Bluetooth > " --reverse --no-info)
|
|
[ -z "$choice" ] && exit 0
|
|
|
|
if [ "$choice" = "Open Bluetooth Manager" ]; then
|
|
${pkgs.blueman}/bin/blueman-manager &
|
|
exit 0
|
|
fi
|
|
|
|
# Extract device name (strip [connected] prefix if present)
|
|
device_name="''${choice#\[connected\] }"
|
|
|
|
# Find MAC address for the device
|
|
mac=$(${pkgs.bluez}/bin/bluetoothctl devices Paired | ${pkgs.gnugrep}/bin/grep "$device_name" | ${pkgs.gawk}/bin/awk '{print $2}')
|
|
[ -z "$mac" ] && exit 1
|
|
|
|
if [[ "$choice" == \[connected\]* ]]; then
|
|
echo "Disconnecting $device_name..."
|
|
${pkgs.bluez}/bin/bluetoothctl disconnect "$mac"
|
|
else
|
|
echo "Connecting to $device_name..."
|
|
${pkgs.bluez}/bin/bluetoothctl connect "$mac"
|
|
fi
|
|
|
|
sleep 1
|
|
'';
|
|
in
|
|
{
|
|
environment.systemPackages = [
|
|
waybar-bluetooth
|
|
bluetooth-toggle
|
|
bluetooth-connect-menu
|
|
];
|
|
}
|