I got my nvidia system to behave correctly. It seems there is a bug in the driver and or gnome-shell which has been open a long while and not been fixed, causing the shell to try and interact with the driver after it has been suspended, leading to a kernel panic.
It looks to me like this was first documented online here: Trouble suspending with 510.39.01, Linux 5.16.0: Freezing of tasks failed after 20.009 seconds - #12 by devyn.cairns - Linux - NVIDIA Developer Forums
And someone implemented this as a nix expression here: Suspend/resume cycling on system resume - #12 by blablablerg
I’ve now been able to suspend/resume tens of times without hitting the hang.
I also had an additional problem that my machine was immediately waking up due to some misbehaviour of the logitech keyboard dongle. That’s documented here How does Solaar affect sleep/suspend and wakeup? · Issue #2893 · pwr-Solaar/Solaar · GitHub and Suspend/resume cycling on system resume - #17 by pwaller.
My relevant nixos config is currently:
# Uncertain this is necessary but there are various reports it makes a difference.
boot.kernelParams = [ "nvidia.NVreg_TemporaryFilePath=/var/tmp" ];
boot.kernelPackages = pkgs.linuxPackages_latest;
boot.extraModulePackages = [ config.hardware.nvidia.package ];
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.latest;
hardware.nvidia.powerManagement.enable = true;
hardware.nvidia.open = true;
systemd = {
# Uncertain if this is still required or not.
services.systemd-suspend.environment.SYSTEMD_SLEEP_FREEZE_USER_SESSIONS = "false";
services."gnome-suspend" = {
description = "suspend gnome shell";
before = [
"systemd-suspend.service"
"systemd-hibernate.service"
"nvidia-suspend.service"
"nvidia-hibernate.service"
];
wantedBy = [
"systemd-suspend.service"
"systemd-hibernate.service"
];
serviceConfig = {
Type = "oneshot";
ExecStart = ''${pkgs.procps}/bin/pkill -f -STOP ${pkgs.gnome-shell}/bin/gnome-shell'';
};
};
services."gnome-resume" = {
description = "resume gnome shell";
after = [
"systemd-suspend.service"
"systemd-hibernate.service"
"nvidia-resume.service"
];
wantedBy = [
"systemd-suspend.service"
"systemd-hibernate.service"
];
serviceConfig = {
Type = "oneshot";
ExecStart = ''${pkgs.procps}/bin/pkill -f -CONT ${pkgs.gnome-shell}/bin/gnome-shell'';
};
};
};