···2525 export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
2626 export NIX_STATE_DIR=$TEST_ROOT/var/nix
2727 export NIX_STORE_DIR=$TEST_ROOT/store
2828+2929+ # cargo tests run in parallel by default, which would then run into
3030+ # https://github.com/NixOS/nix/issues/2706 unless the store is initialised first
3131+ nix-store --init
2832 '';
2933 postCheck = ''
3034 cargo fmt --check
+9-3
pkgs/test/nixpkgs-check-by-name/src/eval.rs
···3030 // Write the list of packages we need to check into a temporary JSON file.
3131 // This can then get read by the Nix evaluation.
3232 let attrs_file = NamedTempFile::new().context("Failed to create a temporary file")?;
3333+ // We need to canonicalise this path because if it's a symlink (which can be the case on
3434+ // Darwin), Nix would need to read both the symlink and the target path, therefore need 2
3535+ // NIX_PATH entries for restrict-eval. But if we resolve the symlinks then only one predictable
3636+ // entry is needed.
3737+ let attrs_file_path = attrs_file.path().canonicalize()?;
3838+3339 serde_json::to_writer(&attrs_file, &nixpkgs.package_names).context(format!(
3440 "Failed to serialise the package names to the temporary path {}",
3535- attrs_file.path().display()
4141+ attrs_file_path.display()
3642 ))?;
37433844 // With restrict-eval, only paths in NIX_PATH can be accessed, so we explicitly specify the
···5763 // Pass the path to the attrs_file as an argument and add it to the NIX_PATH so it can be
5864 // accessed in restrict-eval mode
5965 .args(["--arg", "attrsPath"])
6060- .arg(attrs_file.path())
6666+ .arg(&attrs_file_path)
6167 .arg("-I")
6262- .arg(attrs_file.path())
6868+ .arg(&attrs_file_path)
6369 // Same for the nixpkgs to test
6470 .args(["--arg", "nixpkgsPath"])
6571 .arg(&nixpkgs.path)
+36
pkgs/test/nixpkgs-check-by-name/src/main.rs
···140140 Ok(())
141141 }
142142143143+ /// Tests symlinked temporary directories.
144144+ /// This is needed because on darwin, `/tmp` is a symlink to `/private/tmp`, and Nix's
145145+ /// restrict-eval doesn't also allow access to the canonical path when you allow the
146146+ /// non-canonical one.
147147+ ///
148148+ /// The error if we didn't do this would look like this:
149149+ /// error: access to canonical path '/private/var/folders/[...]/.tmpFbcNO0' is forbidden in restricted mode
150150+ #[test]
151151+ fn test_symlinked_tmpdir() -> anyhow::Result<()> {
152152+ // Create a directory with two entries:
153153+ // - actual (dir)
154154+ // - symlinked -> actual (symlink)
155155+ let temp_root = tempdir()?;
156156+ fs::create_dir(temp_root.path().join("actual"))?;
157157+ std::os::unix::fs::symlink("actual", temp_root.path().join("symlinked"))?;
158158+ let tmpdir = temp_root.path().join("symlinked");
159159+160160+ // Then set TMPDIR to the symlinked directory
161161+ // Make sure to persist the old value so we can undo this later
162162+ let old_tmpdir = env::var("TMPDIR").ok();
163163+ env::set_var("TMPDIR", &tmpdir);
164164+165165+ // Then run a simple test with this symlinked temporary directory
166166+ // This should be successful
167167+ test_nixpkgs("symlinked_tmpdir", Path::new("tests/success"), "")?;
168168+169169+ // Undo the env variable change
170170+ if let Some(old) = old_tmpdir {
171171+ env::set_var("TMPDIR", old);
172172+ } else {
173173+ env::remove_var("TMPDIR");
174174+ }
175175+176176+ Ok(())
177177+ }
178178+143179 fn test_nixpkgs(name: &str, path: &Path, expected_errors: &str) -> anyhow::Result<()> {
144180 let extra_nix_path = Path::new("tests/mock-nixpkgs.nix");
145181