mirror of
https://github.com/ryan4yin/nixos-and-flakes-book
synced 2024-11-23 04:33:08 +00:00
polish: cross-platform compilation
This commit is contained in:
parent
d7b1518882
commit
ff26302e6b
2 changed files with 11 additions and 11 deletions
|
@ -6,7 +6,7 @@ On any Linux platform, there are two ways to do cross-platform compilation. For
|
|||
- The disadvantage is that you cannot use the NixOS binary cache, and you need to compile everything yourself (cross-compilation also has a cache, but there is basically nothing in it).
|
||||
- The advantages are that you don't need to emulate the instruction set, and the performance is high.
|
||||
2. Use QEMU to emulate the `aarch64` architecture and then compile the program in the emulator.
|
||||
- The disadvantage is that the instruction set is emulated, and the performance is low.
|
||||
- The disadvantage is that the instruction set is emulated, and the performance is poor.
|
||||
- The advantage is that you can use the NixOS binary cache, and you don't need to compile everything yourself.
|
||||
|
||||
If you use method one, you don't need to enable `binfmt_misc`, but you need to execute the compilation through the cross-compilation toolchain.
|
||||
|
@ -62,7 +62,7 @@ pkgsCross.mmix
|
|||
|
||||
If you want to set `pkgs` to a cross-compilation toolchain globally in a flake, you only need to add a Module in `flake.nix`, as shown below:
|
||||
|
||||
```nix
|
||||
```nix{15-20}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
@ -99,7 +99,7 @@ The second method is to cross-compile through the emulated system. This method d
|
|||
|
||||
To use this method, first your building machine needs to enable the binfmt_misc module in the configuration. If your building machine is NixOS, add the following configuration to your NixOS Module to enable the simulated build system of `aarch64-linux` and `riscv64-linux` architectures:
|
||||
|
||||
```nix
|
||||
```nix{6}
|
||||
{ ... }:
|
||||
{
|
||||
# ......
|
||||
|
@ -113,7 +113,7 @@ To use this method, first your building machine needs to enable the binfmt_misc
|
|||
|
||||
As for `flake.nix`, its setting method is very simple, even simpler than the setting of cross-compilation, as shown below:
|
||||
|
||||
```nix
|
||||
```nix{11}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
@ -189,7 +189,7 @@ nix-repl> pkgs.pkgsCross.riscv64.stdenv.cc
|
|||
|
||||
So how to use this method in Flakes? The example `flake.nix` is as follows:
|
||||
|
||||
```nix
|
||||
```nix{13-20}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
@ -222,7 +222,7 @@ So how to use this method in Flakes? The example `flake.nix` is as follows:
|
|||
|
||||
To avoid this problem, a better way is to create a new `pkgs` instance, and only use this instance when building the packages we want to modify. The example `flake.nix` is as follows:
|
||||
|
||||
```nix
|
||||
```nix{10-19,34-37}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ pkgsCross.mmix
|
|||
|
||||
如果想将一个 flake 全局的 `pkgs` 设置为交叉编译工具链,只需要在 `flake.nix` 中添加一个 Module,示例如下:
|
||||
|
||||
```nix
|
||||
```nix{14-20}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
@ -100,7 +100,7 @@ pkgsCross.mmix
|
|||
|
||||
要使用这种方法,首先你的构建机需要在配置中启用 binfmt_misc 模块,如果你的构建机是 NixOS,将如下配置添加到你的 NixOS Module 即可启用 `aarch64-linux` 与 `riscv64-linux` 两种架构的模拟构建系统:
|
||||
|
||||
```nix
|
||||
```nix{6}
|
||||
{ ... }:
|
||||
{
|
||||
# ......
|
||||
|
@ -114,7 +114,7 @@ pkgsCross.mmix
|
|||
|
||||
至于 `flake.nix`,它的设置方法非常简单,比前面交叉编译的设置还要简单,示例如下:
|
||||
|
||||
```nix
|
||||
```nix{11}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
@ -189,7 +189,7 @@ nix-repl> pkgs.pkgsCross.riscv64.stdenv.cc
|
|||
|
||||
那么如何在 Flakes 中使用这种方法呢?示例 `flake.nix` 内容如下:
|
||||
|
||||
```nix
|
||||
```nix{13-20}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
@ -222,7 +222,7 @@ nix-repl> pkgs.pkgsCross.riscv64.stdenv.cc
|
|||
|
||||
为了避免这个问题,更好的办法是创建一个新的 `pkgs` 实例,仅在构建我们想修改的包时才使用这个实例,`flake.nix` 示例如下:
|
||||
|
||||
```nix
|
||||
```nix{10-19,34-37}
|
||||
{
|
||||
description = "NixOS running on LicheePi 4A";
|
||||
|
||||
|
|
Loading…
Reference in a new issue