Skip to content
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

with_nix(): do not use --vanilla because it does not respect custom code startup via .Rprofile #134

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions R/with_nix.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
#' can easily do so by providing Nix expressions in custom `.nix` or
#' `default.nix` files in different subfolders of the project.
#'
#' It is recommended that you use `rix_init()` to generate a custom `.Rprofile`
#' file for the subshell in `project_dir`. The defaults in that file ensure
#' that only R packages from the Nix store, that are defined in the subshell
#' `.nix` file are loaded and system's libraries are excluded.
#'
#'
#' To do its job, `with_nix()` heavily relies on patterns that manipulate
#' language expressions (aka computing on the language) offered in base R as
#' well as the \{codetools\} package by Luke Tierney.
Expand Down Expand Up @@ -288,14 +294,14 @@ with_nix <- function(expr,
exec_mode, " mode:\n\n"#,
# paste0(rnix_deparsed, collapse = " ")
))

# command to run deparsed R expression via nix-shell
cmd_rnix_deparsed <- c(
file.path(project_path, "default.nix"),
"--pure", # required for to have nix glibc
"--run",
sprintf(
"Rscript --vanilla '%s'",
"Rscript --no-site-file --no-environ --no-restore '%s'",
rnix_file
)
)
Expand Down
50 changes: 48 additions & 2 deletions dev/flat_with_nix.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ environment.
#' can easily do so by providing Nix expressions in custom `.nix` or
#' `default.nix` files in different subfolders of the project.
#'
#' It is recommended that you use `rix_init()` to generate a custom `.Rprofile`
#' file for the subshell in `project_dir`. The defaults in that file ensure
#' that only R packages from the Nix store, that are defined in the subshell
#' `.nix` file are loaded and system's libraries are excluded.
#'
#'
#' To do its job, `with_nix()` heavily relies on patterns that manipulate
#' language expressions (aka computing on the language) offered in base R as
#' well as the \{codetools\} package by Luke Tierney.
Expand Down Expand Up @@ -297,14 +303,14 @@ with_nix <- function(expr,
exec_mode, " mode:\n\n"#,
# paste0(rnix_deparsed, collapse = " ")
))

# command to run deparsed R expression via nix-shell
cmd_rnix_deparsed <- c(
file.path(project_path, "default.nix"),
"--pure", # required for to have nix glibc
"--run",
sprintf(
"Rscript --vanilla '%s'",
"Rscript --no-site-file --no-environ --no-restore '%s'",
rnix_file
)
)
Expand Down Expand Up @@ -971,4 +977,44 @@ testthat::test_that("Test `with_nix()` if Nix is installed on R 4.2.0", {
)
})


testthat::test_that("Test `with_nix()` correct .libPaths()", {

if (isFALSE(is_nix_rsession())) {
# needed for the GitHub test runners with system's R
set_nix_path()
}

skip_if_not(nix_shell_available())

path_subshell <- tempdir()

rix_init(
project_path = path_subshell,
rprofile_action = "overwrite",
message_type = "simple"
)

rix(
r_ver = "4.3.1",
overwrite = TRUE,
project_path = path_subshell,
shell_hook = NULL
)

out_subshell <- with_nix(
expr = function() {
.libPaths()
},
program = "R",
exec_mode = "blocking",
project_path = path_subshell,
message_type = "verbose"
)

testthat::expect_true(
is.character(out_subshell)
)
})

```
5 changes: 5 additions & 0 deletions man/with_nix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tests/testthat/test-with_nix.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,43 @@ testthat::test_that("Test `with_nix()` if Nix is installed on R 4.2.0", {
)
})


testthat::test_that("Test `with_nix()` correct .libPaths()", {

if (isFALSE(is_nix_rsession())) {
# needed for the GitHub test runners with system's R
set_nix_path()
}

skip_if_not(nix_shell_available())

path_subshell <- tempdir()

rix_init(
project_path = path_subshell,
rprofile_action = "overwrite",
message_type = "simple"
)

rix(
r_ver = "4.3.1",
overwrite = TRUE,
project_path = path_subshell,
shell_hook = NULL
)

out_subshell <- with_nix(
expr = function() {
.libPaths()
},
program = "R",
exec_mode = "blocking",
project_path = path_subshell,
message_type = "verbose"
)

testthat::expect_true(
is.character(out_subshell)
)
})

Loading