Skip to content

Commit

Permalink
Merge pull request #195 from b-rodrigues/rix-micro-formatting
Browse files Browse the repository at this point in the history
- Fine-tune formatting of *.nix files generated
  • Loading branch information
philipp-baumann authored Jun 23, 2024
2 parents 82c994e + 043989f commit 86fac58
Show file tree
Hide file tree
Showing 13 changed files with 633 additions and 441 deletions.
50 changes: 27 additions & 23 deletions R/fetchgit.R → R/fetchers.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ fetchgit <- function(git_pkg){
output <- get_sri_hash_deps(repo_url, branch_name, commit)
sri_hash <- output$sri_hash
imports <- output$deps
imports <- unlist(strsplit(imports, split = " "))
imports <- paste(c("", imports), collapse = '\n ')

sprintf('
(pkgs.rPackages.buildRPackage {
name = \"%s\";
src = pkgs.fetchgit {
url = \"%s\";
branchName = \"%s\";
rev = \"%s\";
sha256 = \"%s\";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) %s;
};
})
(pkgs.rPackages.buildRPackage {
name = \"%s\";
src = pkgs.fetchgit {
url = \"%s\";
branchName = \"%s\";
rev = \"%s\";
sha256 = \"%s\";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) %s;
};
})
',
package_name,
repo_url,
Expand Down Expand Up @@ -59,22 +61,24 @@ fetchzip <- function(archive_pkg, sri_hash = NULL){
output <- get_sri_hash_deps(repo_url, branch_name = NULL, commit = NULL)
sri_hash <- output$sri_hash
imports <- output$deps
imports <- unlist(strsplit(imports, split = " "))
imports <- paste(c("", imports), collapse = '\n ')
} else {
sri_hash <- sri_hash
imports <- NULL
}

sprintf('
(pkgs.rPackages.buildRPackage {
name = \"%s\";
src = pkgs.fetchzip {
url = \"%s\";
sha256 = \"%s\";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) %s;
};
})
(pkgs.rPackages.buildRPackage {
name = \"%s\";
src = pkgs.fetchzip {
url = \"%s\";
sha256 = \"%s\";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) %s;
};
})
',
package_name,
repo_url,
Expand Down Expand Up @@ -148,7 +152,7 @@ fetchlocal <- function(local_pkg){
package_name <- package_name[1]

# Remove rest of path from name
package_name <- unlist(strsplit(package_name, split = "/")) |> tail(1)
package_name <- tail(unlist(strsplit(package_name, split = "/")), 1)

sprintf('
(pkgs.rPackages.buildRPackage {
Expand Down
2 changes: 1 addition & 1 deletion R/get_sri_hash_deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ get_sri_hash_deps <- function(repo_url, branch_name, commit) {

# plumber endpoint delivers list with
# - `sri_hash`: string with SRI hash of the NAR serialization of a Github repo
# - `deps`: string with R package dependencies separarated by `" "`
# - `deps`: string with R package dependencies separated by `" "`
sri_hash_deps_list <- jsonlite::fromJSON(rawToChar(req$content))

return(sri_hash_deps_list)
Expand Down
65 changes: 34 additions & 31 deletions R/internal_functions.R → R/rix_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ generate_header <- function(nix_repo,
if(identical(Sys.getenv("TESTTHAT"), "true")){
sprintf('
let
pkgs = import (fetchTarball "%s") {};
pkgs = import (fetchTarball "%s") {};
',
nix_repo$url)
} else {
Expand Down Expand Up @@ -130,9 +130,9 @@ generate_rpkgs <- function(rPackages, flag_rpkgs) {
NULL
} else {
sprintf('
rpkgs = builtins.attrValues {
inherit (pkgs.rPackages) %s;
};
rpkgs = builtins.attrValues {
inherit (pkgs.rPackages) %s;
};
',
rPackages)
}
Expand All @@ -148,9 +148,9 @@ generate_local_pkgs <- function(local_pkgs, flag_local_pkgs) {
NULL
} else {
sprintf('
local_pkgs = [
%s
];
local_pkgs = [
%s
];
',
fetchlocals(local_pkgs))
}
Expand All @@ -165,12 +165,12 @@ generate_tex_pkgs <- function(tex_pkgs) {

tex_pkgs <- unique(c("scheme-small", tex_pkgs))

tex_pkgs <- paste(tex_pkgs, collapse = ' ')
tex_pkgs <- paste(c("", tex_pkgs), collapse = '\n ')

sprintf('
tex = (pkgs.texlive.combine {
inherit (pkgs.texlive) %s;
});
tex = (pkgs.texlive.combine {
inherit (pkgs.texlive) %s;
});
',
tex_pkgs)
}
Expand All @@ -191,8 +191,7 @@ get_system_pkgs <- function(system_pkgs, r_pkgs){
} else {
unique(system_pkgs)
}

paste(system_pkgs, collapse = ' ')
paste(c("", system_pkgs), collapse = '\n ')
}


Expand All @@ -202,9 +201,9 @@ get_system_pkgs <- function(system_pkgs, r_pkgs){
#' @noRd
generate_system_pkgs <- function(system_pkgs, r_pkgs){
sprintf('
system_packages = builtins.attrValues {
inherit (pkgs) %s;
};
system_packages = builtins.attrValues {
inherit (pkgs) %s;
};
',
get_system_pkgs(system_pkgs, r_pkgs))
}
Expand All @@ -220,7 +219,7 @@ generate_git_archived_pkgs <- function(git_pkgs, archive_pkgs, flag_git_archive)
NULL
} else {
sprintf('
git_archive_pkgs = [%s];\n', fetchpkgs(git_pkgs, archive_pkgs))
git_archive_pkgs = [%s ];\n', fetchpkgs(git_pkgs, archive_pkgs))
}
}

Expand Down Expand Up @@ -272,19 +271,23 @@ generate_locale_variables <- function() {
#' @param flag_rpkgs Character, are there any R packages at all?
#' @param flag_local_pkgs Character, are there any local R packages at all?
#' @noRd
generate_wrapped_pkgs <- function(ide, attrib, flag_git_archive, flag_rpkgs, flag_local_pkgs){
generate_wrapped_pkgs <- function(ide,
attrib,
flag_git_archive,
flag_rpkgs,
flag_local_pkgs){
if (flag_rpkgs == ""){
return(NULL)
} else if(ide %in% names(attrib)){
sprintf('
wrapped_pkgs = pkgs.%s.override {
packages = [ %s %s %s ];
};
wrapped_pkgs = pkgs.%s.override {
packages = [ %s %s %s ];
};
',
attrib[ide],
flag_git_archive,
flag_rpkgs,
flag_local_pkgs
attrib[ide],
flag_git_archive,
flag_rpkgs,
flag_local_pkgs
)
} else {
NULL
Expand All @@ -309,12 +312,12 @@ generate_shell <- function(flag_git_archive,
sprintf('
in
pkgs.mkShell {
%s
%s
buildInputs = [ %s %s %s system_packages %s %s ];
%s
}',
pkgs.mkShell {
%s
%s
buildInputs = [ %s %s %s system_packages %s %s ];
%s
}',
generate_locale_archive(detect_os()),
generate_locale_variables(),
flag_git_archive,
Expand Down
139 changes: 87 additions & 52 deletions tests/testthat/_snaps/rix/bleeding_edge_default.nix
Original file line number Diff line number Diff line change
@@ -1,73 +1,108 @@

let
pkgs = import (fetchTarball "https://github.com/rstats-on-nix/nixpkgs/archive/refs/heads/r-daily.tar.gz") {};
pkgs = import (fetchTarball "https://github.com/rstats-on-nix/nixpkgs/archive/refs/heads/r-daily.tar.gz") {};

rpkgs = builtins.attrValues {
inherit (pkgs.rPackages)
rpkgs = builtins.attrValues {
inherit (pkgs.rPackages)
dplyr
janitor
quarto;
};
};

git_archive_pkgs = [
(pkgs.rPackages.buildRPackage {
name = "housing";
src = pkgs.fetchgit {
url = "https://github.com/rap4all/housing/";
branchName = "fusen";
rev = "1c860959310b80e67c41f7bbdc3e84cef00df18e";
sha256 = "sha256-s4KGtfKQ7hL0sfDhGb4BpBpspfefBN6hf+XlslqyEn4=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) dplyr ggplot2 janitor purrr readxl rlang rvest stringr tidyr;
};
})
git_archive_pkgs = [
(pkgs.rPackages.buildRPackage {
name = "housing";
src = pkgs.fetchgit {
url = "https://github.com/rap4all/housing/";
branchName = "fusen";
rev = "1c860959310b80e67c41f7bbdc3e84cef00df18e";
sha256 = "sha256-s4KGtfKQ7hL0sfDhGb4BpBpspfefBN6hf+XlslqyEn4=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages)
dplyr
ggplot2
janitor
purrr
readxl
rlang
rvest
stringr
tidyr;
};
})


(pkgs.rPackages.buildRPackage {
name = "fusen";
src = pkgs.fetchgit {
url = "https://github.com/ThinkR-open/fusen";
branchName = "main";
rev = "d617172447d2947efb20ad6a4463742b8a5d79dc";
sha256 = "sha256-TOHA1ymLUSgZMYIA1a2yvuv0799svaDOl3zOhNRxcmw=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) attachment cli desc devtools glue here magrittr parsermd roxygen2 stringi tibble tidyr usethis yaml;
};
})
(pkgs.rPackages.buildRPackage {
name = "fusen";
src = pkgs.fetchgit {
url = "https://github.com/ThinkR-open/fusen";
branchName = "main";
rev = "d617172447d2947efb20ad6a4463742b8a5d79dc";
sha256 = "sha256-TOHA1ymLUSgZMYIA1a2yvuv0799svaDOl3zOhNRxcmw=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages)
attachment
cli
desc
devtools
glue
here
magrittr
parsermd
roxygen2
stringi
tibble
tidyr
usethis
yaml;
};
})

(pkgs.rPackages.buildRPackage {
name = "AER";
src = pkgs.fetchzip {
url = "https://cran.r-project.org/src/contrib/Archive/AER/AER_1.2-8.tar.gz";
sha256 = "sha256-OqxXcnUX/2C6wfD5fuNayc8OU+mstI3tt4eBVGQZ2S0=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages) car lmtest sandwich survival zoo Formula;
};
})
];
(pkgs.rPackages.buildRPackage {
name = "AER";
src = pkgs.fetchzip {
url = "https://cran.r-project.org/src/contrib/Archive/AER/AER_1.2-8.tar.gz";
sha256 = "sha256-OqxXcnUX/2C6wfD5fuNayc8OU+mstI3tt4eBVGQZ2S0=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages)
car
lmtest
sandwich
survival
zoo
Formula;
};
})
];

tex = (pkgs.texlive.combine {
inherit (pkgs.texlive) scheme-small amsmath;
});
tex = (pkgs.texlive.combine {
inherit (pkgs.texlive)
scheme-small
amsmath;
});

system_packages = builtins.attrValues {
inherit (pkgs) R glibcLocales nix quarto;
};
system_packages = builtins.attrValues {
inherit (pkgs)
R
glibcLocales
nix
quarto;
};

in

pkgs.mkShell {
LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
LANG = "en_US.UTF-8";
pkgs.mkShell {
LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
LANG = "en_US.UTF-8";
LC_ALL = "en_US.UTF-8";
LC_TIME = "en_US.UTF-8";
LC_MONETARY = "en_US.UTF-8";
LC_PAPER = "en_US.UTF-8";
LC_MEASUREMENT = "en_US.UTF-8";

buildInputs = [ git_archive_pkgs rpkgs tex system_packages ];
}
buildInputs = [ git_archive_pkgs rpkgs tex system_packages ];

}
Loading

0 comments on commit 86fac58

Please sign in to comment.