From 24948e536b82979a86231eacf31d2ac512a71857 Mon Sep 17 00:00:00 2001 From: linsui <36977733+linsui@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:52:55 +0800 Subject: [PATCH] nixos/xdg/mime: support glob --- nixos/modules/config/xdg/mime.nix | 61 ++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/nixos/modules/config/xdg/mime.nix b/nixos/modules/config/xdg/mime.nix index 2b2905bb2f0c..79955a03ef2f 100644 --- a/nixos/modules/config/xdg/mime.nix +++ b/nixos/modules/config/xdg/mime.nix @@ -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 { } { - "Added Associations" = cfg.addedAssociations; - "Default Applications" = cfg.defaultApplications; - "Removed Associations" = cfg.removedAssociations; - }; + 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" ];