System randomly hangs

Hello, I had a problem that my system would randomly restart. I managed to fix that issue by updating BIOS, but now instead of restarting the system hangs and I need to forcibly power it down with power button. When I was debugging I found out that nixos documentation mentions my exact gpu (AMD Radeon RX 6600)

Currently on the latest kernel/mesa (currently 6.13 and 24.3.4 respectively), Vega integrated graphics (and other GPUs like the RX 6600[1]) will have a possibility to hang due to context-switching between Graphics and Compute.[2] There are currently two sets of patches to choose between stability or speed that can be applied: amdgpu-stable and amdgpu-testing.

There are some patches for different kernel packages, but because I am still really new to nixos and would really appreciate if anyone could explain how exacly it works and how to set it up with the latest kernel package: boot.kernelPackages = pkgs.linuxPackages_latest;

I thank everyone it advance

Hello
So it is rather easy. The let part defines the patch with the right version hash. The defined patch is than used in the boot.extramodulesPackages option.
There are two ways you could apply it.

  1. Put this part at the top of your config:
let
  amdgpu-kernel-module = pkgs.callPackage ./packages/amdgpu-kernel-module.nix {
    # Make sure the module targets the same kernel as your system is using.
    kernel = config.boot.kernelPackages.kernel;
  };
  # linuxPackages_latest 6.13 (or linuxPackages_zen 6.13)
  amdgpu-stability-patch = pkgs.fetchpatch {
    name = "amdgpu-stability-patch";
    url = "https://github.com/torvalds/linux/compare/ffd294d346d185b70e28b1a28abe367bbfe53c04...SeryogaBrigada:linux:4c55a12d64d769f925ef049dd6a92166f7841453.diff";
    hash = "sha256-q/gWUPmKHFBHp7V15BW4ixfUn1kaeJhgDs0okeOGG9c=";
  };
  /*
in
{ 
#after that put your config

than put this part to your boot.extraModules:

  boot.extraModulePackages = [
    (amdgpu-kernel-module.overrideAttrs (_: {
      patches = [
        amdgpu-stability-patch
      ];
    }))
  ];

The second option, which is in my opinion the better way, is to write the patch in a new file and import this in your config. Therfor create a new file in /etc/nixos called something like amdpatch.nix. In this file you put this whole workaround:

{ config, pkgs, ... }:
let
  amdgpu-kernel-module = pkgs.callPackage ./packages/amdgpu-kernel-module.nix {
    # Make sure the module targets the same kernel as your system is using.
    kernel = config.boot.kernelPackages.kernel;
  };
  # linuxPackages_latest 6.13 (or linuxPackages_zen 6.13)
  amdgpu-stability-patch = pkgs.fetchpatch {
    name = "amdgpu-stability-patch";
    url = "https://github.com/torvalds/linux/compare/ffd294d346d185b70e28b1a28abe367bbfe53c04...SeryogaBrigada:linux:4c55a12d64d769f925ef049dd6a92166f7841453.diff";
    hash = "sha256-q/gWUPmKHFBHp7V15BW4ixfUn1kaeJhgDs0okeOGG9c=";
  };
  /*
in
{
  # amdgpu instability with context switching between compute and graphics
  # https://bbs.archlinux.org/viewtopic.php?id=301798
  # side-effects: plymouth fails to show at boot, but does not interfere with booting
  boot.extraModulePackages = [
    (amdgpu-kernel-module.overrideAttrs (_: {
      patches = [
        amdgpu-stability-patch
      ];
    }))
  ];
}

After that you import this new file to your existing config with:

  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix #you should already have this
      ./amdpatch.nix
    ];

Thanks so much for your quick answer. I just have a few questions? Will this work even though I am using flakes and running kernel 6.15.0?

Well can you first try it with kernel 6.13?
When this works try it without the kernel patch but on kernel 6.14.8.
It seems that there is also a issue with kernel 6.15 that lets the amdgpu driver crash:
https://www.reddit.com/r/linux_gaming/comments/1kvnl52/is_kernel_615_causing_trouble_on_amd_gpus_for_you/

Thanks mate. You are a lifesaver. Just in case. If I were to try it with the 6.14.8, how would I change the amdpatch.nix?

No I meant that you try Kernel 6.14.8 but without the kernel patch and see if that works.
Maybe the problem is only with kernel 6.15.