NVIDIA Driver doesn't work with optimus laptop(Geoforce MX 250)

Hi everyone, I am using a HP pavillion x360 intel laptop with integrated graphics card + Geoforce MX 250 card. Unfortunately the nvidia card does not seem to work at all. I have been able to find some stack traces with dmesg:

[    9.632711] nvidia: loading out-of-tree module taints kernel.
[    9.652647] nvidia 0000:02:00.0: Unable to change power state from D3cold to D0, device inaccessible
[    9.653024] NVRM: This is a 64-bit BAR mapped above 4GB by the system
               NVRM: BIOS or the Linux kernel, but the PCI bridge
               NVRM: immediately upstream of this GPU does not define
               NVRM: a matching prefetchable memory window.
[    9.653025] NVRM: This may be due to a known Linux kernel bug.  Please
               NVRM: see the README section on 64-bit BARs for additional
               NVRM: information.
[    9.653026] nvidia: probe of 0000:02:00.0 failed with error -1
[    9.653183] NVRM: The NVIDIA probe routine failed for 1 device(s).
[    9.653184] NVRM: None of the NVIDIA graphics adapters were initialized!
[    9.653184] [drm] Module unloaded

Some lines later:

[   10.232126] ------------[ cut here ]------------
[   10.232141] proc_dir_entry 'driver/nvidia' already registered
[   10.232147] WARNING: CPU: 2 PID: 589 at fs/proc/generic.c:376 proc_register+0x131/0x1c0
[   10.232153] Modules linked in: intel_ishtp processor_thermal_device intel_wmi_thunderbolt intel_lpss_pci(+) i2c_hid nvidia(O+) wmi_bmof processor_thermal_rfim intel_lpss hid processor_thermal_mbox idma64 mei_me
 8250_pci processor_thermal_rapl i2c_i801 tpm_crb virt_dma intel_rapl_common int3403_thermal thermal fan tpm_tis video mei battery mfd_core int340x_thermal_zone soc_button_array ac intel_soc_dts_iosf int3400_therm
al tpm_tis_core i2c_smbus intel_pch_thermal acpi_thermal_rel evdev hp_accel lis3lv02d input_leds intel_pmc_core intel_hid mac_hid sparse_keymap sch_fq_codel wmi pinctrl_cannonlake tiny_power_button wireless_hotkey
 acpi_tad acpi_pad button serio_raw uinput agpgart ctr loop cpufreq_powersave tun tap macvlan bridge stp llc drm kvm irqbypass backlight i2c_core fuse deflate configfs efi_pstore efivarfs tpm rng_core dmi_sysfs ip
