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!