2019-09-05 11:25:31 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.xdg.mimeApps;
|
|
|
|
|
|
|
|
strListOrSingleton = with types;
|
|
|
|
coercedTo (either (listOf str) str) toList (listOf str);
|
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
in {
|
2019-09-05 11:25:31 +02:00
|
|
|
meta.maintainers = with maintainers; [ pacien ];
|
|
|
|
|
|
|
|
options.xdg.mimeApps = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 01:30:13 +02:00
|
|
|
Whether to manage {file}`$XDG_CONFIG_HOME/mimeapps.list`.
|
|
|
|
|
2019-09-05 11:25:31 +02:00
|
|
|
The generated file is read-only.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
# descriptions from
|
|
|
|
# https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html
|
|
|
|
|
|
|
|
associations.added = mkOption {
|
|
|
|
type = types.attrsOf strListOrSingleton;
|
|
|
|
default = { };
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2019-09-05 11:25:31 +02:00
|
|
|
{
|
|
|
|
"mimetype1" = [ "foo1.desktop" "foo2.desktop" "foo3.desktop" ];
|
|
|
|
"mimetype2" = "foo4.desktop";
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-09-05 11:25:31 +02:00
|
|
|
Defines additional associations of applications with
|
|
|
|
mimetypes, as if the .desktop file was listing this mimetype
|
|
|
|
in the first place.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
associations.removed = mkOption {
|
|
|
|
type = types.attrsOf strListOrSingleton;
|
|
|
|
default = { };
|
|
|
|
example = { "mimetype1" = "foo5.desktop"; };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-09-05 11:25:31 +02:00
|
|
|
Removes associations of applications with mimetypes, as if the
|
2023-07-01 01:30:13 +02:00
|
|
|
.desktop file was *not* listing this
|
2019-09-05 11:25:31 +02:00
|
|
|
mimetype in the first place.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultApplications = mkOption {
|
|
|
|
type = types.attrsOf strListOrSingleton;
|
|
|
|
default = { };
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2019-09-05 11:25:31 +02:00
|
|
|
{
|
|
|
|
"mimetype1" = [ "default1.desktop" "default2.desktop" ];
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-09-05 11:25:31 +02:00
|
|
|
The default application to be used for a given mimetype. This
|
|
|
|
is, for instance, the one that will be started when
|
|
|
|
double-clicking on a file in a file manager. If the
|
|
|
|
application is no longer installed, the next application in
|
|
|
|
the list is attempted, and so on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-01 01:22:54 +02:00
|
|
|
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)));
|
|
|
|
|
|
|
|
processLine = str:
|
|
|
|
let
|
|
|
|
entry = splitString ";" str;
|
|
|
|
k = elemAt entry 0;
|
|
|
|
v = elemAt entry 1;
|
|
|
|
in if length entry == 2 then { ${k} = v; } else null;
|
|
|
|
|
|
|
|
associations = ps:
|
|
|
|
pkgs.runCommand "mime-assoc" { inherit ps; } ''
|
|
|
|
for p in $ps ; do
|
|
|
|
for path in "$p"/share/applications/*.desktop ; do
|
|
|
|
name="''${path##*/}"
|
2022-07-26 10:08:25 +02:00
|
|
|
sed -n -E "/^MimeType=/ { s/.*=//; s/;?$|;/;$name\n/g; p; }" "$path"
|
2022-04-01 01:22:54 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2019-09-05 11:25:31 +02:00
|
|
|
}
|