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

let
  steam_ares = pkgs.writeScriptBin "steam_ares" ''
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.libglvnd}/lib"
    exec ${pkgs.ares}/bin/ares "''${@}"
  '';
in
{
  imports = [ ];

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

  config = lib.mkIf config.me.ares.enable (
    lib.mkMerge [
      (lib.mkIf config.me.graphical {
        nixGL.vulkan.enable = true;

        home.packages = with pkgs; [
          ares
          steam_ares
        ];

        me.persist.directories = [
          ".local/share/ares/saves"
          ".local/share/ares/screenshots"
        ];

        home.file.".local/share/ares/settings.bml" = {
          source = ./files/settings.bml;
        };

        nixpkgs.overlays = [
          (
            final: prev:
            let
              optimizeWithFlags =
                pkg: flags:
                pkg.overrideAttrs (old: {
                  env.NIX_CFLAGS_COMPILE =
                    (old.env.NIX_CFLAGS_COMPILE or "") + (lib.strings.concatStringsSep " " flags);
                });
              original_package =
                if config.me.optimizations.enable then
                  (optimizeWithFlags prev.ares [
                    "-march=znver2"
                    "-mtune=znver2"
                  ])
                else
                  prev.ares;
            in
            {
              ares = pkgs.buildEnv {
                name = prev.ares.name;
                paths = [
                  (config.lib.nixGL.wrap original_package)
                ];
                extraOutputsToInstall = [
                  "man"
                  "doc"
                  "info"
                ];
                # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them.
                postBuild = ''
                  chmod 0555 $out/share/applications/ares.desktop
                '';
              };
            }
          )
        ];
      })
    ]
  );
}