Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default applications for xserver specified by mime types #5966

Closed
wants to merge 1 commit into from

Conversation

matejc
Copy link
Contributor

@matejc matejc commented Jan 25, 2015

A much needed feature!

This works in enlightenment and should work in other window/desktop managers, I haven't touched any specific window/desktop manager settings.

When you set

services.xserver.defaultApps = [
    {mimetypes = ["text/plain" "text/css"]; exec = "${pkgs.sublime3}/bin/sublime";}
    {mimetypes = ["image/png" "image/jpeg" "image/gif" "image/x-apple-ios-png"]; exec = "${pkgs.e19.ephoto}/bin/ephoto";}
    {
        mimetypes = ["video/x-msvideo" "video/mp4" "application/mp4" "video/x-m4v" "video/x-ms-wmv"
            "video/x-matroska" "video/quicktime"];
        exec = "${pkgs.mpv}/bin/mpv --hwdec=vdpau --vo=vdpau --no-terminal --force-window";
    }
];

then for example: plain text files beside css files should be opened by sublime text editor.

Right mime types are tricky to find and different applications see for the same file differently, for example:
Mimetype image/png is supposed to match png files, but enlightenment_filemanager sees this as image/x-apple-ios-png.

And yes, I know that I am creating two the same files named differently: defaults.list and mimeapps.list, the first one should be deprecated but for example enlightenment file manager uses it (I strace'd it).

Suggestions and questions are more than welcome before merging.

@offlinehacker
Copy link
Contributor

Looks cool :)

@matejc
Copy link
Contributor Author

matejc commented Jan 26, 2015

Please do try it out in your desktop environment.

@pSub
Copy link
Member

pSub commented Jan 29, 2015

Looks really cool, I will try to give it a try on the weekend.

@vcunat
Copy link
Member

vcunat commented Jan 29, 2015

Looks nice, although I'm not sure if having it under services is the best places, as it looks mostly a user-specific thing. Maybe we could factor the core out into something like pkgs.mkDefaultApps which could then be just added to systemPackages or installed via nix-env or activated by the option you suggest.

But I guess we can do such improvements later, even move this into another place if we find a better one.

@matejc
Copy link
Contributor Author

matejc commented Jan 30, 2015

I like the pkgs.mkDefaultApps idea. I will refactor few things that are bothering me and move the script somewhere else (any suggestions?), but I will leave services.xserver.defaultApps, at least for now.

@edolstra
Copy link
Member

Hm, not sure what this is for. Users shouldn't need to specify MIME type associations, applications should do that by creating an appropriate .desktop file in $out/share/applications.

@matejc
Copy link
Contributor Author

matejc commented Jan 30, 2015

@edolstra at least in E19 and i3 (that I tested), that way is not really working that well - well its not. Probably because new entries can not be auto-added to $out/share/applications/mimeapps.list because it is read-only.
defaults.list should be deprecated but some desktop managers still use it... otherwise defaults.list == mimeapps.list.

The modern method to start applications is using Desktop entries. This way, programs can advertise which kind of files (to be exact: what MIME-types) they can open. For instance, gimp.desktop states MimeType=image/bmp;image/gif;....

freedesktop.org recommends how to specify default (preferred) applications for MIME-types in their Association between MIME types and applications standard. This involves writing into files called mimeapps.list

Taken from: https://wiki.archlinux.org/index.php/Default_applications

Entries inside mimeapps.list and those mimetype entries inside .desktop files are not solving the same problem. mimeapps.list are setting default apps. And entries inside .desktop files are there for desktop managers or various apps for the way of knowing what apps can open what files. There is one more file that I should mention... ~/.local/share/applications/defaults.list, when you open an app that is inside $out/share/applications/defaults.list it will copy that entry to the file (~/.local/share/applications/defaults.list) inside your home. The result is that we get deterministic way to set default apps for specific file types, which is what I was looking for.
As far as I tested this pull request, it works in E19 and KDE5.

@vcunat
Copy link
Member

vcunat commented Jan 31, 2015

For systemPackages the mime database should be fine thanks to https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/system-path.nix#L126 (which is not done with nix-env).

@matejc
Copy link
Contributor Author

matejc commented Jan 31, 2015

yeah, I know, I am still figuring out how to solve that, I can not just run the update-mime-database because on system rebuild, it would do that twice, and this command takes its time to run, probably as an input parameter like ... pkgs.makeDefaultApps { inherit applist; updateMimeDatabase = true; };

or there is another solution... throw out this 3 lines: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/system-path.nix#L128
like the comment is somehow suggesting that: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/system-path.nix#L125

... what do you say?

@matejc matejc self-assigned this Jan 31, 2015
@matejc
Copy link
Contributor Author

matejc commented Jan 31, 2015

I found a bug that has a bigger priority...
(At least) in E19 it has own cache for default apps, and it only refreshes when desktop file name changes, example:
desktop file name is generated from:

name = (zeroArgv (lastInPath exec));

ex: from /nix/store/hash-app-0.1/bin/app --switch1 arg1 -> name == "app"

so it does not distinguish these entries:

exec = "/nix/store/hash-app-0.1/bin/app --switch1 arg1"

and

exec = "/nix/store/hash-app-0.1/bin/app"

and

exec = "app"

Solution 1: get a hash (somehow - runCommand?) from exec parameter and stick it in the name example "${name}-${hash}.desktop", (the desktopName - the name which we see inside for example file managers.. will stay the same - without hash in its name), with this solution I can not use the hash from the file path as path can be like this: "/run/current-system/sw/bin/app", but this solution seems over-engineered...

suggestions?

@matejc
Copy link
Contributor Author

matejc commented Jan 31, 2015

fixed the last one.. I knew I saw builtins.hashString somewhere before... its quite a small fix, and it works, I like

@matejc
Copy link
Contributor Author

matejc commented Jan 31, 2015

This pull request is ready to try out in different desktop environments
and just to recap: it works with the services.xserver.defaultApps option which adds a package to systemPackages with appropriate desktop, mimeapps.list and defaults.list files, example is in the code and on the top of this pull request. You might need to re-login or restart your desktop manager service.

@matejc
Copy link
Contributor Author

matejc commented Jan 31, 2015

added support for xdg-open with fallback to mimeopen which uses .cache file

@matejc
Copy link
Contributor Author

matejc commented Feb 10, 2015

  • add option noDisplay to make-desktopitem/default.nix and make all generated desktop files to NoDisplay=true so they do not appear in menus
  • add only one file per multiple mimetypes
  • a bit of refactoring
  • rebased to master

PS: Has anyone already tried this feature on their setup?

@domenkozar
Copy link
Member

I'm going to close this, we can introduce declarative configuration for mimetypes, but it shouldn't be specific to X11.

For example: https://help.ubuntu.com/community/AddingMimeTypes

@domenkozar domenkozar closed this Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants