summaryrefslogtreecommitdiffstats
path: root/nixos/modules/config/xdg/mime.nix
blob: 59d62a32010b03f933a219293f51853c4a7edf37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.xdg.mime;
  associationOptions =
    with lib.types;
    attrsOf (coercedTo (either (listOf str) str) (x: lib.concatStringsSep ";" (lib.toList x)) str);
in

{
  meta = {
    teams = [ lib.teams.freedesktop ];
  };

  options = {
    xdg.mime.enable = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = ''
        Whether to install files to support the
        [XDG Shared MIME-info specification](https://specifications.freedesktop.org/shared-mime-info-spec/latest) and the
        [XDG MIME Applications specification](https://specifications.freedesktop.org/mime-apps-spec/latest).
      '';
    };

    xdg.mime.addedAssociations = lib.mkOption {
      type = associationOptions;
      default = { };
      example = {
        "application/pdf" = "firefox.desktop";
        "text/*" = [
          "nvim.desktop"
          "codium.desktop"
        ];
      };
      description = ''
        Adds associations between mimetypes and applications. See the
        [specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
        Globs in all variations are supported.
      '';
    };

    xdg.mime.defaultApplications = lib.mkOption {
      type = associationOptions;
      default = { };
      example = {
        "application/pdf" = "firefox.desktop";
        "image/*" = [
          "sxiv.desktop"
          "gimp.desktop"
        ];
      };
      description = ''
        Sets the default applications for given mimetypes. See the
        [specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/default) for more information.
        Globs in all variations are supported.
      '';
    };

    xdg.mime.removedAssociations = lib.mkOption {
      type = associationOptions;
      default = { };
      example = {
        "audio/*" = [
          "mpv.desktop"
          "umpv.desktop"
        ];
        "inode/directory" = "codium.desktop";
      };
      description = ''
        Removes associations between mimetypes and applications. See the
        [specifications](https://specifications.freedesktop.org/mime-apps-spec/latest/associations) for more information.
        Globs in all variations are supported.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    environment.etc."xdg/mimeapps.list" =
      let
        generateMimeScript =
          title: attrs:
          lib.optionalString (attrs != { }) ''
            echo "[${title}]" >> $out
          ''
          + (lib.concatStringsSep "\n" (
            lib.attrValues (
              lib.mapAttrs (
                k: v: ''generateMimeItem "${k}" "${if lib.isList v then lib.concatStringsSep ";" v else v}"''
              ) attrs
            )
          ));
      in
      lib.mkIf
        (cfg.addedAssociations != { } || cfg.defaultApplications != { } || cfg.removedAssociations != { })
        {
          source = pkgs.runCommandLocal "mimeapps.list" { } ''
            function generateMimeItem() {
              mime=$1
              app=$2
              if [[ $mime == *"*"* ]]; then
                while read line; do
                  if [[ $line == $mime ]]; then
                    echo "$line=$app" >> $out
                  fi
                done < ${pkgs.shared-mime-info}/share/mime/types
              else
                echo "$mime=$app" >> $out
              fi
            }
            ${lib.concatStringsSep "\n" (
              lib.attrValues (
                lib.mapAttrs generateMimeScript {
                  "Added Associations" = cfg.addedAssociations;
                  "Default Applications" = cfg.defaultApplications;
                  "Removed Associations" = cfg.removedAssociations;
                }
              )
            )}
          '';
        };

    environment.pathsToLink = [ "/share/mime" ];

    environment.systemPackages = [
      # this package also installs some useful data, as well as its utilities
      pkgs.shared-mime-info
    ];

    environment.extraSetup = ''
      if [ -w $out/share/mime ] && [ -d $out/share/mime/packages ]; then
          XDG_DATA_DIRS=$out/share PKGSYSTEM_ENABLE_FSYNC=0 ${pkgs.buildPackages.shared-mime-info}/bin/update-mime-database -V $out/share/mime > /dev/null
      fi

      if [ -w $out/share/applications ]; then
          ${pkgs.buildPackages.desktop-file-utils}/bin/update-desktop-database $out/share/applications
      fi
    '';
  };

}