Skip to content

Commit

Permalink
Merge pull request #122 from b-rodrigues/nix-build-set-r-libs-user
Browse files Browse the repository at this point in the history
- `nix_build()`: another guardrail for run-time purity of Nixpkgs R
  • Loading branch information
philipp-baumann authored Jan 31, 2024
2 parents 4d89dfb + b5c3666 commit c8d98a0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
14 changes: 14 additions & 0 deletions R/nix_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ nix_build <- function(project_path = ".",
exec_mode = c("blocking", "non-blocking")) {
# if nix store is not PATH variable; e.g. on macOS (system's) RStudio
PATH <- set_nix_path()
if (isTRUE(nzchar(Sys.getenv("NIX_STORE")))) {
remove_r_libs_user()
}
has_nix_build <- nix_build_installed() # TRUE if yes, FALSE if no
nix_file <- file.path(project_path, "default.nix")

Expand Down Expand Up @@ -72,6 +75,17 @@ nix_build <- function(project_path = ".",
return(invisible(proc))
}

#' @noRd
remove_r_libs_user <- function() {
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths)
new_paths <- current_paths[-user_dir]
# sets new library path without user library, making nix-R pure at
# run-time
invisible(.libPaths(new_paths))
}

#' @noRd
poll_sys_proc_blocking <- function(cmd, proc,
what = c("nix-build", "expr")) {
Expand Down
2 changes: 1 addition & 1 deletion dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ flat_nix_build.Rmd:
path: dev/flat_nix_build.Rmd
state: active
R: R/nix_build.R
tests: [tests/testthat/test-nix_build.R]
tests: tests/testthat/test-nix_build.R
vignettes: []
inflate:
flat_file: dev/flat_nix_build.Rmd
Expand Down
18 changes: 16 additions & 2 deletions dev/flat_nix_build.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ nix_build <- function(project_path = ".",
exec_mode = c("blocking", "non-blocking")) {
# if nix store is not PATH variable; e.g. on macOS (system's) RStudio
PATH <- set_nix_path()
if (isTRUE(nzchar(Sys.getenv("NIX_STORE")))) {
remove_r_libs_user()
}
has_nix_build <- nix_build_installed() # TRUE if yes, FALSE if no
nix_file <- file.path(project_path, "default.nix")
Expand Down Expand Up @@ -85,6 +88,17 @@ nix_build <- function(project_path = ".",
return(invisible(proc))
}
#' @noRd
remove_r_libs_user <- function() {
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths)
new_paths <- current_paths[-user_dir]
# sets new library path without user library, making nix-R pure at
# run-time
invisible(.libPaths(new_paths))
}
#' @noRd
poll_sys_proc_blocking <- function(cmd, proc,
what = c("nix-build", "expr")) {
Expand Down Expand Up @@ -151,9 +165,9 @@ testthat::test_that("Testing that `nix_build()` builds derivation", {
skip_if_not(nix_shell_available())
skip_on_covr()
# skip_on_covr()
path_subshell <- "."
path_subshell <- tempdir()
rix_init(
project_path = path_subshell,
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-nix_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ testthat::test_that("Testing that `nix_build()` builds derivation", {

skip_if_not(nix_shell_available())

skip_on_covr()
# skip_on_covr()

path_subshell <- "."
path_subshell <- tempdir()

rix_init(
project_path = path_subshell,
Expand Down

0 comments on commit c8d98a0

Please sign in to comment.