Create desktop files to open applications via xdg-open in wsl

Hi,

I use wsl in my laptop, it is a debian wsl, in it i have home manager installed.

I wanted to open pdf files with windows default application via xdg-open, for this i was trying to make a desktop file and set it as default but i cant seem to get it to open the files i want

This is my home-manger.xdg config

   xdg = {
        enable = true;
        mime.enable = true;

        desktopEntries = {
            windows = {
                name = "Window";
                genericName = "Windows";
                exec = ''wslview %U""'';
                terminal = false;
                categories = [ ];
                mimeType = [ "text/html" "text/xml" "application/pdf" ];
            };
        };

        mimeApps = {
            enable = true;
            associations.added = {
                "application/pdf" = "windows.desktop";
            };

            defaultApplications = {
                "text/plain" = [ "neovim.desktop" ];
                "application/pdf" = [ "windows.desktop" ];
            };
        };
        
        userDirs = {
            enable = true;
            createDirectories = false;
        };

        systemDirs.data = [ "${config.home.homeDirectory}/.nix-profile/share/applications" ];
    };

and this is the output i get when i try to open a pdf file via xdg-open

WARNING: You don't seem to have a mime-info database. The shared-mime-info package is available from http://freedesktop.org/ at /usr/bin/mimetype line 175.
No mimeinfo database found
xdg-mime: mimetype argument missing
Try 'xdg-mime --help' for more information.
Error: no "view" mailcap rules found for type "application/pdf"
WARNING: You don't seem to have any mimeinfo.cache files.
Try running the update-desktop-database command. If you
don't have this command you should install the
desktop-file-utils package. This package is available from
http://freedesktop.org/wiki/Software/desktop-file-utils/
WARNING: You don't seem to have a mime-info database. The shared-mime-info package is available from http://freedesktop.org/ at /usr/bin/mimeopen line 129.
No mimeinfo database found

Thank you for any and all help.

Curious if you have shared-mime-info installed?

I was also looking at this post and thought you may wish to try the following to debug a bit:

XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query filetype some-pdf-file.pdf

XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query default application/pdf

i do have the package update-mime-database so i assume i have the shared-mime-info package installed.

Furthermore after XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query default application/pdf I only seem to get nothing:

Checking /home/azevedo/.config/mimeapps.list
Checking /home/azevedo/.local/share/applications/mimeapps.list
Checking /home/azevedo/.local/share/applications/defaults.list and /home/azevedo/.local/share/applications/mimeinfo.cache
Checking /home/azevedo/.local/share/applications/defaults.list and /home/azevedo/.local/share/applications/mimeinfo.cache
Checking /home/azevedo/.nix-profile/share/applications/applications/defaults.list and /home/azevedo/.nix-profile/share/applications/applications/mimeinfo.cache
Checking /home/azevedo/.nix-profile/share/applications/applications/defaults.list and /home/azevedo/.nix-profile/share/applications/applications/mimeinfo.cache

And i should note that the file /home/azevedo/.config/mimeapps.list is generated by home-manager and contains the following:

[Added Associations]
application/pdf=windows.desktop

[Default Applications]
application/pdf=windows.desktop
text/plain=neovim.desktop

[Removed Associations]

Following this I tried running XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query filetype some-pdf-file.pdf only to be met with:

Running mimetype --brief --dereference "/home/azevedo/Docs/a.pdf"
WARNING: You don't seem to have a mime-info database. The shared-mime-info package is available from http://freedesktop.org/ at /usr/bin/mimetype line 175.
No mimeinfo database found

Are you able to find the windows.desktop file that was created? I wonder if case-sensitivity is an issue.

Yes, it is at ~/.nix-profile/share/applications/windows.desktop

I already added it’s folder to $XDG_DATA_DIRS as i thought that was the issue

for reference this is the windows.desktop file:

[Desktop Entry]
Exec=wslview %U
GenericName=Windows
MimeType=text/html;text/xml;application/pdf
Name=Window
Terminal=false
Type=Application
Version=1.4

I have been thinking about your problem, and I keep coming back to the text of the warning: You don't seem to have a mime-info database. What happens if you install shared-mime-info, just to be safe?

Pretty sure sudo update-mime-database /usr/share/mime is not the right thing to do on nix, but that would typically solve this on other distros.

I installed shared-mime-info package trough home-manager home.packages option, and i ran the command upadte-mime-database but no luck, is still get the exact same error

@Azeved00 I am trying to replicate your issue. I am using NixOS, though, not Debian. But I am using WSL 2.

And, I agree, the behavior of xdg-open is weird. I am not getting the exact same errors and issues as you. But no matter what I do, xdg-open insists on opening a PDF with elinks (which I have installed), even though xdg-mime query filetype and xdg-mime query default application/pdf do, in my case, return the correct values. Go figure.

For an entertaining read/rant on xdg-open, see this thread.

So… might you consider using an xdg-open alternative, like handlr or wsl-open? Both are available in nixpkgs. If I were in your shoes (which I kinda am, using WSL on my laptop at work), I would just use wslview by itself, or look at handlr.

Something else interesting: what happense if you execute the following?

export BROWSER=wslview

Then try xdg-open? I was pleasantly surprised by the results in my setup.

1 Like

Ill stick with wslview as it does the job, i will also take a look at handlr, but it is sad to see that it does not options in home-manager.

This:

export BROWSER=wslview

does the job, so i think I will use it while searching for a better solution.

Thank you so much for your answers!

1 Like

Glad that works for you! I believe there are conditions in which xdg-open does not use xdg-mime, but instead uses file for some reason. Maybe when it is… a file? I need to find where I read that.

I have played with handlr, and really like it. It kinda replaces xdg-open. Let us know if you get a chance to try it.