{
  config,
  lib,
  pkgs,
  ...
}:

let
  rpcs3_config_yaml = settingsFormat.generate "config.yml" config.me.rpcs3.config;
  settingsFormat = pkgs.formats.yaml { };
in
{
  imports = [ ];

  options.me = {
    rpcs3.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to install rpcs3.";
    };

    rpcs3.config = lib.mkOption rec {
      apply = lib.recursiveUpdate default;
      inherit (settingsFormat) type;
      default = {
        Core = {
          "Use LLVM CPU" = lib.mkIf (config.me.optimizations.enable) config.me.optimizations.arch;
        };
        VFS = {
          "Enable /host_root/" = false;
        };
        Video = {
          "Write Color Buffers" = true;
          VSync = true;
          "Performance Overlay" = {
            Enabled = false;
          };
        };
        Miscellaneous = {
          "Pause emulation on RPCS3 focus loss" = true;
          "Start games in fullscreen mode" = true;
          "Pause Emulation During Home Menu" = false; # true makes the home menu slow
        };
      };
      example = null;
      description = "RPCS3's config.yml in nix form.";
    };
  };

  config = lib.mkIf (config.me.rpcs3.enable && config.me.graphical) {
    environment.systemPackages = with pkgs; [
      rpcs3
    ];

    allowedUnfree = [ "rpcs3" ];

    security.pam.loginLimits = [
      {
        domain = "@wheel";
        item = "memlock";
        type = "hard";
        value = "unlimited";
      }
      {
        domain = "@wheel";
        item = "memlock";
        type = "soft";
        value = "unlimited";
      }
    ];

    me.install.user.talexander.file = {
      ".config/rpcs3/config.yml" = lib.mkIf (config.me.rpcs3.config != null) {
        source = rpcs3_config_yaml;
      };
      ".config/rpcs3/GuiConfigs/CurrentSettings.ini" = {
        source = ./files/CurrentSettings.ini;
      };
      ".config/rpcs3/custom_configs/config_BLUS30443.yml" = {
        # Demon's Souls per-game config.
        source = ./files/config_BLUS30443.yml;
      };
      ".config/rpcs3/patches/patch.yml" = {
        # All of the available patches.
        source = ./files/patch.yml;
      };
      ".config/rpcs3/patch_config.yml" = {
        # Patches that I have enabled.
        source = ./files/patch_config.yml;
      };
    };

    environment.persistence."/persist" = lib.mkIf (config.me.mountPersistence) {
      hideMounts = true;
      users.talexander = {
        directories = [
          {
            # Location of ROMs.
            directory = ".config/rpcs3/games";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/rpcs3/dev_hdd0";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/rpcs3/dev_hdd1";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/rpcs3/savestates";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/rpcs3/dev_usb000";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            # Seems to be where the firmware is installed.
            directory = ".config/rpcs3/dev_flash";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            # Controller config.
            directory = ".config/rpcs3/input_configs";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            # Game icons.
            directory = ".config/rpcs3/Icons";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
        ];
        files = [
          {
            # play times and recently played
            file = ".config/rpcs3/GuiConfigs/persistent_settings.dat";
            parentDirectory = {
              mode = "0755";
            };
          }
          {
            # Netplay (RPCN) config and credentials
            file = ".config/rpcs3/rpcn.yml";
            parentDirectory = {
              mode = "0755";
            };
          }
        ];
      };
    };

    environment.persistence."/state" = lib.mkIf (config.me.mountPersistence) {
      hideMounts = true;
      users.talexander = {
        directories = [
          {
            directory = ".cache/rpcs3";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
        ];
      };
    };
  };
}