_tables x_tables autofs4 ext4 crc32c_generic crc16 mbcache jbd2 ahci libahci xhci_pci xhci_pci_renesas xhci_hcd nvme libata atkbd libps2 vivaldi_fmap
[   10.232206]  sdhci_pci nvme_core cqhci usbcore sdhci led_class scsi_mod t10_pi crc32c_intel mmc_core rtc_cmos crc64_rocksoft crc64 crc_t10dif crct10dif_generic crct10dif_pclmul crct10dif_common usb_common scsi_
common i8042 serio dm_mod dax
[   10.232220] CPU: 2 PID: 589 Comm: (udev-worker) Tainted: G           O       6.1.29 #1-NixOS
[   10.232222] Hardware name: HP HP Pavilion x360 Convertible 14-dh0xxx/85C6, BIOS F.14 08/31/2020
[   10.232223] RIP: 0010:proc_register+0x131/0x1c0
[   10.232226] Code: e8 48 83 c4 08 5b 5d 41 5c 41 5d 41 5e 41 5f c3 cc cc cc cc 49 8b b5 a0 00 00 00 4c 89 fa 48 c7 c7 80 3b c9 94 e8 ef 16 cc ff <0f> 0b 48 c7 c7 e8 f3 b0 95 e8 31 96 5a 00 8b 45 5c 48 c7 c7 50 b
6
[   10.232228] RSP: 0018:ffffa5b50075fd38 EFLAGS: 00010286
[   10.232230] RAX: 0000000000000000 RBX: ffff904602e14e08 RCX: 0000000000000000
[   10.232232] RDX: 0000000000000003 RSI: ffffffff94c70e99 RDI: 00000000ffffffff
[   10.232233] RBP: ffff904601161e40 R08: 0000000000000000 R09: ffffa5b50075fbc0
[   10.232234] R10: 0000000000000003 R11: ffffffff95339e28 R12: 0000000000000006
[   10.232236] R13: ffff9046001de180 R14: 0000000000000006 R15: ffff904601161eec
[   10.232237] FS:  00007fc10c6a4c40(0000) GS:ffff904960680000(0000) knlGS:0000000000000000
[   10.232239] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   10.232240] CR2: 00007fc10b96f4fb CR3: 0000000100dda005 CR4: 00000000003706e0
[   10.232242] Call Trace:
[   10.232244]  <TASK>
[   10.232247]  nv_register_procfs+0x66/0x1f0 [nvidia]
[   10.232438] intel-lpss 0000:00:15.1: enabling device (0000 -> 0002)
[   10.232419]  nvidia_init_module+0x2b4/0x73e [nvidia]
[   10.232556]  ? nv_drm_init+0x14/0x14 [nvidia]
[   10.232692]  nvidia_frontend_init_module+0x4c/0x8ae [nvidia]
[   10.232851]  ? nv_drm_init+0x14/0x14 [nvidia]
[   10.232986]  do_one_initcall+0x56/0x220
[   10.232992]  do_init_module+0x4a/0x1f0
[   10.232996]  __do_sys_finit_module+0xac/0x120
[   10.233000]  do_syscall_64+0x37/0x90
[   10.233004]  entry_SYSCALL_64_after_hwframe+0x63/0xcd
[   10.233007] RIP: 0033:0x7fc10d0c1d8d
[   10.233017] Code: 5d c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 5b 70 0d 00 f7 d8 64 89 01 4
8
[   10.233019] RSP: 002b:00007ffe5f177bc8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[   10.233021] RAX: ffffffffffffffda RBX: 000055cf9a163900 RCX: 00007fc10d0c1d8d
[   10.233023] RDX: 0000000000000000 RSI: 00007fc10d5a2b0d RDI: 0000000000000006
[   10.233024] RBP: 00007fc10d5a2b0d R08: 0000000000000000 R09: 000055cf9a0a81d0
[   10.233025] R10: 0000000000000006 R11: 0000000000000246 R12: 0000000000020000
[   10.233026] R13: 0000000000000000 R14: 000055cf9a160ed0 R15: 0000000000000000
[   10.233029]  </TASK>
[   10.233030] ---[ end trace 0000000000000000 ]---
[   10.233032] NVRM: failed to register procfs!
[   10.233056] NVRM: This is a 64-bit BAR mapped above 4GB by the system
               NVRM: BIOS or the Linux kernel, but the PCI bridge
               NVRM: immediately upstream of this GPU does not define
NVRM: a matching prefetchable memory window.
[   10.233057] NVRM: This may be due to a known Linux kernel bug.  Please
               NVRM: see the README section on 64-bit BARs for additional
               NVRM: information.
[   10.233058] nvidia: probe of 0000:02:00.0 failed with error -1
[   10.233069] idma64 idma64.1: Found Intel integrated DMA 64-bit
[   10.233227] NVRM: The NVIDIA probe routine failed for 1 device(s).
[   10.233229] NVRM: None of the NVIDIA graphics adapters were initialized!

Any help is greatly appreciated. My config is at hosts/nixos/hardware-configuration.nix · master · binarydigitz01 / dotfiles · GitLab
EDIT:
I believe this line highlights the core issue:

[    9.652647] nvidia 0000:02:00.0: Unable to change power state from D3cold to D0, device inaccessible

I checked your config and noticed this:

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

Try adding services.xserver.videoDrivers = [ "intel" "nvidia" ]; to your configuration.nix, hopefully it’s enough to fix your nix :frog:

I got the information from here and here

Ah sorry, I haven’t updated that line in the repo yet as it doesn’t work, here is the updated line i have added:

  services.xserver.videoDrivers = [ "modesetting" "nvidia" ];
  hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable; ```