Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NixOS on RISC-V #101651

Closed
malte-v opened this issue Oct 25, 2020 · 46 comments
Closed

NixOS on RISC-V #101651

malte-v opened this issue Oct 25, 2020 · 46 comments
Assignees
Labels
6.topic: cross-compilation Building packages on a different sort platform than than they will be run on 6.topic: exotic Exotic hardware or software platform

Comments

@malte-v
Copy link
Contributor

malte-v commented Oct 25, 2020

Issue description

Judging from the screenshot in #36187, @shlevy has already been successful in running some form of NixOS on RISC-V. I'd just like to know what the status of this is, as naively trying to build an ISO installer failed:

➜  nixos git:(master) ✗ nix-build -A config.system.build.isoImage -I nixos-config=modules/installer/cd-dvd/installation-cd-minimal.nix default.nix --show-trace
error: while evaluating the attribute 'buildInputs' of the derivation 'nixos-21.03pre-git-riscv64-linux.iso' at /home/malte/repos/nixpkgs/nixos/lib/make-iso9660-image.nix:49:3:
while evaluating the attribute 'buildInputs' of the derivation 'xorriso-1.5.2' at /home/malte/repos/nixpkgs/pkgs/tools/cd-dvd/xorriso/default.nix:4:3:
while evaluating the attribute 'buildInputs' of the derivation 'libcdio-2.1.0' at /home/malte/repos/nixpkgs/pkgs/development/libraries/libcdio/default.nix:4:3:
while evaluating the attribute 'src' of the derivation 'libcddb-1.3.2' at /home/malte/repos/nixpkgs/pkgs/development/libraries/libcddb/default.nix:4:3:
while evaluating the attribute 'mirrorsFile' of the derivation 'libcddb-1.3.2.tar.bz2' at /home/malte/repos/nixpkgs/pkgs/build-support/fetchurl/default.nix:122:3:
while evaluating the attribute 'stdenv' of the derivation 'mirrors-list' at /home/malte/repos/nixpkgs/pkgs/build-support/fetchurl/default.nix:16:7:
while evaluating the attribute 'initialPath' of the derivation 'stdenv' at /home/malte/repos/nixpkgs/pkgs/stdenv/generic/default.nix:87:14:
while evaluating the attribute 'src' of the derivation 'xz-5.2.5' at /home/malte/repos/nixpkgs/pkgs/tools/compression/xz/default.nix:9:3:
while evaluating the attribute 'nativeBuildInputs' of the derivation 'xz-5.2.5.tar.bz2' at /home/malte/repos/nixpkgs/pkgs/build-support/fetchurl/default.nix:122:3:
while evaluating the attribute 'configureFlags' of the derivation 'curl-7.72.0' at /home/malte/repos/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the attribute 'nativeBuildInputs' of the derivation 'openssl-1.1.1g' at /home/malte/repos/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the attribute 'disallowedReferences' of the derivation 'perl-5.32.0' at /home/malte/repos/nixpkgs/pkgs/development/interpreters/perl/default.nix:22:5:
while evaluating the derivation attribute 'name' at /home/malte/repos/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:11:
cannot coerce null to a string, at /home/malte/repos/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:19

