{ pkgs, lib, config, ... }:
let cfg = config.mods.unfree.nvidia;
in {
  options.mods.unfree.nvidia = {
    enable = lib.mkEnableOption {
      default = false;
      description = "enables proprietary nvidia drivers";
    };
    packages = lib.mkOption {
      default = [ "nvidia-x11" "nvidia-settings" ];
      description = "unfree packages";
    };
    driver = lib.mkOption {
      default = "stable";
      description = "nvidiaPackages driver kernel package";
    };
  };

  config = lib.mkIf cfg.enable {
    # nixos.wiki/wiki/Nvidia
    # hardware.graphics = {
    #   enable = true;
    #   enable32Bit = true;
    # };

    services.xserver.videoDrivers = [ "nvidia" "nvidia-dkms" ];

    hardware.nvidia = {
      modesetting.enable = true;

      # Set in configuration.nix
      # powerManagement.enable = false;

      powerManagement.finegrained = false;

      open = lib.mkDefault false;
      nvidiaSettings = lib.mkDefault false;

      package = config.boot.kernelPackages.nvidiaPackages."${cfg.driver}";
    };
  };
}
