{ config, lib, pkgs, util, ... }: # NetworkManager-managed networking. let networkStatus = pkgs.writeShellScript "waybar-network-status" '' active=$( ${lib.getExe' pkgs.networkmanager "nmcli"} -t -f DEVICE,TYPE,STATE,CONNECTION device status \ | ${lib.getExe' pkgs.gawk "awk"} -F: '$3 == "connected" && $2 != "loopback" { print; exit }' ) if [ -z "$active" ]; then ${lib.getExe pkgs.jq} -cn \ --arg text "${util.glyph "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="${util.glyph "f1eb"}" ;; ethernet) icon="LAN" ;; *) icon="NET" ;; esac ipaddr=$(${lib.getExe' pkgs.iproute2 "ip"} -4 -o addr show dev "$device" | ${lib.getExe' pkgs.gawk "awk"} '{ print $4; exit }') ${lib.getExe pkgs.jq} -cn \ --arg text "$icon $connection" \ --arg tooltip "$device: ''${ipaddr:-no IPv4}" \ --arg class "$type" \ '{ text: $text, tooltip: $tooltip, class: $class }' ''; in { options.modules.network.enable = lib.mkEnableOption "NetworkManager-managed networking"; config = lib.mkIf config.modules.network.enable { networking.networkmanager.enable = true; registry.waybar.${config.networking.hostName}."custom/network" = { order = 60; settings = { return-type = "json"; exec = "${networkStatus}"; interval = 5; }; }; }; }