MIME Types – How to Associate Custom MIME Type with File Extensions

mime-typesxdg-open

This is part 2 of How to install a new (custom) mime type on my Linux system using CLI tools?

Using the steps in the accepted answer at the above question, I created the following mime-type mx-publickey.xml

<?xml version="1.0" encoding="utf-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="text/x-publickey">
<comment>Custom type for public key files (plain text)</comment>
    <glob-deleteall/>
    <glob pattern="*.pub"/>
</mime-type>
</mime-info>

I installed it system-wide with this command:

# xdg-mime install --mode system mx-publickey.xml

I added the desired icon:

xdg-icon-resource install --context mimetypes --size 256 x-publickey-icon.png text-x-publickey

Then I tested it. My '*.pub` files still have the old association:

$ xdg-mime query filetype id_rsa_test.pub
text/plain

$ xdg-mime query default text/plain
org.kde.kate.desktop

What additional steps are required to associate '*.pub` files with my new mime-type?

Edit:

I performed the following steps, but *.pub public key files are still not being opened by default with Kate from Electron applications.

# xdg-mime default  org.kde.kate.desktop text/x-publickey

# xdg-mime query default text/x-publickey
org.kde.kate.desktop

$ xdg-mime query default text/x-publickey
org.kde.kate.desktop

Next I used the GUI tools (KDE System Settings > Applications > File Associations) and associated *.pub public key files with Kate. Electron applications still refuse to open *.pub files with Kate.

Electron apps previously used Okular. Afer the xdg-mime default command shown above, the Okular association is gone, but nothing has replaced it. Electron apps now present a KIO dialog asking me to pick the application to use. (That's better than forcing me to use the wrong application, but it is still not correct behavior. It appears Electron applications are looking other places for the file associations. I would like to understand that.)

Am I missing a needed .desktop file in some location?

Another thought: After the above steps, I believe I should now see an entry for text/x-publickey in /usr/share/applications/mimeinfo.cache. However, there is not one.

Best Answer

The shared-mime-info repository already specifies the application/pgp-keys mimetype. You can see it here:

<mime-type type="application/pgp-keys">
  <comment>PGP keys</comment>
  <acronym>PGP</acronym>
  <expanded-acronym>Pretty Good Privacy</expanded-acronym>
  <sub-class-of type="text/plain"/>
  <generic-icon name="text-x-generic"/>
  <magic priority="50">
    <match type="string" value="-----BEGIN PGP PUBLIC KEY BLOCK-----" offset="0"/>
    <match type="string" value="-----BEGIN PGP PRIVATE KEY BLOCK-----" offset="0"/>
    <match type="big16" value="0x9501" offset="0"/>
    <match type="big16" value="0x9500" offset="0"/>
    <match type="big16" value="0x9900" offset="0"/>
    <match type="big16" value="0x9901" offset="0"/>
  </magic>
  <glob pattern="*.skr"/>
  <glob pattern="*.pkr"/>
  <glob pattern="*.asc" weight="10"/>
  <glob pattern="*.pgp"/>
  <glob pattern="*.gpg"/>
  <glob pattern="*.key"/>
</mime-type>

https://gitlab.freedesktop.org/xdg/shared-mime-info/-/blob/6bf9e4ff0fb7eff11a02bd937045bf5dc291841a/data/freedesktop.org.xml.in#L282

or here on your own machine:

/usr/share/mime/packages/freedesktop.org.xml

However, it does not use the *.pub glob pattern, probably to avoid conflicts with MS Publisher format. One workaround is just to rename the files as e.g. *.asc files. But let's continue on, assuming that renaming the files is not an option. Here is the mimeinfo file we need (note that it must be named pgp-keys.xml):

$ cat pgp-keys.xml
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
    <mime-type type="application/pgp-keys">
        <comment>PGP keys</comment>
        <acronym>PGP</acronym>
        <expanded-acronym>Pretty Good Privacy</expanded-acronym>
        <sub-class-of type="text/plain"/>
        <generic-icon name="text-x-generic"/>
        <magic priority="10">
          <match value="-----BEGIN PGP PUBLIC KEY BLOCK-----" type="string" offset="0"/>
        </magic>
        <glob weight="10" pattern="*.pub"/>
    </mime-type>
</mime-info>

The advantage of the "magic" part is that it will look at the beginning of the file for this string, then determine the mimetype based on whether or not it matches. This means that files with mimetype application/vnd.ms-publisher can still have the .pub file extension and live in relative harmony alongside public keys that also have the .pub file extension. To achieve this, we must install the mimeinfo file.

To install it for a single user:

xdg-mime install --mode user pgp-keys.xml
update-mime-database ~/.local/share/mime

To install it system-wide:

sudo xdg-mime install --mode system pgp-keys.xml
sudo /usr/bin/update-mime-database /usr/share/mime

I've tested the outcome with an example public key from here:

https://www.intel.com/content/www/us/en/security-center/pgp-public-key.html

and an example MS Publisher file from here:

https://github.com/apache/tika/blob/0bf11aec86079b8f1ae2f1ea680910ba79665c4f/tika-parsers/src/test/resources/test-documents/testPUBLISHER.pub

You can try it yourself with the git repository here:

https://github.com/nbeaver/custom-pub-file-mimetype