Skip to content

Instantly share code, notes, and snippets.

@major-gnuisance
Last active May 30, 2024 03:34
Show Gist options
  • Save major-gnuisance/4fad491e37c5ac7efa72f9473d4e95bd to your computer and use it in GitHub Desktop.
Save major-gnuisance/4fad491e37c5ac7efa72f9473d4e95bd to your computer and use it in GitHub Desktop.
Installer for a workaround Steam shim.
#!/bin/bash
# exit on error or unset variables
set -ue
# configurable paths and filenames
readonly shim_name=steam-shim
readonly desktop_filename=steam.desktop
readonly applications_dir=/usr/share/applications
# array to store uninstallation commands
declare -a uninstall_commands
if [ "$UID" == 0 ]; then
# when running as root
readonly shim_install_dir=/usr/local/bin
else
# when running as user
readonly shim_install_dir="$HOME/.local/bin"
fi
# create work directory
tempdir="$(mktemp -d)"
readonly tempdir
# cleanup
function cleanup() {
cd
rm -r "$tempdir"
}
trap cleanup EXIT
# create shim script
cat >"$shim_name" <<-"EOF"
#!/bin/bash
# Shim to work around issues with Steam
declare -a steam_options
# Disable GPU acceleration in Steam's embedded browser when using
# non-default GPU, to avoid crashes.
# See: https://github.com/ValveSoftware/steam-for-linux/issues/9383
if [ -n "$DRI_PRIME" ] ; then
steam_options+=(-cef-disable-gpu)
fi
exec steam "${steam_options[@]}" "$@"
EOF
readonly shim_full_path="$shim_install_dir/$shim_name"
# install shim script
install -D --mode=755 --target-directory="$shim_install_dir" "$shim_name"
uninstall_commands+=("rm ${shim_full_path@Q}")
readonly shim_full_path_escaped="${shim_full_path//\//\\/}"
readonly desktop_file_source="$applications_dir/$desktop_filename"
# create altered version of desktop file
sed "s/^Exec=[^ ]*/Exec=$shim_full_path_escaped/" "$desktop_file_source" >"$desktop_filename"
# install altered version of desktop file
if [ "$UID" == 0 ]; then
install -D --mode=755 --target-directory="/usr/local/share/applications" "$desktop_filename"
uninstall_commands+=("rm /usr/local/share/applications/${desktop_filename@Q}"
"xdg-desktop-menu forceupdate")
else
xdg-desktop-menu install --novendor "$desktop_filename"
uninstall_commands+=("xdg-desktop-menu uninstall ${desktop_filename@Q}")
fi
# Print uninstallation instructions
echo "Installation successful."
function print_uninstallation_commands() {
echo "To uninstall, run the following commands:"
if [ "$UID" == 0 ]; then
printf "sudo %s\n" "${uninstall_commands[@]}"
else
printf "%s\n" "${uninstall_commands[@]}"
fi
}
print_uninstallation_commands
print_uninstallation_commands | sed 's/^/# /' >> "$shim_full_path"
echo "You may also find these instructions at the end of the shim script, found here:"
echo "${shim_full_path@Q}"
@major-gnuisance
Copy link
Author

major-gnuisance commented May 26, 2024

This script installs a fix for this issue: ValveSoftware/steam-for-linux#9383
Tested on Fedora 40 Workstation with Gnome 3 and with Steam installed using the system's package manager, not Flatpak.

How to use, for the careless

Copy and paste this line into a terminal and press enter:
(Protip: triple clicking on text selects whole lines at once. Middle click pastes your most recent text selection.)

bash <(wget -qO- https://gist.githubusercontent.com/major-gnuisance/4fad491e37c5ac7efa72f9473d4e95bd/raw/install-steam-workaround.sh)

Disclaimer: you shouldn't really execute scripts straight off the web like this if you care about security. See the next section for a more cautious approach.

How to use, for the cautious

  1. Download the script
wget https://gist.githubusercontent.com/major-gnuisance/4fad491e37c5ac7efa72f9473d4e95bd/raw/install-steam-workaround.sh
  1. Review the script using your favorite text editor
  2. Run the script
bash install-steam-workaround.sh

Notes

Your desktop environment might not reflect the change immediately.
If it doesn't seem to work, try logging out and back in.

The script does two main things:

  1. Install a shim shell script that launches Steam with the -cef-disable-gpu flag if DRI_PRIME is set
  2. Install a modified version of steam.desktop (overriding the one from your package manager) so that it uses the shim
  3. Print uninstallation commands for what it just did

If the script is run as a regular user, it will install just for that user.
If it's run as root, it will install for all users.

The effective locations for the files it installs are as follows.

When run as root:
/usr/local/bin/steam-shim
/usr/local/share/applications/steam.desktop

When run as a regular user:
~/.local/bin/steam-shim
~/.local/share/applications/steam.desktop

Critical assumptions:

  1. Steam's Desktop file is found at /usr/share/applications/steam.desktop
  2. DRI_PRIME being set means it's a multi-GPU system and a non-default GPU is being used, so the workaround should be applied

@major-gnuisance
Copy link
Author

Updated to support systems with proprietary Nvidia drivers. Hopefully. Waiting for reports that it works.

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