mirror of
https://github.com/nix-community/home-manager
synced 2024-11-26 21:19:45 +01:00
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:
parent
742c6cb3e9
commit
8d38ca8868
1 changed files with 44 additions and 14 deletions
|
@ -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;
|
||||
};
|
||||
};
|
||||
})
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue