Set default application for mime type with home-manager

xdg-open foo.pdf opens the pdf in LibreOffice Draw, but I want it to open in evince. I tried this but it didn’t change anything:

  home.xdg.mimeApps = {
    enable = true;
    defaultApplications = {
      "application/pdf" = ["evince.desktop"];
    };
  };
2 Likes

Two shots in the dark.

  1. Maybe there are some other xdg config files sitting in your home dir was generated by some other prog and its superseding the one generated by hm.

  2. Did you try setting home.xdg.enable = true;?

1 Like

Useful debugging tools:

XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query filetype foo.pdf

XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query default application/pdf

fd evince.desktop /

This works:

  xdg.mimeApps = {
    enable = true;
    associations.added = {
      "application/pdf" = ["org.gnome.Evince.desktop"];
    };
    defaultApplications = {
      "application/pdf" = ["org.gnome.Evince.desktop"];
    };
  };
7 Likes

Sorry to re-open this.

I had

  xdg.mimeApps.defaultApplications = {
    "x-scheme-handler/http" = ["firefox.desktop"];
    "x-scheme-handler/https" = ["firefox.desktop"];
    "text/html" = ["firefox.desktop"];
    "application/pdf" = ["zathura.desktop" "evince.desktop" "firefox.desktop"];
  };

But it still opened for example .pdf with chrome

I added this to be sure it’s dead.

  xdg.mimeApps.associations.removed = {
      "application/pdf" = ["chromium-browser.desktop"];
      "image/png" = ["chromium-browser.desktop"];
    };

But

› XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query default application/pdf
Checking /home/tilli/.config/mimeapps.list
Checking /home/tilli/.local/share/applications/mimeapps.list
Checking /home/tilli/.local/share/applications/defaults.list and /home/tilli/.local/share/applications/mimeinfo.cache
Checking /home/tilli/.local/share/applications/defaults.list and /home/tilli/.local/share/applications/mimeinfo.cache
Checking /nix/store/gwy6kafcc59irnc07j5i5agpb2bfv81x-sway-1.9/share/applications/defaults.list and /nix/store/gwy6kafcc59irnc07j5i5agpb2bfv81x-sway-1.9/share/applications/mimeinfo.cache
Checking /nix/store/gwy6kafcc59irnc07j5i5agpb2bfv81x-sway-1.9/share/applications/defaults.list and /nix/store/gwy6kafcc59irnc07j5i5agpb2bfv81x-sway-1.9/share/applications/mimeinfo.cache
Checking /nix/store/4qvv1l5jirm3351784kb5j8krsqql1si-gsettings-desktop-schemas-46.0/share/gsettings-schemas/gsettings-desktop-schemas-46.0/applications/defaults.list and /nix/store/4qvv1l5jirm3351784kb5j8krsqql1si-gsettings-desktop-schemas-46.0/share/gsettings-schemas/gsettings-desktop-schemas-46.0/applications/mimeinfo.cache
Checking /nix/store/4qvv1l5jirm3351784kb5j8krsqql1si-gsettings-desktop-schemas-46.0/share/gsettings-schemas/gsettings-desktop-schemas-46.0/applications/defaults.list and /nix/store/4qvv1l5jirm3351784kb5j8krsqql1si-gsettings-desktop-schemas-46.0/share/gsettings-schemas/gsettings-desktop-schemas-46.0/applications/mimeinfo.cache
Checking /nix/store/nx27cp48ylj2bmrf1r72ncl3pcq7w5df-gtk+3-3.24.41/share/gsettings-schemas/gtk+3-3.24.41/applications/defaults.list and /nix/store/nx27cp48ylj2bmrf1r72ncl3pcq7w5df-gtk+3-3.24.41/share/gsettings-schemas/gtk+3-3.24.41/applications/mimeinfo.cache
Checking /nix/store/nx27cp48ylj2bmrf1r72ncl3pcq7w5df-gtk+3-3.24.41/share/gsettings-schemas/gtk+3-3.24.41/applications/defaults.list and /nix/store/nx27cp48ylj2bmrf1r72ncl3pcq7w5df-gtk+3-3.24.41/share/gsettings-schemas/gtk+3-3.24.41/applications/mimeinfo.cache
Checking /nix/store/3bwmjk2pai9yn6jykgg4lxxc70kpykxw-desktops/share/applications/defaults.list and /nix/store/3bwmjk2pai9yn6jykgg4lxxc70kpykxw-desktops/share/applications/mimeinfo.cache
Checking /nix/store/3bwmjk2pai9yn6jykgg4lxxc70kpykxw-desktops/share/applications/defaults.list and /nix/store/3bwmjk2pai9yn6jykgg4lxxc70kpykxw-desktops/share/applications/mimeinfo.cache
Checking /home/tilli/.local/share/flatpak/exports/share/applications/defaults.list and /home/tilli/.local/share/flatpak/exports/share/applications/mimeinfo.cache
Checking /home/tilli/.local/share/flatpak/exports/share/applications/defaults.list and /home/tilli/.local/share/flatpak/exports/share/applications/mimeinfo.cache
Checking /var/lib/flatpak/exports/share/applications/defaults.list and /var/lib/flatpak/exports/share/applications/mimeinfo.cache
Checking /var/lib/flatpak/exports/share/applications/defaults.list and /var/lib/flatpak/exports/share/applications/mimeinfo.cache
Checking /home/tilli/.nix-profile/share/applications/defaults.list and /home/tilli/.nix-profile/share/applications/mimeinfo.cache
Checking /home/tilli/.nix-profile/share/applications/defaults.list and /home/tilli/.nix-profile/share/applications/mimeinfo.cache
Checking /nix/profile/share/applications/defaults.list and /nix/profile/share/applications/mimeinfo.cache
Checking /nix/profile/share/applications/defaults.list and /nix/profile/share/applications/mimeinfo.cache
Checking /home/tilli/.local/state/nix/profile/share/applications/defaults.list and /home/tilli/.local/state/nix/profile/share/applications/mimeinfo.cache
Checking /home/tilli/.local/state/nix/profile/share/applications/defaults.list and /home/tilli/.local/state/nix/profile/share/applications/mimeinfo.cache
Checking /etc/profiles/per-user/tilli/share/applications/defaults.list and /etc/profiles/per-user/tilli/share/applications/mimeinfo.cache
chromium-browser.desktop

