dot

vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks

43 lines1838 bytesBourne Again Shell (bash)executable
1#!/usr/bin/env bash
2# Network state as one line of JSON for the Eww control center. Wired wins over
3# Wi-Fi when both are up.
4ICON_WIRED=$'\U000F0200' # nf-md-ethernet
5ICON_WIFI=$'\U000F05A9' # nf-md-wifi
6ICON_OFF=$'\U000F0202' # nf-md-ethernet_off
7
8state() { # available on connected icon label
9 printf '{"available":%s,"on":%s,"connected":%s,"icon":"%s","label":"%s"}\n' "$@"
10}
11
12command -v nmcli >/dev/null 2>&1 || { state false false false "$ICON_OFF" "Unavailable"; exit 0; }
13networking=$(timeout 2 nmcli networking 2>/dev/null) || { state false false false "$ICON_OFF" "Unavailable"; exit 0; }
14[ "$networking" = enabled ] || { state true false false "$ICON_OFF" "Off"; exit 0; }
15
16# DEVICE:TYPE:STATE:CONNECTION
17devices=$(timeout 2 nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device 2>/dev/null)
18wired=$(grep -m1 -E '^[^:]+:ethernet:connected:' <<<"$devices")
19# eno1 is set up by systemd-networkd (hosts/desktop), so NetworkManager only
20# sees it as unmanaged; it counts as connected when the link is up.
21if [ -z "$wired" ]; then
22 while IFS= read -r line; do
23 [ "$(cat "/sys/class/net/${line%%:*}/operstate" 2>/dev/null)" = up ] && { wired=$line; break; }
24 done < <(grep -E '^[^:]+:ethernet:unmanaged:' <<<"$devices")
25fi
26wifi=$(grep -m1 -E '^[^:]+:wifi:connected:' <<<"$devices")
27
28if [ -n "$wired" ]; then
29 dev=${wired%%:*}
30 speed=$(cat "/sys/class/net/$dev/speed" 2>/dev/null)
31 label="Wired"
32 if [ "${speed:-0}" -ge 1000 ] 2>/dev/null; then
33 label+=" · $(awk "BEGIN { print $speed / 1000 }") Gb/s"
34 elif [ "${speed:-0}" -gt 0 ] 2>/dev/null; then
35 label+=" · $speed Mb/s"
36 fi
37 state true true true "$ICON_WIRED" "$label"
38elif [ -n "$wifi" ]; then
39 name=$(cut -d: -f4- <<<"$wifi" | tr -d '"\\')
40 state true true true "$ICON_WIFI" "${name:-Wi-Fi}"
41else
42 state true true false "$ICON_OFF" "Disconnected"
43fi