mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 19:46:05 +01:00
xdg-mime-apps: add defaultApplicationPackages option
This option allows a user to inject default applications directly from a list of specified packages. It was previously possible to do this, but only through import from derivation.
This commit is contained in:
parent
e387519338
commit
efcba687d3
4 changed files with 102 additions and 38 deletions
|
|
@ -75,6 +75,24 @@ in
|
||||||
the list is attempted, and so on.
|
the list is attempted, and so on.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
defaultApplicationPackages = mkOption {
|
||||||
|
type = types.listOf types.package;
|
||||||
|
default = [ ];
|
||||||
|
example = [
|
||||||
|
pkgs.eog
|
||||||
|
pkgs.evince
|
||||||
|
];
|
||||||
|
description = ''
|
||||||
|
Packages whose `.desktop` files will be used to establish default
|
||||||
|
mimetype associations.
|
||||||
|
|
||||||
|
These associations are appended to the associations in
|
||||||
|
[](#opt-xdg.mimeApps.defaultApplications). If multiple packages
|
||||||
|
associate with the same mime type, then the priority among them is
|
||||||
|
determined by their order in the list.
|
||||||
|
'';
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = lib.mkMerge [
|
config = lib.mkMerge [
|
||||||
|
|
@ -106,8 +124,12 @@ in
|
||||||
done
|
done
|
||||||
done > "$out"
|
done > "$out"
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
processAll = p: processLines (builtins.readFile (associations p));
|
||||||
|
|
||||||
|
warning = "The Home Manager `lib.xdg.mimeAssociations` function is deprecated, you can now instead use the option `xdg.mimeApps.defaultApplicationPackages` to achieve the same without import from derivation";
|
||||||
in
|
in
|
||||||
p: processLines (builtins.readFile (associations p));
|
lib.warn warning processAll;
|
||||||
}
|
}
|
||||||
|
|
||||||
(lib.mkIf cfg.enable {
|
(lib.mkIf cfg.enable {
|
||||||
|
|
@ -118,15 +140,44 @@ in
|
||||||
# Deprecated but still used by some applications.
|
# Deprecated but still used by some applications.
|
||||||
xdg.dataFile."applications/mimeapps.list".source = config.xdg.configFile."mimeapps.list".source;
|
xdg.dataFile."applications/mimeapps.list".source = config.xdg.configFile."mimeapps.list".source;
|
||||||
|
|
||||||
xdg.configFile."mimeapps.list".text =
|
xdg.configFile."mimeapps.list".source =
|
||||||
let
|
let
|
||||||
joinValues = lib.mapAttrs (n: lib.concatStringsSep ";");
|
joinValues = lib.mapAttrs (n: lib.concatStringsSep ";");
|
||||||
|
|
||||||
|
baseFile = (pkgs.formats.ini { }).generate "mimeapps.list" {
|
||||||
|
"Added Associations" = joinValues cfg.associations.added;
|
||||||
|
"Removed Associations" = joinValues cfg.associations.removed;
|
||||||
|
"Default Applications" = joinValues cfg.defaultApplications;
|
||||||
|
};
|
||||||
|
|
||||||
|
# With default application packages merged into the generated base file.
|
||||||
|
mergedFile = pkgs.runCommand "mimeapps.list" { ps = cfg.defaultApplicationPackages; } ''
|
||||||
|
export PATH=$PATH:${pkgs.crudini}/bin
|
||||||
|
|
||||||
|
function mergeEntry() {
|
||||||
|
local mime="$1"
|
||||||
|
local name="$2"
|
||||||
|
local existing
|
||||||
|
|
||||||
|
existing="$(crudini --get $out 'Default Applications' "$mime" 2>/dev/null || true)"
|
||||||
|
local value="$existing''${existing:+;}''$name"
|
||||||
|
crudini --inplace --set $out 'Default Applications' "$mime" "$value"
|
||||||
|
}
|
||||||
|
|
||||||
|
install -m644 ${baseFile} $out
|
||||||
|
|
||||||
|
for p in $ps ; do
|
||||||
|
for path in "$p"/share/applications/*.desktop ; do
|
||||||
|
name="''${path##*/}"
|
||||||
|
mimes=$(crudini --get "$path" 'Desktop Entry' MimeType 2>/dev/null || true)
|
||||||
|
for mime in ''${mimes//;/ }; do
|
||||||
|
mergeEntry "$mime" "$name"
|
||||||
|
done
|
||||||
|
done
|
||||||
|
done
|
||||||
|
'';
|
||||||
in
|
in
|
||||||
lib.generators.toINI { } {
|
if cfg.defaultApplicationPackages == [ ] then baseFile else mergedFile;
|
||||||
"Added Associations" = joinValues cfg.associations.added;
|
|
||||||
"Removed Associations" = joinValues cfg.associations.removed;
|
|
||||||
"Default Applications" = joinValues cfg.defaultApplications;
|
|
||||||
};
|
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ let
|
||||||
inner = self: super: {
|
inner = self: super: {
|
||||||
inherit (pkgs)
|
inherit (pkgs)
|
||||||
coreutils
|
coreutils
|
||||||
|
crudini
|
||||||
jq
|
jq
|
||||||
desktop-file-utils
|
desktop-file-utils
|
||||||
diffutils
|
diffutils
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
[Added Associations]
|
[Added Associations]
|
||||||
mimetype1=foo1.desktop;foo2.desktop;foo3.desktop
|
image/jpeg=foo4.desktop
|
||||||
mimetype2=foo4.desktop
|
image/png=foo1.desktop;foo2.desktop;foo3.desktop
|
||||||
|
|
||||||
[Default Applications]
|
[Default Applications]
|
||||||
mimetype1=default1.desktop;default2.desktop
|
image/png=default1.desktop;default2.desktop;test.desktop
|
||||||
|
image/svg+xml = test.desktop
|
||||||
|
|
||||||
[Removed Associations]
|
[Removed Associations]
|
||||||
mimetype1=foo5.desktop
|
image/png=foo5.desktop
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,44 @@
|
||||||
|
{ pkgs, ... }:
|
||||||
|
|
||||||
{
|
{
|
||||||
config = {
|
xdg.mimeApps = {
|
||||||
xdg.mimeApps = {
|
enable = true;
|
||||||
enable = true;
|
associations = {
|
||||||
associations = {
|
added = {
|
||||||
added = {
|
"image/png" = [
|
||||||
"mimetype1" = [
|
"foo1.desktop"
|
||||||
"foo1.desktop"
|
"foo2.desktop"
|
||||||
"foo2.desktop"
|
"foo3.desktop"
|
||||||
"foo3.desktop"
|
|
||||||
];
|
|
||||||
"mimetype2" = "foo4.desktop";
|
|
||||||
};
|
|
||||||
removed = {
|
|
||||||
mimetype1 = "foo5.desktop";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
defaultApplications = {
|
|
||||||
"mimetype1" = [
|
|
||||||
"default1.desktop"
|
|
||||||
"default2.desktop"
|
|
||||||
];
|
];
|
||||||
|
"image/jpeg" = "foo4.desktop";
|
||||||
|
};
|
||||||
|
removed = {
|
||||||
|
"image/png" = "foo5.desktop";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
defaultApplications = {
|
||||||
nmt.script = ''
|
"image/png" = [
|
||||||
assertFileExists home-files/.config/mimeapps.list
|
"default1.desktop"
|
||||||
assertFileContent \
|
"default2.desktop"
|
||||||
home-files/.config/mimeapps.list \
|
];
|
||||||
${./mime-apps-basics-expected.ini}
|
};
|
||||||
'';
|
defaultApplicationPackages = [
|
||||||
|
(pkgs.makeDesktopItem {
|
||||||
|
type = "Application";
|
||||||
|
name = "test";
|
||||||
|
desktopName = "Test";
|
||||||
|
mimeTypes = [
|
||||||
|
"image/png"
|
||||||
|
"image/svg+xml"
|
||||||
|
];
|
||||||
|
})
|
||||||
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
nmt.script = ''
|
||||||
|
assertFileExists home-files/.config/mimeapps.list
|
||||||
|
assertFileContent \
|
||||||
|
home-files/.config/mimeapps.list \
|
||||||
|
${./mime-apps-basics-expected.ini}
|
||||||
|
'';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue