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

{
  imports = [ ];

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

  config = lib.mkIf (config.me.kodi.enable && config.me.graphical) {
    services.cage.user = "kodi";
    services.cage.program = "${pkgs.kodi-wayland}/bin/kodi-standalone";
    services.cage.enable = true;

    nixpkgs.overlays = [
      (final: prev: {
        kodi-wayland = prev.kodi-wayland.withPackages (
          kodiPkgs: with kodiPkgs; [
            joystick
            vfs-sftp
          ]
        );
      })
    ];

    users.users.kodi = {
      isNormalUser = true;
      createHome = true; # https://github.com/NixOS/nixpkgs/issues/6481
      group = "kodi";
      extraGroups = [ ];
      uid = 12000;
      packages = with pkgs; [
        tree
      ];
      # Generate with `mkpasswd -m scrypt`
      hashedPassword = "$7$CU..../....VXvNQ8za3wSGpdzGXNT50/$HcFtn/yvwPMCw4888BelpiAPLAxe/zU87fD.d/N6U48";
      openssh.authorizedKeys.keys = [
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0+4zi26M3eYWnIrciR54kOlGxzfgCXG+o4ea1zpzrk openpgp:0x7FF123C8"
        "sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIEI6mu6I5Jp+Ib0vJxapGHbEShZjyvzV8jz5DnzDrI39AAAABHNzaDo="
        "sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIAFNcSXwvy+brYTOGo56G93Ptuq2MmZsjvRWAfMqbmMLAAAABHNzaDo="
      ];
    };
    users.groups.kodi.gid = 12000;

    environment.persistence."/persist" = lib.mkIf (config.me.mountPersistence) {
      hideMounts = true;
      users.kodi = {
        directories = [
          {
            directory = ".ssh";
            user = "kodi";
            group = "kodi";
            mode = "0755";
          }
          {
            directory = ".kodi";
            user = "kodi";
            group = "kodi";
            mode = "0755";
          }
        ];
      };
    };

    # home.file.".kodi/userdata/mediasources.xml".source = ./files/mediasources.xml;

    # home.file.".kodi/userdata/addon_data/peripheral.joystick/resources/buttonmaps/xml/linux/DualSense_Wireless_Controller_13b_8a.xml".source =
    #   ./files/DualSense_Wireless_Controller_13b_8a.xml;

    # TODO: Maybe .kodi/userdata/sources.xml
    # TODO: ./userdata/guisettings.xml:303:    <setting id="filecache.memorysize">128</setting>
  };
}
