How to test desktop item MIME types?

Based on this post I’ve created an appimage-run desktop item and assigned it the relevant MIME types, but I’m unsure how to test it properly. I tried to simply xdg-open ${sample-appImage}, but that doesn’t seem to know about the desktop item MIME type association:

xdg-open: no method available for opening ‘/nix/store/il5f52xapdvyb11nbb5wxxhzb7axfl4s-appimagetool-x86_64.AppImage’

xdg-mime query filetype /nix/store/il5f52xapdvyb11nbb5wxxhzb7axfl4s-appimagetool-x86_64.AppImage prints “application/vnd.appimage”, so it already knows about the MIME type, but somehow doesn’t associate it with the desktop item yet.

How should I set the desktop item as the default for those MIME types? Is that automagic when $out includes desktop items? I can’t seem to find any code related to this in other packages, makeDesktopItem, or buildFHSUserEnv.

I’ve tried creating ~/.config/mimeapps.list and running update-mime-database ~/.config in the test, but it doesn’t seem to work. mimeopen --database=$HOME/.config ${sample-appImage} still complains that I need to run update-mime-database, and xdg-open ${sample-appImage} still fails in the same way.

First you need to have the directory containing applications/ directory with the desktop file in XDG_DATA_DIRS environment variable.

Maybe I’m misunderstanding, but adding ${appimage-run}/share/applications to XDG_DATA_DIRS doesn’t seem to make any difference.

You need to add the share/ directory, see Desktop Entry Specification.

Sorry, fixed, but I get the exact same result.

Try exporting the variable. Otherwise it might remain a shell variable rather than an environment one.

It’s already exported, so adding export gave the same result:

++ declare -p XDG_DATA_DIRS
declare -x XDG_DATA_DIRS="/nix/store/g2nfbpind37b3v3y6zbszmcgfmrj5v7x-patchelf-0.15.0/share"
++ export XDG_DATA_DIRS=/nix/store/g2nfbpind37b3v3y6zbszmcgfmrj5v7x-patchelf-0.15.0/share:/nix/store/xfdxs98xlwpnb7rbpbxnchh0ni1s399r-appimage-run/share
++ XDG_DATA_DIRS=/nix/store/g2nfbpind37b3v3y6zbszmcgfmrj5v7x-patchelf-0.15.0/share:/nix/store/xfdxs98xlwpnb7rbpbxnchh0ni1s399r-appimage-run/share
++ xdg-open /nix/store/il5f52xapdvyb11nbb5wxxhzb7axfl4s-appimagetool-x86_64.AppImage
/nix/store/f2ax95kbpk6f8k8ncmlmpi6pc4knq8kv-xdg-utils-unstable-2020-10-21/bin/xdg-open: line 881: www-browser: command not found
/nix/store/f2ax95kbpk6f8k8ncmlmpi6pc4knq8kv-xdg-utils-unstable-2020-10-21/bin/xdg-open: line 881: links2: command not found
/nix/store/f2ax95kbpk6f8k8ncmlmpi6pc4knq8kv-xdg-utils-unstable-2020-10-21/bin/xdg-open: line 881: elinks: command not found
/nix/store/f2ax95kbpk6f8k8ncmlmpi6pc4knq8kv-xdg-utils-unstable-2020-10-21/bin/xdg-open: line 881: links: command not found
/nix/store/f2ax95kbpk6f8k8ncmlmpi6pc4knq8kv-xdg-utils-unstable-2020-10-21/bin/xdg-open: line 881: lynx: command not found
/nix/store/f2ax95kbpk6f8k8ncmlmpi6pc4knq8kv-xdg-utils-unstable-2020-10-21/bin/xdg-open: line 881: w3m: command not found
xdg-open: no method available for opening '/nix/store/il5f52xapdvyb11nbb5wxxhzb7axfl4s-appimagetool-x86_64.AppImage'

Running xdg-mime in the tests reveals that MIME database is missing as well:

++ xdg-mime query filetype /nix/store/il5f52xapdvyb11nbb5wxxhzb7axfl4s-appimagetool-x86_64.AppImage
WARNING: You don't seem to have a mime-info database. The shared-mime-info package is available from http://freedesktop.org/ at /nix/store/pw3gp7j2w9r5p0rwijhbqnjjqg8axdmm-perl5.36.0-File-MimeInfo-0.30/bin/mimetype line 175.
No mimeinfo database found

But moving further, the desktop file does not seem to be detected, even when using GLib rather than the wonky shell script that requires display to work:

gio: file:///nix/store/il5f52xapdvyb11nbb5wxxhzb7axfl4s-appimagetool-x86_64.AppImage: Failed to find default application for content type ?application/vnd.appimage?

You will probably need to step through the shell script to find out why it does not work:

What I tried so far:

--- a/pkgs/tools/package-management/appimage-run/test.nix
+++ b/pkgs/tools/package-management/appimage-run/test.nix
@@ -1,4 +1,7 @@
-{ runCommand, fetchurl, appimage-run, glibcLocales, file, xdg-utils }:
+{ runCommand, fetchurl, appimage-run, glibcLocales, file, xdg-utils
+, shared-mime-info
+, glib
+}:
 let
   # any AppImage usable on cli, really
   sample-appImage = fetchurl {
@@ -11,11 +14,14 @@ let
   };
 in
   runCommand "appimage-run-tests" {
-    buildInputs = [ appimage-run glibcLocales file xdg-utils ];
+    buildInputs = [ appimage-run glibcLocales file xdg-utils
+      glib
+    ];
     meta.platforms = [ "x86_64-linux" ];
   }
   ''
     export HOME=$(mktemp -d)
+    mkdir -p "$HOME/.config"
     set -x
 
     # regression test for #101137, must come first
@@ -29,8 +35,13 @@ in
     appimage-run owdtest.AppImage
 
     # Verify desktop entry
-    export XDG_DATA_DIRS="$XDG_DATA_DIRS:${appimage-run}/share"
-    xdg-open ${sample-appImage}
+    export XDG_DATA_DIRS="$XDG_DATA_DIRS:${shared-mime-info}/share:${appimage-run}/share"
+    test "$(xdg-mime query filetype "${sample-appImage}")" = application/vnd.appimage
+
+    export XDG_UTILS_DEBUG_LEVEL=9
+    bash -x "$(type -p xdg-open)" ${sample-appImage}
+    # xdg-open ${sample-appImage}
+    # gio open "${sample-appImage}"
 
     set +x
     touch $out
1 Like

If I explicitly register the MIME type with gio mime application/vnd.appimage appimage-run.desktop then it works with gio open. But I would expect it to work regardless explicitly setting a default per mime-apps-spec:

no entry could be found, the implementation should pick the most-preferred .desktop files associated with the mimetype

xdg-open will probably need to executed using xvfb-run to work inside the builder, since it requires a display.

1 Like

Thanks, that got the test passing! It doesn’t seem to be working as expected after installing, though. Would you be willing to help some more getting appimage-run: Create desktop entry by l0b0 · Pull Request #209832 · NixOS/nixpkgs · GitHub over the line?

Hosted by Flying Circus.