Compile Busybox using RISC-V toolchain

I try to compile Busybox but it occurs some issues, here are my steps.

Step 1. Get the RISC-V toolchain

$ vim nix.shell

let
  pkgs = import <nixpkgs> {
    crossSystem = (import <nixpkgs/lib>).systems.examples.riscv64;
  };
  shell = { mkShell, gdb, qemu, dtc }: mkShell {
    nativeBuildInputs = [ gdb qemu dtc ];
  };
in
pkgs.callPackage shell { }
$ nix-shell shell.nix  

Step 2. Get Busybox source code

$ git clone https://git.busybox.net/busybox

Step 3. Try to compile Busybox source code

$ mv busybox busybox_build
$ cd busybox_build
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make defconfig
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make -j $(nproc)

But it occurs the following messages

  CC      libbb/xfuncs_printf.o
  CC      libbb/xgetcwd.o
  CC      libbb/xgethostbyname.o
  CC      libbb/xreadlink.o
  CC      libbb/xrealloc_vector.o
  CC      libbb/xregcomp.o
  AR      libbb/lib.a
  LINK    busybox_unstripped
Trying libraries: crypt m resolv rt
 Library crypt is not needed, excluding it
 Library m is needed, can't exclude it (yet)
 Library resolv is needed, can't exclude it (yet)
 Library rt is not needed, excluding it
 Library m is needed, can't exclude it (yet)
 Library resolv is needed, can't exclude it (yet)
Final link with: m resolv

So I think I need to install the libraries

  1. crypt
  2. m
  3. resolv
  4. rt

And I try to search for them in NixOS Packages but it seems they don’t exist. Does anyone know more information? Thank you.

My algorithm to resolve such issues is to

  1. look at the nixpkgs implementation
  2. look at other distribution packaging (ubuntu, arch, alpine)
  3. search the Internet

Hopefully one of those will present a solution.

1 Like

Why not simply nix-build -A pkgsCross.riscv64.busybox? It’s even in the cache :wink:

Same goes for using nix-shell -A pkgsCross.riscv64.busybox for hacking on a local tree.

I may need to modify the Busybox source code so I can’t use this approach.

Thanks for your hit.

Yes, you can: just override the src attribute of the package.

1 Like

Or use the Nix shell command I gave you; it includes all the dependencies required to build busybox and even the build commands.

1 Like

Do you mean the storePath path?

https://nixos.org/manual/nix/stable/language/builtins.html#builtins-storePath

I don’t try this approach before. Thank you for your hit.

I tried using nix-shell -A as suggested by @Atemu and using the example in nix-shell - Nix Reference Manual, but I couldn’t get the shell to open. (I suspect my system is configured in some way that breaks it.)

I did have success using nix develop in this way

git clone -o upstream https://git.busybox.net/busybox.git
cd busybox/
nix develop nixpkgs#pkgsCross.riscv64.busybox
CROSS_COMPILE=riscv64-unknown-linux-gnu- make defconfig
CROSS_COMPILE=riscv64-unknown-linux-gnu- make
1 Like