NixOS on ARM: Difference between revisions

mNo edit summary
mNo edit summary
Line 498: Line 498:
You can customize image by using the following snippet.
You can customize image by using the following snippet.


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
# save as sd-image.nix somewhere
# save as sd-image.nix somewhere
{ ... }: {
{ ... }: {
Line 509: Line 509:
   ];
   ];
}
}
</syntaxHighlight>
</syntaxhighlight>


Then build with:
Then build with:


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
$ nix-build '<nixpkgs/nixos>' -A config.system.build.sdImage -I nixos-config=./sd-image.nix
$ nix-build '<nixpkgs/nixos>' -A config.system.build.sdImage -I nixos-config=./sd-image.nix
</syntaxHighlight>
</syntaxhighlight>


Note that this requires a machine with aarch64. You can however also build it from your laptop using an aarch64 remote builder as described in [[Distributed build]] or ask for access on the [https://github.com/nix-community/aarch64-build-box community aarch64 builder].
Note that this requires a machine with aarch64. You can however also build it from your laptop using an aarch64 remote builder as described in [[Distributed build]] or ask for access on the [https://github.com/nix-community/aarch64-build-box community aarch64 builder].
Line 521: Line 521:
if you use the experimental flake, instead of doing the above stuff, can put the following lines in <code>flake.nix</code>, <code>git add flake.nix</code> and build with <code>nix build .#images.rpi2</code>:
if you use the experimental flake, instead of doing the above stuff, can put the following lines in <code>flake.nix</code>, <code>git add flake.nix</code> and build with <code>nix build .#images.rpi2</code>:


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{
{
   description = "Build image";
   description = "Build image";
Line 540: Line 540:
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>


=== Cross-compiling ===
=== Cross-compiling ===
Line 546: Line 546:
It is possible to cross-compile from a different architecture. To cross-compile to <code>armv7l</code>, on the same <code>sd-image.nix</code> add in <code>crossSystem</code>:
It is possible to cross-compile from a different architecture. To cross-compile to <code>armv7l</code>, on the same <code>sd-image.nix</code> add in <code>crossSystem</code>:


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{ ... }: {
{ ... }: {
   nixpkgs.crossSystem.system = "armv7l-linux";
   nixpkgs.crossSystem.system = "armv7l-linux";
Line 554: Line 554:
   # ...
   # ...
}
}
</syntaxHighlight>
</syntaxhighlight>


=== Compiling through binfmt QEMU ===
=== Compiling through binfmt QEMU ===
Line 562: Line 562:
To enable the binfmt wrapper on NixOS, add the following to <code>configuration.nix</code>
To enable the binfmt wrapper on NixOS, add the following to <code>configuration.nix</code>


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{
{
   boot.binfmt.emulatedSystems = [ "aarch64-linux" ];
   boot.binfmt.emulatedSystems = [ "aarch64-linux" ];
}
}
</syntaxHighlight>
</syntaxhighlight>


Then, add <code>--argstr system aarch64-linux</code> to the build command:
Then, add <code>--argstr system aarch64-linux</code> to the build command:


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
$ nix-build '<nixpkgs/nixos>' -A config.system.build.sdImage -I nixos-config=./sd-image.nix --argstr system aarch64-linux
$ nix-build '<nixpkgs/nixos>' -A config.system.build.sdImage -I nixos-config=./sd-image.nix --argstr system aarch64-linux
</syntaxHighlight>
</syntaxhighlight>


If you are building on non-NixOS machine with QEMU binfmt wrapper configured, you will want to configure nix daemon to let it know that it can build for aarch64. Add the following line to <code>/etc/nix/nix.conf</code>:
If you are building on non-NixOS machine with QEMU binfmt wrapper configured, you will want to configure nix daemon to let it know that it can build for aarch64. Add the following line to <code>/etc/nix/nix.conf</code>:
Line 580: Line 580:


If you want to build just one specific package, use this:
If you want to build just one specific package, use this:
<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
nix-build '<nixpkgs/nixos>' -A pkgs.theRequiredPackage --argstr system aarch64-linux -I nixos-config=/path/to/target/machine/nixos/config/copy
nix-build '<nixpkgs/nixos>' -A pkgs.theRequiredPackage --argstr system aarch64-linux -I nixos-config=/path/to/target/machine/nixos/config/copy
</syntaxHighlight>
</syntaxhighlight>
(the last option should not be required on NixOS machines)
(the last option should not be required on NixOS machines)