fix(nix): keep integration dirs traversable for all users

This commit is contained in:
Phoebus 2026-09-28 00:49:35 -07:00
parent 2261c5483c
commit 7124c8ed5c

View file

@ -49,6 +49,32 @@ fn target_path(root: &Path, rel: &str) -> PathBuf {
root.join(rel) root.join(rel)
} }
/// Top-level Nix integration directories that must stay traversable by
/// every user (0755). The store *contents* keep their shipped modes.
fn integration_dirs() -> &'static [&'static str] {
&[
"nix",
"nix/store",
"nix/var",
"nix/var/nix",
"nix/var/nix/profiles",
"nix/var/nix/profiles/per-user",
"nix/var/nix/gcroots",
"nix/var/nix/temproots",
"nix/var/nix/daemon-socket",
]
}
fn normalize_integration_permissions(root: &Path) {
use std::os::unix::fs::PermissionsExt;
for rel in integration_dirs() {
let dir = target_path(root, rel);
if dir.symlink_metadata().is_ok() {
let _ = std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755));
}
}
}
/// Whether the pinned manifest entry looks usable (not a placeholder). /// Whether the pinned manifest entry looks usable (not a placeholder).
pub fn manifest_is_pinned() -> bool { pub fn manifest_is_pinned() -> bool {
NIX_URL.ends_with(".tar.xz") NIX_URL.ends_with(".tar.xz")
@ -138,6 +164,14 @@ fn bootstrap(exec: &Exec, root: &Path, tarball: &Path) -> Result<()> {
let dir = target_path(root, rel); let dir = target_path(root, rel);
std::fs::create_dir_all(&dir).map_err(|e| ExecError::io(dir.clone(), e))?; std::fs::create_dir_all(&dir).map_err(|e| ExecError::io(dir.clone(), e))?;
} }
// Every user must traverse the store and read profiles: the client
// binary, the daemon socket and per-user generations all live under
// /nix. Staging (`cp -a`) preserves tarball modes and the live shell's
// umask may be restrictive, so normalize the top-level integration
// directories explicitly (never the store contents themselves). Proven
// by VM boot: without this, `nix` is "command not found" for the user
// and `sys list --nix` reports Nix as not installed.
normalize_integration_permissions(root);
{ {
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
let sock = target_path(root, "nix/var/nix/daemon-socket"); let sock = target_path(root, "nix/var/nix/daemon-socket");
@ -743,6 +777,33 @@ mod tests {
assert!(!crate::opencode::checksum_ok(&file, "deadbeef").unwrap()); assert!(!crate::opencode::checksum_ok(&file, "deadbeef").unwrap());
} }
#[test]
fn integration_dirs_stay_traversable_after_normalize() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
for rel in integration_dirs() {
let path = root.join(rel);
std::fs::create_dir_all(&path).unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700)).unwrap();
}
// A store payload keeps its own mode (never touched).
let payload = root.join("nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello-2.12");
std::fs::create_dir_all(&payload).unwrap();
std::fs::set_permissions(&payload, std::fs::Permissions::from_mode(0o700)).unwrap();
normalize_integration_permissions(root);
for rel in integration_dirs() {
let mode = std::fs::metadata(root.join(rel))
.unwrap()
.permissions()
.mode()
& 0o777;
assert_eq!(mode, 0o755, "{rel}");
}
let payload_mode = std::fs::metadata(&payload).unwrap().permissions().mode() & 0o777;
assert_eq!(payload_mode, 0o700);
}
#[test] #[test]
fn conf_enables_enforced_sandbox_and_store_bounds() { fn conf_enables_enforced_sandbox_and_store_bounds() {
let conf = nix_conf(); let conf = nix_conf();