{
  config,
  pkgs,
  lib,
  ...
}: let
  cfg = config.services.sunshine;
in {
  options.services.sunshine = with lib; {
    enable = mkEnableOption "Sunshine server for Moonlight";

    package = mkOption {
      type = types.package;
      default = pkgs.sunshine;
      defaultText = literalExpression "pkgs.sunshine";
    };

    systemdTarget = mkOption {
      type = types.str;
      default = "graphical-session.target";
      description = ''
        Systemd target to bind to.
      '';
    };
  };

  config = with lib;
    mkIf cfg.enable {
      security.wrappers.sunshine = {
        source = "${cfg.package}/bin/sunshine";
        capabilities = "cap_sys_admin+p";
        owner = "root";
        group = "root";
      };

      services.udev = {
        enable = mkDefault true;
        extraRules = ''
          KERNEL=="uinput", GROUP="input", MODE="0660", OPTIONS+="static_node=uinput"
        '';
      };

      systemd.user.services.sunshine = {
        description = "Sunshine self-hosted game stream host for Moonlight.";
        wantedBy = [cfg.systemdTarget];
        serviceConfig = {
          Type = "simple";
          ExecStart = "${cfg.package}/bin/sunshine";
        };
      };
    };
}