MIME Types – How to Install a New Custom MIME Type on Linux Using CLI

mime-types

I would like know the full steps to create and register a new custom mime type on my system. If it matters, I am running KDE on Arch Linux.

The steps should include writing the XML file and associating an icon with the file type and anything else I need to do. I prefer to use xdg-mime command line utilities only.

I wish to understand both adding this mime association for 1) my user account as well as 2) system-wide.

For the example, let's say I want to open certificate files with a custom application I will call MyCertInspector. I believe the XML should look similar to this:

<?xml version="1.0" encoding="utf-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="text/publickey">
    <glob-deleteall/>
    <glob pattern="*.crt"/>
    <glob pattern="*.cert"/>
  </mime-type>
</mime-info>

As I understand it, when including glob-deleteall in a user-local definition, it removes any existing system-wide mime associations. I would like to confirm that my understanding is correct.

I also understand that the first step in this process is to check for the existence of a mime-type with the file extension I plan to use. I'm not sure exactly how to do that on Arch because there is no /usr/share/applications/defaults.list on Arch and (at least on my system) there is no system-wide mimeapps.list file.

For the answer, I am hoping for a step by step guide that covers every needed step and that mentions the specific directories where things should be located on an Arch Linux system.

From my research so far, I believe the general steps are:

  1. check for the existence of a mime-type with the file extension I plan to use
  2. create an XML file for my desired mime-type.
  3. register my XML file using sudo xdg-mime install [options] (and we should address both system mode and local user mode.
  4. associate the new mime-type with the application(s) used to open it.
  5. register the icon for the mime-type.
  6. anything else (such as checks & verification steps a person should do)

My goal is that anyone who reads the answer to this question will be able to master all the complete process of creating and installing a custom mime-type on their system.

Best Answer

I will start by quoting the EXAMPLES section in man xdg-mime

EXAMPLES
           xdg-mime install shinythings-shiny.xml

       Adds a file type description for "shiny"-files. "shinythings-" is used as the vendor prefix. The file type description could look as follows.

           shinythings-shiny.xml:

           <?xml version="1.0"?>
           <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
             <mime-type type="text/x-shiny">
               <comment>Shiny new file type</comment>
               <glob pattern="*.shiny"/>
               <glob pattern="*.shi"/>
             </mime-type>
           </mime-info>

       An icon for this new file type must also be installed, for example with:

           xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text-x-shiny

I wish to understand both adding this mime association for 1) my user account as well as 2) system-wide.

From the same man page

xdg-mime install [--mode mode] [--novendor] mimetypes-file
--mode mode

mode can be user or system. In user mode the file is (un)installed for the current user only. In system mode the file is (un)installed for all users on the system. Usually only root is allowed to install in system mode.
The default is to use system mode when called by root and to use user mode when called by a non-root user.

when including glob-deleteall in a user-local definition, it removes any existing system-wide mime associations

Yes, glob-deleteall is used to overwrite the glob part of a mimetype definition but not only system-wide. Both depending on the mode

mentions the specific directories where things should be located on an Arch Linux system

system mode would install to /usr/share/mime/. user mode to .local/share/mime and the file list is:

./.local/share/mime/generic-icons
./.local/share/mime/mime.cache
./.local/share/mime/types
./.local/share/mime/text
./.local/share/mime/text/x-shiny.xml
./.local/share/mime/version
./.local/share/mime/treemagic
./.local/share/mime/globs
./.local/share/mime/globs2
./.local/share/mime/aliases
./.local/share/mime/subclasses
./.local/share/mime/magic
./.local/share/mime/icons
./.local/share/mime/XMLnamespaces
./.local/share/mime/packages/shinythings-shiny.xml

After all, run

update-mime-database ~/.local/share/mime/

to activate the configuartion.

Related Question