diff --git a/R/find_rev.R b/R/find_rev.R index eca0cb27..eae1bf89 100644 --- a/R/find_rev.R +++ b/R/find_rev.R @@ -800,15 +800,13 @@ with_nix <- function(expr, # cast list of symbols/names to list of strings; this is to prepare # deparsed version (string) of deserializing arguments from disk args_vec <- vapply(args, as.character, FUN.VALUE = character(1L)) - - # https://nixos.wiki/wiki/Nix-shell_shebang + + r_version_file <- file.path(temp_dir, "nix-r-version.txt") rnix_deparsed <- switch(program, # do 2), 3), 4) in nix-shell-R session (check how to deal with shellHook) "R" = sprintf( -'#! /usr/bin/env nix-shell -#! nix-shell -i bash -p bash -temp_dir <- \"%s\" +'temp_dir <- \"%s\" r_version_num <- paste0(R.version$major, ".", R.version$minor) # assign `args_vec` as in c(...) form. args_vec <- %s @@ -818,7 +816,8 @@ args_vec <- %s %s # evaluate function read_args(args_vec, temp_dir) -cat(paste0(R.version$major, ".", R.version$minor)\n', +cat(paste0(R.version$major, ".", R.version$minor)) +writeLines(text = as.character(r_version_num), file(\"%s\"))\n', temp_dir, # `args_vec` needs to be assigned, too, some combination of quoting # to make a call and also making use of some substitution tricks @@ -826,14 +825,13 @@ cat(paste0(R.version$major, ".", R.version$minor)\n', # with reconstructed symbols bound to objects with_assign_args_vec(args_vec), with_multiassign_args_vec(args_vec), - with_deserialize_args_deparse(args_vec, temp_dir) # step 2 + with_deserialize_args_deparse(args_vec, temp_dir), # step 2 + r_version_file ), "shell" = expr, # this has to be properly composed/decomposed stop('invalid `where` to evaluate `expr`. Either use "R" or "shell".') ) - # construct code to reconstruct `args_vec` - rnix_file <- file.path(temp_dir, "with_nix_r.R") # write deparsed expressions into R script. @@ -850,15 +848,21 @@ cat(paste0(R.version$major, ".", R.version$minor)\n', paste0(rnix_deparsed, collapse = " "))) # command to run deparsed R expression via nix-shell + # also check this out: https://github.com/NixOS/nix/issues/4230 cmd_rnix_deparsed <- c( - "nix-shell", file.path(project_path, "default.nix"), - "--run", - sprintf("Rscript --vanilla -e \"source('%s')\"", rnix_file) + "-c", "nix-shell", file.path(project_path, "default.nix"), + "--pure", + sprintf( + "--run 'Rscript --vanilla %s'", + rnix_file + ) ) + # browser() + proc <- switch(exec_mode, - "blocking" = sys::exec_internal(cmd = cmd_rnix_deparsed), - "non-blocking" = sys::exec_background(cmd = cmd_rnix_deparsed), + "blocking" = sys::exec_internal(cmd = "bash", cmd_rnix_deparsed), + "non-blocking" = sys::exec_background(cmd = "bash", cmd_rnix_deparsed), stop('invalid `exec_mode`. Either use "blocking" or "non-blocking"') ) @@ -868,6 +872,8 @@ cat(paste0(R.version$major, ".", R.version$minor)\n', poll_sys_proc_blocking(cmd = cmd_rnix_deparsed, proc, what = "expr") } + cat("File:", r_version_file) + return(invisible(proc)) } diff --git a/dev/build_envs.Rmd b/dev/build_envs.Rmd index a7c6421a..90afa8e5 100644 --- a/dev/build_envs.Rmd +++ b/dev/build_envs.Rmd @@ -898,15 +898,13 @@ with_nix <- function(expr, # cast list of symbols/names to list of strings; this is to prepare # deparsed version (string) of deserializing arguments from disk args_vec <- vapply(args, as.character, FUN.VALUE = character(1L)) - - # https://nixos.wiki/wiki/Nix-shell_shebang + + r_version_file <- file.path(temp_dir, "nix-r-version.txt") rnix_deparsed <- switch(program, # do 2), 3), 4) in nix-shell-R session (check how to deal with shellHook) "R" = sprintf( -'#! /usr/bin/env nix-shell -#! nix-shell -i bash -p bash -temp_dir <- \"%s\" +'temp_dir <- \"%s\" r_version_num <- paste0(R.version$major, ".", R.version$minor) # assign `args_vec` as in c(...) form. args_vec <- %s @@ -916,7 +914,8 @@ args_vec <- %s %s # evaluate function read_args(args_vec, temp_dir) -cat(paste0(R.version$major, ".", R.version$minor)\n', +cat(paste0(R.version$major, ".", R.version$minor)) +writeLines(text = as.character(r_version_num), file(\"%s\"))\n', temp_dir, # `args_vec` needs to be assigned, too, some combination of quoting # to make a call and also making use of some substitution tricks @@ -924,14 +923,13 @@ cat(paste0(R.version$major, ".", R.version$minor)\n', # with reconstructed symbols bound to objects with_assign_args_vec(args_vec), with_multiassign_args_vec(args_vec), - with_deserialize_args_deparse(args_vec, temp_dir) # step 2 + with_deserialize_args_deparse(args_vec, temp_dir), # step 2 + r_version_file ), "shell" = expr, # this has to be properly composed/decomposed stop('invalid `where` to evaluate `expr`. Either use "R" or "shell".') ) - # construct code to reconstruct `args_vec` - rnix_file <- file.path(temp_dir, "with_nix_r.R") # write deparsed expressions into R script. @@ -948,15 +946,21 @@ cat(paste0(R.version$major, ".", R.version$minor)\n', paste0(rnix_deparsed, collapse = " "))) # command to run deparsed R expression via nix-shell + # also check this out: https://github.com/NixOS/nix/issues/4230 cmd_rnix_deparsed <- c( - "nix-shell", file.path(project_path, "default.nix"), - "--run", - sprintf("Rscript --vanilla -e \"source('%s')\"", rnix_file) + "-c", "nix-shell", file.path(project_path, "default.nix"), + "--pure", + sprintf( + "--run 'Rscript --vanilla %s'", + rnix_file + ) ) + # browser() + proc <- switch(exec_mode, - "blocking" = sys::exec_internal(cmd = cmd_rnix_deparsed), - "non-blocking" = sys::exec_background(cmd = cmd_rnix_deparsed), + "blocking" = sys::exec_internal(cmd = "bash", cmd_rnix_deparsed), + "non-blocking" = sys::exec_background(cmd = "bash", cmd_rnix_deparsed), stop('invalid `exec_mode`. Either use "blocking" or "non-blocking"') ) @@ -966,6 +970,8 @@ cat(paste0(R.version$major, ".", R.version$minor)\n', poll_sys_proc_blocking(cmd = cmd_rnix_deparsed, proc, what = "expr") } + cat("File:", r_version_file) + return(invisible(proc)) }