From 8d38ca886880265d523a66fe3da4d42e92ab0748 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Fri, 1 Apr 2022 01:22:54 +0200 Subject: [PATCH] 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 --- modules/misc/xdg-mime-apps.nix | 58 ++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/modules/misc/xdg-mime-apps.nix b/modules/misc/xdg-mime-apps.nix index 5372c6259..cda9bd19b 100644 --- a/modules/misc/xdg-mime-apps.nix +++ b/modules/misc/xdg-mime-apps.nix @@ -72,20 +72,50 @@ in { }; }; - config = mkIf cfg.enable { - assertions = - [ (hm.assertions.assertPlatform "xdg.mimeApps" pkgs platforms.linux) ]; + 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))); - # Deprecated but still used by some applications. - xdg.dataFile."applications/mimeapps.list".source = - config.xdg.configFile."mimeapps.list".source; + processLine = str: + let + entry = splitString ";" str; + k = elemAt entry 0; + v = elemAt entry 1; + in if length entry == 2 then { ${k} = v; } else null; - xdg.configFile."mimeapps.list".text = - let joinValues = mapAttrs (n: concatStringsSep ";"); - in generators.toINI { } { - "Added Associations" = joinValues cfg.associations.added; - "Removed Associations" = joinValues cfg.associations.removed; - "Default Applications" = joinValues cfg.defaultApplications; - }; - }; + 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) ]; + + # Deprecated but still used by some applications. + xdg.dataFile."applications/mimeapps.list".source = + config.xdg.configFile."mimeapps.list".source; + + xdg.configFile."mimeapps.list".text = + let joinValues = mapAttrs (n: concatStringsSep ";"); + in generators.toINI { } { + "Added Associations" = joinValues cfg.associations.added; + "Removed Associations" = joinValues cfg.associations.removed; + "Default Applications" = joinValues cfg.defaultApplications; + }; + }) + ]; }