1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.xdg.mime;
9 associationOptions =
10 with lib.types;
11 attrsOf (coercedTo (either (listOf str) str) (x: lib.concatStringsSep ";" (lib.toList x)) str);
12in
13
14{
15 meta = {
16 maintainers = lib.teams.freedesktop.members ++ (with lib.maintainers; [ figsoda ]);
17 };
18
19 options = {
20 xdg.mime.enable = lib.mkOption {
21 type = lib.types.bool;
22 default = true;
23 description = ''
24 Whether to install files to support the
25 [XDG Shared MIME-info specification](https://specifications.freedesktop.org/shared-mime-info-spec/latest) and the
26 [XDG MIME Applications specification](https://specifications.freedesktop.org/mime-apps-spec/latest).
27 '';
28 };
29
30 xdg.mime.addedAssociations = lib.mkOption {
31 type = associationOptions;
32 default = { };
33 example = {
34 "application/pdf" = "firefox.desktop";
35 "text/xml" = [
36 "nvim.desktop"
37 "codium.desktop"
38 ];
39 };
40 description = ''
41 Adds associations between mimetypes and applications. See the
42 [
43 specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
44 '';
45 };
46
47 xdg.mime.defaultApplications = lib.mkOption {
48 type = associationOptions;
49 default = { };
50 example = {
51 "application/pdf" = "firefox.desktop";
52 "image/png" = [
53 "sxiv.desktop"
54 "gimp.desktop"
55 ];
56 };
57 description = ''
58 Sets the default applications for given mimetypes. See the
59 [
60 specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/default) for more information.
61 '';
62 };
63
64 xdg.mime.removedAssociations = lib.mkOption {
65 type = associationOptions;
66 default = { };
67 example = {
68 "audio/mp3" = [
69 "mpv.desktop"
70 "umpv.desktop"
71 ];
72 "inode/directory" = "codium.desktop";
73 };
74 description = ''
75 Removes associations between mimetypes and applications. See the
76 [
77 specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
78 '';
79 };
80 };
81
82 config = lib.mkIf cfg.enable {
83 environment.etc."xdg/mimeapps.list" =
84 lib.mkIf
85 (cfg.addedAssociations != { } || cfg.defaultApplications != { } || cfg.removedAssociations != { })
86 {
87 text = lib.generators.toINI { } {
88 "Added Associations" = cfg.addedAssociations;
89 "Default Applications" = cfg.defaultApplications;
90 "Removed Associations" = cfg.removedAssociations;
91 };
92 };
93
94 environment.pathsToLink = [ "/share/mime" ];
95
96 environment.systemPackages = [
97 # this package also installs some useful data, as well as its utilities
98 pkgs.shared-mime-info
99 ];
100
101 environment.extraSetup = ''
102 if [ -w $out/share/mime ] && [ -d $out/share/mime/packages ]; then
103 XDG_DATA_DIRS=$out/share PKGSYSTEM_ENABLE_FSYNC=0 ${pkgs.buildPackages.shared-mime-info}/bin/update-mime-database -V $out/share/mime > /dev/null
104 fi
105
106 if [ -w $out/share/applications ]; then
107 ${pkgs.buildPackages.desktop-file-utils}/bin/update-desktop-database $out/share/applications
108 fi
109 '';
110 };
111
112}