Skip to content

Using nix within Positron in a remote session (and fix docs about IDE) #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
joelnitta opened this issue Apr 17, 2025 · 24 comments
Open

Comments

@joelnitta
Copy link

I would like to connect to a remote server in Positron using the Remote-SSH: Connect to host command, then start an R Interpreter that is from a nix shell. Can you help me with this?

My first attempt has been starting from Configuring Positron installed on macOS to interact with Nix shells. I first ran "Install 'positron' command in PATH". Next, I connected to the server using my local Positron install, then ran nix-shell from a terminal in the remote session, but I don't see the nix instance in the menu of interpreters like I do when running locally.

Thanks!

@b-rodrigues
Copy link
Contributor

I have no experience with this, but this thread might help: #387 (reply in thread)

@joelnitta
Copy link
Author

Thanks for the suggestion @b-rodrigues .

I had a look at that post. I'm hoping to avoid the container route, because that is why I switched to rix/nix in the first place - no need to deal with root in the container, missing symlinks, etc.

Honestly now I would be happy to get R within nix running in either VS Code or Positron, as long as I can access it via SSH. I have heavy computations that I can't run on my laptop.

I think I need to use the local install of my IDE, because I had the same experience as @atsyplenkov with the frustratingly (IMHO unusably) slow response of the IDE within nix over SSH GUI.

I've been testing with this repo: https://github.com/joelnitta/2025-04-18_nix. It works fine locally - from a terminal in VS Code I run nix-shell, then R, then attach it to the current session with .vsc.attach().

However, when I try to do the same thing remotely (over SSH), although .vsc.attach() does not throw an error, I am in doubt that the session is really attached, because plots and help windows don't show up.

I know this is diverging from the OP, but if you have any ideas that would be great...

@b-rodrigues
Copy link
Contributor

Did you install direnv on the server and the corresponding extension on your local IDE? that seems to be the solution https://docs.ropensci.org/rix/articles/e-configuring-ide.html#configuring-native-vs-codecodium-on-your-operating-system

@joelnitta
Copy link
Author

So I did try that, but it doesn't seem to do anything. That is, I don't get the lag that you would expect while direnv runs nix-shell, and nothing seems to change (still same local shell).

I noticed there are multiple versions of direnv around, some seem to use use nix in .envrc. May that have something to do with it?

@b-rodrigues
Copy link
Contributor

maybe is it working locally when you switch folders, do you get the shell started?

@joelnitta
Copy link
Author

joelnitta commented Apr 18, 2025

You mean something like this:

(base) jnitta:~$ cd 2025-04-18_nix/
(base) jnitta:~/2025-04-18_nix$ 

Should drop me into the nix shell? (where 2025-04-18_nix is this repo)

That isn't happening.

@b-rodrigues
Copy link
Contributor

yes if direnv is configured correctly

@b-rodrigues
Copy link
Contributor

opened a pr, it should be "use nix"

@joelnitta
Copy link
Author

OK got that.

So at a minimum (not bothering with VS Code here), I should be able to:

  1. Put the line use nix in .envrc in the folder with a functioning default.nix
  2. run direnv allow in that folder
  3. cd into the folder from somewhere else, and it will drop me into the nix-shell

?

That is what I have tried, and I still see only my local shell.

FYI,

$ direnv --version
2.35.0

@b-rodrigues
Copy link
Contributor

did you hook your shell https://direnv.net/docs/hook.html

@joelnitta
Copy link
Author

No (it wasn't in the rix docs). Doing that does indeed seem to get direnv functioning as expected, so I can now do R: Create R Terminal from the VS Code command palette in my SSH session and immediately get to R running in the nix shell. So that's pretty great! But unfortunately help() and plot() still do not result in any output. I'm beginning to suspect the reason lies somewhere in the R extension...

Anyways, I would recommend a few (more) updates to the docs - clarifying about direnv vs nix-direnv (is either OK? does only one work?) and adding the part about hooking the shell.

@b-rodrigues
Copy link
Contributor

I'll update the docs, thanks

so is it working then? can we close this ?

@b-rodrigues
Copy link
Contributor

b-rodrigues commented Apr 18, 2025

Regarding your question about direnv vs nix-direnv, seems like both should be ok, but nix-direnv should be faster for nix based projects. However, it requires a modern bash, so they recommend direnv instead on macOS: https://github.com/nix-community/nix-direnv?tab=readme-ov-file#installation

EDIT: no that's wrong: nix-direnv is actually an extension to direnv. So you need to install both! direnv alone works, but is not optimized for nix shells. With nix-direnv, it's actually much faster!

@b-rodrigues
Copy link
Contributor

b-rodrigues commented Apr 18, 2025

fyi: 0cc73af

@joelnitta joelnitta changed the title Using nix within Positron in a remote session Using nix within Positron in a remote session (and fix docs about IDE) Apr 19, 2025
@joelnitta
Copy link
Author

joelnitta commented Apr 19, 2025

I get the feeling this is out-of-scope for {rix}: I've confirmed (using local R install instead of R in nix) that the lack of plots or help menus when using R in VS Code remotely via SSH is due to the VS Code R extension, and it seems getting the nix env to show up in Positron's list of interpreters is a Positron thing.

So let's consider the update to the docs sufficient to close this for now.

@b-rodrigues
Copy link
Contributor

b-rodrigues commented Apr 19, 2025

OK but when positron connects does it now see the nix-managed R from the server with direnv ? or at least with VS code ?

@joelnitta
Copy link
Author

Positron: No, with a local (i.e., not from nix) install of Positron, even when opening the dir with .envrc, I don't see the R from nix in the list of interpreters.

Image

VS Code: Local install of VS Code, connecting to the server with SSH then running R on the server in nix via direnv "works" (I can start an R terminal with R: Create R Terminal), but as previously mentioned help and plot windows don't appear. I also noticed that the "Workspace" tab that shows up when I click the R extension icon says "R workspace viewer requires that session watcher be enabled". I followed all instructions on that page, and it doesn't work. As I said, I think this is an VS Code R extension problem, but given the large number of similar stale issues, I have little hope of getting an answer if I file another...

Image

@b-rodrigues
Copy link
Contributor

In vscode, did you install the direnv extension as well ?

@b-rodrigues
Copy link
Contributor

(on a sidenote, I'm not confronted by such issues because because I use emacs in terminal mode directly from the server)

@joelnitta
Copy link
Author

In vscode, did you install the direnv extension as well ?

Yes, that is working as expected. As soon as I open the project folder, I'm in the nix env, and the R terminal I open is from nix.

@b-rodrigues
Copy link
Contributor

so to summarize

  • direnv and nix-direnv on the server
  • vscode with direnv extension locally
  • local vscode is able to work with remote nix shell through ssh and thanks to direnv
  • but plots aren't being shown ?

@joelnitta
Copy link
Author

yep.

but plots aren't being shown ?

I think this is because the VS Code R extension session watcher is not working when I connect via SSH. (as you can see the screenshot above)

@b-rodrigues
Copy link
Contributor

thanks a lot, this issue improved my understanding of remote ssh connections with a local editor and also the documentation

@joelnitta
Copy link
Author

FYI filed an issue about the VS Code R extension problem: REditorSupport/vscode-R#1604

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants