BusyBox environment

I’d like to run/debug a script with the BusyBox-environment only. No extra tools available. I couldn’t find any tutorials related to this. I tried the following:

❯ nix-shell -p busybox --pure --run 'busybox sh'
~/code/vital/uas-data $ which ls
/nix/store/ysqx2xfzygv2rxl7nxnw48276z5ckppn-coreutils-9.5/bin/ls
# not what I want. I want the busybox ls, not coreutils' ls
yann in yann-desktop-nixos in …/uas-data on  main [!?] 
🐟 ❯ nix-shell --pure -p busybox-sandbox-shell --run 'which ls'
/tmp/nix-shell-1334913-0/rc: line 3: which: command not found
# aha, on the right track
yann in yann-desktop-nixos in …/uas-data on  main [!?] 
❌127 🐟 ❯ nix-shell --pure -p busybox-sandbox-shell --run 'command -v ls'
/nix/store/ysqx2xfzygv2rxl7nxnw48276z5ckppn-coreutils-9.5/bin/ls
# nope, still not busybox ls
❌127 🐟 ❯ nix-shell --pure -p busybox-sandbox-shell --run 'ln -sf "$(command -v busybox)" file;./file --version'
file: applet not found
# what the heck is this? Isn't this how busybox is supposed to work? Creating symlinks to it?
❌127 🐟 ❮ nix-shell --pure -p busybox --run 'ln -sf "$(command -v busybox)" file;./file --version'
file: applet not found
# same here

I must be doing something terribly wrong here. Any hints are appreciated. :slightly_smiling_face:

When you use nix-shell with -p flag, it uses the environment provided by stdenv.mkDerivation, which includes coreutils:

$ echo $PATH
/nix/store/c481fhrvslr8nmhhlzdab3k7bpnhb46a-bash-interactive-5.2p26/bin:/nix/store/dv5vgsw8naxnkcc88x78vprbnn1pp44y-patchelf-0.15.0/bin:/nix/store/62zpnw69ylcfhcpy1di8152zlzmbls91-gcc-wrapper-13.3.0/bin:/nix/store/zw4dkm2hl72kfz7j2ci4qbc0avgxzz75-gcc-13.3.0/bin:/nix/store/xfk033jdxdc5758wrpb0h2w462zlsb73-glibc-2.39-52-bin/bin:/nix/store/cnknp3yxfibxjhila0sjd1v3yglqssng-coreutils-9.5/bin:/nix/store/pg90p34kys2famxnq7925sbgj4jrnsi8-binutils-wrapper-2.42/bin:/nix/store/qsx2xqqm0lp6d8hi86r4y0rz5v9m62wn-binutils-2.42/bin:/nix/store/dflmj677g6hidprwgwdq38qpy6rkrrb7-busybox-1.36.1/bin:/nix/store/cnknp3yxfibxjhila0sjd1v3yglqssng-coreutils-9.5/bin:/nix/store/5my5b6mw7h9hxqknvggjla1ci165ly21-findutils-4.10.0/bin:/nix/store/fy6s9lk05yjl1cz2dl8gs0sjrd6h9w5f-diffutils-3.10/bin:/nix/store/9zsm74npdqq2lgjzavlzaqrz8x44mq9d-gnused-4.9/bin:/nix/store/k8zpadqbwqwalggnhqi74gdgrlf3if9l-gnugrep-3.11/bin:/nix/store/2ywpssz17pj0vr4vj7by6aqx2gk01593-gawk-5.2.2/bin:/nix/store/nzzl7dnay9jzgfv9fbwg1zza6ji7bjvr-gnutar-1.35/bin:/nix/store/7m0l19yg0cb1c29wl54y24bbxsd85f4s-gzip-1.13/bin:/nix/store/cx1220ll0pgq6svfq7bmhpdzp0avs09w-bzip2-1.0.8-bin/bin:/nix/store/70anjdzz5rj9lcamll62lvp5ib3yqzzr-gnumake-4.4.1/bin:/nix/store/i1x9sidnvhhbbha2zhgpxkhpysw6ajmr-bash-5.2p26/bin:/nix/store/6rv8ckk0hg6s6q2zay2aaxgirrdy4l6v-patch-2.7.6/bin:/nix/store/xzdawyw3njki7gx2yx4bkmhdzymgjawm-xz-5.6.2-bin/bin:/nix/store/rnndls2fiid1sic81i06dkqjhh24lpvr-file-5.45/bin

This is because Nix will, by default, use an expression based on stdenv.mkDerivation for the environment. See also the Nix manual (search for dummy).

You can use a custom shell derivation instead of the default by passing --expr option.

Look into the following for minimal shell Nix expressions:

busybox-sandbox-shell is just a slimmed down busybox, which omits which program, among other things, hence your result. The environment will still contain coreutils and all the other stdenv dependencies though.

2 Likes