self: {
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.services.rlru;
in {
  options.services.rlru = {
    enable = lib.mkEnableOption "rlru, a Rocket League replay uploader";

    package = lib.mkOption {
      type = lib.types.package;
      default = self.packages.${pkgs.stdenv.hostPlatform.system}.rlru-dioxus-desktop;
      defaultText = lib.literalExpression "inputs.rlru.packages.\${pkgs.stdenv.hostPlatform.system}.rlru-dioxus-desktop";
      description = "The rlru desktop package to run.";
    };

    environment = lib.mkOption {
      type = lib.types.attrsOf lib.types.str;
      default = {};
      example = lib.literalExpression ''{ RUST_LOG = "info"; }'';
      description = "Environment variables to set for the rlru user service.";
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [cfg.package];

    systemd.user.services.rlru = {
      description = "rlru Rocket League replay uploader";
      wantedBy = ["graphical-session.target"];
      after = ["graphical-session.target"];

      unitConfig = {
        Requires = ["graphical-session.target"];
        PartOf = ["graphical-session.target"];
      };

      serviceConfig = {
        ExecStart = lib.getExe cfg.package;
        Environment = lib.mapAttrsToList (name: value: "${name}=${value}") cfg.environment;
        Restart = "on-failure";
        RestartSec = "5s";
      };
    };
  };
}
