Add script to reset thunderbolt dock

This commit is contained in:
Fabian Hauser 2023-01-21 19:08:05 +01:00
parent 8ab6dd5772
commit 6ea4ac9fba

View file

@ -1,4 +1,39 @@
{ config, lib, pkgs, modulesPath, ... }: { { config, lib, pkgs, modulesPath, ... }:
let
thunderboltDevices = ''
THUNDERBOLT_DEVICES="$(${pkgs.pciutils}/bin/lspci -D | ${pkgs.gnugrep}/bin/grep -i thunderbolt | cut --delimiter=' ' --fields=1)"
'';
forceThunderboltOnScript = pkgs.writeScriptBin "force-thunderbolt-power-on" ''
#!${pkgs.stdenv.shell}
${thunderboltDevices}
echo "Force PCI power on all thunderbolt devices"
for DEVICE in $THUNDERBOLT_DEVICES; do
echo 'on' > "/sys/bus/pci/devices/$DEVICE/power/control"
done
'';
thunderboltDockRestart = pkgs.writeScriptBin "thunderbolt-dock-restart" ''
#!${pkgs.stdenv.shell}
${thunderboltDevices}
echo "Force PCI remove on all thunderbolt devices"
for DEVICE in $THUNDERBOLT_DEVICES; do
echo 1 > /sys/bus/pci/devices/0000\:$DEVICE/remove
echo "Dropped device $DEVICE"
done
echo 'Please re-plug the dock and press enter to continue [enter]'
read
echo 'Rescanning PCI devices...'
echo 1 > /sys/bus/pci/rescan
echo 'Done.'
'';
in {
imports = [ (modulesPath + "/installer/scan/not-detected.nix") ]; imports = [ (modulesPath + "/installer/scan/not-detected.nix") ];
boot.initrd.availableKernelModules = [ boot.initrd.availableKernelModules = [
@ -16,23 +51,11 @@
boot.extraModulePackages = [ ]; boot.extraModulePackages = [ ];
boot.kernelParams = [ "acpi_enforce_resources=lax" ]; boot.kernelParams = [ "acpi_enforce_resources=lax" ];
environment.systemPackages = [ thunderboltDockRestart ];
hardware.cpu.amd.updateMicrocode = true; hardware.cpu.amd.updateMicrocode = true;
nix.settings.max-jobs = lib.mkDefault 24; nix.settings.max-jobs = lib.mkDefault 24;
powerManagement.powerUpCommands = let powerManagement.powerUpCommands =
forceThunderboltOnScript = "${forceThunderboltOnScript}/bin/force-thunderbolt-power-on";
pkgs.writeScriptBin "force-thunderbolt-power-on" ''
#!${pkgs.stdenv.shell}
#echo 'on' > '/sys/bus/pci/devices/0000:03:00.0/power/control'; #TODO: Is the main controller required?
THUNDERBOLT_DEVICES="$(${pkgs.pciutils}/bin/lspci -D | ${pkgs.gnugrep}/bin/grep -i thunderbolt | cut --delimiter=' ' --fields=1)"
echo "Force PCI power on all thunderbolt devices"
for DEVICE in $THUNDERBOLT_DEVICES; do
echo 'on' > "/sys/bus/pci/devices/$DEVICE/power/control"
done
'';
in "${forceThunderboltOnScript}/bin/force-thunderbolt-power-on";
} }