Skip to content

Commit

Permalink
Fix example with .Rprofile and check it using Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosell committed Sep 14, 2024
1 parent bb474ed commit d423da7
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM ubuntu:latest

WORKDIR /workspace

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*

RUN curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux \
--extra-conf "sandbox = false" \
--init none \
--no-confirm

ENV PATH="${PATH}:/nix/var/nix/profiles/default/bin"


COPY rix-run inst/extdata/.Rprofile examples/data-visualize.R /workspace/

CMD /workspace/rix-run examples/data-visualize.R

16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,24 @@ Once `rix-run` is installed, to run the script execute:
$ rix-run examples/data-visualize.R
```

To use docker:

```
$ docker build -f Dockerfile -t rix-run-image . \
&& docker run --name rix-run-container --rm -v $(pwd)/examples:/workspace/examples/:rw rix-run-image
```

To clean up the used docker images and containers:

```
(docker container rm -f rix-run-container || true) \
&& (docker rmi $(docker images --format '{{.Repository}}:{{.ID}}'| egrep 'rix-run-image' | cut -d':' -f2 | uniq) --force || true)
```

## Troubleshooting

* It's tested on Ubuntu. If you want to help, please test Debian, Fedora, Centos, MacOS, etc. Windows it's not (yet?) supported.
* Please, open an issue if you have any problem.
* Feel free to open a PR to improve `rix-run`.


2 changes: 1 addition & 1 deletion examples/data-visualize.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ library(palmerpenguins)
library(ggthemes)
library(R.devices)

file_path <- here('examples', 'data-visualize-penguin-plot.png')
file_path <- here('examples', 'data-visualize-plot.png')

str(penguins)
p <- penguins |>
Expand Down
41 changes: 41 additions & 0 deletions inst/extdata/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
### File generated by `rix::rix_init()` ###
# 1. Currently, system RStudio does not inherit environmental variables
# defined in `$HOME/.zshrc`, `$HOME/.bashrc` and alike. This is workaround to
# make the path of the nix store and hence basic nix commands available
# in an RStudio session
# 2. For nix-R session, remove `R_LIBS_USER`, system's R user library.`.
# This guarantees no user libraries from the system are loaded and only
# R packages in the Nix store are used. This makes Nix-R behave in pure manner
# at run-time.
{
is_rstudio <- Sys.getenv("RSTUDIO") == "1"
is_nix_r <- nzchar(Sys.getenv("NIX_STORE"))
if (isFALSE(is_nix_r) && isTRUE(is_rstudio)) {
cat("{rix} detected RStudio R session")
old_path <- Sys.getenv("PATH")
nix_path <- "/nix/var/nix/profiles/default/bin"
has_nix_path <- any(grepl(nix_path, old_path))
if (isFALSE(has_nix_path)) {
Sys.setenv(PATH = paste(old_path, nix_path, sep = ":"))
}
rm(old_path, nix_path)
}
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.")
}
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.")
}
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.")
}
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths, fixed = TRUE)
new_paths <- current_paths[-user_dir]
.libPaths(new_paths)
rm(current_paths, userlib_paths, user_dir, new_paths)
}
rm(is_rstudio, is_nix_r)
}
65 changes: 59 additions & 6 deletions rix-run
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,63 @@ then
exit 1
fi

echo "Install nix if not yet installed..."
echo "Installing nix if not yet installed..."
if ! command -v nix-shell &> /dev/null
then
curl --proto '=https' --tlsv1.2 -sSf \
-L https://install.determinate.systems/nix | \
sh -s -- install
sh -s -- install --no-confirm
fi


if test -f "${1}"; then
echo "Generating default.nix configuration..."
else
echo "Error: No R script provided. Use: rix-run <your-script>.R"
exit 2
fi

