Skip to content

Commit 4369e24

Browse files
authored
Rollup merge of rust-lang#115117 - pnkfelix:detect-and-report-nix-shell, r=albertlarsan68
Detect and report nix shell Better diagnostics for people using nix subshell on non-NixOS. 1. Turned patch-binaries-for-nix from a boolean into a ternary flag: true, false, and unset. 2. When patch-binaries-for-nix is unset, we continue with the existing NixOS detection heuristic (look for nixos in /etc/os-release, if present), but if we are not atop NixOS, then issue a note if we see the IN_NIX_SHELL environment variable telling the user to consider setting patch-binaries-for-nix explicitly. Fix rust-lang#115073
2 parents f846d7d + ec2c95e commit 4369e24

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/bootstrap/bootstrap.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def _download_component_helper(
623623

624624
def should_fix_bins_and_dylibs(self):
625625
"""Whether or not `fix_bin_or_dylib` needs to be run; can only be True
626-
on NixOS.
626+
on NixOS or if config.toml has `build.patch-binaries-for-nix` set.
627627
"""
628628
if self._should_fix_bins_and_dylibs is not None:
629629
return self._should_fix_bins_and_dylibs
@@ -643,18 +643,31 @@ def get_answer():
643643
if ostype != "Linux":
644644
return False
645645

646-
# If the user has asked binaries to be patched for Nix, then
647-
# don't check for NixOS.
646+
# If the user has explicitly indicated whether binaries should be
647+
# patched for Nix, then don't check for NixOS.
648648
if self.get_toml("patch-binaries-for-nix", "build") == "true":
649649
return True
650+
if self.get_toml("patch-binaries-for-nix", "build") == "false":
651+
return False
650652

651653
# Use `/etc/os-release` instead of `/etc/NIXOS`.
652654
# The latter one does not exist on NixOS when using tmpfs as root.
653655
try:
654656
with open("/etc/os-release", "r") as f:
655-
return any(ln.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"') for ln in f)
657+
is_nixos = any(ln.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"')
658+
for ln in f)
656659
except FileNotFoundError:
657-
return False
660+
is_nixos = False
661+
662+
# If not on NixOS, then warn if user seems to be atop Nix shell
663+
if not is_nixos:
664+
in_nix_shell = os.getenv('IN_NIX_SHELL')
665+
if in_nix_shell:
666+
print("The IN_NIX_SHELL environment variable is `{}`;".format(in_nix_shell),
667+
"you may need to set `patch-binaries-for-nix=true` in config.toml",
668+
file=sys.stderr)
669+
670+
return is_nixos
658671

659672
answer = self._should_fix_bins_and_dylibs = get_answer()
660673
if answer:

src/bootstrap/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub struct Config {
137137
pub json_output: bool,
138138
pub test_compare_mode: bool,
139139
pub color: Color,
140-
pub patch_binaries_for_nix: bool,
140+
pub patch_binaries_for_nix: Option<bool>,
141141
pub stage0_metadata: Stage0Metadata,
142142

143143
pub stdout_is_tty: bool,
@@ -1339,7 +1339,7 @@ impl Config {
13391339
set(&mut config.local_rebuild, build.local_rebuild);
13401340
set(&mut config.print_step_timings, build.print_step_timings);
13411341
set(&mut config.print_step_rusage, build.print_step_rusage);
1342-
set(&mut config.patch_binaries_for_nix, build.patch_binaries_for_nix);
1342+
config.patch_binaries_for_nix = build.patch_binaries_for_nix;
13431343

13441344
config.verbose = cmp::max(config.verbose, flags.verbose as usize);
13451345

src/bootstrap/download.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ impl Config {
9191
// NOTE: this intentionally comes after the Linux check:
9292
// - patchelf only works with ELF files, so no need to run it on Mac or Windows
9393
// - On other Unix systems, there is no stable syscall interface, so Nix doesn't manage the global libc.
94-
if self.patch_binaries_for_nix {
95-
return true;
94+
if let Some(explicit_value) = self.patch_binaries_for_nix {
95+
return explicit_value;
9696
}
9797

9898
// Use `/etc/os-release` instead of `/etc/NIXOS`.
@@ -105,6 +105,15 @@ impl Config {
105105
matches!(l.trim(), "ID=nixos" | "ID='nixos'" | "ID=\"nixos\"")
106106
}),
107107
};
108+
if !is_nixos {
109+
let in_nix_shell = env::var("IN_NIX_SHELL");
110+
if let Ok(in_nix_shell) = in_nix_shell {
111+
eprintln!(
112+
"The IN_NIX_SHELL environment variable is `{in_nix_shell}`; \
113+
you may need to set `patch-binaries-for-nix=true` in config.toml"
114+
);
115+
}
116+
}
108117
is_nixos
109118
});
110119
if val {

0 commit comments

Comments
 (0)