Configure how xdg-open opens html files?

Is there a way I can configure in /etc/nixos/configuration.nix which browser to open when using ‘xdg-open’ on a file with mime type ‘text/html’?

1 Like

home-manager has a module for it. Maybe you can just edit a global file in /etc

I think the official, not distro specific way of configuring that according to their docs is via export BROWSER=firefox.

Do you mean whatever program was using xdg-open should have checked the BROWSER environment variable first instead? Because setting the BROWSER environment variable doesn’t seem to have any effect on xdg-open AFAICS.

Well I think xdg-open should check that var. It’s set it on my machine and it works. See xdg-settings man page - xdg-utils - General Commands . I can’t find docs for $BROWSER at the moment but on my machine:

$ xdg-settings set default-web-browser firefox.desktop
xdg-settings: $BROWSER is set and can't be changed with xdg-settings

Which makes sense.

Interesting, however:

$ xdg-settings get default-web-browser
$ BROWSER=firefox xdg-settings get default-web-browser
$ xdg-mime query default text/html
$ BROWSER=firefox xdg-mime query default text/html

… so xdg-mime (which is used by xdg-open) is not consistent with xdg-settings in this regard…

This may sound contrarion and I have to mince words to be polite but I’ve spent countless hours getting things to work and i’ve come to the realization that xdg-open and it’s associates and software that tries to conform to the associated spec are all 100% broken on Linux.

Here’s something really interesting- apparently xdg-open can read upto 3 files in the user home directory, not to mention it also check a global database. However, you have to be careful how you manage those 3 files. One of those files is such that it’s deleted and rewritten upon every change. So you cannot symlink it to either of the other two.

I’d love for it to work. I’d love to know if somebody can give me a single file (or two) whose contents I can change to my heart’s wish but it never works. I’ve actually thrown this challenge to a SUSE engineer who thereafter said nothing.

If this was meant for me- yes my experience is from the arch days :). And I’ve tried everything from there… Every time I see a discussion on xdg-open I say to myself, maybe, just maybe, things are better now? And I regret the following 30 minutes wasted.

1 Like

It is a tricky topic. A simple way to change default files is to open nautilus, right click properties, and default application. There you can pick the right app.

If this process does not work, you system is in a broken state. May I suggest to delete anything mime-related in you home, and try again ?

If you do not find your (installed) app, then the .desktop is not visible to the xgd ecosystem. Not sure how this is supposed to happen, but I have special logic to do that in my ~/.nixpkgs/config.nix[1] copied from nixos mime setup.

Otherwise you can as I did spend countless hours to strace -ff -e trace=%files -s1000 xdg-open and the like to discover how it works, and why. You will begin to understand how complex the system is, and all the parts that need to cooperate together for the whole system to work[2].


[2] not sure if it is because the task itself is complex, of the system ill designed.

Want some pictures ? :slight_smile:


That being said, if you want a declarative way to set the default program, I fear there is none. There may be a way to declaratively set the default default program, but that is mostly useless as any user can override that pretty easily.

There actually is. I think I’vefound the nix code which decides the global defaults (mimeapps.list?) which makes my PDFs open in inkscape. I haven’t gotten around to wasting time on that though :).

Also there doesnt have to be any complexity in the system. There is a reason why there is a freedesktop spec. Simpel- you take the global config, override with per user config (the config can have priorities maybe for each file and bam, you’ve got a system.)

There is also /run/current-system/sw/share/applications/mimeinfo.cache which seems relevant. Not sure where that one comes from, though.

1 Like

The cache is generated with a listing of all the know applications for a given mime type. That one is the system one, and will only list applications installed system-wide.

xdg-open 's behavior of overriding any symlink drives me crazy since it prevents versioning it. I sent a patch maybe ~4 years ago. It is still pending.
And while xdg-open is nice, firefox doesn’t support it, ranger has its own launcher (rifle, that you can redirect to use xdg-open). There is room for improvement for sure.

1 Like

Yes, I was indeed talking about my system-wide configuration - between my system-wide configuration and project-specific shell.nix configurations I haven’t felt a need to have any user-scoped configuration yet.

I’d love for it to work. I’d love to know if somebody can give me a
single file (or two) whose contents I can change to my heart’s wish
but it never works. I’ve actually thrown this challenge to a SUSE
engineer who thereafter said nothing.

This is the reason I use mimi [1], the drop-in xdg-open replacement. It works
wonders. Here’s my current mimi config:

text/: edit
application/pdf: zathura
text/html: browser
video/: vlc
image/: feh
audio/: vlc
application/x-tar: urxvt -e una
application/x-gzip: urxvt -e una
application/x-bzip2: urxvt -e una
application/x-rar: urxvt -e una
application/x-xz: urxvt -e una
application/zip: urxvt -e una
inode/directory: urxvt -e ranger
gopher: urxvt -e sacc
zoommtg: zoom-us
mailto: notmuch-emacs-mua --client --hello
spotify: /home/jb55/bin/spotify-open

[1] GitHub - march-linux/mimi: better xdg-open

1 Like

To be clear (and I obviously appreciate your suggestion, thank you!), my problem is with the fact that no software can properly follow the (hopefully) simple freedesktop spec. I’ve read the spec for trash and it’s very simple. I don’t know about xdg-open but it seems to me that it cannot be difficult.

XDG_UTILS_DEBUG_LEVEL=2 xdg-mime query default text/html is helpful to get insight into where xdg-mime looks to determine how to open these files.

Based on that I found Changing programs used to launch an Application in Gnome3 - #2 by jtojnar, which I can confirm works. You only need the environment.etc addition, the environment variable is OK by default nowadays (at least on unstable).