Refactor & add sdkFull

This commit is contained in:
adisbladis 2024-02-06 19:25:00 +13:00
parent c2a268a4a0
commit 509e3da4d9
3 changed files with 151 additions and 81 deletions

View File

@ -4,8 +4,34 @@ Develop Zephyr projects using Nix
## Features ## Features
- SDK packaging * SDK packaging
- Host tools packaging * `sdk`
The minimal SDK.
Can be overriden with additional targets.
``` nix
sdk.override {
targets = [
"arm-zephyr-eabi"
];
}
```
* `sdkFull`
SDK with all targets enabled.
* Host tools packaging
* `hosttools`
Binary `hosttools` from the Zephyr SDK.
Because of libc incompatibilities not all binaries in this derivation actually works.
* `hosttools-nix`
A re-packaging of the Zephyr SDK hosttools using nixpkgs packages.
## Basic usage ## Basic usage
@ -29,11 +55,11 @@ mkShell {
ninja ninja
]; ];
env.ZEPHYR_SDK_INSTALL_DIR = zephyr.sdk.overrideAttrs(old: { env.ZEPHYR_SDK_INSTALL_DIR = zephyr.sdk.override {
targets = old.targets ++ [ targets = [
"arm-zephyr-eabi" "arm-zephyr-eabi"
]; ];
}); };
} }
``` ```

View File

@ -1,28 +1,21 @@
{ callPackage { callPackage
, stdenv
, zephyr-src , zephyr-src
, pyproject-nix , pyproject-nix
, lib , lib
, fetchurl , fetchurl
, which
, autoPatchelfHook
, cmake
, python38 , python38
, pkgs
}: }:
let let
sdk = lib.importJSON ./sdk.json; sdk' = lib.importJSON ./sdk.json;
inherit (sdk) version; inherit (sdk') version;
python3 = python38; getPlatform = stdenv:
platform =
if stdenv.isLinux then "linux" if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "macos" else if stdenv.isDarwin then "macos"
else throw "Unsupported platform"; else throw "Unsupported platform";
arch = getArch = stdenv:
if stdenv.isLinux then stdenv.hostPlatform.linuxArch if stdenv.isLinux then stdenv.hostPlatform.linuxArch
else if stdenv.isDarwin then stdenv.hostPlatform.darwinArch else if stdenv.isDarwin then stdenv.hostPlatform.darwinArch
else throw "Unsupported arch"; else throw "Unsupported arch";
@ -31,11 +24,15 @@ let
fetchSDKFile = file: fetchurl { fetchSDKFile = file: fetchurl {
url = "${baseURL}/${file}"; url = "${baseURL}/${file}";
sha256 = sdk.files.${file}; sha256 = sdk'.files.${file};
};
sdkArgs = {
python3 = python38;
}; };
in in
{ rec {
# Zephyr/west Python environment. # Zephyr/west Python environment.
pythonEnv = callPackage ./python.nix { pythonEnv = callPackage ./python.nix {
inherit zephyr-src; inherit zephyr-src;
@ -43,19 +40,33 @@ in
}; };
# Pre-package Zephyr SDK. # Pre-package Zephyr SDK.
sdk = stdenv.mkDerivation (finalAttrs: { sdk = callPackage
({ stdenv
, which
, cmake
, autoPatchelfHook
, python3
, targets ? [ ]
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
in
stdenv.mkDerivation {
pname = "zephyr-sdk"; pname = "zephyr-sdk";
inherit version; inherit version;
srcs = [ srcs = [
(fetchSDKFile "zephyr-sdk-${version}_${platform}-${arch}_minimal.tar.xz") (fetchSDKFile "zephyr-sdk-${version}_${platform}-${arch}_minimal.tar.xz")
] ++ map fetchSDKFile (map (target: "toolchain_${platform}-${arch}_${target}.tar.xz") finalAttrs.targets); ] ++ map fetchSDKFile (map (target: "toolchain_${platform}-${arch}_${target}.tar.xz") targets);
targets = [ ]; # Zephyr targets passthru = {
inherit platform arch targets;
};
nativeBuildInputs = [ which cmake autoPatchelfHook ]; nativeBuildInputs = [ which cmake autoPatchelfHook ];
buildInputs = [ stdenv.cc.cc python38 ]; buildInputs = [ stdenv.cc.cc python3 ];
dontBuild = true; dontBuild = true;
dontUseCmakeConfigure = true; dontUseCmakeConfigure = true;
@ -73,10 +84,32 @@ in
runHook postInstall runHook postInstall
''; '';
}); })
sdkArgs;
# # SDK with all targets selected
sdkFull =
let
inherit (sdk.passthru) platform arch;
mToolchain = builtins.match "toolchain_${platform}-${arch}_(.+)\.tar\.xz";
allTargets = map (x: builtins.head (mToolchain x)) (builtins.filter (f: mToolchain f != null) (lib.attrNames sdk'.files));
in
sdk.override {
targets = allTargets;
};
# Binary host tools provided by the Zephyr project. # Binary host tools provided by the Zephyr project.
hosttools = stdenv.mkDerivation { hosttools = callPackage
({ stdenv
, which
, autoPatchelfHook
, python3
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
in
stdenv.mkDerivation {
pname = "zephyr-sdk-hosttools"; pname = "zephyr-sdk-hosttools";
inherit version; inherit version;
@ -97,16 +130,25 @@ in
ln -s $out/usr/share/zephyr/hosttools/sysroots/${arch}-pokysdk-${platform}/usr/bin $out/bin ln -s $out/usr/share/zephyr/hosttools/sysroots/${arch}-pokysdk-${platform}/usr/bin $out/bin
runHook postInstall runHook postInstall
''; '';
}; })
sdkArgs;
# A variant of hosttools, but all tools are taken from nixpkgs. # A variant of hosttools, but all tools are taken from nixpkgs.
hosttools-nix = stdenv.mkDerivation { hosttools-nix = callPackage
({ stdenv
, bossa
, dtc
, nettle
, openocd
, qemu_full
, shared-mime-info
}: stdenv.mkDerivation {
name = "zephyr-sdk-hosttools-nix"; name = "zephyr-sdk-hosttools-nix";
dontUnpack = true; dontUnpack = true;
dontBuild = true; dontBuild = true;
propagatedBuildInputs = with pkgs; [ propagatedBuildInputs = [
bossa bossa
dtc dtc
nettle nettle
@ -118,5 +160,6 @@ in
installPhase = '' installPhase = ''
mkdir $out mkdir $out
''; '';
}; })
{ };
} }

View File

@ -23,10 +23,11 @@
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
in in
pkgs.callPackages ./. { builtins.removeAttrs
(pkgs.callPackage ./. {
zephyr-src = zephyr; zephyr-src = zephyr;
inherit pyproject-nix; inherit pyproject-nix;
} }) [ "override" "overrideDerivation" ]
); );
} }
); );