From 7e826ea733f734f5df13ee4a895017f4c74f60cc Mon Sep 17 00:00:00 2001 From: Brian Connelly Date: Mon, 28 Aug 2023 14:26:09 -0700 Subject: [PATCH] Add `using_nix_shell()` --- NAMESPACE | 1 + NEWS.md | 2 ++ R/nix.R | 36 ++++++++++++++++++++++++++++++++++++ _pkgdown.yml | 1 + man/nix.Rd | 26 ++++++++++++++++++++++++++ tests/testthat/test-nix.R | 27 +++++++++++++++++++++++++++ 6 files changed, 93 insertions(+) create mode 100644 R/nix.R create mode 100644 man/nix.Rd create mode 100644 tests/testthat/test-nix.R diff --git a/NAMESPACE b/NAMESPACE index 36e410f..47ecb70 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(using_kubernetes) export(using_latest_r_version) export(using_linux) export(using_macos) +export(using_nix_shell) export(using_option) export(using_os) export(using_podman_container) diff --git a/NEWS.md b/NEWS.md index 0be4f43..32f7c49 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ami 0.1.0.9000 +* Added `using_nix_shell()` for detecting [Nix shell environments](https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html) + # ami 0.1.0 * Initial public release diff --git a/R/nix.R b/R/nix.R new file mode 100644 index 0000000..5d497c4 --- /dev/null +++ b/R/nix.R @@ -0,0 +1,36 @@ +#' @rdname nix +#' @title Detect `Nix` Shell +#' +#' @description `using_nix_shell()` checks whether code is running within an +# nolint start +#' environment defined by a [Nix expression](https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html). +# nolint end +#' +#' @param pure Whether or not the environment is pure, meaning most environment +#' variables have been cleared before the shell started. +#' +#' @return A logical value +#' @export +#' +#' @examples +#' # Check for Nix +#' using_nix_shell() +#' +#' # Check for Nix in a pure environment +#' using_nix_shell(pure = TRUE) +using_nix_shell <- function(pure = NULL) { + if (!rlang::is_logical(pure, n = 1) && !rlang::is_null(pure)) { + rlang::abort( + message = "`pure` should be a logical scalar", + class = "arg_not_scalar_logical" + ) + } + + if (isTRUE(pure)) { + using_envvar("IN_NIX_SHELL", "pure") + } else if (isFALSE(pure)) { + using_envvar("IN_NIX_SHELL", "impure") + } else { + using_envvar("IN_NIX_SHELL") + } +} diff --git a/_pkgdown.yml b/_pkgdown.yml index fc69e6f..d229013 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -73,4 +73,5 @@ reference: desc: Additional checks contents: - using_databricks + - using_nix_shell diff --git a/man/nix.Rd b/man/nix.Rd new file mode 100644 index 0000000..edf1294 --- /dev/null +++ b/man/nix.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/nix.R +\name{using_nix_shell} +\alias{using_nix_shell} +\title{Detect \code{Nix} Shell} +\usage{ +using_nix_shell(pure = NULL) +} +\arguments{ +\item{pure}{Whether or not the environment is pure, meaning most environment +variables have been cleared before the shell started.} +} +\value{ +A logical value +} +\description{ +\code{using_nix_shell()} checks whether code is running within an +environment defined by a \href{https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html}{Nix expression}. +} +\examples{ +# Check for Nix +using_nix_shell() + +# Check for Nix in a pure environment +using_nix_shell(pure = TRUE) +} diff --git a/tests/testthat/test-nix.R b/tests/testthat/test-nix.R new file mode 100644 index 0000000..f42cd36 --- /dev/null +++ b/tests/testthat/test-nix.R @@ -0,0 +1,27 @@ +test_that("using_nix_shell validates input properly", { + expect_error(using_nix_shell(pure = NA_character_)) + expect_error(using_nix_shell(pure = 1)) + expect_error(using_nux_shell(pure = "pure")) + expect_error(using_nix_shell(pure = c(TRUE, TRUE))) +}) + +test_that("using_nix_shell works as expected when `IN_NIX_SHELL` not set", { + withr::local_envvar(list("IN_NIX_SHELL" = NA)) + expect_false(using_nix_shell()) + expect_false(using_nix_shell(pure = TRUE)) + expect_false(using_nix_shell(pure = FALSE)) +}) + +test_that("using_nix_shell works as expected in pure environment", { + withr::local_envvar(list("IN_NIX_SHELL" = "pure")) + expect_true(using_nix_shell()) + expect_true(using_nix_shell(pure = TRUE)) + expect_false(using_nix_shell(pure = FALSE)) +}) + +test_that("using_nix_shell works as expected in impure environment", { + withr::local_envvar(list("IN_NIX_SHELL" = "impure")) + expect_true(using_nix_shell()) + expect_false(using_nix_shell(pure = TRUE)) + expect_true(using_nix_shell(pure = FALSE)) +})