{
  lib,
  config,
  username,
  pkgs,
  ...
}:
let
  cfg = config.nvidia;
in
{
  options = {
    nvidia = {
      enable = lib.mkEnableOption "Enable nvidia in NixOS & home-manager";
    };
  };
  config = lib.mkIf cfg.enable {
    boot.kernelPackages = lib.mkForce pkgs.linuxPackages_xanmod_latest;
    boot.initrd.kernelModules = [
      "nvidia"
      "nvidia_modeset"
      "nvidia_uvm"
      "nvidia_drm"
    ];
    boot.blacklistedKernelModules = [ "nouveau" ];
    boot.kernelParams = [
      "nvidia_drm.fbdev=1"
      "nvidia-drm.modeset=1"
      "module_blacklist=nouveau"
    ];
    environment.systemPackages = with 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 -a "$0" "$@"
      '')
    ];
    hardware = {
      graphics.enable = true;
      nvidia = {
        modesetting.enable = true;
        powerManagement.enable = false;
        powerManagement.finegrained = false;
        open = true;
        nvidiaPersistenced = true;
        nvidiaSettings = true;
        package = lib.mkForce config.boot.kernelPackages.nvidiaPackages.latest;

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

    home-manager.users.${username} = { };
  };
}
