How do I set mimeApps without listing every variation (or: how do I prevent calibre opening everything)

Hi all,

I wondered whether anyone had any tips for nice clean MIME associations. Currently I have the following:

xdg = {
  enable = true;
  mimeApps =
    let
      mimeTypes = {
        "image/*" = [ "org.gnome.Loupe.desktop" ];
        "image/jpeg" = [ "org.gnome.Loupe.desktop" ];
        "image/png" = [ "org.gnome.Loupe.desktop" ];
        "image/webp" = [ "org.gnome.Loupe.desktop" ];
        "image/gif" = [ "org.gnome.Loupe.desktop" ];
        "application/zip" = [ "org.gnome.Nautilus.desktop" ];
        "application/gzip" = [ "org.gnome.Nautilus.desktop" ];
      };
    in
    {
      enable = true;
      associations.added = mimeTypes;
      defaultApplications = mimeTypes;
    };
};

but listing them all like this is a bit of a pain.

Can I set an app as the default for all subtypes under a top-level? I tried with image/*, hoping this would encompass all the other image types, but it doesn’t seem to work.

Thanks :slight_smile:

Wildcards aren’t supported right now. You could use a genAttrs call to generate the attrset instead.

I had the same problem with Calibre after setting home-manager’s xdg.mimeApps.enable option.

It was annoying that MS Word docs got added to my Calibre library when I clicked on them. Perhaps worse though, Calibre’s ebook “viewer” was modifying my EPUB files.

Rather than trying to fix the multitude of mime types that Calibre needlessly registers associations with, my approach was to delete .desktop files from the Calibre package.

This could be done with

calibre.overrideAttrs (oa: {
  postInstall = ''rm $out/share/applications/calibre-*.desktop'';
})

However, such an override requires rebuilding Calibre. So, in my NixOS configuration, I added this:

{ pkgs, ... }: {
    environment.systemPackages = with pkgs; [
      calibre
      # desktop launcher for calibre - minus file associations
      (pkgs.runCommandLocal "calibre.desktop" { inherit (pkgs) calibre; } ''
        mkdir -p $out/share/applications
        sed -e '/^MimeType/d' \
          $calibre/share/applications/calibre-gui.desktop \
          > $out/share/applications/calibre.desktop
      '')
      # a better reader for epubs
      foliate
    ];

    environment.pathsToDelete = map (junk: "/share/applications/calibre-${junk}.desktop") [
      "ebook-edit"
      "ebook-viewer"
      "lrfviewer"
      "gui"
    ];
}

It would be nice to have a better solution though. I find home-manager’s xdg.mimeApps rather tricky to use. And also I lack a good method for tweaking application desktop launchers.

2 Likes

I switched to unstable because it says globs in all variations are supported (stable did not say this).

Still does not seem to work – am I misunderstanding the word ‘globs’ here?

The problem is that I am using home manager xdg not nixos xdg :slight_smile: I will see if I can add the globbing functionality to the home manager module :crossed_fingers:

My answer’s still the same.

Yes, sorry, I got into a bit of a muddle then de-muddled myself :slight_smile: thank you for your help