{
  pkgs,
  lib,
  ...
}: let
  browser = ["zen.desktop"];
  imageViewer = ["lightview.desktop"];
  videoPlayer = ["mpv.desktop"];
  audioPlayer = ["io.bassi.Amberol.desktop"];

  xdgAssociations = type: program: list:
    builtins.listToAttrs (map (e: {
        name = "${type}/${e}";
        value = program;
      })
      list);

  image = xdgAssociations "image" imageViewer ["png" "jpg" "jpeg" "gif" "webp" "bmp" "tiff" "tif" "ico" "svg" "avif" "heic" "heif"];
  video = xdgAssociations "video" videoPlayer ["mp4" "avi" "mkv" "mov" "wmv" "flv" "webm" "m4v" "3gp" "ogv" "ts" "mts" "m2ts"];
  audio = xdgAssociations "audio" audioPlayer ["mp3" "flac" "wav" "aac" "ogg" "oga" "opus" "m4a" "wma" "ape" "alac" "aiff"];

  browserTypes =
    (xdgAssociations "application" browser ["json" "x-extension-htm" "x-extension-html" "x-extension-shtml" "x-extension-xht" "x-extension-xhtml"])
    // (xdgAssociations "x-scheme-handler" browser ["about" "ftp" "http" "https" "unknown"]);

  associations =
    {
      "application/pdf" = ["org.gnome.Papers.desktop"];
      "application/zip" = ["org.gnome.FileRoller.desktop"];
      "application/x-7z-compressed" = ["org.gnome.FileRoller.desktop"];
      "application/x-rar-compressed" = ["org.gnome.FileRoller.desktop"];
      "application/x-tar" = ["org.gnome.FileRoller.desktop"];
      "application/gzip" = ["org.gnome.FileRoller.desktop"];
      "text/html" = browser;
      "text/plain" = ["org.gnome.TextEditor.desktop"];
      "text/markdown" = ["org.gnome.TextEditor.desktop"];
      "x-scheme-handler/chrome" = ["zen.desktop"];
    }
    // image // video // audio // browserTypes;

  userDirsConfig = pkgs.writeText "user-dirs.dirs" ''
    XDG_DESKTOP_DIR="$HOME/Desktop"
    XDG_DOWNLOAD_DIR="$HOME/Downloads"
    XDG_TEMPLATES_DIR="$HOME/Templates"
    XDG_PUBLICSHARE_DIR="$HOME/Public"
    XDG_DOCUMENTS_DIR="$HOME/Documents"
    XDG_MUSIC_DIR="$HOME/Music"
    XDG_PICTURES_DIR="$HOME/Pictures"
    XDG_VIDEOS_DIR="$HOME/Videos"
  '';

  yaziWrapper = pkgs.writeShellScriptBin "yazi-wrapper" ''
    set -e

    multiple="$1"
    directory="$2"
    save="$3"
    path="$4"
    out="$5"

    # We use alacritty directly to run yazi
    # For saving (save=1), yazi is passed the directory and we write the final path output
    # For directories (directory=1), we pass yazi the dir selection flag
    if [ "$save" = "1" ]; then
        exec ${pkgs.alacritty}/bin/alacritty -e ${pkgs.yazi}/bin/yazi --chooser-file="$out" "$path"
    elif [ "$directory" = "1" ]; then
        exec ${pkgs.alacritty}/bin/alacritty -e ${pkgs.yazi}/bin/yazi --chooser-file="$out" --cwd-file="$out"".1" "$path"
    else
        exec ${pkgs.alacritty}/bin/alacritty -e ${pkgs.yazi}/bin/yazi --chooser-file="$out" "$path"
    fi
  '';
in {
  users.users.someone.packages = with pkgs; [
    xdg-utils
    (writeShellScriptBin "xdg-terminal-exec" ''${pkgs.alacritty}/bin/alacritty'')
  ];

  xdg = {
    mime = {
      enable = true;
      defaultApplications = associations;
    };
    configFile."xdg-desktop-portal-termfilechooser/config".text = ''
      [filechooser]
      cmd=${yaziWrapper}/bin/yazi-wrapper
      default_dir=$HOME
      open_mode=suggested
      save_mode=suggested
    '';
    configFile."user-dirs.dirs".source = userDirsConfig;
    configFile."mimeapps.list".text = ''
      [Default Applications]
      ${lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: "${k}=${lib.concatStringsSep ";" v}") associations)}
      [Added Associations]
      ${lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: "${k}=${lib.concatStringsSep ";" v}") associations)}
    '';
  };
}
