Can't get nvidia graphics card to run on an Amd CPU laptop

Hello,
I have an HP Victus with a Ryzen 7 5800H with an Nvidia 3050 and I cannot get my graphics card to work on it

I tried following Nixos Wiki but I opened to a tty, and when I tried starting KDE plasma it said that $DISPLAY is not set

here are the config files
harware-configuration.nix

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];

  boot.initrd.availableKernelModules = [ "nvme" "xhci_pci" "usb_storage" "sd_mod" "sdhci_pci" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-amd" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/a3d354f6-289e-499f-a77c-edc09066c9bd";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/AD64-A561";
      fsType = "vfat";
    };

  swapDevices = [ ];

  # Enables DHCP on each ethernet and wireless interface. In case of scripted networking
  # (the default) this is the recommended approach. When using systemd-networkd it's
  # still possible to use this option, but it's recommended to use it in conjunction
  # with explicit per-interface declarations with `networking.interfaces.<interface>.useDHCP`.
  networking.useDHCP = lib.mkDefault true;
  # networking.interfaces.eno1.useDHCP = lib.mkDefault true;
  # networking.interfaces.wlo1.useDHCP = lib.mkDefault true;

  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
  hardware.cpu.amd.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
}

nvidia.nix

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];

  boot.initrd.availableKernelModules = [ "nvme" "xhci_pci" "usb_storage" "sd_mod" "sdhci_pci" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-amd" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/a3d354f6-289e-499f-a77c-edc09066c9bd";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/AD64-A561";
      fsType = "vfat";
    };

  swapDevices = [ ];

  # Enables DHCP on each ethernet and wireless interface. In case of scripted networking
  # (the default) this is the recommended approach. When using systemd-networkd it's
  # still possible to use this option, but it's recommended to use it in conjunction
  # with explicit per-interface declarations with `networking.interfaces.<interface>.useDHCP`.
  networking.useDHCP = lib.mkDefault true;
  # networking.interfaces.eno1.useDHCP = lib.mkDefault true;
  # networking.interfaces.wlo1.useDHCP = lib.mkDefault true;

  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
  hardware.cpu.amd.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
}

configuration.nix

# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
      ./nvidia.nix
    ];

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

  hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;

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

  networking.hostName = "nixos"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Enable networking
  networking.networkmanager.enable = true;

  # Set your time zone.
  time.timeZone = "Asia/Kolkata";

  # Select internationalisation properties.
  i18n.defaultLocale = "en_IN";

  i18n.extraLocaleSettings = {
    LC_ADDRESS = "en_IN";
    LC_IDENTIFICATION = "en_IN";
    LC_MEASUREMENT = "en_IN";
    LC_MONETARY = "en_IN";
    LC_NAME = "en_IN";
    LC_NUMERIC = "en_IN";
    LC_PAPER = "en_IN";
    LC_TELEPHONE = "en_IN";
    LC_TIME = "en_IN";
  };

  # Enable the X11 windowing system.
  services.xserver.enable = true;

  # Enable the KDE Plasma Desktop Environment.
  services.xserver.displayManager.sddm.enable = true;
  services.xserver.desktopManager.plasma5.enable = true;

  # Configure keymap in X11
  services.xserver = {
    layout = "us";
    xkbVariant = "";
  };

  # Enable CUPS to print documents.
  services.printing.enable = true;

  services.flatpak.enable = true;
  xdg.portal.extraPortals = [ pkgs.xdg-desktop-portal-gtk ];

  # Enable Bluetooth
  hardware.bluetooth.enable = true;

  # Enable sound with pipewire.
  sound.enable = true;
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    # If you want to use JACK applications, uncomment this
    #jack.enable = true;

    # use the example session manager (no others are packaged yet so this is enabled by default,
    # no need to redefine it in your config for now)
    #media-session.enable = true;
  };

  # Enable touchpad support (enabled default in most desktopManager).
  # services.xserver.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.owly = {
    shell = pkgs.fish;
    isNormalUser = true;
    description = "Owly";
    extraGroups = [ "networkmanager" "wheel" ];
    packages = with pkgs; [
      caffeine-ng
      lshw
      gcc
      eww-wayland
      hyprland
      firefox
      open-dyslexic
      fira-code
      lato
      kate
      thunderbird
      bitwarden
      fish
      git
      kitty
      emacs
      steam
      spotify
      discord
      sioyek
      ranger
      rustup
      tor-browser-bundle-bin
      qbittorrent
      jq
      acpi
      inotify-tools
      bluez
      pavucontrol
      brightnessctl
      playerctl
      networkmanagerapplet
      imagemagick
      gjs
      wl-gammactl
      wlsunset
      wl-clipboard
      hyprpicker
      blueberry
      polkit_gnome
      flameshot
      obsidian
    ];


  };

  # Enable Hyprland
  programs.fish.enable = true;
  programs.hyprland.enable = true;
  xdg.portal.wlr.enable = true;

  fonts.fontDir.enable = true;
  fonts.fonts = with pkgs; [
    fira-code
    montserrat
    fira-code-symbols
    noto-fonts
    lato
    ubuntu_font_family
    socat
    open-dyslexic
  ];

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

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
    neovim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
    wget
    appimage-run
  ];

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  # services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # 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 = "23.05"; # Did you read the comment?
  
  nixpkgs.config.permittedInsecurePackages = [
    "electron-24.8.6"
  ];

  system.autoUpgrade.enable = true;
}

nix-info -m output

 - system: `"x86_64-linux"`
 - host os: `Linux 6.1.60, NixOS, 23.05 (Stoat), 23.05.4662.34bdaaf1f0b7`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.13.6`
 - channels(root): `"nixos-23.05"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

Hey there,
I think you are missing the services.xserver.videoDrivers = ["nvidia"]; option but maybe you just haven’t shown it because it seems like you have uploaded the hardware-configuration.nix twice

The nvidia.nix you posted looks exactly like hardware-configuration.nix to me. I can’t tell the difference on first glance. Maybe you made a mistake there?

That being said, is this a case of Nvidia Optimus for laptops? Because I am writing things from an older intel-basel laptop (i7 8th Gen) with an Nvidia MX150 dedicated GPU. And I have to use an offloading script to run things on the dedicated GPU. It works however.

My bad I had uploaded the wrong file for nvidia.nix that contains the other line

yes, my bad, I did put hardware-configuration.nix, I have posted a comment with the correct file

I am unable to edit the post actually, so here is my nvidia.nix

{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 {
    environment.systemPackages = [ nvidia-offload ];
    services.xserver.videoDrivers = [ "nvidia" ];

    hardware.nvidia = {
        modesetting.enable = true;

	powerManagement.enable = true;
	powerManagement.finegrained = false;
        
	open = false;

	nvidiaSettings = true;

	prime = {
	   offload = {
	       enable = true;
	       enableOffloadCmd = true;
	   };
	   amdgpuBusId = "PCI:0:6:0";
	   nvidiaBusId = "PCI:0:1:0";
        };
    };
}

try using the config for you in the hardware repo

I’m a bit uncertain at this point. But your machine works if you leave out the nvidia config alltogether, yes? So you can run KDE plasma without issues on the AMD gpu only?

@lovirent, I did not find a configuration for specifically may laptop, and I am not sure how I am supposed to pick something else


@APCodes Yep

Do you have the drivers running? is there an nvidia-smi output?