如何在nix包中指定主机和构建平台(在Nix中交叉编译)

问题描述 投票:0回答:1

我是 Nix 的新手,正在努力掌握 Nix 的交叉编译概念。 我知道可以访问 Nix 包中的 stdenv.hostPlatform 和 buildPlatform 属性,并相应地选择库和工具。

但是我对两件事感到困惑:

  • 我正在创建一个 nix 包 -> 如何指定该包的主机平台和构建平台?默认情况下,Nix 选择用户构建的系统作为“构建”平台,“主机”平台默认为“构建”,“目标”平台默认为“主机”。然而,作为一个包创建者,我如何告诉 Nix 有关构建和主机平台的信息。
  • 如何提供 Nix 不知道的构建和托管平台?是否可以在我的 Nix 包中定义我自己的自定义平台?

我阅读了nix文档(https://nixos.org/manual/nixpkgs/stable/#chap-cross),但是无法回答上述2个问题。

任何帮助或指示将不胜感激。

我做了一些尝试:

我的原始代码

{cmake, stdenv, gcc, coreutils, bar, git, doxygen, lib}:

stdenv.mkDerivation rec {

  pname = "foo";
  
  version = "1.0";

  src = ./foo;

  buildInputs = [ bar ];

  nativeBuildInputs = [ cmake doxygen ];

  strictDeps = true;

  configurePhase = ''
    cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=$src/tools/toolchains/mytoolchain.cmake .
  '';

  installPhase = ''
    make install
    mkdir -p $out;
    cp -R lib $out;
    cp -R include $out;
    cp -R bin $out;
  '';

  dontFixup = true;
}

我在上面的代码中更改或添加了“系统”属性,Nix 不会重建包。

  system = "mycpu-myos";

我假设 Nix 会检测到它是一个新系统或构建平台;这是未知的并且应该失败;因为用户尝试构建的当前系统不是 nix 表达式中提到的“系统”。

我知道,我在这里缺少对概念的非常基本的理解。但想不通,什么。

cross-compiling nix
1个回答
0
投票

如何指定此包的主机平台和构建平台是什么?

meta.platforms中,您可以限制主机平台。
实际值在

stdenv.hostPlatform

{ lib
, stdenv
, fetchurl
}:

stdenv.mkDerivation rec {
  pname = "hello";
  version = "2.12.1";

  src = fetchurl {
    url = "mirror://gnu/hello/hello-${version}.tar.gz";
    sha256 = "sha256-jZkUKv2SV28wsM18tCqNxoCZmLxdYH2Idh9RLibH2yA=";
  };

  meta = with lib; {
    description = "A program that produces a familiar, friendly greeting";
    homepage = "https://www.gnu.org/software/hello/manual/";
    license = licenses.gpl3Plus;
    platforms = platforms.all;
    #platforms = with platforms; [ "x86_64-linux" "x86_64-darwin" ];
    #platforms = with platforms; linux;
    #platforms = with platforms; linux ++ darwin;
    #platforms = with platforms; unix;
  };
}

要限制构建平台,您可以使用

assert

{ lib
, stdenv
}:

assert (stdenv.buildPlatform.isLinux == false) ->
  throw "this package builds only on linux";

stdenv.mkDerivation {
  # ...
}

如何提供 Nix 不知道的构建和托管平台?

扩展nixpkgs/lib/systems


相关:stdenv.hostPlatform.system的可能值是什么

$ nix repl

nix-repl> :l <nixpkgs>

nix-repl> builtins.length lib.platforms.all
73

nix-repl> lib.platforms.all
[ "i686-cygwin" "x86_64-cygwin" "x86_64-darwin" "i686-darwin" "aarch64-darwin" "armv7a-darwin" "i686-freebsd13" "x86_64-freebsd13" "aarch64-genode" "i686-genode" "x86_64-genode" "x86_64-solaris" "javascript-ghcjs" "aarch64-linux" "armv5tel-linux" "armv6l-linux" "armv7a-linux" "armv7l-linux" "i686-linux" "loongarch64-linux" "m68k-linux" "microblaze-linux" "microblazeel-linux" "mips-linux" "mips64-linux" "mips64el-linux" "mipsel-linux" "powerpc64-linux" "powerpc64le-linux" "riscv32-linux" "riscv64-linux" "s390-linux" "s390x-linux" "x86_64-linux" "mmix-mmixware" "aarch64-netbsd" "armv6l-netbsd" "armv7a-netbsd" "armv7l-netbsd" "i686-netbsd" "m68k-netbsd" "mipsel-netbsd" "powerpc-netbsd" "riscv32-netbsd" "riscv64-netbsd" "x86_64-netbsd" "aarch64_be-none" "aarch64-none" "arm-none" "armv6l-none" "avr-none" "i686-none" "microblaze-none" "microblazeel-none" "msp430-none" "or1k-none" "m68k-none" "powerpc-none" "powerpcle-none" "riscv32-none" "riscv64-none" "rx-none" "s390-none" "s390x-none" "vc4-none" "x86_64-none" "i686-openbsd" "x86_64-openbsd" "x86_64-redox" "wasm64-wasi" "wasm32-wasi" "x86_64-windows" "i686-windows" ]

另请参阅:交叉编译#平台参数

Nixpkgs 遵循 GNU autoconf 的约定。在构建派生时,我们区分 3 种类型的平台:构建平台、主机平台和目标平台。总之,build 是构建包的平台,host 是运行包的平台。 第三个属性 target 仅与某些特定的编译器和构建工具相关。

© www.soinside.com 2019 - 2024. All rights reserved.