Skip to content

Instantly share code, notes, and snippets.

@indivisible
Created August 5, 2018 20:10
Show Gist options
  • Save indivisible/b917fefb2461acdbec13abe08f8e9345 to your computer and use it in GitHub Desktop.
Save indivisible/b917fefb2461acdbec13abe08f8e9345 to your computer and use it in GitHub Desktop.
#!/bin/sh
# if systemd is used and you need sound, login to $console prior https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=747882
# if you can't stand cursor bug, start this script from console, not another X https://bugs.launchpad.net/ubuntu/+source/plasma-workspace/+bug/1684240
# for many games you need running window manager, in that case run script which runs both wm and game via this script
display=1
console=3
tmpdir=$XDG_RUNTIME_DIR/nvidia
busid=$(/usr/bin/lspci | awk '/(3D)|(VGA compatible) controller: NVIDIA/{gsub(/\./,":",$1); print $1}')
kernel="$(uname -r)"
mkdir -p $tmpdir/modules
cat > $tmpdir/xorg.conf << EOF
Section "Files"
ModulePath "/usr/lib/xorg/modules/input"
ModulePath "/usr/lib/xorg/modules"
ModulePath "$tmpdir/modules"
EndSection
Section "InputClass"
Identifier "libinput pointer catchall"
MatchIsPointer "on"
MatchDevicePath "/dev/input/event*"
Driver "libinput"
EndSection
Section "InputClass"
Identifier "libinput keyboard catchall"
MatchIsKeyboard "on"
MatchDevicePath "/dev/input/event*"
Driver "libinput"
EndSection
Section "InputClass"
Identifier "libinput touchpad catchall"
MatchIsTouchpad "on"
MatchDevicePath "/dev/input/event*"
Driver "libinput"
Option "Tapping" "on"
Option "DisableWhileTyping" "off"
Option "MiddleEmulation" "on"
EndSection
Section "Module"
Load "modesetting"
EndSection
Section "Device"
Identifier "nvidia"
Driver "nvidia"
BusID "$busid"
Option "AllowEmptyInitialConfiguration"
EndSection
Section "Screen"
Identifier "nvidia"
Device "nvidia"
EndSection
EOF
red='\033[0;31m'
if [ ! -d $tmpdir ]; then
echo -e "$red Given tmpdir doesn't exist"
exit 1
fi
if [ "$1" == "" ]; then
echo -e "$red Give path to game as argument"
exit 1
fi
if [ "$busid" == "" ]; then
echo -e "$red No Optimus card found"
exit 1
fi
# module nvidia_drm is in use
if [ "$(sudo fgconsole)" == "$console" ]; then
echo -e "$red You need to run $0 from another console"
exit 1
fi
if [ -z "$XAUTHORITY" ]; then
export XAUTHORITY=$HOME/.Xauthority
fi
cat > $tmpdir/session << EOF
#!/bin/sh
/usr/bin/xrandr --setprovideroutputsource modesetting NVIDIA-0
/usr/bin/xrandr --auto
export LD_LIBRARY_PATH="/usr/lib64/nvidia/:/usr/lib32/nvidia:/usr/lib:${LD_LIBRARY_PATH}"
#export PATH="\$(nix-build --no-out-link -E '
# with import <nixpkgs> {};
#
# buildEnv {
# name = "nvidia-bin";
# paths = [ '$package'.settings '$package'.bin ];
# }
#')/bin:\$PATH"
export VK_ICD_FILENAMES="/usr/share/vulkan/icd.d/nvidia.json";
export OCL_ICD_VENDORS="/etc/OpenCL/vendors/nvidia.icd";
# https://bugzilla.gnome.org/show_bug.cgi?id=774775
export GDK_BACKEND=x11
$@
EOF
chmod +x $tmpdir/session
# switch user
cat > $tmpdir/wrapper << EOF
#!/bin/sh
sudo chown $USER $XAUTHORITY
sudo -u $USER $tmpdir/session
EOF
chmod +x $tmpdir/wrapper
# dixRegisterPrivateKey: Assertion `!global_keys[type].created' failed
#cat > $tmpdir/X << EOF
##!/bin/sh
##LD_LIBRARY_PATH="$(nix-build --no-out-link '<nixpkgs>' -A libglvnd)/lib"
#exec /usr/bin/X "\$@"
#EOF
#chmod +x $tmpdir/X
#sudo insmod $bbswitch/lib/modules/$kernel/misc/bbswitch.ko
sudo modprobe bbswitch
sudo tee /proc/acpi/bbswitch <<<ON
sudo modprobe ipmi_devintf
for m in nvidia nvidia-modeset nvidia-drm nvidia-uvm
do
#sudo insmod $nvidia/lib/modules/$kernel/misc/$m.ko
sudo modprobe $m
done
# https://bugs.freedesktop.org/show_bug.cgi?id=94577
ln -sf /usr/lib/xorg/modules/* $tmpdir/modules
rm $tmpdir/modules/libglamoregl.so
sudo startx $tmpdir/wrapper -- :$display -config $tmpdir/xorg.conf -logfile $tmpdir/X.log vt$console
sudo chown $USER $XAUTHORITY
for m in nvidia_uvm nvidia_drm nvidia_modeset nvidia
do
sudo rmmod $m
done
sudo tee /proc/acpi/bbswitch <<<OFF
@indivisible
Copy link
Author

Warning: this has a very good chance to crash your system. It might have new exciting bugs, but I will not fix them.

@dukzcry
Copy link

dukzcry commented Sep 6, 2018

@indivisible latest version has better arch compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment