{
  lib,
  config,
  pkgs,
  ...
}:
let
  inherit (lib) mkEnableOption mkIf;

  cfg = config.marleyos.services.picom;
  hasXorg = config.xsession.enable;
in
{
  options.marleyos.services.picom.enable = mkEnableOption "picom";

  config = mkIf (cfg.enable && hasXorg) {
    services.picom = {
      enable = true;
      package = config.lib.nixGL.wrap pkgs.picom;
    };

    # The module config options are a nightmare.
    xdg.configFile."picom/picom.conf".text = # conf
      ''
        # Shadows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        shadow = true;
        shadow-radius = 20;
        shadow-opacity = .30;
        shadow-offset-x = -7;
        shadow-offset-y = 7;
        shadow-exclude = [
          "name = 'Notification'",
          "class_g = 'Conky'",
          "class_g ?= 'Notify-osd'",
          "class_g = 'Cairo-clock'"
        ];

        # Fading - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        fading = true;
        fade-in-step = 0.03;
        fade-out-step = 0.03;

        # Transparency / Opacity - - - - - - - - - - - - - - - - - - - - - - -
        inactive-opacity = 0.7;
        active-opacity = 0.9;
        frame-opacity = 0.7;
        inactive-opacity-override = false;
        focus-exclude = [ "class_g = 'Cairo-clock'" ];
        opacity-rule = [
          "100:class_g = 'firefox'",
          "60:class_g = 'dolphin'",
          "50:class_g = 'cava'"
        ];

        # Corners - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        corner-radius = 20;
        rounded-corners-exclude = [
          "window_type = 'dock'",
          "window_type = 'desktop'",
          "class_g = 'Dunst'"
        ];

        # Background Blurring - - - - - - - - - - - - - - - - - - - - - - - - -
        blur-method = "dual_kawase";
        blur-size = 12;
        blur-strength = 10;
        blur-background = false;
        blur-kern = "3x3box";
        blur-background-exclude = [
          "window_type = 'desktop'",
          "name = 'firefox'",
          "class_g = 'slop'"
        ];

        # General - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        backend = "glx";
        dithered-present = false;
        vsync = true;
        mark-wmwin-focused = true;
        detect-rounded-corners = true;
        detect-client-opacity = true;
        detect-transient = true;
        use-damage = false;
        log-level = "warn";

        wintypes:
        {
          tooltip = { fade = true; shadow = true; opacity = 0.75; focus = true; full-shadow = false; };
          dock = { shadow = false; clip-shadow-above = true; }
          dnd = { shadow = false; }
          popup_menu = { opacity = 0.8; }
          dropdown_menu = { opacity = 0.8; }
        };
      '';
  };
}
