Screen Flickering and Tearing with NixOS + Sway + NVIDIA

Problem

Howdy! I’ve been struggling the past few days to get my NixOS+Sway+Docked Laptop setup to play nice with my Nvidia GPU. If I am only using the laptop, no problem. However, the second that I connect up my 2 external monitors, firefox, steam, and some other apps, start pitching a fit and having this weird screen-tearing/screen flickering issue. I’m a little stumped on where to even begin finding the issue much less how to fix it.

Config

My full config is here:

And the specific configuration I have done is split between my home file and my host file here: nix-dotfiles/users/john/sway.nix at 65b96c7568a0a80ed785e020a409356f7307ff34 · ProfDoof/nix-dotfiles · GitHub and here: nix-dotfiles/hosts/buford/configuration.nix at 65b96c7568a0a80ed785e020a409356f7307ff34 · ProfDoof/nix-dotfiles · GitHub

Side note, I’m fairly new to this so if anyone pokes through my config files and sees something nonsensical or bad practice, feel free to say something.

Hardware Info

$ sudo lshw -c display
  *-display
       product: nvidia-drmdrmfb
       physical id: 0
       bus info: pci@0000:01:00.0
       logical name: /dev/fb1
       version: a1
       width: 64 bits
       clock: 33MHz
       capabilities: pm msi pciexpress bus_master cap_list rom fb
       configuration: depth=32 driver=nvidia latency=0 resolution=1920,1080
       resources: irq:162 memory:dc000000-dcffffff memory:b0000000-bfffffff memory:c0000000-c1ffffff ioport:e000(size=128) memory:dd000000-dd07ffff
  *-display
       product: i915drmfb
       physical id: 2
       bus info: pci@0000:00:02.0
       logical name: /dev/fb0
       version: 04
       width: 64 bits
       clock: 33MHz
       capabilities: pciexpress msi pm bus_master cap_list rom fb
       configuration: depth=32 driver=i915 latency=0 resolution=1920,1080
       resources: irq:161 memory:db000000-dbffffff memory:70000000-7fffffff ioport:f000(size=64) memory:c0000-dffff
$ lspci | grep VGA
00:02.0 VGA compatible controller: Intel Corporation HD Graphics 630 (rev 04)
01:00.0 VGA compatible controller: NVIDIA Corporation GP104M [GeForce GTX 1070 Mobile] (rev a1)

This is a long-standing nvidia bug, caused by lack of support for “explicit sync” in sway (and initially the existence of said feature in wayland/X at all, it took a few years to get it implemented). Other GPUs don’t have this problem because nvidia don’t provide a normal mesa driver and didn’t want to implement “implicit sync” when a better option was right around the corner (read: a few years off).

Gnome/KDE support it by now, but sway’s taking a little longer. AIUI, these two issues are what’s left to get the flickering to stop:

Meanwhile, using the vulkan renderer might help a bit. Or make it worse. It’s a bit finnicky, and depends on driver versions…

1 Like

In addition, if you want explicit sync support at the driver level, you’ll want at least the version 555 driver. Your change from stable (555) to production (550) was actually a downgrade.

Well, looks like they just merged both of those a couple of days ago so we’ll see if that fixes my issues.

You mean yesterday! As in, last 48 hours or so! Exciting, let’s see when it hits nixpkgs.

I’m using the following overlay for the git versions of wlroots and sway. Flickering is gone. :beers:

3 Likes

Thanks for sharing! I am new to nix so I could not do it way you did it in your config, but I found a simple way of just adding it to my configuration.nix like this:

nixpkgs.overlays = [
  (
    final: prev: {
      sway-unwrapped = prev.sway-unwrapped.overrideAttrs (attrs: {
        version = "0-unstable-2024-08-11";
        src = final.fetchFromGitHub {
          owner = "swaywm";
          repo = "sway";
          rev = "b44015578a3d53cdd9436850202d4405696c1f52";
          hash = "sha256-gTsZWtvyEMMgR4vj7Ef+nb+wcXkwGivGfnhnBIfPHOA=";
        };
        buildInputs = attrs.buildInputs ++ [final.wlroots];
        mesonFlags = let
          inherit (lib.strings) mesonEnable mesonOption;
        in [
          (mesonOption "sd-bus-provider" "libsystemd")
          (mesonEnable "tray" attrs.trayEnabled)
        ];
      });
      wlroots = prev.wlroots.overrideAttrs (_attrs: {
        version = "0-unstable-2024-08-11";
        src = final.fetchFromGitLab {
          domain = "gitlab.freedesktop.org";
          owner = "wlroots";
          repo = "wlroots";
          rev = "6214144735b6b85fa1e191be3afe33d6bea0faee";
          hash = "sha256-nuG2xXLDFsGh23CnhaTtdOshCBN/yILqKCSmqJ53vgI=";
        };
      });
    }
  )
];

It did, indeed, fix flickering, but there were still some issues. Firefox kept crashing and kitty stopped even launching. Back on iGPU everything works fine. My hardware is RTX 3070 and Ryzen 5 7600.

If you do still want to use the updated version of wlroots and sway, you can use the nixpkgs-wayland repo as an overlay. They are using the updated versions as far as I can tell.

1 Like

I ended up giving up on using sway until I get rid of this machine and buy a new one and am just using the cosmic DE alpha instead. Because even though it’s jank (due to its alpha state), at least it runs correctly with NVIDIA.

My trusty 2070 started causing crashes (it frankly arrived with some issues back in 2020, I only ended up not returning it because of scummy Amazon resellers), so I switched to a 4060ti, and boy is the flickering much worse with that GPU. I’d assumed things would be mostly the same across GPUs, I appreciate why this bothers other user so much more.

I’ve since also tested the latest commits, and yeah, firefox crashes occasionally. Otherwise things are pretty stable with the 560.35.03 driver. Clearly YMMV depending on GPU.

Why would I buy nvidia again? Well, neither competitor has released a GPU that fits in my PC case in over 4 years… At least I have two models (ish) to test stuff with now.