From 539cb58493b621705c94b739f1397dfa7a9444d1 Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Fri, 27 Feb 2026 12:32:59 +0100 Subject: [PATCH] feat: add bluetooth to waybar and fix battery max loading --- hosts/nb/configuration.nix | 1 + hosts/nb/modules/bluetooth-waybar.nix | 106 ++++++++++++++++++++++++++ hosts/nb/modules/charge-control.nix | 2 +- hosts/nb/modules/sway/waybar.conf | 12 ++- hosts/nb/modules/sway/waybar.css | 11 +++ 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 hosts/nb/modules/bluetooth-waybar.nix diff --git a/hosts/nb/configuration.nix b/hosts/nb/configuration.nix index da643ef..d0c41b8 100644 --- a/hosts/nb/configuration.nix +++ b/hosts/nb/configuration.nix @@ -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 diff --git a/hosts/nb/modules/bluetooth-waybar.nix b/hosts/nb/modules/bluetooth-waybar.nix new file mode 100644 index 0000000..919ae09 --- /dev/null +++ b/hosts/nb/modules/bluetooth-waybar.nix @@ -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 + ]; +} diff --git a/hosts/nb/modules/charge-control.nix b/hosts/nb/modules/charge-control.nix index f9045f3..0fd60fe 100644 --- a/hosts/nb/modules/charge-control.nix +++ b/hosts/nb/modules/charge-control.nix @@ -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 ;; diff --git a/hosts/nb/modules/sway/waybar.conf b/hosts/nb/modules/sway/waybar.conf index c43e39f..eff3d4e 100644 --- a/hosts/nb/modules/sway/waybar.conf +++ b/hosts/nb/modules/sway/waybar.conf @@ -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, diff --git a/hosts/nb/modules/sway/waybar.css b/hosts/nb/modules/sway/waybar.css index 490ae18..192545b 100644 --- a/hosts/nb/modules/sway/waybar.css +++ b/hosts/nb/modules/sway/waybar.css @@ -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,