There seems to be some issue with Perl. I guess 306d5cd addressed this, but now it's broken again. I'm still pretty much a biginner to Nix(OS) and unfortunately have no idea how to fix it :( Maybe we could get NixOS on RISC-V running again? I don't know how much work it would be though, since #36187 got merged 2.5 years ago and a lot has happened since then.

@veprbl veprbl added the 6.topic: cross-compilation Building packages on a different sort platform than than they will be run on label Oct 25, 2020
@shlevy
Copy link
Member

shlevy commented Oct 26, 2020

My risc-v board crapped out and I haven't been keeping this up to date, sorry. At the time though I was able to run full cross-compiled NixOS in qemu, not too far from native compliation if I was patient enough.

@zhaofengli
Copy link
Member

zhaofengli commented Dec 27, 2020

I managed to cross-compile Nix itself for riscv64:

with (import <nixpkgs> {
  overlays = [
    (self: super: {
      coreutils = super.coreutils.overrideAttrs (old: {
        # https://github.com/gentoo/gentoo/raw/1070aa3245b38881a050507e365a2c9c60c23534/sys-apps/coreutils/files/coreutils-8.32-ls-restore-8.31-behavior.patch
        patches = old.patches ++ [ ./coreutils-8.32-ls-restore-8.31-behavior.patch ];
      });
    })
  ];
}).pkgsCross.riscv64;
nix.override {
  boost = boost17x;
}

Run under QEMU user mode, I would expect most things to behave as if we were compiling natively on RISC-V:

$ rvnix=/nix/store/42mk9y9x26rr06lymdw2f58lkar4x53v-nix-2.3.10-riscv64-unknown-linux-gnu/bin/
$ $rvnix/nix-instantiate --eval -E "builtins.currentSystem"
"riscv64-linux"

However, when building NixOS, I received the same error as @malte-v. Digging deeper reveals it's actually from stdenv.cc.

Edit: I'm mistaken about what's going on. I'll try some more stuff before posting anything.

We can quickly check by adding trace prints to pkgs/stdenv/generic/make-derivation.nix:

name = "${lib.debug.traceVal attrs.pname}-${lib.debug.traceVal attrs.version}";

Building NixOS then gives me:

trace: systemd
trace: 247.2
trace: gettext
trace: 0.21
trace: curl
trace: 7.74.0
trace: openssl
trace: 1.1.1i
trace: cc-native
trace: null
error: while evaluating the attribute 'config.system.build.toplevel' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/nixos/modules/system/activation/top-level.nix:294:5:
[rest of log]

And indeed, building stdenv.cc yielded the same problem, but this time with a more helpful traceback:

$ $rvnix/nix-build cc.nix --show-trace
trace: linux-headers
trace: 5.9.8
trace: cc-native
trace: null
error: while evaluating the attribute 'cc' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/stdenv/generic/default.nix:163:14:
while evaluating the attribute 'gcc' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:9080:3:
while evaluating 'addMetaAttrs' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/lib/meta.nix:15:28, called from /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:9248:10:
while evaluating 'wrapCC' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:10347:12, called from /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:9248:19:
while evaluating 'wrapCCWith' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:10321:5, called from /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:10347:16:
while evaluating 'callPackageWith' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/lib/customisation.nix:117:35, called from /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/top-level/all-packages.nix:10335:7:
while evaluating 'makeOverridable' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/lib/customisation.nix:67:24, called from /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/lib/customisation.nix:121:8:
while evaluating anonymous function at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/build-support/cc-wrapper/default.nix:8:1, called from /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/lib/customisation.nix:69:16:
while evaluating the attribute 'buildInputs' of the derivation 'glibc-2.32-10-riscv64-unknown-linux-gnu' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:198:11:
while evaluating the attribute 'depsBuildBuild' of the derivation 'linux-headers-5.9.8' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the derivation attribute 'name' at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:11:
cannot coerce null to a string, at /home/zhaofeng/Git/fleet/nix/deps/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:192:19

(My Nixpkgs is pinned at 84917aa)

Well, cc-native is being used as we don't have bootstrap binaries for riscv64, and the version will indeed be null given how cc-wrapper is called from pkgs/stdenv/native/default.nix. The same story applies to cc.bintools.

Any idea on how to fix this cleanly? Changing null to some dummy value in cc-wrapper will not help, as packages still depend on stdenv.cc.cc being valid.

@zhaofengli
Copy link
Member

zhaofengli commented Dec 27, 2020

Ok, I think I misunderstood. Of course building natively is not possible without the bootstrap binaries.

When I attempted to build the bootstrap tools myself from x86_64, I got:

cycle detected in the references of '/nix/store/76vjw1asqssgf82vixbay8qfrp6yzvba-gcc-debug-9.3.0-riscv64-unknown-linux-gnu' from '/nix/store/dwsnp73ml283ckjs3fqncrq544ii6k4c-gcc-debug-9.3.0-riscv64-unknown-linux-gnu-lib'

Looking at the store paths, indeed I see libraries in the lib output depending on out:

$ strings /nix/store/dwsnp73ml283ckjs3fqncrq544ii6k4c-gcc-debug-9.3.0-riscv64-unknown-linux-gnu-lib/lib/* | grep 76vjw1asqssgf82vixbay8qfrp6yzvba
GNU C++17 9.3.0 -march=rv64gc -mabi=lp64d -g -g -O2 -O2 -O2 -std=gnu++17 -fstack-protector-strong -fno-strict-overflow -ffunction-sections -fdata-sections -frandom-seed=cow-string-inst.lo -fimplicit-templates -fPIC -frandom-seed=76vjw1asqssgf82vixbay8qfrp6yzvba-gcc-debug-9.3.0-riscv64-unknown-linux-gnu --param ssp-buffer-size=4
[...]

Those strings are coming from the .debug_str sections in the resulting libraries, so it should be safe to remove them without actually affecting the behavior. The same thing occurs for bootstrap-tools itself, which contains -frandom-seed references to gcc (bootGCC) and glibc:

GNU C17 9.3.0 -march=rv64gc -mabi=lp64d -g -g -O2 -O2 -O2 -fstack-protector-strong -fno-strict-overflow -fPIC -frandom-seed=9h9l9dr78k9blqqq47qp4hix80z3q02r-gcc-debug-9.3.0-riscv64-unknown-linux-gnu --param ssp-buffer-size=4
GNU C11 9.3.0 -march=rv64gc -mabi=lp64d -g -ggdb -O2 -std=gnu11 -fno-strict-overflow -fgnu89-inline -fmerge-all-constants -frounding-math -fno-stack-protector -fmath-errno -fPIC -ftls-model=initial-exec -frandom-seed=zfahziiglvgr2wbqw78l87hnnl9fzbwp-glibc-2.32-10-riscv64-unknown-linux-gnu

Unfortunately nuke-refs and remove-references-to don't work in such cases where the references don't contain the path to Nix store itself (otherwise other parts of the files may be damaged). So I did some quick hack and created a dirty copy of remove-references-to. Should we add the functionality to remove-references-to as a flag?

Afterwards, during the tests I encountered two problems:

  • The linker could not find crt1.o. Looking at the script it seems like -B is added for ${bootstrapTools} but not ${bootstrapTools}/lib. I added the flag and the build continued. Please help me understand why only -B${bootstrapTools} is normally needed where cc-wrapper will add the lib directory as well.
  • gnu-hello's autoconf emitted the cannot guess build type error which I solved by copying updated versions of config.{sub,guess} from pkgs.gnu-config.

Then I successfully cross-compiled bootstrap-tools.

My (very ugly) changes so far, which i have pushed to my fork:

  • coreutils: Apply the SYS_getdents patch unconditionally
    • It's already merged upstream and riscv64 builds are also affected
  • nix: Use boost17x for RISC-V support
  • gcc/bootstrap-tools: Hacks to remove references to the compiler itself
  • rust: Add support for riscv64
    • Platforms without G or C should be covered by riscv64-embedded

Edit: Remove part about prebuilt bootstrap-tools as now I know the reason for the cannot find crt1.o error.

@zhaofengli
Copy link
Member

(Deleted my last post as I thought I had some progress, but I don't)

Ok, I'm moving closer to native compilation, but encountered this error when building xz:

/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld: ../../src/liblzma/.libs/liblzma.so: undefined reference to `pthread_join'
/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld: ../../src/liblzma/.libs/liblzma.so: undefined reference to `pthread_create'
Expand for full logs with NIX_DEBUG=1
HARDENING: disabled flags: pie
HARDENING: Is active (not completely disabled with "all" flag)
HARDENING: enabling fortify
HARDENING: enabling stackprotector
HARDENING: enabling strictoverflow
HARDENING: enabling format
HARDENING: enabling pic
extra flags before to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/gcc:
  -O2
  -D_FORTIFY_SOURCE=2
  -fstack-protector-strong
  --param
  ssp-buffer-size=4
  -fno-strict-overflow
  -Wformat
  -Wformat-security
  -Werror=format-security
  -fPIC
  -Wl\,-dynamic-linker
  -Wl\,/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib/ld-linux-riscv64-lp64d.so.1
original flags to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/gcc:
  -fvisibility=hidden
  -Wall
  -Wextra
  -Wvla
  -Wformat=2
  -Winit-self
  -Wmissing-include-dirs
  -Wstrict-aliasing
  -Wfloat-equal
  -Wundef
  -Wshadow
  -Wpointer-arith
  -Wbad-function-cast
  -Wwrite-strings
  -Wlogical-op
  -Waggregate-return
  -Wstrict-prototypes
  -Wold-style-definition
  -Wmissing-prototypes
  -Wmissing-declarations
  -Wmissing-noreturn
  -Wredundant-decls
  -g
  -O2
  -o
  .libs/xzdec
  xzdec-xzdec.o
  xzdec-tuklib_progname.o
  xzdec-tuklib_exit.o
  ../../src/liblzma/.libs/liblzma.so
  -Wl\,-rpath
  -Wl\,/nix/store/bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5/lib
extra flags after to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/gcc:
  -B/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
  -B/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib/
  -idirafter
  /nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/include
  -idirafter
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/gcc/riscv64-unknown-linux-gnu/9.3.0/include-fixed
  -B/nix/store/avkbr9kaq2mdnk4ddylraslj7wn08i30-bootstrap-stage1-gcc-wrapper-/bin/
  -frandom-seed=bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5
  -Wl\,-rpath
  -Wl\,/nix/store/bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5/lib
  -L/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
HARDENING: disabled flags: pie
HARDENING: Is active (not completely disabled with "all" flag)
HARDENING: enabling bindnow
HARDENING: enabling relro
extra flags before to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld:
  -z
  now
  -z
  relro
original flags to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld:
  --eh-frame-hdr
  -melf64lriscv
  -dynamic-linker
  /lib/ld-linux-riscv64-lp64d.so.1
  -o
  .libs/xzdec
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/crt1.o
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/crti.o
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc/riscv64-unknown-linux-gnu/9.3.0/crtbegin.o
  -L/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
  -L/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -L/nix/store/avkbr9kaq2mdnk4ddylraslj7wn08i30-bootstrap-stage1-gcc-wrapper-/bin
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc/riscv64-unknown-linux-gnu/9.3.0
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc
  -dynamic-linker
  /nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib/ld-linux-riscv64-lp64d.so.1
  xzdec-xzdec.o
  xzdec-tuklib_progname.o
  xzdec-tuklib_exit.o
  ../../src/liblzma/.libs/liblzma.so
  -rpath
  /nix/store/bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5/lib
  -rpath
  /nix/store/bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5/lib
  -lssp_nonshared
  -lssp
  -lgcc
  --push-state
  --as-needed
  -lgcc_s
  --pop-state
  -lc
  -lgcc
  --push-state
  --as-needed
  -lgcc_s
  --pop-state
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc/riscv64-unknown-linux-gnu/9.3.0/crtend.o
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/crtn.o
extra flags after to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld:
  -rpath
  /nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -rpath
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld: ../../src/liblzma/.libs/liblzma.so: undefined reference to `pthread_join'
/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld: ../../src/liblzma/.libs/liblzma.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

This looks very similar to #79793 (comment), but the cause is different.

If you expand the full logs you will see that -lpthread was not passed, and the linking succeeds when it's added. But why wasn't it passed in the first place? There are two related flags, -pthread and -lpthread where -pthread implies -lpthread.

From config.log we see that the test for -pthread fails because -pthread also implies -latomic on riscv64 (but -lpthread doesn't), and we do not have libatomic at this stage. GCC is working around this problem by pulling in libatomic for riscv targets (accepted upstream).

Expand for config.log with NIX_DEBUG=1
configure:7029: checking whether pthreads work with "-pthread" and "-lpthread"
configure:7134: gcc -o conftest -g -O2 -pthread   conftest.c -lpthread  >&5
HARDENING: disabled flags: pie
HARDENING: Is active (not completely disabled with "all" flag)
HARDENING: enabling fortify
HARDENING: enabling stackprotector
HARDENING: enabling strictoverflow
HARDENING: enabling format
HARDENING: enabling pic
extra flags before to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/gcc:
  -O2
  -D_FORTIFY_SOURCE=2
  -fstack-protector-strong
  --param
  ssp-buffer-size=4
  -fno-strict-overflow
  -Wformat
  -Wformat-security
  -Werror=format-security
  -fPIC
  -Wl\,-dynamic-linker
  -Wl\,/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib/ld-linux-riscv64-lp64d.so.1
original flags to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/gcc:
  -o
  conftest
  -g
  -O2
  -pthread
  conftest.c
  -lpthread
extra flags after to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/gcc:
  -B/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
  -B/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib/
  -idirafter
  /nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/include
  -idirafter
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/gcc/riscv64-unknown-linux-gnu/9.3.0/include-fixed
  -B/nix/store/avkbr9kaq2mdnk4ddylraslj7wn08i30-bootstrap-stage1-gcc-wrapper-/bin/
  -frandom-seed=bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5
  -Wl\,-rpath
  -Wl\,/nix/store/bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5/lib
  -L/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
HARDENING: disabled flags: pie
HARDENING: Is active (not completely disabled with "all" flag)
HARDENING: enabling bindnow
HARDENING: enabling relro
extra flags before to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld:
  -z
  now
  -z
  relro
original flags to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld:
  --eh-frame-hdr
  -melf64lriscv
  -dynamic-linker
  /lib/ld-linux-riscv64-lp64d.so.1
  -o
  conftest
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/crt1.o
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/crti.o
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc/riscv64-unknown-linux-gnu/9.3.0/crtbegin.o
  -L/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
  -L/nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -L/nix/store/avkbr9kaq2mdnk4ddylraslj7wn08i30-bootstrap-stage1-gcc-wrapper-/bin
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc/riscv64-unknown-linux-gnu/9.3.0
  -L/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc
  -dynamic-linker
  /nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib/ld-linux-riscv64-lp64d.so.1
  /run/user/1000/ccc9VvjB.o
  -lpthread
  -rpath
  /nix/store/bzlylfvgf76gzfra67k94jicr0wzvhaw-xz-5.2.5/lib
  -lssp_nonshared
  -lssp
  -lgcc
  --push-state
  --as-needed
  -lgcc_s
  --pop-state
  -lpthread
  -lc
  --push-state
  --as-needed
  -latomic
  --pop-state
  -lgcc
  --push-state
  --as-needed
  -lgcc_s
  --pop-state
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/../lib/gcc/riscv64-unknown-linux-gnu/9.3.0/crtend.o
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib/crtn.o
extra flags after to /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld:
  -rpath
  /nix/store/0y7rj1g1smfmp7aq7hnwyxjwd359k92c-bootstrap-stage0-glibc/lib
  -rpath
  /nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/lib
/nix/store/dsh0d6paf2m1q5gkfx915wsgvjfnmfb4-bootstrap-tools/bin/ld: cannot find -latomic
collect2: error: ld returned 1 exit status
configure:7134: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "XZ Utils"
| #define PACKAGE_TARNAME "xz"
| #define PACKAGE_VERSION "5.2.5"
| #define PACKAGE_STRING "XZ Utils 5.2.5"
| #define PACKAGE_BUGREPORT "lasse.collin@tukaani.org"
| #define PACKAGE_URL "https://tukaani.org/xz/"
| #define NDEBUG 1
| #define HAVE_ENCODER_LZMA1 1
| #define HAVE_ENCODER_LZMA2 1
| #define HAVE_ENCODER_DELTA 1
| #define HAVE_ENCODER_X86 1
| #define HAVE_ENCODER_POWERPC 1
| #define HAVE_ENCODER_IA64 1
| #define HAVE_ENCODER_ARM 1
| #define HAVE_ENCODER_ARMTHUMB 1
| #define HAVE_ENCODER_SPARC 1
| #define HAVE_ENCODERS 1
| #define HAVE_DECODER_LZMA1 1
| #define HAVE_DECODER_LZMA2 1
| #define HAVE_DECODER_DELTA 1
| #define HAVE_DECODER_X86 1
| #define HAVE_DECODER_POWERPC 1
| #define HAVE_DECODER_IA64 1
| #define HAVE_DECODER_ARM 1
| #define HAVE_DECODER_ARMTHUMB 1
| #define HAVE_DECODER_SPARC 1
| #define HAVE_DECODERS 1
| #define HAVE_MF_HC3 1
| #define HAVE_MF_HC4 1
| #define HAVE_MF_BT2 1
| #define HAVE_MF_BT3 1
| #define HAVE_MF_BT4 1
| #define HAVE_CHECK_CRC32 1
| #define HAVE_CHECK_CRC64 1
| #define HAVE_CHECK_SHA256 1
| #define ASSUME_RAM 128
| #define PACKAGE "xz"
| #define VERSION "5.2.5"
| #define STDC_HEADERS 1
| #define HAVE_SYS_TYPES_H 1
| #define HAVE_SYS_STAT_H 1
| #define HAVE_STDLIB_H 1
| #define HAVE_STRING_H 1
| #define HAVE_MEMORY_H 1
| #define HAVE_STRINGS_H 1
| #define HAVE_INTTYPES_H 1
| #define HAVE_STDINT_H 1
| #define HAVE_UNISTD_H 1
| #define __EXTENSIONS__ 1
| #define _ALL_SOURCE 1
| #define _GNU_SOURCE 1
| #define _POSIX_PTHREAD_SEMANTICS 1
| #define _TANDEM_SOURCE 1
| /* end confdefs.h.  */
| #include <pthread.h>
| #                       if !defined(_REENTRANT)
| #                        error "_REENTRANT must be defined"
| #                       endif
|                         static void *some_global = NULL;
|                         static void routine(void *a)
|                           {
|                              /* To avoid any unused-parameter or
|                                 unused-but-set-parameter warning.  */
|                              some_global = a;
|                           }
|                         static void *start_routine(void *a) { return a; }
| int
| main ()
| {
| pthread_t th; pthread_attr_t attr;
|                         pthread_create(&th, 0, start_routine, 0);
|                         pthread_join(th, 0);
|                         pthread_attr_init(&attr);
|                         pthread_cleanup_push(routine, 0);
|                         pthread_cleanup_pop(0) /* ; */
|   ;
|   return 0;
| }
configure:7143: result: no

The dependency looks like:

builder for '/nix/store/chbhgv849d47irqjw3fvvpp5vyjk4s39-xz-5.2.5.drv' failed with exit code 2
cannot build derivation '/nix/store/y60ywc5p9wihf4azl4p8g9nxfp89gnjj-gettext-0.21.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/pnlayr415x453yn64wd88v2rg7pi68ax-binutils-2.31.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/hzih9nh61259h9z41cq57sq86ssywjgb-binutils-wrapper-2.31.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/pmbfdbad7993561yq950g1a1zm6v0qka-binutils-wrapper-2.31.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/17q050qysmr7rrg5l94dk8axwd1mqzg7-bootstrap-stage2-gcc-wrapper-.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/x7rilw004qwqxby0l281xq9lmwm0sgwi-bootstrap-stage3-gcc-wrapper-.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/r646vagbwhd6lxsr415hs416j7dsph93-bootstrap-stage4-gcc-wrapper-9.3.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/4m7vbw67xb8qhipi52k3qyf04fmq1c63-gcc-9.3.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/wf79ml4d4shvkkp3w1gh1hypzgdkf6gc-bash-4.4-p23.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/jwmz2ncxp6sc8zbwyyk7vfk1sh7a1pl8-binutils-2.31.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/5lb38amp16j26flbwqlb5nka8imr928y-bootstrap-stage2-stdenv-linux.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/45aycgd90a26738biw2ls972k6d6wm21-bootstrap-stage3-stdenv-linux.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/vskl5vwhrfp9damgc6af2qlycy9pnpb4-bootstrap-stage4-stdenv-linux.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/yd69kqm426n9mmab1wwfxd6nkkxfmpqy-gcc-wrapper-9.3.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/4d7z1j4f6dxzaskha65zf1wi1wgn5mvc-gdb-10.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/jnfapcfrf82gyh8p7zxc9a514m6f0m8g-glibc-2.32-10.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/rx07m3a9jwqv6xa5w5cxwxdzvd4ksrbz-gmp-6.2.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/2n67ss14jm8zdhckmwa4bf5pwhhj0wnx-libidn2-2.3.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/pv5hhxf8ya2j1qpdw623rbrffm0xysj0-nix-2.3.10.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/k3xgq6x7n163ly6kxj14kx4n97cl92p0-perl-5.32.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/nalslbgz6khybdgdilihh2s96dff9bpm-perl-5.32.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/pk6f03wcdq9jhpid5m1lm64a5b5d8s09-perl-5.32.0.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/k8wz4hzs7sdnqpc9yyq1dy1ah7dj485r-stdenv-linux.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/v4x8ixgjl9h81z992283f8b11f2nc9qw-stdenv-linux.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/c3vy42nkl1gq11xg9r9ddk6yafvl2hpv-texinfo-6.7.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/y5x4jgww005y0lk7qraws2snxrx3hi1g-nixos-system-nixos-21.03pre-git.drv': 1 dependencies couldn't be built

I don't know what the correct way to solve this is, as a toolchain with libatomic is only built in the later stages. But we don't actually need gettext for our binutils which will be replaced later anyways. Is it possible to drop the gettext dependency from binutils or is there another way around this problem?

Would like to hear your opinions.

@flokli
Copy link
Contributor

flokli commented Dec 28, 2020

cc @Ericson2314 @luc65r

@r-burns
Copy link
Contributor

r-burns commented Jan 3, 2021

@zhaofengli Fyi the gcc reference cycles should be fixed by #106954

@rastertail
Copy link
Member

Any progress on getting this to work? I'm currently in the same boat as the original author of this issue with the same cannot coerce null to a string error, and I am confused by the progress that has been made since.

@r-burns
Copy link
Contributor

r-burns commented Feb 18, 2021

Are there more RISC-V specific issues that need to be resolved? The cross toolchain is working now:
https://hydra.nixos.org/eval/1648761?filter=riscv&compare=1648406&full=#tabs-still-succeed

Beyond that, NixOS cross-compilation in general is pretty unstable, and will probably continue to be as long as there are no channel blockers for it.

@Atemu
Copy link
Member

Atemu commented Feb 18, 2021

I think the problem is rather that there aren't a lot of maintainers who need/want cross-compilation.

Making the channels block on cross-compilation issues would only cause headaches I think.

Some sort of health indicator other than "look at hydra builds" might be helpful but that'd probably also just be red all the time.

@r-burns
Copy link
Contributor

r-burns commented Feb 18, 2021

Possibly relevant #105294 (comment)

@rastertail
Copy link
Member

I've made a bit of progress on my end, but I am still running into tons of issues. My goal is to build a minimal U-Boot compatible root filesystem image for RISC-V. Note that full native compilation is not one of my goals. Here's what I have run into so far:

  • Tries to build qemu for RISC-V (that is, RISC-V is the host CPU) which fails since numactl is unsupported on RISC-V. I tried enabling allowUnsupportedSystem but that does not seem like the right route to go.
  • Tries to build kexec-tools for RISC-V which absolutely does not work (cdf9a78 seems to try to fix this?)
  • Pipewire is included in the system path for some unknown reason, and I definitely do not want it.

Here is the configuration.nix I am using, and I am building it using the command nix-build ./nixpkgs/nixos/default.nix -A config.system.build.qcow2 -I nixos-config=$PWD/configuration.nix.

@r-burns
Copy link
Contributor

r-burns commented Feb 19, 2021

Hmm, I've run into issues with numactl too. I'll open a PR later to enable it on powerpc64 and riscv since it seems to be supported on both of those platforms.

I don't know what to do about kexec. That looks like a tough one. I'll look into pipewire though

@r-burns
Copy link
Contributor

r-burns commented Feb 20, 2021

Fyi #96336 (comment)

@JohnAZoidberg
Copy link
Member

Fyi #96336 (comment)

Yeah I don't understand how @zhaofengli was able to cross compile nix with boost.
I still get the same error just with nix-build -A pkgsCross.riscv64.boost and his instructions.

@zhaofengli
Copy link
Member

Fyi #96336 (comment)

Yeah I don't understand how @zhaofengli was able to cross compile nix with boost.
I still get the same error just with nix-build -A pkgsCross.riscv64.boost and his instructions.

For Nix I bumped the boost dependency to boost17x (see my comment). I haven't touched this thing for a while, but you can try my old tree. Hopefully we can get somewhere further before I receive the HiFive Unmatched board later next month.

@DieracDelta
Copy link
Member

DieracDelta commented Apr 21, 2021

@Rytone it's not clear to me from your configuration: how are you controlling the uboot configuration settings when cross compiling for a riscv target? Suppose I wanted to build just uboot targeting riscv with a custom .config. Do you have a sense for how I could do that?

Relatedly, any idea how to build just opensbi for riscv with nix? (I haven't looked deeply into this; I'm about to do a deep dive).

@rastertail
Copy link
Member

rastertail commented Apr 23, 2021

@DieracDelta To be completely honest, I'm not totally sure what I'm doing makes any sense, so take this with a grain of salt: What I [think I] am doing right now is building a NixOS image that will be detected by U-Boot where I have built an OpenSBI+U-Boot firmware image following these and these instructions, not at all integrated into the NixOS build. Ultimately, I want to be able to run something like this (I think):

qemu-system-riscv64 -M virt -m 256M \
	-bios path/to/opensbi/fw_payload.elf \
	-drive file=path/to/nixos.img,format=raw,id=hd0 \
	-device virtio-blk-device,drive=hd0

Also, related to picking this up again, I am now running into the issues in #116207 when trying to do a fully bootstrapped build, and when just cross-compiling, I run into other random errors depending on what NixOS revision I use.

@DieracDelta
Copy link
Member

DieracDelta commented Apr 24, 2021

That's awesome. @Rytone could you elaborate more on what you mean by fully bootstrapped?

I'm able to just spin up a nix shell with crossPkgs.riscv64.gcc successfully. For some reason crossPkgs.riscv64.clang won't build; the given error is that llvm7 doesn't exist for riscv. I'm not exactly sure how to override the llvm version.

From that nix-shell, I can build opensbi and u-boot targeting riscv. I'd like to make derivations for opensbi and u-boot that allows for extra targets to be added as well as .config to be tweaked in the case of uboot. I'm not really sure where to start. I'm not familiar with the pre-existing infrastructure for uboot on arm; it seems a bit magical right now.

@rastertail
Copy link
Member

Yeah, not sure if bootstrapped is the right term, but its what I went with 😅. By "fully bootstrapped" I meant what I have in this configuration where the majority of the RISC-V packages are built under RISC-V virtualization rather than being cross-compiled. The alternative is to import a fresh copy of nixpkgs in the call to make-disk-image instead of inheriting pkgs from the configuration; that way all of the packages are cross-compiled.

With regards to building OpenSBI and U-Boot, I built them like you did just using a shell, though I didn't try using clang at all. I agree that in the long run being able to build them as a Nix derivation would be nicer.

@rastertail
Copy link
Member

Quick update: Just realized that while my cross-compilation technique is able to build all of the derivations necessary for a RISC-V NixOS installation, it is unable to completely build the image itself as it tries to run some final setup of the RISC-V image under an x86 VM. I looked and it doesn't seem the VM infrastructure is suited for this sort of compilation... I tried hacking it to spin up a RISC-V VM instead but it got stuck booting. Going forward I would say abandon that idea of cross-compiling everything and instead focus on getting the proper "Nix way" to work.

@DieracDelta
Copy link
Member

I got confused with my paths. It turns out I am having trouble getting a nix shell with a gcc cross compiler toolchain with x8664 as my host and riscv64 as my target. It seems to be more involved than just defining a cross system like this:

      riscvPkgs = import nixpkgs {                                 
        localSystem = "${system}";                                           
        crossSystem = {
          config = "riscv64-unknown-linux-gnu";                 
            abi = "lp64d";                                            
        };                                   
      }; 

And then passing in riscvPkgs.pkgs as a nativeBuildInput to nixpkgs.pkgs.mkShell. This gets me a riscv gcc toolchain that only runs on riscv.

@DieracDelta
Copy link
Member

DieracDelta commented Apr 26, 2021

https://github.com/oxalica/rust-overlay/blob/0bb9ef6d8b34e5579d7384201f3106a49ce3deca/examples/cross-aarch64/shell.nix fixed my issue. TLDR is to put riscvPkgs.buildPackages.gcc into the nativeBuildInputs when running mkshell.

For reference, here's a more complete picture of what I'm doing:

let 
riscvPkgs = import nixpkgs {                                 
        localSystem = "${system}";                                           
        crossSystem = {
          config = "riscv64-unknown-linux-gnu";                 
            abi = "lp64d";                                            
        };                                   
      };
in pkgs.mkShell {
  nativeBuildInputs = [ riscvPkgs.buildPackages.gcc riscvPkgs.buildPackages.clang ];
};

This gets me both the clang and gcc compilers, both prefixed with riscv64-unknown-linux-gnu-.

Relatedly, any ideas where the gnu-toolchain source is being passed in? I've got a fork that's somewhat diverged from the master branch that I'd like to build. I was hoping to override the src attribute of its derivation, but I'm not finding any derivation when grepping Nixpkgs...

@zhaofengli
Copy link
Member

Firefox is now running on native RISC-V NixOS. For this I cheated a little bit by using the X86 version of Node.js during build since a native build of node hangs on node_mksnapshot (more investigation needed). It's only used during the build process and does not affect the results.

20220115_20h30m23s_grim

If you want to try out Firefox and SuperTuxKart on RISC-V NixOS yourself, I put together a new graphical image. With VirtIO-GPU + VirGL (use the included launch.sh), SuperTuxKart is pretty playable with some texture glitches. Now there are also more packages in the binary cache.

@ius ius mentioned this issue Feb 6, 2022
14 tasks
@Madouura
Copy link
Contributor

I was planning to get one of the StarFive VisionFive RISC-V boards, I'm surprised there's already been an attempt at this.
I'll try my hand and testing some software from the repo and maybe making a list of what works right now on NixOS and what doesn't.

@Madouura
Copy link
Contributor

Got it ordered, should be here in about a week.
Any suggestions/requests on what to test first?

@Madouura Madouura self-assigned this Apr 14, 2022
@fgaz
Copy link
Member

fgaz commented Apr 15, 2022

Could you try qbe.tests? iirc it recently got riscv support

@Madouura
Copy link
Contributor

Sure thing. It cross-compiles well, so I'll test it as soon as I get a working test environment on it when it arrives.

@Madouura
Copy link
Contributor

Here's some preliminary footwork: #168826

@Madouura
Copy link
Contributor

Could you try qbe.tests? iirc it recently got riscv support

nix-build "<nixos>" -A qbe.tests
It appears to work on hardware!

@Madouura
Copy link
Contributor

We're now in nixos-hardware at https://github.com/NixOS/nixos-hardware/tree/master/starfive/visionfive/v1.

@YoshiRulz
Copy link
Contributor

Just got my VisionFive 2, where do I start? Is there a Matrix room specifically for RISC-V?

@Madouura
Copy link
Contributor

I got mine today as well.
I'll be trying to add it to nixos-hardware when I get a chance.

@0x4A6F
Copy link
Member

0x4A6F commented Dec 17, 2022

Matrix Room: Exotic Nix Targets is used for RISC-V.

@NickCao
Copy link
Member

NickCao commented Jan 19, 2023

Homemade (without using the starfive proprietary tools) firmware for visionfive2: https://hydra.nichi.co/job/nixos/riscv/firmware-vf2/latest/
(u-boot hasn't been adapted to nixos standards yet)

@skeuchel
Copy link
Contributor

Can anyone interested look at #221306 to make gn compile on RISC-V?

@fgaz
Copy link
Member

fgaz commented Jul 7, 2023

Star64 nixos-hardware pr: NixOS/nixos-hardware#662

And here's an image I built directly on the board: https://app.cachix.org/api/v1/cache/fgaz/serve/hj72i7znp0rvc4dxs2xslw6b3ik3wifb/sd-image/nixos-sd-image-23.11pre-git-riscv64-linux-pine64-star64.img.zst (8GB RAM, user/pw: nixos)

@fgaz
Copy link
Member

fgaz commented Jul 7, 2023

To build the image on buildPlatform=riscv64, these two patches are needed: #240348 #242019

@nyabinary
Copy link
Contributor

@fgaz Both merged now

@fgaz
Copy link
Member

fgaz commented Sep 28, 2023

@nyabinary yes I have working images here: https://sr.ht/~fgaz/nixos-star64

@fgaz
Copy link
Member

fgaz commented Sep 28, 2023

And we have a new matrix room at #riscv:nixos.org

@samueldr
Copy link
Member

Is there any reason to keep this issue open still?

I fear it's going to become an unactionable grab bag of comments and unrelated issues.

AFAIK, NixOS kinda just works, as much as any other similarly-tiered platforms at the moment, no?

@RaitoBezarius
Copy link
Member

Correct. There's no further reason to keep an issue much more than "wow, look how NixOS came to be on RISC-V". We can move on to more actionable fine grained issues when it pertains to RISC-V support in NixOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: cross-compilation Building packages on a different sort platform than than they will be run on 6.topic: exotic Exotic hardware or software platform
Projects
None yet
Development

No branches or pull requests