Missing /bin/sh on live image

I’ve been helping a friend debug some issues with their NixOS live image their building with parts of their config integrated. I had a “well that’ll do it” moment when I realized that /bin/sh was completely absent when the image was booted. To be clear: Not a broken symlink, just completely not there: /bin/ is an empty folder.

My normal debugging approaches would be one of either: A) creating a minimal reproducer, or B) digging into the Nix source code to figure out what options could cause this. Unfortunately A) I’m working with a config directory that I’m not familiar with, using a foreign-to-me framework, that’s responsible for configuring many devices, so creating a minimal reproducer by subdivision is a little complex, and B) I’ve got no idea where to even start looking for this sort of thing. I’ve always taken /bin/sh for granted, I had no idea it could get turned off :p.

Of course, I could get through these with enough elbow grease, but I was hoping that I might be able to draw from the expertise of the knowledgeable folks on this forum, and in doing so, create a record for future coders who have this same problem (since my searches turned up nothing).

In particular, any advice as to where to dig in the source code to find what options could cause this, or if anyone’s had this problem before would be really appreciated! Failing that, I’ll be updating this thread with my findings when I start my deep dive!

Thanks so much for the help!

That’d be the result of someone disabling environment.shell.

The option is deliberarely hidden because no real system ought to use it (though some folks on this discourse run their systems without /bin/sh and like it that way).

Container image generators and such may disable this option, since they have different requirements. Your friends’ image was probably built based on an image generator that they shouldn’t be using.

Oh thank you for the quick response! I wasn’t expecting to get an answer that clear and helpful! This will be a huge help!

FYI, within nixpkgs almost nothing should actually refer to /bin/sh, which is why people often get away with turning off environment.shell. The only thing I can think of that would use it is the C library function system, but (1) I’m not sure it does, (2) if it does that’s arguably a bug in the nixpkgs package of the C library, (3) it’s almost always a bad idea to use that function anyway.

The main thing we keep /bin/sh around for is third-party unpackaged software that expects the standard Unix shell environment to live in its usual location.

[EDIT: Indeed, at least some versions of glibc-as-packaged-by-nixpkgs do refer to /bin/sh and not e.g. /nix/store/gik3rh1vz2jlgnifb9dh6vc6sxwwz9jj-bash-5.3p9/bin/sh. That’s probably worth filing a bug on. I don’t have time to do that today, please feel free to beat me to it.]

Oh interesting! The error is coming from nixpkgs#kdePackages.plasma-workspace/libexec/plasma-dbus-run-session-if-needed which has a /bin/sh shebang. I wonder if it’s an oversight?

Either way, I want to try to get /bin/sh on here to prevent any weirdness. In other news, following TLATER’s advice, I investigated environment.shell.enable, which (according to loading the flake in the repl and checking it’s .config.environment.shell.enable) is apparently set to true, so now I’m digging in the source code for activation scripts to see where the breakdown is occurring. Updates forthcoming!

Problem origin identified! My friend was using envfs, which automatically turns of /bin/sh and some other activation script snippets since normally envfs would take care of that. Unfortunately, due to the way that the live image sets up its environment, envfs never gets put into place, and the result is a broken system without /bin/sh!

Not exactly. @zwol is generally correct - NixOS’ raison d’être is not having these “global variables”, and thereby preventing implicit dependencies which may cause unexpected behavior when you run the same binary on two different systems. Or, exactly the issue you ran into, envfs should in theory do nothing on NixOS :stuck_out_tongue:

The community has grown sufficiently that there are plenty of people around who don’t care about that specific ideal, though. AFAICT NixOS has become a more generalist distro, and is used more for the convenience of having almost every piece of software known to man packaged under one organization so you can tell compliance folk that you only have one vendor (pinky swear). Seems to be most of the use cases driving adoption anyway.

There are definitely scripts which just use global paths and are done with it. I spot them occasionally when reading random modules. I doubt every package is scanned for occurrences of this shebang on every update, either, so even if all module code was written by purists occasionally package updates might result in breakage without a /bin/sh. Even historically, entire programming languages like R have never been packaged in ways that avoid calls to /bin/sh, some of my earliest posts around here are explaining this problem to R folks.

All that said, you can absolutely write issues for or patch these kinds of things, there’s still a good portion of this community who will appreciate that.

There just comes a point at which “oversight” feels like the wrong term - don’t expect /bin/sh to be optional, it’s explicitly not, even though it could be if we tried harder, and it may even be for a given system config because some software is more sensible than other software.

Forgive my ignorance – at least for shebangs, isn’t this supposed to be handled somewhat automatically by patchShebangsAuto during fixupPhase?

In theory, yes, but it’s not 100% reliable. Always worth double checking your build outputs if you package something.