Skip to content

Commit

Permalink
Merge pull request #244 from b-rodrigues/fix-nix-hash-debian
Browse files Browse the repository at this point in the history
Fix `nix-hash` system call on Debian and Debian-based systems
fixes #243
  • Loading branch information
b-rodrigues authored Jul 11, 2024
2 parents dcf89c3 + 1a88127 commit 58ac45d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
26 changes: 26 additions & 0 deletions R/nix_hash.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ nix_sri_hash <- function(path) {
if (isFALSE(has_nix_shell)) {
stop_no_nix_shell()
}

# not needed for Nix R sessions, workaround on Debian and Debian-based
# systems with nix installed
LD_LIBRARY_PATH_default <- Sys.getenv("LD_LIBRARY_PATH")
needs_ld_fix <- isFALSE(nzchar(Sys.getenv("NIX_STORE"))) &&
nzchar(LD_LIBRARY_PATH_default)

if (isTRUE(needs_ld_fix)) {
# On Debian and Debian-based systems, like Ubuntu 22.04, we found that a
# preset `LD_LIBRARY_PATH` environment variable in the system's R session
# leads to errors like
# nix-hash: /usr/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.38' not found (required by nix-hash)
# nix-hash: /usr/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.38' not found (required by /nix/store/4z754a0vzl98asv0pa95i5d9szw5jqbs-lowdown-1.0.2-lib/lib/liblowdown.so.3)
# etc...
# for both `nix-hash`; it occurs via
# `sys::exec_internal`, `base::system()` or `base::system2()` from R.
# Therefore, we set it to `""` and set back the default (old)
# `LD_LIBRARY_PATH` when `with_nix()` exits.
fix_ld_library_path()
}

cmd <- "nix-hash"
args <- c("--type", "sha256", "--sri", path)
Expand All @@ -104,6 +124,12 @@ nix_sri_hash <- function(path) {
what = cmd,
message_type = "quiet"
)

if (isTRUE(needs_ld_fix)) {
# set old LD_LIBRARY_PATH (only if non-Nix R session, and if it wasn't
# `""`)
on.exit(Sys.setenv(LD_LIBRARY_PATH = LD_LIBRARY_PATH_default))
}

sri_hash <- sys::as_text(proc$stdout)
return(sri_hash)
Expand Down
23 changes: 8 additions & 15 deletions R/rix.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,14 @@ for more details."
project_path <- normalizePath(path = project_path)
}

default.nix_path <- if (project_path == ".") {
"default.nix"
} else {
file.path(project_path, "default.nix")
}

.Rprofile_path <- if (project_path == ".") {
".Rprofile"
} else {
file.path(project_path, ".Rprofile")
}

default.nix_path <- file.path(project_path, "default.nix")
.Rprofile_path <- file.path(project_path, ".Rprofile")

# Find url to use
# In case of bleeding or frozen edge, the rstats-on-nix/nixpkgs
# fork is used. Otherwise, upstream NixOS/nixpkgs
nix_repo <- make_nixpkgs_url(r_ver)

default.nix_path <- file.path(default.nix_path)
.Rprofile_path <- file.path(.Rprofile_path)

rix_call <- match.call()

# Get the two lists. One list is current CRAN packages
Expand Down Expand Up @@ -293,6 +281,11 @@ for more details."
}

if (!file.exists(default.nix_path) || overwrite) {

if (!dir.exists(project_path)) {
dir.create(project_path)
}
file.create(default.nix_path)
writeLines(default.nix, default.nix_path)

if (file.exists(.Rprofile_path)) {
Expand Down
2 changes: 1 addition & 1 deletion R/rix_init.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ rix_init <- function(project_path = ".",

if (message_type == "verbose") {
cat("\n* Current lines of local `.Rprofile` are\n:")
cat(readLines(con = file(rprofile_file)), sep = "\n")
cat(readLines(con = rprofile_file), sep = "\n")
}
set_message_session_PATH(message_type = message_type)
}
Expand Down

0 comments on commit 58ac45d

Please sign in to comment.