How do I get a shell.nix with cross compiler and qemu?

How do I get a shell.nix with riscv64 cross compiler for kernel dev, and qemu for running it? (Not Linux, it’s an OS course.)

This post seems to apply, and I got this far:

{ pkgs ? import <nixpkgs> {
    crossSystem =
      let lib = import <nixpkgs/lib>;
      in lib.systems.examples.riscv64-embedded;
  }
}:

with pkgs;

mkShell {
  nativeBuildInputs = with buildPackages; [ qemu ];
}

But then:

error: while evaluating the attribute 'nativeBuildInputs' of the derivation 'ucore-riscv64-none-elf' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/make-derivation.nix:198:11:
while evaluating the attribute 'buildInputs' of the derivation 'qemu-4.2.0' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the attribute 'preFixup' of the derivation 'libpulseaudio-13.0' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/servers/pulseaudio/default.nix:33:3:
while evaluating 'optionalString' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/lib/strings.nix:180:5, called from /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/servers/pulseaudio/default.nix:116:14:
while evaluating the attribute 'buildInputs' of the derivation 'dconf-0.36.0' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the attribute 'nativeBuildInputs' of the derivation 'bash-completion-2.10' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the attribute 'postPatch' of the derivation 'perl-5.30.2-riscv64-none-elf' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/make-derivation.nix:198:11:
while evaluating the attribute 'handled' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/check-meta.nix:251:7:
while evaluating 'handleEvalIssue' at /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/check-meta.nix:147:38, called from /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/stdenv/generic/check-meta.nix:252:14:
Package ‘coreutils-8.31’ in /nix/store/qsjg22b6w76g3az52alcrdw96i25iv79-nixos-20.09pre228622.029a5de0839/pkgs/tools/misc/coreutils/default.nix:135 is not supported on ‘riscv64-none’, refusing to evaluate.

a) For `nixos-rebuild` you can set
  { nixpkgs.config.allowUnsupportedSystem = true; }
in configuration.nix to override this.

b) For `nix-env`, `nix-build`, `nix-shell` or any other Nix command you can add
  { allowUnsupportedSystem = true; }
to ~/.config/nixpkgs/config.nix.

Seems like it wants the wrong coreutils? Removing qemu makes things work. I’m now running nix run nixpkgs.qemu on top of nix-shell, but what was I doing wrong?

Got it after trying a bit: qemu is something ‘completely native’ and needs an additional buildPackages step:

  nativeBuildInputs = [
    buildPackages.buildPackages.qemu
    buildPackages.gdb
  ];

Can anyone elaborate on what this solution is actually doing?

I have since learned that the original solution isn’t quite the right way to do it. To make use of splicing you need to use callPackage, otherwise things like nativeBuildInputs and depsBuildBuild don’t really work. For example:

1 Like

These dependency types are documented at Nixpkgs 23.11 manual | Nix & NixOS