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 theapplication/pgp-keys
mimetype. You can see it here: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 namedpgp-keys.xml
):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:
To install it system-wide:
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