Add Udev rules for the framwork realtek ethernet usb so when it's unplugged the wifi turns on and when it's plugged in the wifi turns off.

This commit is contained in:
Joseph Hanson 2024-07-30 14:38:36 -05:00
parent 7aae607601
commit 1e4e70bfa3
Signed by: jahanson
SSH key fingerprint: SHA256:vy6dKBECV522aPAwklFM3ReKAVB086rT3oWwiuiFG7o
3 changed files with 52 additions and 0 deletions

View file

@ -48,5 +48,6 @@
system.motd.networkInterfaces = [ "wlp1s0" ];
security._1password.enable = true;
system.fingerprint-reader-on-laptop-lid.enable = true;
framework_wifi_swap.enable = true;
};
}

View file

@ -12,6 +12,7 @@
./security.nix
./systempackages.nix
./time.nix
./wifi_swap
./zfs.nix
];
}

View file

@ -0,0 +1,50 @@
# turns off the wifi when the usb device 0bda:8156 is connected.
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.mySystem.framework_wifi_swap;
wifiSwap = pkgs.writeShellScriptBin "wifi_swap"
''
#! /usr/bin/env bash
# This script turns off the wifi and on when the usb device 0bda:8156 is connected or removed.
# It is useful when you want to use a wired connection instead of wifi.
# The script is run by udev when the usb device is connected.
# The script is located at /run/current-system/sw/bin/wifi_swap
# The udev rule is located at <nix-store>-extra-udev-rules/etc/udev/rules.d/99-local.rules
# The udev rule is:
# ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0bda", ATTR{idProduct}=="8156", RUN+="/run/current-system/sw/bin/wifi_swap"
# ACTION=="remove", SUBSYSTEM=="net", ENV{ID_USB_VENDOR_ID}=="0bda", ENV{ID_USB_MODEL_ID}=="8156", RUN+="/run/current-system/sw/bin/wifi_swap"
echo "wifi_swap ACTION: $ACTION" | systemd-cat -t wifi_swap
# Case or switch for $ACTION
case $ACTION in
add)
echo "Plugged in USB device 0bda:8156 (Realtek 2.5gbe). Turning Wi-Fi off." | systemd-cat -t wifi_swap
${pkgs.networkmanager.outPath}/bin/nmcli radio wifi off
;;
remove)
echo "unplugged in USB device 0bda:8156 (Realtek 2.5gbe) Turning Wi-Fi on." | systemd-cat -t wifi_swap
${pkgs.networkmanager.outPath}/bin/nmcli radio wifi on
;;
*)
echo "Uknown ACTION: $ACTION" | systemd-cat -t wifi_swap
;;
esac
'';
in
{
options.mySystem.framework_wifi_swap = {
enable = mkEnableOption "framework_wifi_swap" // { default = false; };
};
config = mkIf cfg.enable {
# Create bash script and add it to nix store
environment.systemPackages = [
wifiSwap
];
# Add udev rule to run script when usb device 0bda:8156 is connected or disconnected.
services.udev.extraRules = ''
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0bda", ATTR{idProduct}=="8156", RUN+="${wifiSwap.outPath}/bin/wifi_swap"
ACTION=="remove", SUBSYSTEM=="net", ENV{ID_USB_VENDOR_ID}=="0bda", ENV{ID_USB_MODEL_ID}=="8156", RUN+="${wifiSwap.outPath}/bin/wifi_swap"
'';
};
}