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,
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’,
Create new configuration for the type. The name of the file should be something relevant, but is not important,
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’,
Nearly there.
Update Cache
Now check mime was registered,
Crunchtime
Make or adapt a file with the new extension,
Look at it’s mimetype,
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,
Make fun.
Any examples of config?
The common configurations are bulked into a Freedesktop common file. Warning, this file is big, pipe it out,
Why did you read mimetype with that silly line and not ‘file’?
It is widely reported that you can read mimetypes using,
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,
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,
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,
What’s happening in the Freedesktop mime directory?
You, or a package install, put configs in,
When you build cache, code generates refinements of the data like,
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,
Editing this file had no effect.
The above file is headed with a comment that recommends a local file for associations. I quote,
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?
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,
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 what is happening.
Hints for using magic
Have a look at,
Though no /etc file had any effect for me.
There is general information on magic in man pages,
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,
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,
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,