Unverified Commit bcfb2951 authored by Sandro Jäckel's avatar Sandro Jäckel Committed by GitHub
Browse files

nixos/xdg/mime: support glob (#340800)

parents df03b2d7 24948e53
Loading
Loading
Loading
Loading
+47 −14
Original line number Diff line number Diff line
@@ -32,15 +32,15 @@ in
      default = { };
      example = {
        "application/pdf" = "firefox.desktop";
        "text/xml" = [
        "text/*" = [
          "nvim.desktop"
          "codium.desktop"
        ];
      };
      description = ''
        Adds associations between mimetypes and applications. See the
        [
        specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
        [specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
        Globs in all variations are supported.
      '';
    };

@@ -49,15 +49,15 @@ in
      default = { };
      example = {
        "application/pdf" = "firefox.desktop";
        "image/png" = [
        "image/*" = [
          "sxiv.desktop"
          "gimp.desktop"
        ];
      };
      description = ''
        Sets the default applications for given mimetypes. See the
        [
        specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/default) for more information.
        [specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/default) for more information.
        Globs in all variations are supported.
      '';
    };

@@ -65,7 +65,7 @@ in
      type = associationOptions;
      default = { };
      example = {
        "audio/mp3" = [
        "audio/*" = [
          "mpv.desktop"
          "umpv.desktop"
        ];
@@ -73,22 +73,55 @@ in
      };
      description = ''
        Removes associations between mimetypes and applications. See the
        [
        specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
        [specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
        Globs in all variations are supported.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    environment.etc."xdg/mimeapps.list" =
      let
        generateMimeScript =
          title: attrs:
          lib.optionalString (attrs != { }) ''
            echo "[${title}]" >> $out
          ''
          + (lib.concatStringsSep "\n" (
            lib.attrValues (
              lib.mapAttrs (
                k: v: ''generateMimeItem "${k}" "${if lib.isList v then lib.concatStringsSep ";" v else v}"''
              ) attrs
            )
          ));
      in
      lib.mkIf
        (cfg.addedAssociations != { } || cfg.defaultApplications != { } || cfg.removedAssociations != { })
        {
          text = lib.generators.toINI { } {
          source = pkgs.runCommandLocal "mimeapps.list" { } ''
            function generateMimeItem() {
              mime=$1
              app=$2
              if [[ $mime == *"*"* ]]; then
                while read line; do
                  if [[ $line == $mime ]]; then
                    echo "$line=$app" >> $out
                  fi
                done < ${pkgs.shared-mime-info}/share/mime/types
              else
                echo "$mime=$app" >> $out
              fi
            }
            ${lib.concatStringsSep "\n" (
              lib.attrValues (
                lib.mapAttrs generateMimeScript {
                  "Added Associations" = cfg.addedAssociations;
                  "Default Applications" = cfg.defaultApplications;
                  "Removed Associations" = cfg.removedAssociations;
          };
                }
              )
            )}
          '';
        };

    environment.pathsToLink = [ "/share/mime" ];