Firefox Hardware Acceleration Nvidia

Hi, NixOS newbie here, I’m trying to set up HW acceleration using nvidia-vaapi-driver but I’m having some issues.

Here are my configs:
configuration.nix

 { config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
      ./nvidia.nix
    ];
  
  # Ntfs support
  boot.supportedFilesystems = [ "ntfs" ];

  # Install firefox.
  programs.firefox.enable = true;

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

  # Flatpaks
  services.flatpak.enable = true;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [

    vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
    ffmpeg
    fastfetch
    ntfs3g
    flatpak
    nvidia-vaapi-driver
    vaapiVdpau
    libvdpau-va-gl
    libva
    libva-utils
    gnome.gnome-tweaks
    btop
  #  wget
  ];

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "24.05"; # Did you read the comment?

}

nvidia.nix:

{ config, lib, pkgs, ... }: 

{
 # Enable OpenGL
  hardware.opengl = {
    enable = true;
  };

  services.xserver.videoDrivers = ["nvidia"];

  hardware.nvidia = {
    package = config.boot.kernelPackages.nvidiaPackages.mkDriver {
      version = "560.35.03";
      sha256_64bit = "sha256-8pMskvrdQ8WyNBvkU/xPc/CtcYXCa7ekP73oGuKfH+M=";
      sha256_aarch64 = "sha256-s8ZAVKvRNXpjxRYqM3E5oss5FdqW+tv1qQC2pDjfG+s=";
      openSha256 = "sha256-/32Zf0dKrofTmPZ3Ratw4vDM7B+OgpC4p7s+RHUjCrg=";
      settingsSha256 = "sha256-kQsvDgnxis9ANFmwIwB7HX5MkIAcpEEAHc8IBOLdXvk=";
      persistencedSha256 = "sha256-E2J2wYYyRu7Kc3MMZz/8ZIemcZg68rkzvqEwFAL3fFs=";
    };

    # The nvidia-settings build is currently broken due to a missing
    # vulkan header; re-enable whenever
    # 0384602eac8bc57add3227688ec242667df3ffe3the hits stable.
    nvidiaSettings = false;

    modesetting.enable = true;
    powerManagement.enable = true;
    open = true;
  };

  environment.variables = {
    GBM_BACKEND = "nvidia-drm";
    __GLX_VENDOR_LIBRARY_NAME = "nvidia";
    MOZ_DISABLE_RDD_SANDBOX= "1" ;
    LIBVA_DRIVER_NAME = "nvidia";
  };
}

and when I run the command vainfo I get this error:

Trying display: wayland
libva info: VA-API version 1.21.0
libva info: User environment variable requested driver 'nvidia'
libva info: Trying to open /run/opengl-driver/lib/dri/nvidia_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva error: /run/opengl-driver/lib/dri/nvidia_drv_video.so init failed
libva info: va_openDriver() returns 1
vaInitialize failed with error code 1 (operation failed),exit

I don’t know if this helps but if I run the command journalctl -xe | grep nvidia the result is this:

set 11 20:54:35 nixos org.gnome.Shell.desktop[1583]: DRM kernel driver 'nvidia-drm' in use. NVK requires nouveau.
set 11 20:54:45 nixos .gnome-shell-wr[1927]: Added device '/dev/dri/card1' (nvidia-drm) using atomic mode setting.
set 11 20:54:48 nixos gnome-shell[2513]: DRM kernel driver 'nvidia-drm' in use. NVK requires nouveau.

I’ve set all the environment variables needed and also modified the about:config settings in firefox, what am I missing? Thx in advance.

I wonder if it works if you add nvidia-vaapi-driver to hardware.graphics.extraPackages

Yup, that should do the trick. I wonder if it’d make sense to introduce a programs.nvidia-vaapi-driver NixOS option that sets the required Firefox options and environment variables, and adds the package to hardware.graphics.extraPackages.

Edit: or to add it as an option to the existing hardware.nvidia module

Edit 2: hm, I never noticed, but this is already added to hardware.graphics.extraPackages by the nvidia module: nixpkgs/nixos/modules/hardware/video/nvidia.nix at 1355a0cbfeac61d785b7183c0caaec1f97361b43 · NixOS/nixpkgs · GitHub

If I add this: hardware.graphics.extraPackages = with pkgs; [ nvidia-vaapi-driver ];
to my nvidia.nix file, I get this error while rebuilding:

 error: The option `hardware.nvidia.hardware' does not exist. Definition values:
       - In `/etc/nixos/nvidia.nix':
           {
             graphics = {
               extraPackages = [
                 <derivation nvidia-vaapi-driver-0.0.12>
               ];

I tried changing hardware.graphics.extraPackages with hardware.opengl.extraPackage but I get the same error.

Sorry if I am missing something, is this what you meant?

Because you need to put it at the top-level not nested within hardware.nvidia.

It isn’t?

{ config, lib, pkgs, ... }: 

{
 # Enable OpenGL
  hardware.opengl = {
    enable = true;
  };
  
  hardware.graphics.extraPackages = with pkgs; [
    nvidia-vaapi-driver
  ];

  services.xserver.videoDrivers = ["nvidia"];

  boot.blacklistedKernelModules = [ "nouveau" ];

  hardware.nvidia = {
    package = config.boot.kernelPackages.nvidiaPackages.mkDriver {
      version = "560.35.03";
      sha256_64bit = "sha256-8pMskvrdQ8WyNBvkU/xPc/CtcYXCa7ekP73oGuKfH+M=";
      sha256_aarch64 = "sha256-s8ZAVKvRNXpjxRYqM3E5oss5FdqW+tv1qQC2pDjfG+s=";
      openSha256 = "sha256-/32Zf0dKrofTmPZ3Ratw4vDM7B+OgpC4p7s+RHUjCrg=";
      settingsSha256 = "sha256-kQsvDgnxis9ANFmwIwB7HX5MkIAcpEEAHc8IBOLdXvk=";
      persistencedSha256 = "sha256-E2J2wYYyRu7Kc3MMZz/8ZIemcZg68rkzvqEwFAL3fFs=";
    };

That’s not the entire file, nor do I know how you’ve integrated that file into your config. But if you didn’t have it nested you wouldn’t be getting that error.

If you have this set, it means you’re using NixOS 24.05, in which case the option’s name is hardware.opengl.extraPackages, so this should work:

  hardware.opengl = {
    enable = true;
    extraPackages = with pkgs; [
      nvidia-vaapi-driver
    ];
  };