fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . xdg . mimeApps ; 8 9strListOrSingleton = with types ; 10coercedTo ( either ( listOf str ) str ) toList ( listOf str ); 11 12in { 13meta . maintainers = with maintainers ; [ pacien ]; 14 15options . xdg . mimeApps = { 16enable = mkOption { 17type = types . bool ; 18default = false ; 19description = '' 20Whether to manage {file}`$XDG_CONFIG_HOME/mimeapps.list`. 21 22The generated file is read-only. 23'' ; 24}; 25 26# descriptions from 27# https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html 28 29associations . added = mkOption { 30type = types . attrsOf strListOrSingleton ; 31default = { }; 32example = literalExpression '' 33{ 34"mimetype1" = [ "foo1.desktop" "foo2.desktop" "foo3.desktop" ]; 35"mimetype2" = "foo4.desktop"; 36} 37'' ; 38description = '' 39Defines additional associations of applications with 40mimetypes, as if the .desktop file was listing this mimetype 41in the first place. 42'' ; 43}; 44 45associations . removed = mkOption { 46type = types . attrsOf strListOrSingleton ; 47default = { }; 48example = { "mimetype1" = "foo5.desktop" ; }; 49description = '' 50Removes associations of applications with mimetypes, as if the 51.desktop file was *not* listing this 52mimetype in the first place. 53'' ; 54}; 55 56defaultApplications = mkOption { 57type = types . attrsOf strListOrSingleton ; 58default = { }; 59example = literalExpression '' 60{ 61"mimetype1" = [ "default1.desktop" "default2.desktop" ]; 62} 63'' ; 64description = '' 65The default application to be used for a given mimetype. This 66is, for instance, the one that will be started when 67double-clicking on a file in a file manager. If the 68application is no longer installed, the next application in 69the list is attempted, and so on. 70'' ; 71}; 72}; 73 74config = mkMerge [ 75{ 76# Given a package that installs .desktop files in the usual location, 77# return a mapping from mime types to lists of desktop file names. This is 78# suitable for use with `xdg.mimeApps.defaultApplications`. 79lib . xdg . mimeAssociations = let 80processLines = str : 81zipAttrs 82( filter ( e :e != null ) ( map processLine ( splitString "\n" str ))); 83 84processLine = str : 85let 86entry = splitString ";" str ; 87k = elemAt entry 0 ; 88v = elemAt entry 1 ; 89in if length entry == 2 then { ${ k } = v ; } else null ; 90 91associations = ps : 92pkgs . runCommand "mime-assoc" { inherit ps ; } '' 93for p in $ ps ; do 94for path in " $ p "/share/applications/*.desktop ; do 95name ="'' $ { path ##*/}" 96sed -n -E "/^MimeType=/ { s/.*=//; s/;? $ |;/; $ name \n/g; p; }" " $ path " 97done 98done > " $ out " 99'' ; 100in p :processLines ( builtins . readFile ( associations p )); 101} 102 103( mkIf cfg . enable { 104assertions = 105[ ( hm . assertions . assertPlatform "xdg.mimeApps" pkgs platforms . linux ) ]; 106 107# Deprecated but still used by some applications. 108xdg . dataFile . "applications/mimeapps.list" . source = 109config . xdg . configFile . "mimeapps.list" . source ; 110 111xdg . configFile . "mimeapps.list" . text = 112let joinValues = mapAttrs ( n :concatStringsSep ";" ); 113in generators . toINI { } { 114"Added Associations" = joinValues cfg . associations . added ; 115"Removed Associations" = joinValues cfg . associations . removed ; 116"Default Applications" = joinValues cfg . defaultApplications ; 117}; 118}) 119]; 120}