Trying to understane MIME

I was having problems changing the default browsers, but I eventually solved it by editing /etc/environments and setting BROWSER env in .bashrc.

However, it still seems like Firefox is triggered in some situations which I guess might be related to MIME (via xdg-open, not sure, still learning).

I also just installed a PDF viewer, but Chrome is opened when I for example use xdg-open . Trying to make sense of how all this works.

I found https://discovery.endeavouros.com/applications/default-applications/2022/01/

but it appears to be outdated? Neither /usr/local/share/applications/mimeapps.list, nor /usr/share/applications/mimeapps.list exists.

Furthermore, the article has a screenshot of a GUI applications that looks like something that would be great to have, but doesn’t mention its name.

Anyone want to give points?

Your local (not available system-wide) default applications list is defined in this file: ~/.config/mimeapps.list

Inside that file, you will typically see something like this:

[Default Applications]
application/pdf=someapplicationname.desktop
image/jpeg=someapp.desktop
..
..

Internally, pre-defined mime types (image/jpeg, application/pdf, etc.) are stored in databases (~/.local/share/mime and /usr/share/mime). These databases are essentially directories with a certain structure containing a bunch of files.

You can even define your own mime type by creating an xml file specifying the properties of the new mimetype and then manually update your mime database with cli tools.

For instance, here is a mimetype I created for my own use: application/cherrytree

I first create an xml (application-cherrytree.xml) inside ~/.local/share/mime/packages (it’s important for the xml file to be placed in this directory so that the tools I mention later can find it). That xml file has the following content:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/cherrytree">
        <comment>Cherry Tree Note</comment>
        <glob-deleteall/>
        <glob pattern="*.ctb"/>
    </mime-type>
</mime-info>

The xml file provides sufficient information for mime tools to be able to “detect” and “identify” the application/cherrytree mime type. If you look at the contents of the xml file, you can already guess what it does: it tells the mime utilities to associate any file with the extension .ctb as a application/cherrytree type.

At this point, although I have written the xml file, the mime system still doesn’t know about my new mime type yet. I have to register it inside the mimetype database. The shared-mime-info package provides a command line tool called update-mime-database to do just that. If you run this command:

$ update-mime-database

What happens is that it will look for all the .xml files inside ~/.local/share/mime/packages and check whether the mime types defined in those .xml files are already registered inside the mime databases. In our example, the application/cherrytree mime type is newly defined (it is not yet registered), so update-mime-database will add it into database by parsing the specifications contained inside application-cherrytree.xml.

After the new mime type has been registered, the final step is to associate .desktop files with it. Application packages typically ship with their own .desktop files (look inside /usr/share/applications if you’re curious). These associations are entered inside the ~/.config/mimeapps.list file I mentioned previously.

For more information, refer to https://wiki.archlinux.org/title/XDG_MIME_Applications

The article has all the details.

1 Like

There are two ways to change your default applications manually:

  1. Use xdg-mime
  2. Edit ~/.config/mimeapps.list manually.

Here are a few example use cases of xdg-mime:

  1. To find out the mime type of a pdf file foobar.pdf, run xdg-mime query filetype foobar.pdf ( it should return application/pdf)
  2. To find out the default .desktop file currently associated with application/pdf, run xdg-mime query default application/pdf.
  3. If you want to change the default application for all pdf files, run xdg-mime default /path/to/mypdfapp.desktop application/pdf.

For this, you have to be more specific regarding the use case in question. What was the trigger that launched the browser? For example, did you click on a .html file on your filesystem? Or did you click on a link in your terminal or some other application? Either way, you can try to change the default desktop file associated with the text/html mime type first:

$ xdg-mime default chrome.desktop text/html

P/S: I’m not sure about the name of Chrome’s desktop file. You should check the /usr/share/application directory to verify it.

1 Like

Thank you!

Invoking xdg-open "http://www.google.com" would open the URL in Firefox. I think I eventually tracked it down to /usr/share/applications/mimeinfo.cache which contained an entry for x-scheme-handler/http for both firefox, and Chrome. But Firefox was listed first, and I take that to mean that it had higher priority?

Regardless, I simply uninstalled Firefox and that made Chrome the default.

As for PDF viewer, as per your instructions I used xdg-mime and I see now an expected entry in ~/.config/mimeapps.list

Yes. That will automatically use the .desktop file defined in the entry the next you (or any application, for that matter, like your GUI file manager) invoke xdg-open.

You didn’t have to go as far as uninstall firefox. Just do xdg-mime default chrome.desktop x-scheme-handler/http

Great, thanks again. If I understand it correctly I need to learn more about XDG, that XDG is the underlying specification that you can (reasonably but not necessarily) assume that you desktop environment will use.

Yeah, I understand that too. But firefox was listed for many entries anyway, and I didn’t want to use Firefox at all.

Your GUI file manager uses xdg tools under the hood. For example, when you change default applications to open files by right-clicking a file to bring up a context menu. If you are curious, I highly recommend checking out that link to the arch wiki page I posted in my original reply.

I am, and I’m currently reading it. It all makes a lot of sense.