[Nix-dev] Down with /bin/sh!

Ludovic Courtès ludo at gnu.org
Thu Jan 3 16:14:12 CET 2013


Hello {N,Gu}ixers, and happy new year!

The ‘core-updates’ branch of Guix now makes it possible to build
packages in a chroot lacking /bin/sh.

It’s convenient to have /bin/sh in the chroot, because that’s basically
one of the files whose name is hardcoded in many places, from libc to
shebangs.

However, the problem with /bin/sh is that it’s out of control.  On
non-NixOS distros, it’s not necessarily Bash.  Someone with Dash as
/bin/sh may have different results than someone Bash as /bin/sh.  That’s
an “impurity”, with practical consequences on build reproducibility.

Another issue on non-NixOS distros is that /bin/sh is typically a
dynamically-linked executable.  So adding /bin to the chroot is not
enough; one typically needs to also add /lib* and /lib/*-linux-gnu to
the chroot.  At that point, there are many impurities, and a great
potential for non-reproducibility–which defeats the purpose of the chroot.


So, here’s how this is solved in ‘core-updates’.

  • Right after unpacking a source tarball, all the source files go
    through ‘patch-shebang’, which replaces any #!/bin/sh and similar
    with the right path.

  • Once configure has run, all makefiles that define the ‘SHELL’
    variable are patched similarly.

The GNU build system supports ‘CONFIG_SHELL’ and ‘SHELL’, which
simplifies things (info "(autoconf) config.status Invocation").

Occasionally, packages have references to /bin/sh elsewhere, which need
to be patched as well, notably:

  • the ‘system’ and ‘popen’ functions in the GNU libc;

  • the ‘default_shell’ variable in GNU make;

  • io.c in GNU Awk;

  • ice-9/popen.scm in GNU Guile.

For libc, referring to the compile-time Bash would retain a dependency
on the bootstrap environment.  So glibc is changed to contain a copy of
a statically-linked Bash in its store path, which it can refer to
(thanks to Shea and Lluís for the suggestion).

The bootstrap libc and awk need a different trick, though, since they
must be relocated (IOW, their path is not known in advance.)  The trick
is to search for ‘sh’ in $PATH, with these simple patches:

  http://git.savannah.gnu.org/cgit/guix.git/tree/distro/packages/patches/glibc-bootstrap-system.patch?h=core-updates
  http://git.savannah.gnu.org/cgit/guix.git/tree/distro/packages/patches/gawk-shell.patch?h=core-updates

(For security reasons, these patches are /not/ used in the final libc
and awk.)

So far, it seems that little or no manual tweaking is needed beyond the
above, so hopefully it’ll scale.  The main advantage is that it makes it
possible to use Guix reliably on non-NixOS distros; even on a distro
like NixOS, that’s one less impurity, and thus improved reproducibility.

Thanks,
Ludo’.


More information about the nix-dev mailing list