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

This commit is contained in:
Sandro
2026-01-09 22:44:04 +00:00
committed by GitHub
+47 -14
View File
@@ -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" ];