CURRENT_DIR=$(pwd)
RIX_R_PROFILE="$CURRENT_DIR/.Rprofile"
if test -f "$RIX_R_PROFILE"; then
RIX_R_PROFILE_PREVIOUS="$CURRENT_DIR/.Rprofile.tmp"
mv "$RIX_R_PROFILE" "$RIX_R_PROFILE_PREVIOUS"
fi
cat <<EOF > "$RIX_R_PROFILE"
{
is_rstudio <- Sys.getenv("RSTUDIO") == "1"
is_nix_r <- nzchar(Sys.getenv("NIX_STORE"))
if (isFALSE(is_nix_r) && isTRUE(is_rstudio)) {
cat("{rix} detected RStudio R session")
old_path <- Sys.getenv("PATH")
nix_path <- "/nix/var/nix/profiles/default/bin"
has_nix_path <- any(grepl(nix_path, old_path))
if (isFALSE(has_nix_path)) {
Sys.setenv(PATH = paste(old_path, nix_path, sep = ":"))
}
rm(old_path, nix_path)
}
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.")
}
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.")
}
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.")
}
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths, fixed = TRUE)
new_paths <- current_paths[-user_dir]
.libPaths(new_paths)
rm(current_paths, userlib_paths, user_dir, new_paths)
}
rm(is_rstudio, is_nix_r)
}
EOF

echo $CURRENT_DIR
NIX_TMP_DIR=$(mktemp -d /tmp/rix.XXXXXXXXX)
RIX_R_SCRIPT_PATH="$(mktemp /tmp/rix.XXXXXXXXX.R)"
cat <<EOF > "$RIX_R_SCRIPT_PATH"
Expand Down Expand Up @@ -120,21 +161,33 @@ suppressMessages(suppressWarnings({
cat(paste0("\n", path_default_nix, "/default.nix\n"))
EOF
NIX_TMP=$(mktemp /tmp/rix.XXXXXXXXX.tmp)
nix-shell --expr "$(curl -sl https://raw.githubusercontent.com/jrosell/rix-run/master/inst/extdata/rix.nix)" --run "Rscript --no-site-file --no-environ --no-restore $RIX_R_SCRIPT_PATH ${1}" > $NIX_TMP
nix-shell --pure --expr "$(curl -sl https://raw.githubusercontent.com/jrosell/rix-run/master/inst/extdata/rix.nix)" --run "Rscript --no-site-file --no-environ --no-restore $RIX_R_SCRIPT_PATH ${1}" > $NIX_TMP
# nix-shell --expr "$(cat inst/extdata/rix.nix)" --run "Rscript --no-site-file --no-environ --no-restore $RIX_R_SCRIPT_PATH ${1}" > $NIX_TMP
NIX_FILE=`cat $NIX_TMP | grep '^/tmp/' | grep 'default.nix'`
# cat $NIX_TMP
rm $RIX_R_SCRIPT_PATH # echo "RIX_R_SCRIPT_PATH" $RIX_R_SCRIPT_PATH
cat "$NIX_TMP"
rm "$RIX_R_SCRIPT_PATH" # echo "RIX_R_SCRIPT_PATH" $RIX_R_SCRIPT_PATH


if [[ $(wc -l < "$NIX_FILE") -ge 2 ]]; then
echo "Runing nix-shell $NIX_FILE ${1}..."
rm $NIX_TMP
else
echo "Error when generating the configuration file. Check for errors on the $NIX_TMP file."
rm "$RIX_R_PROFILE"
if test -f "$RIX_R_PROFILE_PREVIOUS"; then
mv "$RIX_R_PROFILE_PREVIOUS" "$RIX_R_PROFILE"
fi
exit 2
fi

nix-shell --expr "$(cat $NIX_FILE)" --run "Rscript \
--no-site-file \
--no-environ \
--no-restore \
${1}"

rm "$RIX_R_PROFILE"
if test -f "$RIX_R_PROFILE_PREVIOUS"; then
mv "$RIX_R_PROFILE_PREVIOUS" "$RIX_R_PROFILE"
fi

0 comments on commit d423da7

Please sign in to comment.