{
  config,
  lib,
  ...
}: let
  inherit (lib) mkOption;
  inherit (builtins) typeOf listToAttrs;
in {
  options.flatpaks = mkOption {
    default = [];
    type = with lib.types;
      listOf (either str (submodule {
        options = {
          name = mkOption {
            type = str;
          };
          overrides = mkOption {
            type = attrsOf (attrsOf (either str (listOf str)));
            default = {};
          };
        };
      }));
  };
  config.services.flatpak = let
    userOverrides = listToAttrs (
      map (
        fp:
          if (typeOf fp == "string")
          then {
            name = fp;
            value = {};
          }
          else {
            inherit (fp) name;
            value = fp.overrides;
          }
      )
      config.flatpaks
    );
  in {
    enable = true;
    uninstallUnmanaged = true;
    update.auto.enable = true;
    packages =
      map (
        fp:
          if (typeOf fp == "set")
          then fp.name
          else fp
      )
      config.flatpaks;
    overrides =
      userOverrides
      // {
        global = {
          Context.sockets = ["wayland"];
          Environment = {
            ELECTRON_OZONE_PLATFORM_HINT = "auto";
          };
        };
      };
  };
}
