Skip to content

Instantly share code, notes, and snippets.

@dcrdev
Last active December 28, 2021 22:24
Show Gist options
  • Save dcrdev/2ece086d73acf335d65ef4273e25b74d to your computer and use it in GitHub Desktop.
Save dcrdev/2ece086d73acf335d65ef4273e25b74d to your computer and use it in GitHub Desktop.
nvidia-switch
#!/bin/bash
# A simple wrapper around system76-power to ensure that nvidia mode is reflected
# in Xorg.
XORG_CONF=/etc/X11/xorg.conf.d/nvidia.conf
CURRENT_MODE=`system76-power graphics`
function elevate () {
if [[ $EUID -ne 0 ]];
then
echo "This script must run in privileged mode"
exec sudo /bin/bash "$0" "$1"
fi
}
function perform_reboot () {
read -p "Would you like to reboot now [y/N]: " rflag
if [[ "$rflag" =~ ^[y|Y].* ]]
then
reboot
fi
}
if [ "$1" == "nvidia" ]
then
elevate "$1"
if [ "$CURRENT_MODE" != "nvidia" ]
then
sed -i 's/Option "PrimaryGPU".*/Option "PrimaryGPU" "yes"/' "$XORG_CONF" && \
system76-power graphics "$1" >/dev/null && \
echo "Nvidia graphics mode enabled, this action requires a reboot"
perform_reboot
else
echo "Nvidia graphics mode is already enabled"
exit 1
fi
elif [ "$1" == "hybrid" ]
then
elevate "$1"
if [ "$CURRENT_MODE" != "$1" ]
then
sed -i 's/Option "PrimaryGPU".*/Option "PrimaryGPU" "no"/' "$XORG_CONF" && \
system76-power graphics "$1" >/dev/null && \
echo "Hybrid graphics mode enabled, this action requires a reboot"
perform_reboot
else
echo "Hybrid graphics mode is already enabled"
exit 1
fi
else
echo "You must specify either 'nvidia' or 'hybrid' mode"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment