guix.install <- function (package, profile = NULL, guix = "guix", pure = FALSE) {
    if (is.null (profile)) {
        ## Use the default profile unless otherwise specified.
        guix_profile <- Sys.getenv ("GUIX_PROFILE", unset = NA)
        if (is.na (guix_profile)) {
            profile <- paste (Sys.getenv ("HOME"), ".guix-profile", sep = "/")
        } else {
            profile <- guix_profile
        }
    } else {
        ## Create the parent directory if necessary.
        parent <- dirname (profile)
        if (! dir.exists (parent)) {
            dir.create (parent, recursive = TRUE)
        }
    }

    ## Location of on-the-fly generated packages
    scratch <- paste (Sys.getenv ("HOME"), ".Rguix", "packages.scm", sep = "/")

    ## split package path, put scratch location first
    package_path <- NULL
    old_package_path <- Sys.getenv ("GUIX_PACKAGE_PATH")
    entries <- strsplit (old_package_path, ":")[[1]]
    package_path <- paste (unique (c(dirname (scratch), entries)), sep = ":")
    Sys.setenv (GUIX_PACKAGE_PATH=package_path)

    ## The normalized name used by Guix packages
    guix_name <- paste0 ("r-", gsub ("[^a-z0-9]", "-", tolower (package)))

    ## Does the package already exist?
    error <- system2 (guix, c("show", guix_name),
                      stdout = NULL, stderr = NULL)

    ## Attempt to import the package
    if (error > 0) {
        ## Build a scratch module
        if (! dir.exists (dirname (scratch))) {
            dir.create (dirname (scratch), recursive = TRUE)
        }
        if (! file.exists (scratch)) {
            cat ("
(define-module (packages)
  #:use-module (gnu)
  #:use-module (gnu packages bioinformatics)
  #:use-module (gnu packages bioconductor)
  #:use-module (gnu packages cran)
  #:use-module (gnu packages statistics)
  #:use-module (guix)
  #:use-module (guix build-system r)
  #:use-module (guix licenses))
", file = scratch)
        }

        ## TODO: abort on errors!
        definitions <- system2 (guix, c("import", "cran",
                                        "--recursive",
                                        ## TODO: the importer will retry
                                        ## importing from CRAN if a package is
                                        ## not found on bioconductor
                                        "--archive=bioconductor",
                                        package),
                                stdout = TRUE)
        cat (";; Imported from within R at ", date(), "\n",
             file = scratch, append = TRUE)
        cat (definitions, sep = "\n",
             file = scratch, append = TRUE)
    }

    ## Install the package.
    error <- system2 (guix, c("package", paste ("--profile", profile, sep = "="),
                              "--install", guix_name))

    ## Extend the R load path.
    .libPaths (paste (profile, "site-library", sep = "/"))
}

Generated by Ricardo Wurmus using scpaste at Fri Dec 11 16:39:43 2020. CET. (original)