The definitions in /home/tilli/.config/mimeapps.list look just right. What the eff is still causing this?

Those files exist and match pdf

› grep pdf /home/tilli/.config/mimeapps.list /home/tilli/.local/share/applications/mimeapps.list /home/tilli/.local/share/flatpak/exports/share/applications/mimeinfo.cache /home/tilli/.local/share/flatpak/exports/share/applications/mimeinfo.cache /var/lib/flatpak/exports/share/applications/mimeinfo.cache /var/lib/flat
pak/exports/share/applications/mimeinfo.cache /etc/profiles/per-user/tilli/share/applications/mimeinfo.cache
/home/tilli/.config/mimeapps.list:application/pdf=zathura.desktop;evince.desktop;firefox.desktop
/home/tilli/.config/mimeapps.list:application/pdf=chromium-browser.desktop
/home/tilli/.local/share/applications/mimeapps.list:application/pdf=zathura.desktop;evince.desktop;firefox.desktop
/home/tilli/.local/share/applications/mimeapps.list:application/pdf=chromium-browser.desktop
/etc/profiles/per-user/tilli/share/applications/mimeinfo.cache:application/pdf=chromium-browser.desktop;

/etc/profiles/per-user/tilli/share/applications/mimeinfo.cache is the only one that doesn’t contain the remove section. And it’s read-only.

I suffer with the same problem. For whatever reason xdg-open checks many different files and only one of them has wrong values. Only this file is used it seems. Since it is located in /etc directory it is read only and thus I can’t do anything about it. Rebuilding multiple times simply does not work.

I have a similar issue where my xdg.mimeApps.defaultApplications values are not used.

Running XDG_UTILS_DEBUG_LEVEL=3 xdg-mime query default image/png gives

Checking /home/florian/.config/mimeapps.list
Checking /home/florian/.local/share/applications/mimeapps.list
Checking /run/current-system/sw/share/applications/kde-mimeapps.list
org.kde.gwenview.desktop

The first to files are symlinks to the file mimeapps file generated by home-manager where image/png=nomacs.desktop is defined, so these are okay.
But the kde-mimeapps.list file has the KDE default values and is a symlink to /nix/store/jcsasyx5jnjiappxnygq2hqgvk9rbgsq-plasma-desktop-6.0.5/share/applications/kde-mimeapps.list. Furthermore, I cannot change the default app using the KDE settings, probably because it also wants to change a file in the nix store.

Does this still happen if you also set xdg.mimeApps.associations.added ?

Yes. It behaves exactly the same.

According to XDG MIME Applications - ArchWiki:

desktop-specific overrides take precedence over the corresponding non-desktop-specific file

As such, creating a ~/.config/kde-mimeapps.list or symlinking ~/.config/mimeapps.list to it would probably work in this case.