Skip to content

Commit

Permalink
setup_cachix: better handling if cache already configured
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues committed Dec 9, 2024
1 parent 8b25dd0 commit 9c6b549
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
37 changes: 20 additions & 17 deletions R/setup_cachix.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ setup_cachix <- function(nix_conf_path = "~/.config/nix") {
nix_conf_exists <- file.exists(nix_conf_file) &&
`!=`(file.size(nix_conf_file), 0L)

if(!nix_conf_exists){
stop("~/.config/nix/nix.conf does not exist, did you install Nix?")
if(!nix_conf_exists){
stop("~/.config/nix/nix.conf does not exist, did you install Nix?")
}

nix_conf_content <- readLines(con = nix_conf_file)

if (
(grepl("rstats-on-nix", nix_conf_content[1]))
) {
stop("rstats-on-nix cache already configured!")
} else {
if(identical(Sys.getenv("TESTTHAT"), "false")){
timestamp <- format(Sys.time(), "%Y-%m-%dT%H:%M:%S%z")
nix_conf_backup <- paste0(nix_conf_file, "_backup_", timestamp)
file.copy(from = nix_conf_file, to = nix_conf_backup)
} else {
cat("this is running in a test, no backup performed")
}
if(identical(Sys.getenv("TESTTHAT"), "true")){
cat("this is running in a test, no backup performed")
} else {
timestamp <- format(Sys.time(), "%Y-%m-%dT%H:%M:%S%z")
nix_conf_backup <- paste0(nix_conf_file, "_backup_", timestamp)
file.copy(from = nix_conf_file, to = nix_conf_backup)
}

nix_conf_content <- readLines(con = nix_conf_file)
if (!(any(grepl("https://rstats-on-nix.cachix.org", nix_conf_content)))) {

nix_conf_content[1] <- paste0(
append(nix_conf_content[1],
Expand All @@ -41,13 +47,10 @@ setup_cachix <- function(nix_conf_path = "~/.config/nix") {

nix_conf_content[2] <- paste0(
append(nix_conf_content[2],
"rstats-on-nix.cachix.org-1:mzXrOo5XyDwy6MWSY0v8XYXTeYFSg7QSfv9Vq3Xvwyk="),
"rstats-on-nix.cachix.org-1:mzXrOo5XyDwy6MWSY0v8XYXTeYFSg7QSfv9Vq3Xvwyk="),
collapse = " ")

writeLines(enc2utf8(nix_conf_content), nix_conf_file, useBytes = TRUE)
nix_conf_file
} else {
stop("rstats-on-nix cache already configured!")
writeLines(enc2utf8(nix_conf_content), nix_conf_file, useBytes = TRUE)
nix_conf_file
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
substituters = https://cache.nixos.org
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
substituters = https://cache.nixos.org
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=

0 comments on commit 9c6b549

Please sign in to comment.