Issue with Moving Nvidia Configuration to a Separate Module in NixOS: Unsupported Attribute Error

Hi everyone,

I’m starting to dive deep into NixOS but I’m still new to this. I’ve begun exploring how to modularize my configuration.nix. I decided to start by moving my Nvidia configuration out of configuration.nix and into a separate module located in ./module/nvidia_hybrid_laptop.nix.

Here’s the content of that module:

{lib, config, pkgs, ...}:
let
nvidia-offload = pkgs.writeShellScriptBin "nvidia-offload" ''
export __NV_PRIME_RENDER_OFFLOAD=1
export __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0
export __GLX_VENDOR_LIBRARY_NAME=nvidia
export __VK_LAYER_NV_optimus=NVIDIA_only
exec "$@"
'';
in {
	options = {
		nvidiaHyb.enable = lib.mkEnableOption "Enable Nvidia config";
    };
	config = lib.mkIf config.nvidiaHyb.enable{

		# Import Nvidia drivers for the X server
		services.xserver.videoDrivers = ["nvidia"];
		hardware.nvidia = {
			modesetting.enable = true;
			powerManagement.enable = true;
			powerManagement.finegrained = false;
			open = false;
			nvidiaSettings = true;
			package = config.boot.kernelPackages.nvidiaPackages.stable;
		};
		hardware.nvidia.prime = {
			offload = {
				enable = true;
				enableOffloadCmd = true;
			};
			intelBusId = "PCI:0:2:0";
			nvidiaBusId = "PCI:1:0:0";
		};
	};
	
	# Define Nvidia driver packages
	nvidiaHyb.nvidiaDrivers = [pkgs.vaapiVdpau pkgs.nvidia-vaapi-driver];
}

In my configuration.nix, I import the module and enable it:

imports = [
  ./modules/nvidia_hybrid_laptop.nix
  ./hardware-configuration.nix
];

# Bootloader.
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;

networking.hostName = "nixos";

# Enabling NVIDIA Config with Offload (Hybrid Acer Nitro 5)
nvidiaHyb.enable = true;

Then, I try to configure openGL like this to use the drivers from my Nvidia module:

hardware.opengl = {
	enable = true;
	driSupport = true;
	driSupport32Bit = true;
        #Here is the problem!!!!
	extraPackages = with pkgs; lib.concatLists [nvidiaHyb.nvidiaDrivers [intel-media-driver]];
};

However, I’m encountering the following error:

error: Module `/etc/nixos/modules/nvidia_hybrid_laptop.nix' has an unsupported attribute `nvidiaHyb'. This is caused by introducing a top-level `config' or `options' attribute. Add configuration attributes immediately on the top level instead, or move all of them (namely: nvidiaHyb) into the explicit `config' attribute.

I’ve followed some examples I found, but I’m clearly missing something regarding how to properly structure this module. Any guidance on how to fix this issue or best practices for modularizing configurations would be greatly appreciated!

There are a couple of problems here.

  1. nvidia_hybrid_laptop.nix: if you want to use the nvidiaHyb.nvidiaDrivers this way, it cannot be a top-level attribute on the same level as options and config (that’s what the error is about). You can either:

    a. Define it as an option with list of packages type and use the specified values as default in the option
    b. Define it as an option with list of packages type and specify the values inside the .config

  2. This line probably does not work as you expect:

    extraPackages = with pkgs; lib.concatLists [nvidiaHyb.nvidiaDrivers [intel-media-driver]];

    If you want to reference the value of nvidiaHyb.nvidiaDrivers, you need to refer to it from config:

    extraPackages = with pkgs; lib.concatLists [config.nvidiaHyb.nvidiaDrivers [intel-media-driver]];

    and ensure config is in the arguments to that module (at the top of the file)

1 Like

Ok, thanks for your answer separated into topics, I’ll try the second one since I didn’t understand topic 1 very well lol.

Oh my god, I’m so stupid haha. I accessed the link they provided me on reddit (Welcome to the NixOS documentation! — NixOS Manual documentation). In configuration/modularization there was a text stating the following:

“Note that both configuration.nix and kde.nix define the option environment.systemPackages. When multiple modules define an option, NixOS will try to merge the definitions. In the case of environment.systemPackages, that’s easy: the lists of packages can simply be concatenated. , it will appear at the end of the merged list.”

So I decided to test and changed my module to

hardware.opengl.extraPackages = with pkgs; [vaapiVdpau nvidia-vaapi-driver];

and it worked, when I rebuild it concatenates the two opengl extraPackages.