Open LunNova opened 2 years ago
Related issues:
The
xdg-open
package in NixOS should usexdg-desktop-portal
to launch apps/urls/etc when possible as if it is inside a flatpak to avoid running apps in unexpected FHS envs or with unexpected env from wrappers.
Before we reinvent any wheels, how does Flatpak solve this problem?
xdg-utils detects $XDG_RUNTIME_DIR/flatpak-info
existing and uses xdg-desktop-portal
. This implementation is incomplete and will only handle URLs but hopefully it wouldn't be too hard to get local file support added upstream like in the script I linked.
I'm not sure if making xdg-open
think it's in a flatpak in general is safe or if it'd be better to get a more nix-specific toggle added.
I'm not sure if making
xdg-open
think it's in a flatpak in general is safe
I mean, it basically is?
Is there a non-Flatpak-specific way to handle portals? I imagine Snap and AppImage must be doing it somehow too. If they also use the Flatpak method, we should use it too. If they use a more generic way, we should use that.
I was concerned that telling xdg-utils it's in a flatpak by making sure that flatpak-info
exists would cause problems with unrelated things.
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/who-uses-nixos-with-kde/18212/10
So is this why links from Discord open in Chrome instead of my default browser? :thinking:
@PAI5REECHO
So is this why links from Discord open in Chrome instead of my default browser? thinking
It's possible although it's probably not that, it's more likely an issue with the xdg associations.
If you use home-manager, try something like xdg-mime-apps.nix
@LunNova
it's more likely an issue with the xdg associations
It's not that, firefox is the only thing I have set in my .config/mimeapps.list
I've implemented a workaround for this as a nixos module which is exposed at nixosModules.xdg-open-workaround
in github:LunNova/nixos-configs/dev
.
Implementation at https://github.com/LunNova/nixos-configs/blob/dev/modules/exported/xdg-open-workaround.nix and https://github.com/LunNova/nixos-configs/blob/dev/packages/xdg-open-with-portal/default.nix
Is this approach worth upstreaming?
Is #176962 related to this issue?
@LunNova yes please
I've implemented a workaround for this as a nixos module which is exposed at
nixosModules.xdg-open-workaround
ingithub:LunNova/nixos-configs/dev
.Implementation at https://github.com/LunNova/nixos-configs/blob/dev/modules/exported/xdg-open-workaround.nix and https://github.com/LunNova/nixos-configs/blob/dev/packages/xdg-open-with-portal/default.nix
Is this approach worth upstreaming?
I would be glad if this problem can be solved generically, as it effects many applications (for me for example zettlr and marktext). I haven't tried your workaround, but it looks good when looking at the files.
@LunNova How can I install your nixos module?
Add github:LunNova/nixos-configs/main
as a flake input:
inputs = {
...
lunnova-nixos-configs.url = "github:LunNova/nixos-configs/main";
...
}
Add it as an argument to outputs, then add lunnova-nixos-configs.nixosModules.xdg-open-workaround
into the modules list you're passing to lib.nixosSystem.
Not sure how best to do it if you don't use flakes.
I'll try to get a PR to include this in nixpkgs shortly.
@LunNova I haven't completely tried your approach, but one thing I noticed is that on my system ulimit -Sn
is 1024
, so just running exec 9999< /some/file
fails. Maybe pick your favorite three-digit number as the fd?
Been somewhat distracted, hope to make a PR for this tonight. (Yes, this comment is an accountability post for myself so I'll actually do it :))
Draft PR up here: #187293
@deifactor I swapped it to use bash's "new" (from 2009 lol) syntax for auto selecting an available FD instead, hopefully that works on your machine!
Looks like it works (that is, exec {foo}< /etc/passwd; echo $foo
echoes 10, so I assume this means the syntax is compatible). Thanks!
Not commenting on the quality of the fix itself, more "how did we get here".
So if I understand correctly, the problem is that your FHS application can have an arbitrary environment, which get passed to xdg-open, which get in turn passed down to Firefox. On most distributions, this isn't a problem because you don't do weird stuff with environment variables (or if you do, you do so for your entire user session so you'll break firefox immediately), but nixpkgs' isolation means that the FHS application might have LD_PRELOAD, LD_LIBRARY_PATH, or some other weird thing set that isn't set when invoking it normally.
LD_*
)? Obviously having any fix is better than nothing, but it feels a little weird that this solution relies on the fact that portals happen to exist, if that makes sense.Doesn't any solution that involves gdbus (or any 'wrapper' program) run the risk of having the same issue if you set some env variable in a way that makes gdbus sad?
Yes, but it has a limited scope as once you manage to make a dbus call you're talking to the portal which isn't inheriting your environment, and it's only one combination to test (your program's env/fhs + gdbus) instead of your program's env/fhs + any other program in your system it might launch.
Can we fix this by just having xdg-open clear out variables that are suspected to be problematic (LD_*)? Obviously having any fix is better than nothing, but it feels a little weird that this solution relies on the fact that portals happen to exist, if that makes sense.
This works for wrapped programs but not FHS envs. FHS envs use namespaces/cgroups, you can't undo this just by clearing an environment variable.
If your solution only needs to work for wrapped programs, clearing a set of env variables works.
Ah, I wasn't aware of that re: FHS envs. (How that all works is not exactly within my wheelhouse.)
Opened #197118 with an alternative approach. (#187293 was the original which relied on xdg-open in PATH working or would cause mass rebuilds toggling the setting)
Looking for help getting either approach in.
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
xdg.portal = { enable = true; xdgOpenUsePortal = true; }
should work around this issue.Please report here if it causes any new bugs or does not resolve your issue, along with your desktop environment and the app involved.
@LunNova Is there a way to have xdg-open
still support file://
URIs when using xdgOpenUsePortal = true
? I noticed xdg-open
uses org.freedesktop.portal.OpenURI.OpenURI
which explicitly doesn't support file://
URIs, and that you also mentioned this in the upstream PR.
If there's no fix for this, it might be worth it to mention it in the option's description, since many applications rely on file://
URIs.
We can probably patch support in in nixpkgs' version. I still haven't heard anything from upstream about the last PR which is inconvenient for getting it fixed for everyone :/
Sounds good, is it a simple patch to open_flatpak
that checks for the file
protocol and uses OpenURI.OpenFile
/OpenURI.OpenDirectory
instead?
As a temporary workaround for my system, I'm using flatpak-xdg-utils
, which provides its own xdg-open
that works with file://
.
https://github.com/huantianad/nixos-config/commit/147cb7e91d75d4764eb031875b8e527e1927be21 the main annoyance here is that it causes a rebuild of a bunch of applications that I don't want to build locally (like chromium) 😅
I am experiencing #189851 while trying this with a standalone wm. Just in case someone has the same issue.
It seems that when I tried using this option xdgOpenUsePortal = true
it fixed my issue where I couldn't open links from within vscode-fhs
however now I had problems using xdg-open
command from the terminal normally, which also made so some python application wouldn't open an image because it relied on this command. As an example:
~ ✦ ❯ xdg-open background.png
(objectpath '/org/freedesktop/portal/desktop/request/1_11449/t',)
~ ✦ ❯
No image would open and I would just get that printed to stderr
. Turning this option off and logging off and back on solved the issue but then I am back to not being able to open links in FHS. I am not really sure how but it seems this option messes up with my non FHS environment.
I've opened PR #217590 with a fix for using xdg-open
on filenames when xdgOpenUsePortal = true
@lilyinstarlight Thanks for your patch. A few days ago it landed on NixOS unstable so I tried it.. It seems that it fixed opening folders with xdg-open
but didn't fix opening files. I'm still having the same behaviour that @MathiasSven showed in https://github.com/NixOS/nixpkgs/issues/160923#issuecomment-1411872545
TL;DR;
The plasma-xdg-desktop-portal-kde
needs an additional plasma-workspace
package as build input.
Ok. Got the reason.
It turns out that with the new patched xdg-open
it is possible to open some files, while for some other files it does nothing.
I am using plasma-xdg-desktop-portal-kde
as backend for xdg-desktop-portal
and for every xdg-open
invocation that "does nothing", I could see the following log from plasma-xdg-desktop-portal-kde
.
xdg-desktop-portal-kde[3318]: QQmlApplicationEngine failed to load component
xdg-desktop-portal-kde[3318]: qrc:/AppChooserDialog.qml:15:1: module "org.kde.plasma.w>
xdg-desktop-portal-kde[3318]: QObject::connect: Cannot connect (nullptr)::accept() to >
xdg-desktop-portal-kde[3318]: QObject::connect: Cannot connect (nullptr)::reject() to >
xdg-desktop-portal-kde[3318]: Failed to load dialog, cannot exec
I suspected that the files that worked were the files for which plasma-xdg-desktop-portal-kde
was able to pick a default program to use to open the file, while for the files that didn't work plasma-xdg-desktop-portal-kde
tried to open an application picker dialog but failed and printed those logs. Thus I tried to add the dependency (plasma-workspace
) that is presumably missing to the xdg-desktop-portal-kde
package. I did it via overlay.
let
pkgs-unstable = import inputs.nixpkgs-unstable {
system = pkgs.system;
};
in {
nixpkgs.overlays = [
(final: prev: {
xdg-desktop-portal-kde = pkgs-unstable.xdg-desktop-portal-kde.overrideAttrs (oldAttrs: rec {
buildInputs = oldAttrs.buildInputs ++ [pkgs-unstable.plasma-workspace];
});
})
];
}
After applying this change, indeed xdg-open
started to open an application picker for the files that previously didn't work.
@chenlijun99 interesting, I haven't had any issues with opening files, but actually when opening directories, if dolphin is already open, but I use xdg-open
to open a new folder that isn't already open in the existing dolphin, it'll instead open a completely new instance of dolphin instead of opening it in my existing dolphin window. I'd assume this is a dolphin bug, but I can't seem to find it online anywhere. Wanted to mention this in case anyone else had this same issue and had a fix.
@chenlijun99 I assume this could just be PRed then no?
Could those affected test the PR above?
I made some notes during my efforts with this problem in the hope that it might be helpful:
element-desktop
from unstable, I couldn't open links.xdgOpenUsePortal
didn't help.
xdg-open
from the terminal for me as well.(objectpath '/org/freedesktop/portal/desktop/request/1_168/t',)
in the terminal with nothing else happening when trying to run xdg-open
on any file or link.168
in the output of xdg-open
is only an example, the actual number varies. To be specific, on each invocation it gets incremented by 2. This includes clicking links in Element, so if I'd get 168 in xdg-open, click a link in Element, use xdg-open again, I'd get 172. Quite hilarious, honestly.dbus-monitor
when running xdg-open
.xdg-utils
from unstable, which should include the patch from above, doesn't help either.xdg-open
on a file which doesn't have any handlers available. With xdg-utils
from 22.11 I only get the objectpath
thingy, with unstable xdg-utils
I get a "No Apps Available" window.xdg-desktop-portal-gtk
from both unstable and 22.11.xdgOpenUsePortal
set.I'm using wayland with sway and xdg-desktop-portal-wlr
and xdg-desktop-portal-gtk
. For reference, my config.
Finally, some logs dbus-monitor
spews out when running xdg-open
. Interesting stuff seems to be mostly in the end. My observations: it seems to have found vivaldi-stable.desktop, which is indeed the desktop file it should use to launch the link. Also, the final message is:
error_name=org.freedesktop.DBus.Error.ServiceUnknown reply_serial=777
string "The name :1.346 was not provided by any .service files"
and 346 is the number that xdg-open
showed in the objectpath
message from above. So yeah, no idea what's happening there.
So is this why links from Discord open in Chrome instead of my default browser? thinking
I'm having a similar but with joplin-desktop
and in my case Chromium
is launched instead despite not even being mentioned in XDG outputs (see below).
I have enabled xdg.portal
in nixos-22.11 though:
xdg.portal = {
enable = true;
xdgOpenUsePortal = true;
extraPortals = [ pkgs.xdg-desktop-portal-gtk ];
};
Chromium
is not even among the default http/https handlers:
% nix-shell --pure -p glib
$ gio mime x-scheme-handler/https
Default application for ?x-scheme-handler/https?: userapp-Firefox-IN1NE1.desktop
Registered applications:
userapp-Firefox-IN1NE1.desktop
google-chrome.desktop
Recommended applications:
userapp-Firefox-IN1NE1.desktop
google-chrome.desktop
$ gio mime x-scheme-handler/http
Default application for ?x-scheme-handler/http?: userapp-Firefox-IN1NE1.desktop
Registered applications:
userapp-Firefox-IN1NE1.desktop
google-chrome.desktop
Recommended applications:
userapp-Firefox-IN1NE1.desktop
google-chrome.desktop
Using gio open http://
opens a new Firefox window, as does https://
...
Awfully confused about this. :thinking:
@unode These things run under the xdg-desktop-portal
systemd user service
Can you see if systemd-run --user -t gio mime x-scheme-handler/https
correctly shows the default?
If not, you may need to make sure XDG_DATA_DIRS
+PATH
is correctly imported/set for systemd user scope (I did actually consider adding a systemd.user.services.xdg-desktop-portal.path = lib.mkAfter [ "/run/current-system/sw" ]
to the xdg portal module in NixOS, so that the behavior is less surprising by default)
@lilyinstarlight
% systemd-run --user -t gio mime x-scheme-handler/https
Running as unit: run-u37615.service
Press ^] three times within 1s to disconnect TTY.
Default application for “x-scheme-handler/https”: userapp-Firefox-IN1NE1.desktop
Registered applications:
userapp-Firefox-IN1NE1.desktop
firefox.desktop
chromium-browser.desktop
google-chrome.desktop
Recommended applications:
userapp-Firefox-IN1NE1.desktop
firefox.desktop
chromium-browser.desktop
google-chrome.desktop
and a similar output for http/https.
It doesn't explain why Firefox isn't launched. But at least it's clear where Chromium is coming from.
Adding /run/current-system/sw
to PATH configuration doesn't seem to make a difference.
@unode If you drop into an interactive shell with systemd-run --user -t -S
, are you able to get firefox to open with gio open https://nixos.org/
?
@lilyinstarlight yes. Both gio open
and xdg-open
open a new nixos.org tab in Firefox:
% systemd-run --user -t -S
Running as unit: run-u38802.service
Press ^] three times within 1s to disconnect TTY.
% gio open https://nixos.org/
% xdg-open https://nixos.org/
(objectpath '/org/freedesktop/portal/desktop/request/1_38806/t',)
Wait. So xdg-open there (which still goes through portal) opens firefox and xdg-open from a normal shell opens chromium?
Edit: I just re-read your message and joplin-desktop is what is opening links in chromium -- does that internally use xdg-open?
Yes, as far as I can tell joplin-desktop
uses xdg-open
. I can see it on an strace
, however I can't figure out why Firefox isn't launched and Chromium is used instead.
I did however recently have a x-www-browser
and symlink www-browser
script in PATH
that was being executed. Having this script run an arbitrary command worked but having firefox URL
in it caused Chromium to launch again.
Right now it looks like, for some reason, Firefox fails to launch and Chromium is used as fallback.
I wonder if joplin-desktop is somehow losing the NIXOS_XDG_OPEN_USE_PORTAL
environment variable, causing xdg-open to use default logic instead of delegating it to the portal like it's supposed to
Ok, a few jumps ahead and it turned out that the version of joplin I had on my environment was bundling an unpatched xdg-open
that ignored NIXOS_XDG_OPEN_USE_PORTAL
. I thought it was using the system xdg-open
but I noticed in strace that it was launching /usr/bin/xdg-open
which after inspection pointed to: /nix/store/hr3ks9p076js1r8v4p531nml3k0cqsvb-joplin-desktop-2.8.8-usr-target/bin/xdg-open
.
Updated joplin and now Firefox is opening as expected. Confusion mostly cleared though still surprised with the behavior.
@lilyinstarlight Thanks for the help troubleshooting this. Your last comment pointed me in the right direction.
Edit: Perhaps a warning to future readers of this thread. Even though the system's xdg-open
may have been patched to use the portal, a bundled xdg-open
may still lack the workaround.
I also was having a similar issue where xdg-open
wasn't using my defaults. However, the issue ended up unrelated to this. For some reason, bepinex's doorstop was adding the line "Found UnityPlayer, hooking into it instead" to the start of my .config/mimeapps.list
whenever I launched a game with bepinex modding... I've yet to figure out why the heck it's doing this, but I thought I'd mention it in case it helps someone somehow!
@lilyinstarlight
If not, you may need to make sure XDG_DATA_DIRS+PATH is correctly imported/set for systemd user scope
What's the correct way to do this?
I'm seeing this behavior:
$ nix shell pkgs#glib -c systemd-run --user -t gio mime x-scheme-handler/http
Running as unit: run-u30.service
Press ^] three times within 1s to disconnect TTY.
No default applications for “x-scheme-handler/http”
$ nix shell pkgs#glib -c gio mime x-scheme-handler/http
Default application for “x-scheme-handler/http”: firefox.desktop
Registered applications:
firefox.desktop
Recommended applications:
firefox.desktop
Describe the bug
When calling
xdg-open
from a FHS env or wrapped app, it tries to launch the associated program directly while either having unexpected/unrelated environment variables likeLD_PRELOAD
set or worse being in an FHS env which is entirely missing some dependencies.Ideally,
xdg-open
when used in a FHS env or wrapped package would usexdg-desktop-portal
to launch apps instead, similarly to how flatpaks use this to open other applications outside of the package.There is partial upstream support for this but it only applies in flatpaks:
https://github.com/freedesktop/xdg-utils/blob/1a58bc28f6844898532daf9ee1bf6da7764955a9/scripts/xdg-open.in#L250
https://github.com/freedesktop/xdg-utils/blob/d11b33ec7f24cfb1546f6b459611d440013bdc72/scripts/xdg-utils-common.in#L361-L363
It also doesn't handle local files correctly.
An example script which also handles files is here.
Steps To Reproduce
One example of this issue, although there are many related issues in different contexts.
Steps to reproduce the behavior:
Expected behavior
The
xdg-open
package in NixOS should usexdg-desktop-portal
to launch apps/urls/etc when possible as if it is inside a flatpak to avoid running apps in unexpected FHS envs or with unexpected env from wrappers.Screenshots
Notify maintainers
@edolstra - xdg-utils maintainer entry @Atemu @illegalprime @wentasah - work on bwrap FHS envs
Metadata
Please run
nix-shell -p nix-info --run "nix-info -m"
and paste the result.