andweeb / presence.nvim

Discord Rich Presence for Neovim
816 stars 65 forks source link

Can't connect to discord when using neovim inside `nix develop` environment #20

Closed natto1784 closed 3 years ago

natto1784 commented 3 years ago

When using nix develop with nix (flakes), it can't connect to discord and gives the error ENOENT. Otherwise, it works fine. This might be related to how nix develop actually works because it works fine with the vanilla nix-shell.

andweeb commented 3 years ago

Hi @natto1784, interesting use case! It looks like the plugin isn't able to find the Discord socket due to mismatching $TMPDIR environment variables between the host and Nix build environments.

Both nix-shell and nix develop seem to actually set their own $TMPDIR values:

$ nix develop nixpkgs#neovim
bash-4.4$ echo $TMPDIR
/tmp/nix-shell.IgP5Pt

The probable reason Rich Presence still works in nix-shell for you is that the $TMPDIR value that nix-shell sets coincidentally matches the value in your host machine. For example, in macOS the $TMPDIR is not /tmp but some other path in /var/folders/..., so nix-shell would most likely fail to connect to Discord in that case.

It looks like there's a couple changes to be made in order to support Rich Presence in Nix build environments:

I'll need to do some digging to determine the best approach (especially the second bullet point). If you already have an idea and would like to contribute let me know :) Otherwise I'll get back to you sometime today/tomorrow once I've added support for this!

natto1784 commented 3 years ago

Looks like $TMPDIR in nix-shell falls back to $XDG_RUNTIME_DIR (which has the discord-ipc-0) if $TMPDIR is empty. That is the reason it worked with nix-shell for me. Will look more into how nix develop handles these variables.

natto1784 commented 3 years ago

Just checked and confirmed that putting XDG_RUNTIME_DIR before TMP, TMPDIR and TEMP here works fine (in nixOS) because XDG_RUNTIME_DIR is sustained in the nix develop environment https://github.com/andweeb/presence.nvim/blob/f4c1e227be0a0c863c2de201155401950eda572e/lua/presence/init.lua#L295-L300

andweeb commented 3 years ago

Hmm I seee.. unfortunately that wouldn't work in macOS as $XDG_RUNTIME_DIR isn't defined :'/ Is that set in most Linux distros?

natto1784 commented 3 years ago

pretty sure that if $XDG_RUNTIME_DIR isn't defined on macOS it will skip it and move on to the next var because of the if statement below it but there should be a better method

andweeb commented 3 years ago

Right, it's just that only $TMPDIR is set on macOS (all others aren't) so there would still be issues there, but maybe support for Nix shells in macOS shouldn't block a hotfix for you, especially considering it's an easy enough change to just switch the order.

I can investigate a more comprehensive/cross-platform solution later :) I'll push the change in a little bit after running some smoke tests!

andweeb commented 3 years ago

Just pushed @natto1784, hopefully you're able to get Rich Presence in Nix now :) Feel free to reopen if you still have issues after updating your plugin!

natto1784 commented 3 years ago

ty :)

ehllie commented 1 year ago

This is a bit of an old issue, but I was struggling with getting this plugin to work inside a nix develop shell on macos, and in case someone finds this issue later looking for a solution, what I did was add a TMPDIR=$TMPDIR after the -c flag in nix develop invocation, so that it gets set to the value from outside of the shell. I have a wrapper command for it in my dotfiles here