fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

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