Skip to content

Commit

Permalink
again fix remaing lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-baumann committed Sep 14, 2024
1 parent 24d802f commit 1d7ccb9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
3 changes: 2 additions & 1 deletion R/make_nixpkgs_url.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' make_nixpkgs_url Find the right Nix revision
#' @param r_version Character. R version to look for, for example, "4.2.0". If a nixpkgs revision is provided instead, this gets returned.
#' @param r_version Character. R version to look for, for example, "4.2.0". If a
#' nixpkgs revision is provided instead, this gets returned.
#' @return A character. The url to use
#'
#' @examples
Expand Down
50 changes: 35 additions & 15 deletions R/rix_helpers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#' generate_header Internal function used to generate the header of the `default.nix` file.
#' @param nix_repo Character. nixpkgs reop to use (upstream or rstats-on-nix fork) with latest commit hash.
#' @param r_version Character. R version to look for, for example, "4.2.0". If a nixpkgs revision is provided instead, this gets returned.
#' generate_header Internal function used to generate the header of the
#' `default.nix` file.
#' @param nix_repo Character. nixpkgs reop to use (upstream or rstats-on-nix
#' fork) with latest commit hash.
#' @param r_version Character. R version to look for, for example, "4.2.0". If a
#' nixpkgs revision is provided instead, this gets returned.
#' @param rix_call Character, call to rix().
#' @noRd
generate_header <- function(nix_repo,
Expand Down Expand Up @@ -70,9 +73,11 @@ let
}
}

#' generate_rix_call Internal function used to generate the call to `rix()` as shown in `default.nix`
#' generate_rix_call Internal function used to generate the call to `rix()` as
#' shown in `default.nix`
#' @param rix_call Character, call to rix().
#' @param nix_repo Character. nixpkgs reop to use (upstream or rstats-on-nix fork) with latest commit hash.
#' @param nix_repo Character. nixpkgs reop to use (upstream or rstats-on-nix
#' fork) with latest commit hash.
#' @noRd
generate_rix_call <- function(rix_call, nix_repo) {
if (grepl("NixOS", nix_repo$url)) {
Expand Down Expand Up @@ -108,20 +113,24 @@ get_rpkgs <- function(r_pkgs, ide) {
r_pkgs
}

# nolint start: object_name_linter
rPackages <- paste(c("", r_pkgs), collapse = "\n ")

rPackages <- gsub("\\.", "_", rPackages)
# nolint end

list(
"rPackages" = rPackages,
"archive_pkgs" = archive_pkgs
)
}

#' generate_rpkgs Internal function that generates the string containing the correct Nix expression to get R packages.
#' generate_rpkgs Internal function that generates the string containing the
#' correct Nix expression to get R packages.
#' @param rPackages Character, list of R packages to install.
#' @param flag_rpkgs Character, are there any R packages at all?
#' @noRd
#' # nolint start: object_name_linter
generate_rpkgs <- function(rPackages, flag_rpkgs) {
if (flag_rpkgs == "") {
NULL
Expand All @@ -136,8 +145,10 @@ generate_rpkgs <- function(rPackages, flag_rpkgs) {
)
}
}
# nolint end: object_name_linter

#' generate_local_r_pkgs Internal function that generates the string containing the correct Nix expression for installing local packages
#' generate_local_r_pkgs Internal function that generates the string containing
#' the correct Nix expression for installing local packages
#' @param local_r_pkgs Character, list of local R packages to install.
#' @param flag_local_r_pkgs Character, are there any local R packages at all?
#' @noRd
Expand All @@ -156,7 +167,8 @@ generate_local_r_pkgs <- function(local_r_pkgs, flag_local_r_pkgs) {
}
}

#' generate_tex_pkgs Internal function that generates the string containing the correct Nix expression to get LaTeX packages.
#' generate_tex_pkgs Internal function that generates the string containing the
#' correct Nix expression to get LaTeX packages.
#' @param tex_pkgs Character, list of LaTeX packages to install.
#' @noRd
generate_tex_pkgs <- function(tex_pkgs) {
Expand All @@ -176,15 +188,17 @@ generate_tex_pkgs <- function(tex_pkgs) {
}
}

