Create Freedesktop File Mime
How to allocate a new type to files on a Freedesktop system. The desktop must be based in Freedesktop standards. That would include many Linux distributions, and some others.
This is surprisingly easy. Note,
If you do not have this article to hand, this job is not worth your time. Avoid
This article can not provide cross‐platform code
Background to desktop file types
For the purpose of working with files, desktop software gives files a type. Then it knows that a word‐processing file should be displayed with an icon showing text, and should be launched using a word‐processor.
The types of files are identified by a ‘mime’ string. File mime strings are similar to the ‘mime’ strings used to type web‐data. What is important is that there could be two mime systems running at once. You think I am kidding? No.
Extensions and magic
On Freedesktop systems, there are two ways to decide which mime a file will have. The first is to look at the extension on the filename e.g. ‘myRubyFile.rb’. The other way is to use magic. Magic means to peek at the contents of a file to see if any clues will tell what the file is.
According to, Freedesktop specs, filename extensions beat magic. Probably because they are fast and reliable.
Howto make a new file mime type
Find and choose an XDG path
Run this to find configured paths,
$ printenv XDG_DATA_DIRS
You will see several paths. If you configure at a /home relative path, the changes will only work for the given user. If you configure at a /usr relative path, then the changes will appear systemwide.
If you want a home relative path, but there are none in XDG_DATA_DIRS, follow the instructions in extending Freedesktop paths.
Add the new mime
All examples for the new mime ‘garbage’. I’ve also assumed userspace placement.
Make/ensure the Freedesktop path has a subdirectory ‘packages’,
mkdir -p ~/.local/share/mime/packages
Create new configuration for the type. The name of the file should be something relevant, but is not important,
nano ~/.local/share/mime/packages/garbage.xml
Add something like this. The ‘glob’ attribute looks for a file extension ‘,garbage’. I’ve also added a silly magic configuration—if the file starts with three exclamation marks, it is of type ‘text/x‐garbage’,
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="text/x-garbage">
<sub-class-of type="text/plain"/>
<glob pattern="*.garbage"/>
<magic>
<match type="string" offset="0" value="!!!"/>
</magic>
</mime-type>
</mime-info>
Nearly there.
Update Cache
update-mime-database -V ~/.local/share/mime/
Now check mime was registered,
cat ~/.local/share/mime/types
Crunchtime
Make or adapt a file with the new extension,
touch test.garbage
Look at it’s mimetype,
xdg-mime query filetype test.garbage
Further advice and notes
Some of the following is warnings about other advice. You will find this advice repeated and promoted all over the web. You and me, we know it must be right because it’s on the web.
It is worth scouting your area first
Make a few test files. Try different extensions. I found, for example, that ‘.rb’ extensions did not automatically create a ‘x‐ruby’ mimetype, so those files are probably categorised by magic detection.
Knowing these behaviours from the start can spare you some grief.
I coerced a ‘Python’ file into this mime,
text/x-modelica
Make fun.
Any examples of config?
The common configurations are bulked into a Freedesktop common file. Warning, this file is big, pipe it out,
cat /usr/share/mime/packages/freedesktop.org.xml
Why did you read mimetype with that silly line and not ‘file’?
It is widely reported that you can read mimetypes using,
file -i /path/to/a/file
This will not return the mimes that a Freedesktop system will use. As far as I know, Freedesktop is a different system to the main Linux system.
You need this. You may need to install it,
xdg-mime query filetype /path/to/a/file
Another way to find types is to use the Desktop itself. The GUI should show a type via ‘File > Properties’. Of course, no real Linux users use a Desktop.
This article misspelled the mime!
The mimes look like this,
text/x-garbage
The ‘x’ is a Freedesktop idea, to try to namespace mime types a little more. See the spec.
In configuration this name is used without the slash and lowercase, so,
text-x-garbage
What’s happening in the Freedesktop mime directory?
You, or a package install, put configs in,
/usr/share/mime/packages
When you build cache, code generates refinements of the data like,
/usr/share/mime/globs
/usr/share/mime/types
...
Many recommended configuration locations are likely to fail
I have already listed the XDG environment variable method, which will work.
There’s a place which stores mime types and associates them with file extensions,
/etc/mime.types
Editing this file had no effect.
The above file is headed with a comment that recommends a local file for associations. I quote,
# Users can add their own types if they wish by creating a ".mime.types"
# file in their home directory. Definitions included there will take
# precedence over those listed here.
I found that creating and editing this file had no effect.
I think some people suggest hacking round in the Freedesktop mime directory. But most of that is generated by the cache code. Editing paths like /usr/share/mime/globs, /usr/share/mime/globs2, /usr/share/mime/types etc. will not work.
Nobody talks about overriding
Most advice on the web seems to assume a clean system. Likely you have anything but that.
Know what the mime‐type for my initial test file was?
text/x-matlab
During the course of my work, I found how this mimetype was chosen. It was a piece of magic. My file test started with a double pound, ‘##’. Now look at this MATLAB configuration I found,
<magic priority="10">
<match value="%" type="string" offset="0"/>
</magic>
<magic priority="10">
<match value="##" type="string" offset="0"/>
</magic>
<magic priority="50">
<match value="function" type="string" offset="0"/>
</magic>
The file categorisation was nothing to do with the file extension. This was fortunate for me because, as explained above, my file extension rule overrode the magic. But if you start with a a custom mimetype, beware. Try look where Freedesktop keeps the mime configurations e.g.
find /usr/share/mime | grep matlab
Find what is happening.
Hints for using magic
Have a look at,
cat /etc/magic
Though no /etc file had any effect for me.
There is general information on magic in man pages,
man magic
You see that command? I think someone made a joke. Or I want to think it is a joke.
FreeDesktop. I recall, recommend magic editing here,
/usr/share/misc/magic
On my system, this is empty.
If I wanted magic configuration, I would shove the data into the pseudo‐app mime configuration, as I did above.
Triggering app launches is by ‘mailcap’
Mailcap maps mimes to programs. I am not dealing with that here. But if you want to try launch files with different programs, this is your hint,
cat /etc/mailcap
Why is this job so difficult?
Good question. Though I feel it could be asked as, “Why is this widely adopted, centrally coded, and simple system so prone to configuration idiosyncrasy?”
References
Freedesktop mime info,
https://specifications.freedesktop.org/shared-mime-info-spec/latest/
Freedekstop base directories,
Ubuntu wiki,