diff --git a/rust/nix-expr/src/eval_state.rs b/rust/nix-expr/src/eval_state.rs index d5fd4f7..854cb65 100644 --- a/rust/nix-expr/src/eval_state.rs +++ b/rust/nix-expr/src/eval_state.rs @@ -353,27 +353,32 @@ impl Drop for EvalState { } } +/// Initialize the Nix library for testing. This includes some modifications to the Nix settings, that must not be used in production. +/// Use at your own peril, in rust test suites. +pub fn test_init() { + init().unwrap(); + + // During development, we encountered a problem where the build hook + // would cause the test suite to reinvokes itself, causing an infinite loop. + // While _NIX_TEST_NO_SANDBOX=1 should prevent this, we may also set the + // build hook to "" to prevent this. + // settings::set("build-hook", "")?; + + // When testing in the sandbox, the default build dir would be a parent of the storeDir, + // which causes an error. So we set a custom build dir here. + nix_util::settings::set("sandbox-build-dir", "/custom-build-dir-for-test").unwrap(); + std::env::set_var("_NIX_TEST_NO_SANDBOX", "1"); +} + #[cfg(test)] mod tests { use ctor::ctor; - use nix_util::settings; use super::*; #[ctor] fn setup() { - init().unwrap(); - - // During development, we encountered a problem where the build hook - // would cause the test suite to reinvokes itself, causing an infinite loop. - // While _NIX_TEST_NO_SANDBOX=1 should prevent this, we may also set the - // build hook to "" to prevent this. - // settings::set("build-hook", "")?; - - // When testing in the sandbox, the default build dir would be a parent of the storeDir, - // which causes an error. So we set a custom build dir here. - settings::set("sandbox-build-dir", "/custom-build-dir-for-test").unwrap(); - std::env::set_var("_NIX_TEST_NO_SANDBOX", "1"); + test_init(); } #[test]