diff --git a/DESCRIPTION b/DESCRIPTION index 05f433a9..f4609330 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,6 +12,7 @@ Depends: Imports: httr, jsonlite, + sys, utils Suggests: knitr, diff --git a/NAMESPACE b/NAMESPACE index 6460abb6..c8a4e8e8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(available_r) export(find_rev) export(get_current) +export(nix_build) export(rix) importFrom(httr,GET) importFrom(httr,content) diff --git a/R/find_rev.R b/R/find_rev.R index bacb5a5d..dd197af1 100644 --- a/R/find_rev.R +++ b/R/find_rev.R @@ -388,3 +388,27 @@ USE_RSTUDIO}; } + +#' Invoke shell command `nix-build` from an R session +#' @param nix_file File path to the `default.nix` file. The default is +#' a `default.nix` file in the current working directory of the current R +#' session. +#' @param exec_mode Either `"blocking"` (default) or `"non-blocking`. This +#' will either block the R session while the `nix-build` shell command is +#' executed, or run `nix-build` in the background ("non-blocking"). +#' @return integer of the process ID (PID) of `nix-build` shell command +#' launched. +#' @export +nix_build <- function(nix_file = file.path("default.nix"), + exec_mode = c("blocking", "non-blocking")) { + stopifnot(is.character(nix_file)) + exec_mode <- match.arg(exec_mode) + + proc <- switch(exec_mode, + "blocking" = sys::exec_wait("nix-build", nix_file), + "non-blocking" = sys::exec_background("nix-build", nix_file), + stop('invalid `exec_mode`. Either use "blocking" or "non-blocking"') + ) + + return(invisible(proc)) +} diff --git a/dev/0-dev_history.Rmd b/dev/0-dev_history.Rmd index 108091fe..96446ea0 100755 --- a/dev/0-dev_history.Rmd +++ b/dev/0-dev_history.Rmd @@ -20,7 +20,8 @@ fusen::fill_description( Description = "Provides helper functions to create reproducible development environments using the Nix package manager.", Version = "0.0.9", `Authors@R` = c( - person("Bruno", "Rodrigues", email = "bruno@brodrigues.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3211-3689")) + person("Bruno", "Rodrigues", email = "bruno@brodrigues.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3211-3689")), + person("Philipp", "Baumann", email = "baumann-philipp@protonmail.com", role = "ctb", comment = c(ORCID = "0000-0002-3194-8975")) ) ) ) diff --git a/dev/build_envs.Rmd b/dev/build_envs.Rmd index f9fe3a4d..eaa3ccb8 100644 --- a/dev/build_envs.Rmd +++ b/dev/build_envs.Rmd @@ -470,3 +470,32 @@ USE_RSTUDIO}; } ``` + +The function below is to invoke the shell command `nix-build` from a R +session. + +```{r function-nix_build} +#' Invoke shell command `nix-build` from an R session +#' @param nix_file File path to the `default.nix` file. The default is +#' a `default.nix` file in the current working directory of the current R +#' session. +#' @param exec_mode Either `"blocking"` (default) or `"non-blocking`. This +#' will either block the R session while the `nix-build` shell command is +#' executed, or run `nix-build` in the background ("non-blocking"). +#' @return integer of the process ID (PID) of `nix-build` shell command +#' launched. +#' @export +nix_build <- function(nix_file = file.path("default.nix"), + exec_mode = c("blocking", "non-blocking")) { + stopifnot(is.character(nix_file)) + exec_mode <- match.arg(exec_mode) + + proc <- switch(exec_mode, + "blocking" = sys::exec_wait("nix-build", nix_file), + "non-blocking" = sys::exec_background("nix-build", nix_file), + stop('invalid `exec_mode`. Either use "blocking" or "non-blocking"') + ) + + return(invisible(proc)) +} +``` diff --git a/man/nix_build.Rd b/man/nix_build.Rd new file mode 100644 index 00000000..b5a46411 --- /dev/null +++ b/man/nix_build.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/find_rev.R +\name{nix_build} +\alias{nix_build} +\title{Invoke shell command \code{nix-build} from an R session} +\usage{ +nix_build( + nix_file = file.path("default.nix"), + exec_mode = c("blocking", "non-blocking") +) +} +\arguments{ +\item{nix_file}{File path to the \code{default.nix} file. The default is +a \code{default.nix} file in the current working directory of the current R +session.} + +\item{exec_mode}{Either \code{"blocking"} (default) or \verb{"non-blocking}. This +will either block the R session while the \code{nix-build} shell command is +executed, or run \code{nix-build} in the background ("non-blocking").} +} +\value{ +integer of the process ID (PID) of \code{nix-build} shell command +launched. +} +\description{ +Invoke shell command \code{nix-build} from an R session +}