xdg-mime-apps: add function mimeAssociations

This convenience function allows automatic assignment of a package's
associations to `xdg.mimeApps.defaultApplications`.

For example,

    xdg.mimeApps.defaultApplications =
      config.lib.xdg.mimeAssociations [ pkgs.gnome.evince ];

Co-authored-by: Ryan Trinkle <ryan@trinkle.org>
This commit is contained in:
Robert Helgesson 2022-04-01 01:22:54 +02:00
parent 742c6cb3e9
commit 8d38ca8868
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89

View file

@ -72,7 +72,36 @@ in {
};
};
config = mkIf cfg.enable {
config = mkMerge [
{
# Given a package that installs .desktop files in the usual location,
# return a mapping from mime types to lists of desktop file names. This is
# suitable for use with `xdg.mimeApps.defaultApplications`.
lib.xdg.mimeAssociations = let
processLines = str:
zipAttrs
(filter (e: e != null) (map processLine (splitString "\n" str)));
processLine = str:
let
entry = splitString ";" str;
k = elemAt entry 0;
v = elemAt entry 1;
in if length entry == 2 then { ${k} = v; } else null;
associations = ps:
pkgs.runCommand "mime-assoc" { inherit ps; } ''
for p in $ps ; do
for path in "$p"/share/applications/*.desktop ; do
name="''${path##*/}"
sed -n "/^MimeType=/ { s/.*=//; s/;/;$name\n/g; p; }" "$path"
done
done > "$out"
'';
in p: processLines (builtins.readFile (associations p));
}
(mkIf cfg.enable {
assertions =
[ (hm.assertions.assertPlatform "xdg.mimeApps" pkgs platforms.linux) ];
@ -87,5 +116,6 @@ in {
"Removed Associations" = joinValues cfg.associations.removed;
"Default Applications" = joinValues cfg.defaultApplications;
};
};
})
];
}