{ pkgs, lib, config, ... }: with lib; {
  options.global.jovian = mkEnableOption "set up Jovian NixOS" // { default = true; };

  config = mkIf config.global.jovian {
    jovian = {
      hardware.has.amd.gpu = true;
      steamos.useSteamOSConfig = true;
      steamos.enableDefaultCmdlineConfig = false;

      steam = {
        enable = true;
        autoStart = true;
        desktopSession = "plasma";
        user = "app";
      };
    };

    users.home.persistApp.directories = [
      ".steam" ".local/share/Steam"
      ".config/gamescope" ".config/hhd"
      ".kodi"
    ];

    users.users.app.extraGroups = [ "networkmanager" "gamemode" ];
    programs.gamemode.enable = true;
    programs.regreet.enable = false;
    home.plasma.enable = true;
    home.plasma.extraConfig = {
      configFile = {
        kwinrc.Xwayland.Scale = 1.75;
        kscreenlockerrc.Daemon.Autolock = false;
      };
    };

    home-manager.users.app = {
      home.packages = with pkgs; [
        (pkgs.kodi-wayland.passthru.withPackages (kodiPkgs: with kodiPkgs; [
          joystick # keymap steam-controller
          #controller-topology-project
          libretro libretro-2048
          libretro-fuse libretro-genplus libretro-mgba
          libretro-nestopia libretro-snes9x
          jellycon
        ]))
      ];

      home.pointerCursor = {
        package = pkgs.steamdeck-hw-theme;
        name = "steam";
      };
    };

    services.handheld-daemon = {
      enable = true;
      user = "app";
    };

    jovian.decky-loader = {
      # ~/.steam/steam/.cef-enable-remote-debugging
      enable = true;
      user = "app";
      extraPackages = with pkgs; [
        curl unzip util-linux gnugrep procps pciutils kmod ryzenadj
      ];
      extraPythonPackages = pythonPackages: with pythonPackages; [
        hid pyyaml
      ];
      stateDir = "/nix/persist/decky";
    };

    boot.kernelParams = [
      "iomem=relaxed"
      "amd_pstate=passive"
    ];

    services.udev.extraRules = ''
      SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="${pkgs.ryzenadj}/bin/ryzenadj --max-performance"
    '';
  };
}
