Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rywng/3d8ce02f033c6bd9ce845f368a585aa8 to your computer and use it in GitHub Desktop.
Save rywng/3d8ce02f033c6bd9ce845f368a585aa8 to your computer and use it in GitHub Desktop.
[Tutorial] Minimalist SwayWM + Nvidia + Multi-monitors guide

[Tutorial] Minimalist Wlroots (SwayWM) + Nvidia Graphics + Multi-monitors Guide

I've done the same thing before on a binary distro, which is a great pain, however setting this up in Gentoo is surprisingly easy, because of its source-based nature.

Article also available here and there

This meta-guide focuses on using Nvidia dGPU, with its proprietary driver for display output, and assumes you have a working iGPU, which is present in most computers.

I'm using a laptop with both iGPU and dGPU output, so this guide will mainly focus on laptops, but desktops should theoretically work the same way.

This is mainly about patching wlroots, so anything based on wlroots should work, except for hyprland, which doesn't even need patching wlroots.

My system info and result:

Imgur

If you have a separate computer / android device with termux, you can set up sshd with static IP. This way when your output is broken, you always have ssh to resort to.

Verifying swayWM works with integrated GPU and install nvidia drivers

Plug your monitor to the motherboard instead of the port on GPU. For laptops, find out which port is connected to the iGPU, most of the time it's the port with thunderbolt.

This makes sure your iGPU is used by sway, and since most of them have open source firmware already in kernel, it should work OOTB.

Then continue with the Gentoo wiki on sway, set it up as you please, and verify it works.

Install nvidia-drivers, by following Gentoo wiki on nvidia-drivers.

FYI here's my use flags on make.conf, you'll at least need vdpau, nvidia, nvenc and wayland, run euse -i {flag} for more info.

USE="lto wayland vdpau nvidia nvenc flatpak pulseaudio pipewire \
     screencast gtk3 gles2 networkmanager vaapi v4l zsh-completion \
     appindicator threads cuda clang xxhash jemalloc tcmalloc"

If you only have one monitor or you are using laptop's built-in screen, you can just stop here.

Patch wlroots and swayWM

read this if you don't know about portage's patching function: https://wiki.gentoo.org/wiki//etc/portage/patches, We'll need them later

wlroots

This step is necessary to prevent flicker when monitor is connected to nvidia GPU. The patches originate from this MR which unfortunately got rejected because of slight performance cost (I can't feel it on my 75Hz monitor).

We are going to steal borrow patches from aur, click the link and click nvidia.patch in Sources (3) section.

Use https://aur.archlinux.org/cgit/aur.git/log/?h=wlroots-nvidia to find older versions of wlroot-nvidia patches

OR, you can use patches from my repo: https://github.com/rywng/sway-nvidia-patches, instructions included.

This patch from AUR is unfortunately not usable for portage, so you'll need to convert it to portage format, by following through This Gentoo wiki page (in section "Make Changes", apply the patch downloaded from AUR)

Apply the patch by putting the Gentoo format patch you obtained from aforementioned wiki page, run sudo emerge -at wlroots to re-merge it. Append this variable to /etc/environment:

WLR_NO_HARDWARE_CURSORS=1

This is the minimum requirement for a functioning sway desktop, hyprland wiki has more info on the env vars.

swayWM (optional)

If you reboot and start sway, it will warn you about the presence of Nvidia card, and ask you to use --unsupported-gpu. change Exec in /usr/share/wayland-sessions/sway.desktop to use the flag, and retry.

However, since it's in /usr folder, it will get overridden every re-merge, so you can instead patch swayWM to skip this check. The patch is also in my repo: https://github.com/rywng/sway-nvidia-patches

Plug your cable to Nvidia GPU

If you're on desktop, unplug the cable from motherboard and plug it to your Nvidia graphics card, it should work flawlessly.

For multi-monitor setup on laptops, you can read your laptop's manual to figure out which port uses discrete GPU, and you can connect your monitor to that port to get better gaming performance.

My configs

That should be it, FYI here are some relevant configs:

/etc/environment

#
# This file is parsed by pam_env module
#
# Syntax: simple "KEY=VAL" pairs on separate lines
#

# terminal
TERMINAL=/bin/kitty

# QT theming
QT_QPA_PLATFORMTHEME=qt5ct

# fcitx
XMODIFIERS="@im=fcitx"
QT_IM_MODULE=fcitx
GTK_IM_MODULE=fcitx
SDL_IM_MODULE=fcitx
# GLFW_IM_MODULE=fcitx # kitty

# vscode
GTK_USE_PORTAL=1

# zstd
ZSTD_NBTHREADS="0"

# sway & nvidia
WLR_NO_HARDWARE_CURSORS=1
# LIBVA_DRIVER_NAME=nvidia
# XDG_SESSION_TYPE=wayland
# GBM_BACKEND=nvidia-drm
# __GLX_VENDOR_LIBRARY_NAME=nvidia

/etc/portage/make.conf

# Global settings
# These warnings indicate likely runtime problems with LTO, so promote them
# to errors. If a package fails to build with these, LTO should not be used there.
WARNING_FLAGS="-Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing"

NOLTO_FLAGS="-O2 -pipe -march=raptorlake"
COMMON_FLAGS="${NOLTO_FLAGS} -flto=16 ${WARNING_FLAGS}"

RUSTFLAGS="-C target-cpu=raptorlake"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"
LDFLAGS="${COMMON_FLAGS} -fuse-ld=mold" # Use mold to speed up

MAKEOPTS="-j16 -l16"

CPU_FLAGS_X86="aes avx avx2 f16c fma3 mmx mmxext pclmul popcnt rdrand sha sse sse2 sse3 sse4_1 sse4_2 ssse3"
VIDEO_CARDS="nvidia intel"

USE="lto wayland vdpau nvidia nvenc flatpak pulseaudio pipewire \
     screencast gtk3 gles2 networkmanager vaapi v4l zsh-completion \
     appindicator threads cuda clang xxhash jemalloc tcmalloc"

ACCEPT_LICENSE="*"

# Portage settings
FEATURES="binpkg-request-signature"
EMERGE_DEFAULT_OPTS="--keep-going"

# Use expands
GRUB_PLATFORMS="efi-64"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment