Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect and report nix shell #115117

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def _download_component_helper(

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

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

# Use `/etc/os-release` instead of `/etc/NIXOS`.
# The latter one does not exist on NixOS when using tmpfs as root.
try:
with open("/etc/os-release", "r") as f:
return any(ln.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"') for ln in f)
is_nixos = any(ln.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"')
for ln in f)
except FileNotFoundError:
return False
is_nixos = False

# If not on NixOS, then warn if user seems to be atop Nix shell
if not is_nixos:
in_nix_shell = os.getenv('IN_NIX_SHELL')
if in_nix_shell:
print("The IN_NIX_SHELL environment variable is `{}`;".format(in_nix_shell),
"you may need to set `patch-binaries-for-nix=true` in config.toml",
file=sys.stderr)

return is_nixos

answer = self._should_fix_bins_and_dylibs = get_answer()
if answer:
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct Config {
pub json_output: bool,
pub test_compare_mode: bool,
pub color: Color,
pub patch_binaries_for_nix: bool,
pub patch_binaries_for_nix: Option<bool>,
pub stage0_metadata: Stage0Metadata,

pub stdout_is_tty: bool,
Expand Down Expand Up @@ -1338,7 +1338,7 @@ impl Config {
set(&mut config.local_rebuild, build.local_rebuild);
set(&mut config.print_step_timings, build.print_step_timings);
set(&mut config.print_step_rusage, build.print_step_rusage);
set(&mut config.patch_binaries_for_nix, build.patch_binaries_for_nix);
config.patch_binaries_for_nix = build.patch_binaries_for_nix;

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

Expand Down
13 changes: 11 additions & 2 deletions src/bootstrap/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ impl Config {
// NOTE: this intentionally comes after the Linux check:
// - patchelf only works with ELF files, so no need to run it on Mac or Windows
// - On other Unix systems, there is no stable syscall interface, so Nix doesn't manage the global libc.
if self.patch_binaries_for_nix {
return true;
if let Some(explicit_value) = self.patch_binaries_for_nix {
return explicit_value;
}

// Use `/etc/os-release` instead of `/etc/NIXOS`.
Expand All @@ -105,6 +105,15 @@ impl Config {
matches!(l.trim(), "ID=nixos" | "ID='nixos'" | "ID=\"nixos\"")
}),
};
if !is_nixos {
let in_nix_shell = env::var("IN_NIX_SHELL");
if let Ok(in_nix_shell) = in_nix_shell {
eprintln!(
"The IN_NIX_SHELL environment variable is `{in_nix_shell}`; \
you may need to set `patch-binaries-for-nix=true` in config.toml"
);
}
}
is_nixos
});
if val {
Expand Down