feat: add bluetooth to waybar and fix battery max loading

This commit is contained in:
Dominik Polakovics Polakovics 2026-02-27 12:32:59 +01:00
parent 0801cb17aa
commit 539cb58493
5 changed files with 130 additions and 2 deletions

View file

@ -32,6 +32,7 @@ in
./modules/qdrant.nix
./modules/battery-brightness.nix
./modules/charge-control.nix
./modules/bluetooth-waybar.nix
./modules/suspend-fixes.nix
./cachix.nix

View file

@ -0,0 +1,106 @@
{ 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
];
}

View file

@ -11,7 +11,7 @@ let
'';
charge-limit-menu = pkgs.writeShellScriptBin "charge-limit-menu" ''
choice=$(printf "80%%\n100%%" | ${pkgs.wofi}/bin/wofi --dmenu --prompt "Charge limit")
choice=$(printf "80%%\n100%%" | ${pkgs.fzf}/bin/fzf --prompt="Charge limit > " --reverse --no-info)
case "$choice" in
"80%") /run/wrappers/bin/sudo ${charge-limit-apply}/bin/charge-limit-apply 80 ;;
"100%") /run/wrappers/bin/sudo ${charge-limit-apply}/bin/charge-limit-apply 100 ;;

View file

@ -24,6 +24,9 @@
"network",
"custom/left-arrow-light",
"custom/left-arrow-dark",
"custom/bluetooth",
"custom/left-arrow-light",
"custom/left-arrow-dark",
"pulseaudio",
"custom/left-arrow-light",
"custom/left-arrow-dark",
@ -109,11 +112,18 @@
"interval": 5,
"format": " {usage:2}%"
},
"custom/bluetooth": {
"exec": "waybar-bluetooth",
"return-type": "json",
"interval": 5,
"on-click": "bluetooth-toggle",
"on-click-right": "foot -a launcher -e bluetooth-connect-menu"
},
"custom/battery": {
"exec": "waybar-battery",
"return-type": "json",
"interval": 10,
"on-click": "charge-limit-menu"
"on-click": "foot -a launcher -e charge-limit-menu"
},
"disk": {
"interval": 5,

View file

@ -25,6 +25,7 @@ window#waybar {
#clock.2,
#clock.3,
#network,
#custom-bluetooth,
#pulseaudio,
#memory,
#cpu,
@ -53,6 +54,15 @@ window#waybar {
#network {
color: #8be9fd;
}
#custom-bluetooth {
color: #8be9fd;
}
#custom-bluetooth.off {
color: #6272a4;
}
#custom-bluetooth.connected {
color: #50fa7b;
}
#pulseaudio {
color: #6272a4;
}
@ -74,6 +84,7 @@ window#waybar {
}
#network,
#custom-bluetooth,
#pulseaudio,
#memory,
#cpu,