|
| 1 | +### File generated by `rix::rix_init()` ### |
| 2 | +# 1. Currently, system RStudio does not inherit environmental variables |
| 3 | +# defined in `$HOME/.zshrc`, `$HOME/.bashrc` and alike. This is workaround to |
| 4 | +# make the path of the nix store and hence basic nix commands available |
| 5 | +# in an RStudio session |
| 6 | +# 2. For nix-R session, remove `R_LIBS_USER`, system's R user library.`. |
| 7 | +# This guarantees no user libraries from the system are loaded and only |
| 8 | +# R packages in the Nix store are used. This makes Nix-R behave in pure manner |
| 9 | +# at run-time. |
| 10 | +{ |
| 11 | + is_rstudio <- Sys.getenv("RSTUDIO") == "1" |
| 12 | + is_nix_r <- nzchar(Sys.getenv("NIX_STORE")) |
| 13 | + is_code <- Sys.getenv("TERM_PROGRAM") == "vscode" |
| 14 | + is_positron <- Sys.getenv("POSITRON") == "1" |
| 15 | + if (isFALSE(is_nix_r) && isTRUE(is_rstudio)) { |
| 16 | + cat("{rix} detected RStudio R session") |
| 17 | + old_path <- Sys.getenv("PATH") |
| 18 | + nix_path <- "/nix/var/nix/profiles/default/bin" |
| 19 | + has_nix_path <- any(grepl(nix_path, old_path)) |
| 20 | + if (isFALSE(has_nix_path)) { |
| 21 | + Sys.setenv(PATH = paste(old_path, nix_path, sep = ":")) |
| 22 | + } |
| 23 | + rm(old_path, nix_path) |
| 24 | + } |
| 25 | + if (isTRUE(is_nix_r)) { |
| 26 | + install.packages <- function(...) { |
| 27 | + stop("You are currently in an R session running from Nix.\n", "Don't install packages using install.packages(),\nadd them to ", "the default.nix file instead.") |
| 28 | + } |
| 29 | + update.packages <- function(...) { |
| 30 | + stop("You are currently in an R session running from Nix.\n", "Don't update packages using update.packages(),\n", "generate a new default.nix with a more recent version of R. ", "If you need bleeding edge packages, read the", "'Understanding the rPackages set release cycle and using ", "bleeding edge packages' vignette.") |
| 31 | + } |
| 32 | + remove.packages <- function(...) { |
| 33 | + stop("You are currently in an R session running from Nix.\n", "Don't remove packages using `remove.packages()``,\ndelete them ", "from the default.nix file instead.") |
| 34 | + } |
| 35 | + current_paths <- .libPaths() |
| 36 | + userlib_paths <- Sys.getenv("R_LIBS_USER") |
| 37 | + user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths, fixed = TRUE) |
| 38 | + new_paths <- current_paths[-user_dir] |
| 39 | + .libPaths(new_paths) |
| 40 | + rm(current_paths, userlib_paths, user_dir, new_paths) |
| 41 | + } |
| 42 | + if (isTRUE(is_code) && interactive() && isFALSE(is_rstudio) && isFALSE(is_positron)) { |
| 43 | + vscode_r_init <- file.path(Sys.getenv(if (.Platform$OS.type == "windows") |
| 44 | + "USERPROFILE" |
| 45 | + else "HOME"), ".vscode-R", "init.R") |
| 46 | + if (file.exists(vscode_r_init)) { |
| 47 | + source(vscode_r_init) |
| 48 | + } |
| 49 | + else { |
| 50 | + message("No .vscode-R/init.R file found. If you want to use VSCode-R, you need to source it in your .Rprofile or start vscode from within nix-shell") |
| 51 | + } |
| 52 | + } |
| 53 | + rm(is_rstudio, is_nix_r, is_code, is_positron) |
| 54 | +} |
0 commit comments