Redox+Nix (was: Where does the $CC environment variable come from?)

EDIT: My progress towards Redox+Nix so far:

  • relibc (precompiled, from static.redox-os.org)
  • x86_64-unknown-redox-gcc (compiled from source, using relibc)
  • hello world in C works
  • gmp
  • binutils-unwrapped
  • openssl (not yet tested)
  • curl (compiles but aborts during runtime)
  • sed (functional)

Original Post:

In stdenv.mkDerivation, where does the $CC environment variable come from?

I’m trying to fork nixpkgs to add support for cross-compiling to Redox, which has its own toolchain. (My fork’s commit: b3be327)

When I cross-compile to aarch64, $CC is aarch64-unknown-linux-gnu-gcc. However, when I try to cross-compile to redox, $CC is cc instead of x86_64-unknown-redox-gcc. Any ideas how to fix this?

$ nix-shell -E '(import ./. {crossSystem = {config = "aarch64-unknown-linux-gnu";};}).mkShell'
(nix-shell) $ echo $CC
(nix-shell) aarch64-unknown-linux-gnu-gcc

$ nix-shell -E '(import ./. {crossSystem = {config = "x86_64-unknown-redox"; libc = "relibc"; useRedoxPrebuilt = true;};}).mkShell'
(nix-shell) $ echo $CC
(nix-shell) cc
2 Likes

It’s defined in https://github.com/NixOS/nixpkgs/blob/db7e2b484b12f8bdb2c049c7a64592c42efd82bb/pkgs/build-support/cc-wrapper/setup-hook.sh#L110 You will want to use this cc-wrapper for your C compiler too.

Please loop in us Sign in to GitHub · GitHub on your work! We are always especially excited to try non-unix non-embedded platforms, as they challenge our abstractions to leak like no other.

I would look at how LLVM/Clang toolchains work as it is the gold standard (standard libraries and tools always built separately, multi-target everything). In particular WASI might be a good guide: all we really had to do we swap in the wasi libc (which is sort of a musl cloudabi mashup). If you can just drop in relibc intsead and get something basic like zlib building, that would be an excellent first step.

5 Likes

Conversely, it looks like you are following the android pre-built stuff as a guide right now, which is fine if that’s what’s easiest for you to get something building, but not really a path we’d like to mere things. Indeed, we ought to be building bionic libc from source and avoiding the prebuilt android sdk/ndk too.

You may also find this page helpful that explain other variables used by the c compiler and related buildsystems: C - NixOS Wiki

3 Likes

Thank you! @Ericson2314 @Mic92

I finally figured out how to build relibc (libc for Redox+Linux) from source. However, I couldn’t get it to build without using a specific version of rust, so for now, the derivation isn’t mergable into nixpkgs.

I’ve run into a “Configuration x86_64-unknown-redox not supported” error while building gcc (related github issue for arm). It seems like the solution to that issue was to make changes to upstream gnu-config. I’m going to research how the Redox community is currently building gcc using relibc.

Cheers!

EDIT: I’ve managed to compile semi-functioning gcc using relibc :tada:

3 Likes

I’ve gotten a “hello world” to compile and run on Redox via a gcc built by Nix :tada:

However, my relibc derivation produces a broken output, so I’m currently using a prebuilt relibc from static.redox-os.org.

A lot of this still feels like black magic. I think I need to spend the next few evenings (or weeks) reading about the cross-compiling and the C build system in general. Are there any recommended resources for learning about these topics? If I find any, I’ll edit them into this post for the sake of future readers.

I’m also having trouble understanding the stdenv bootstrapping process in Nixpkgs [1]. @Ericson2314 has written some excellent comments in pkgs/stdenv (and all across nixpkgs in general). Maybe I should spend the next few evenings reading through them?

Thanks!

[1] For example, nix-shell -A pkgsCross.x86_64-unknown-redox.mkShell works, but nix-build -A pkgsCross.x86_64-unknown-redox.gcc fails. This makes zero sense to me now, but I imagine it would make more sense if I understood how stdenv is bootstapped in Nixpkgs Ohh, I think this has to do with cross-compiler packages versus packages meant to be run in Redox

4 Likes

I put most of my knowledge into the mentioned wiki article. If you have any other discoveries beyond that, you are welcome to put them their as well. Maybe I can add some more stuff about how c++ libraries are linked into the compiler wrapper. I know that @ehmry also works on alternative operating systems.

1 Like

Thank you! And yes, I plan to document what I’ve learned once I reach a few more milestones.

I know that @ehmry also works on alternative operating systems.

Oh wow, that repo’s a very helpful reference resource. Maybe that’s how Redox support should be added—as a new target in Nixpkgs but with the numerous package fixes in a separate repo.

P.S. Status update for the curious: I think I’m close to building the Rust cross-compiler via Nix. EDIT: I did it! I just cross-compiled a functioning Hexyl (a rust package) using Nix!

1 Like

See Add Redox OS as a target by aaronjanse · Pull Request #93568 · NixOS/nixpkgs · GitHub :slight_smile:

1 Like

Yes, I think that working with an overlay until you’ve reached a beta level of support is the way to go, but I can only speculate. So far its been a good way to track all the fixes that need to be made, and it allows for low-stress experimentation. Feel free to contact me if you have questions on bootstrapping or cross-compiling.

For an example on how to update gnu-config everywhere, see gnu-config: 2019-04-15 -> 2020-05-04 by ehmry · Pull Request #87234 · NixOS/nixpkgs · GitHub.

2 Likes