#' generate_system_pkgs Internal function that formats the system package names correctly for Nix.
#' generate_system_pkgs Internal function that formats the system package names
#' correctly for Nix.
#' @param system_pkgs Character, list of LaTeX packages to install.
#' @param r_pkgs Character, list of LaTeX packages to install.
#' @noRd
get_system_pkgs <- function(system_pkgs, r_pkgs) {
# We always need these packages
system_pkgs <- c(system_pkgs, "R", "glibcLocales", "nix")

# If the user wants the R {quarto} package, then the quarto software needs to be installed
# If the user wants the R {quarto} package, then the quarto software needs to
# be installed
system_pkgs <- if (any(grepl("quarto", r_pkgs))) {
unique(c(system_pkgs, "quarto"))
} else {
Expand All @@ -194,7 +208,8 @@ get_system_pkgs <- function(system_pkgs, r_pkgs) {
}


#' generate_system_pkgs Internal function that generates the string containing the correct Nix expression to get system packages.
#' generate_system_pkgs Internal function that generates the string containing
#' the correct Nix expression to get system packages.
#' @param system_pkgs Character, list of LaTeX packages to install.
#' @param r_pkgs Character, list of LaTeX packages to install.
#' @noRd
Expand All @@ -210,12 +225,16 @@ generate_system_pkgs <- function(system_pkgs, r_pkgs) {
}


#' generate_git_archived_pkgs Internal function that generates the string containing the correct Nix expression to get system packages.
#' generate_git_archived_pkgs Internal function that generates the string
#' containing the correct Nix expression to get system packages.
#' @param git_pkgs Character, list of R packages to install from Github.
#' @param archive_pkgs Character, list of R packages to install from the CRAN archives.
#' @param archive_pkgs Character, list of R packages to install from the CRAN
#' archives.
#' @param flag_git_archive Character, are there R packages from Github at all?
#' @noRd
generate_git_archived_pkgs <- function(git_pkgs, archive_pkgs, flag_git_archive) {
generate_git_archived_pkgs <- function(git_pkgs,
archive_pkgs,
flag_git_archive) {
if (flag_git_archive == "") {
NULL
} else {
Expand All @@ -225,7 +244,8 @@ generate_git_archived_pkgs <- function(git_pkgs, archive_pkgs, flag_git_archive)
}


#' generate_locale_variables Internal function that generates the string containing the correct Nix expression to set locales.
#' generate_locale_variables Internal function that generates the string
#' containing the correct Nix expression to set locales.
#' @noRd
generate_locale_variables <- function() {
locale_defaults <- list(
Expand Down
54 changes: 39 additions & 15 deletions R/rix_init.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' Initiate and maintain an isolated, project-specific, and runtime-pure R
#' setup via Nix.
#'
#' Creates an isolated project folder for a Nix-R configuration. `rix::rix_init()`
#' also adds, appends, or updates with or without backup a custom `.Rprofile`
#' file with code that initializes a startup R environment without system's user
#' libraries within a Nix software environment. Instead, it restricts search
#' paths to load R packages exclusively from the Nix store. Additionally, it
#' makes Nix utilities like `nix-shell` available to run system commands from
#' the system's RStudio R session, for both Linux and macOS.
#' Creates an isolated project folder for a Nix-R configuration.
#' `rix::rix_init()` also adds, appends, or updates with or without backup a
#' custom `.Rprofile` file with code that initializes a startup R environment
#' without system's user libraries within a Nix software environment. Instead,
#' it restricts search paths to load R packages exclusively from the Nix store.
#' Additionally, it makes Nix utilities like `nix-shell` available to run system
#' commands from the system's RStudio R session, for both Linux and macOS.
#'
#' **Enhancement of computational reproducibility for Nix-R environments:**
#'
Expand Down Expand Up @@ -75,12 +75,14 @@
#' does exist; `"append"` appends the existing file with code that is tailored
#' to an isolated Nix-R project setup.
#' @param message_type Character. Message type, defaults to `"simple"`, which
#' gives minimal but sufficient feedback. Other values are currently
#' `"quiet`, which writes `.Rprofile` without message, and `"verbose"`,
#' which displays the mechanisms implemented to achieve fully controlled R project environments in Nix.
#' gives minimal but sufficient feedback. Other values are currently `"quiet`,
#' which writes `.Rprofile` without message, and `"verbose"`, which displays
#' the mechanisms implemented to achieve fully controlled R project
#' environments in Nix.
#' @export
#' @seealso [with_nix()]
#' @return Nothing, this function only has the side-effect of writing a file called ".Rprofile" to the specified path.
#' @return Nothing, this function only has the side-effect of writing a file
#' called ".Rprofile" to the specified path.
#' @examples
#' \dontrun{
#' # create an isolated, runtime-pure R setup via Nix
Expand Down Expand Up @@ -272,6 +274,7 @@ message_rprofile <- function(action_string = "Added",
#' @return Character vector that lists `PATH` entries after modification, which
#' are separated by `":"`.
#' @noRd
# nolint start: object_name_linter
set_message_session_PATH <- function(message_type =
c("simple", "quiet", "verbose")) {
message_type <- match.arg(message_type,
Expand All @@ -293,6 +296,7 @@ set_message_session_PATH <- function(message_type =
cat("\n\n* Updated `PATH` variable is:\n\n", PATH)
}
}
# nolint end: object_name_linter


#' Report whether the current R session is running in Nix and RStudio, or not.
Expand Down Expand Up @@ -392,6 +396,7 @@ set_nix_path <- function() {
#' @return language object with parsed expression
#' @noRd
nix_rprofile <- function() {
# nolint start: object_name_linter
quote({
is_rstudio <- Sys.getenv("RSTUDIO") == "1"
is_nix_r <- nzchar(Sys.getenv("NIX_STORE"))
Expand All @@ -417,19 +422,37 @@ nix_rprofile <- function() {

if (isTRUE(is_nix_r)) {
install.packages <- function(...) {
stop("You are currently in an R session running from Nix.\nDon't install packages using install.packages(),\nadd them to the default.nix file instead.")
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."
)
}

update.packages <- function(...) {
stop("You are currently in an R session running from Nix.\nDon't update packages using update.packages(),\ngenerate 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.")
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."
)
}

remove.packages <- function(...) {
stop("You are currently in an R session running from Nix.\nDon't remove packages using remove.packages(),\ndelete them from the default.nix file instead.")
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.")
}
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths, fixed = TRUE)
user_dir <- grep(
paste(userlib_paths, collapse = "|"),
current_paths,
fixed = TRUE
)
new_paths <- current_paths[-user_dir]
# sets new library path without user library, making nix-R pure at
# run-time
Expand All @@ -439,4 +462,5 @@ nix_rprofile <- function() {

rm(is_rstudio, is_nix_r)
})
# nolint end: object_name
}

0 comments on commit 1d7ccb9

Please sign in to comment.