Skip to content

Commit

Permalink
Add using_nix_shell()
Browse files Browse the repository at this point in the history
  • Loading branch information
briandconnelly committed Aug 28, 2023
1 parent 97b07bb commit 7e826ea
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions R/nix.R
Original file line number Diff line number Diff line change
@@ -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")
}
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ reference:
desc: Additional checks
contents:
- using_databricks
- using_nix_shell

26 changes: 26 additions & 0 deletions man/nix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/testthat/test-nix.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit 7e826ea

Please sign in to comment.