From 6abf2ca53b8e793bcab52940d435ab7b896d6af1 Mon Sep 17 00:00:00 2001 From: pchabros Date: Tue, 8 Oct 2024 21:32:10 +0200 Subject: [PATCH 1/3] Option to install R packages from DESCRIPTION file --- examples/r/.test.sh | 9 ++++ examples/r/DESCRIPTION | 14 +++++++ examples/r/devenv.nix | 4 ++ .../languages/{r.nix => r/default.nix} | 24 +++++++++-- src/modules/languages/r/descriptionFile.nix | 42 +++++++++++++++++++ src/modules/languages/r/utils/strings.nix | 23 ++++++++++ 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100755 examples/r/.test.sh create mode 100644 examples/r/DESCRIPTION rename src/modules/languages/{r.nix => r/default.nix} (51%) create mode 100644 src/modules/languages/r/descriptionFile.nix create mode 100644 src/modules/languages/r/utils/strings.nix diff --git a/examples/r/.test.sh b/examples/r/.test.sh new file mode 100755 index 000000000..7af89f6db --- /dev/null +++ b/examples/r/.test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -ex + +R --version +radian --version + +for package in readr stringr data.table yaml testthat; do + Rscript -e "library($package)" +done diff --git a/examples/r/DESCRIPTION b/examples/r/DESCRIPTION new file mode 100644 index 000000000..37982c004 --- /dev/null +++ b/examples/r/DESCRIPTION @@ -0,0 +1,14 @@ +Type: Package +Package: myPackage +Title: Devenv test R Package +Version: 0.0.0.1 +Imports: + readr, + stringr, + nonexistentpackage +Depends: + data.table, + yaml +Suggests: + testthat (>= 3.0.0) +Encoding: UTF-8 diff --git a/examples/r/devenv.nix b/examples/r/devenv.nix index f9b39e5c2..0cfd7d8a6 100644 --- a/examples/r/devenv.nix +++ b/examples/r/devenv.nix @@ -7,5 +7,9 @@ enable = true; package = pkgs.python312Packages.radian; }; + descriptionFile = { + path = "${./DESCRIPTION}"; + installPackages.enable = true; + }; }; } diff --git a/src/modules/languages/r.nix b/src/modules/languages/r/default.nix similarity index 51% rename from src/modules/languages/r.nix rename to src/modules/languages/r/default.nix index 6e331be21..3196a07e5 100644 --- a/src/modules/languages/r.nix +++ b/src/modules/languages/r/default.nix @@ -1,7 +1,11 @@ -{ pkgs, config, lib, ... }: - +{ pkgs +, config +, lib +, ... +}: let cfg = config.languages.r; + descriptionFile = import ./descriptionFile.nix; in { options.languages.r = { @@ -21,9 +25,23 @@ in description = "The radian package to use."; }; }; + descriptionFile = { + path = lib.mkOption { + type = lib.types.pathInStore; + description = "Path to DESCRIPTION file"; + }; + installPackages = { + enable = lib.mkEnableOption "installation of R packages listed in DESCRIPTION file"; + }; + }; }; config = lib.mkIf cfg.enable { - packages = with pkgs; [ cfg.package ] ++ lib.lists.optional cfg.radian.enable cfg.radian.package; + packages = + [ cfg.package ] + ++ lib.lists.optional cfg.radian.enable cfg.radian.package + ++ lib.lists.optionals cfg.descriptionFile.installPackages.enable ( + descriptionFile.getRPackages pkgs (builtins.readFile cfg.descriptionFile.path) + ); }; } diff --git a/src/modules/languages/r/descriptionFile.nix b/src/modules/languages/r/descriptionFile.nix new file mode 100644 index 000000000..a3a096bfb --- /dev/null +++ b/src/modules/languages/r/descriptionFile.nix @@ -0,0 +1,42 @@ +let + strings = import ./utils/strings.nix; + inherit (builtins) + concatMap + filter + hasAttr + replaceStrings + warn + ; + inherit (strings) + matches + extractWithRegex + splitWithRegex + ; +in +rec { + getSection = + section: descriptionFile: + extractWithRegex ".*${section}:\n +([a-zA-Z0-9., (>=)\n]*)\n[A-Z].*" descriptionFile; + extractPackages = packages: filter (matches "^[a-zA-Z0-9.]+") packages; + extractSectionPackages = section: extractPackages (splitWithRegex "[\n, ]+" section); + descriptionPackages = + descriptionFile: + concatMap (section: extractSectionPackages (getSection section descriptionFile)) [ + "Imports" + "Depends" + "Suggests" + ]; + normalizePackageName = pkg: replaceStrings [ "." ] [ "_" ] pkg; + getRPackage = + pkg: pkgs: + let + pkgName = normalizePackageName pkg; + in + if hasAttr pkgName pkgs.rPackages then + pkgs.rPackages.${pkgName} + else + warn "Package \"${pkgName}\" does not exist in nixpkgs." null; + getRPackages = + pkgs: descriptionFile: + filter (pkg: pkg != null) (map (pkg: getRPackage pkg pkgs) (descriptionPackages descriptionFile)); +} diff --git a/src/modules/languages/r/utils/strings.nix b/src/modules/languages/r/utils/strings.nix new file mode 100644 index 000000000..cb597f6ff --- /dev/null +++ b/src/modules/languages/r/utils/strings.nix @@ -0,0 +1,23 @@ +let + inherit (builtins) + filter + head + isNull + isString + match + split + ; +in +{ + # matches :: string -> string -> bool + matches = pattern: text: !isNull (match "(${pattern})" text); + # extractWithRegex :: string -> string -> string + extractWithRegex = + regex: string: + let + matched = match regex string; + in + if isNull matched then "" else head matched; + # splitWithRegex :: string -> string -> [string] + splitWithRegex = regex: string: filter isString (split regex string); +} From 535d2e135bcb7930800930c21cc589ed5ca33f05 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 24 Nov 2024 19:42:09 +0000 Subject: [PATCH 2/3] Auto generate docs/reference/options.md --- docs/reference/options.md | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/docs/reference/options.md b/docs/reference/options.md index a10955d57..da2a37084 100644 --- a/docs/reference/options.md +++ b/docs/reference/options.md @@ -34481,7 +34481,7 @@ boolean ` true ` *Declared by:* - - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix](https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix) + - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r](https://github.com/cachix/devenv/blob/main/src/modules/languages/r) @@ -34502,7 +34502,49 @@ package ` pkgs.R ` *Declared by:* - - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix](https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix) + - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r](https://github.com/cachix/devenv/blob/main/src/modules/languages/r) + + + +## languages.r.descriptionFile.installPackages.enable + + + +Whether to enable installation of R packages listed in DESCRIPTION file. + + + +*Type:* +boolean + + + +*Default:* +` false ` + + + +*Example:* +` true ` + +*Declared by:* + - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r](https://github.com/cachix/devenv/blob/main/src/modules/languages/r) + + + +## languages.r.descriptionFile.path + + + +Path to DESCRIPTION file + + + +*Type:* +path in the Nix store + +*Declared by:* + - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r](https://github.com/cachix/devenv/blob/main/src/modules/languages/r) @@ -34528,7 +34570,7 @@ boolean ` true ` *Declared by:* - - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix](https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix) + - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r](https://github.com/cachix/devenv/blob/main/src/modules/languages/r) @@ -34549,7 +34591,7 @@ package ` pkgs.radianWrapper ` *Declared by:* - - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix](https://github.com/cachix/devenv/blob/main/src/modules/languages/r.nix) + - [https://github.com/cachix/devenv/blob/main/src/modules/languages/r](https://github.com/cachix/devenv/blob/main/src/modules/languages/r) From 22fa7eeae1d38fa1d10172c5ae123b2adc73522b Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 24 Nov 2024 19:43:11 +0000 Subject: [PATCH 3/3] Auto generate docs and examples --- docs/supported-languages/r.md | 76 ----------------------------------- 1 file changed, 76 deletions(-) diff --git a/docs/supported-languages/r.md b/docs/supported-languages/r.md index f3b5bf795..32af872d7 100644 --- a/docs/supported-languages/r.md +++ b/docs/supported-languages/r.md @@ -3,80 +3,4 @@ [comment]: # (Please add your documentation on top of this line) -## languages\.r\.enable -Whether to enable tools for R development\. - - - -*Type:* -boolean - - - -*Default:* -` false ` - - - -*Example:* -` true ` - - - -## languages\.r\.package - - - -The R package to use\. - - - -*Type:* -package - - - -*Default:* -` pkgs.R ` - - - -## languages\.r\.radian\.enable - - - -Whether to enable a 21 century R console\. - - - -*Type:* -boolean - - - -*Default:* -` false ` - - - -*Example:* -` true ` - - - -## languages\.r\.radian\.package - - - -The radian package to use\. - - - -*Type:* -package - - - -*Default:* -` pkgs.radianWrapper `