From 55c5fdf1bc236d3158d813b145607f3833df4dd4 Mon Sep 17 00:00:00 2001 From: alexion Date: Fri, 4 Sep 2026 22:58:20 -0400 Subject: [PATCH] fix(nixos): improve waybar network display --- .../nixos/modules/desktop/session/waybar.nix | 54 ++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/configs/nixos/modules/desktop/session/waybar.nix b/configs/nixos/modules/desktop/session/waybar.nix index 3b00b7b..48d82b8 100644 --- a/configs/nixos/modules/desktop/session/waybar.nix +++ b/configs/nixos/modules/desktop/session/waybar.nix @@ -47,6 +47,38 @@ let printf '{"text":"","class":"idle"}\n' fi ''; + + networkStatus = pkgs.writeShellScript "waybar-network-status" '' + active=$( + ${pkgs.networkmanager}/bin/nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device status \ + | ${pkgs.gawk}/bin/awk -F: '$3 == "connected" && $2 != "loopback" { print; exit }' + ) + + if [ -z "$active" ]; then + ${pkgs.jq}/bin/jq -cn \ + --arg text "${g "f127"} off" \ + --arg tooltip "Network disconnected" \ + --arg class "disconnected" \ + '{ text: $text, tooltip: $tooltip, class: $class }' + exit 0 + fi + + IFS=: read -r device type _state connection <<< "$active" + + case "$type" in + wifi) icon="${g "f1eb"}" ;; + ethernet) icon="LAN" ;; + *) icon="NET" ;; + esac + + ipaddr=$(${pkgs.iproute2}/bin/ip -4 -o addr show dev "$device" | ${pkgs.gawk}/bin/awk '{ print $4; exit }') + + ${pkgs.jq}/bin/jq -cn \ + --arg text "$icon $connection" \ + --arg tooltip "$device: ''${ipaddr:-no IPv4}" \ + --arg class "$type" \ + '{ text: $text, tooltip: $tooltip, class: $class }' + ''; in { options.modules.desktop.session.waybar.enable = lib.mkEnableOption "the Waybar status bar"; @@ -63,10 +95,21 @@ in # so the bar comes up with the session and no compositor exec-once. systemd.enable = true; + style = '' + .modules-right { + margin-right: 8px; + } + + #custom-dnd { + margin-right: 8px; + } + ''; + settings.mainBar = { layer = "top"; position = "top"; height = 30; + spacing = 8; modules-left = [ "hyprland/workspaces" ]; modules-center = [ "clock" ]; @@ -75,7 +118,7 @@ in "custom/idle-lock" "mpris" "wireplumber" - "network" + "custom/network" "battery" "custom/dnd" ]; @@ -128,11 +171,10 @@ in on-click = "${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"; }; - network = { - format-wifi = "${g "f1eb"} {essid}"; - format-ethernet = "${g "f796"} {ifname}"; - format-disconnected = "${g "f127"} off"; - tooltip-format = "{ipaddr} via {gwaddr}"; + "custom/network" = { + return-type = "json"; + exec = "${networkStatus}"; + interval = 5; }; battery = {