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
- SDK packaging
- Host tools packaging
* SDK 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
@ -29,11 +55,11 @@ mkShell {
ninja
];
env.ZEPHYR_SDK_INSTALL_DIR = zephyr.sdk.overrideAttrs(old: {
targets = old.targets ++ [
env.ZEPHYR_SDK_INSTALL_DIR = zephyr.sdk.override {
targets = [
"arm-zephyr-eabi"
];
});
};
}
```

View File

@ -1,28 +1,21 @@
{ callPackage
, stdenv
, zephyr-src
, pyproject-nix
, lib
, fetchurl
, which
, autoPatchelfHook
, cmake
, python38
, pkgs
}:
let
sdk = lib.importJSON ./sdk.json;
inherit (sdk) version;
sdk' = lib.importJSON ./sdk.json;
inherit (sdk') version;
python3 = python38;
platform =
getPlatform = stdenv:
if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "macos"
else throw "Unsupported platform";
arch =
getArch = stdenv:
if stdenv.isLinux then stdenv.hostPlatform.linuxArch
else if stdenv.isDarwin then stdenv.hostPlatform.darwinArch
else throw "Unsupported arch";
@ -31,11 +24,15 @@ let
fetchSDKFile = file: fetchurl {
url = "${baseURL}/${file}";
sha256 = sdk.files.${file};
sha256 = sdk'.files.${file};
};
sdkArgs = {
python3 = python38;
};
in
{
rec {
# Zephyr/west Python environment.
pythonEnv = callPackage ./python.nix {
inherit zephyr-src;
@ -43,19 +40,33 @@ in
};
# 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";
inherit version;
srcs = [
(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 ];
buildInputs = [ stdenv.cc.cc python38 ];
buildInputs = [ stdenv.cc.cc python3 ];
dontBuild = true;
dontUseCmakeConfigure = true;
@ -73,10 +84,32 @@ in
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.
hosttools = stdenv.mkDerivation {
hosttools = callPackage
({ stdenv
, which
, autoPatchelfHook
, python3
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
in
stdenv.mkDerivation {
pname = "zephyr-sdk-hosttools";
inherit version;
@ -97,16 +130,25 @@ in
ln -s $out/usr/share/zephyr/hosttools/sysroots/${arch}-pokysdk-${platform}/usr/bin $out/bin
runHook postInstall
'';
};
})
sdkArgs;
# 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";
dontUnpack = true;
dontBuild = true;
propagatedBuildInputs = with pkgs; [
propagatedBuildInputs = [
bossa
dtc
nettle
@ -118,5 +160,6 @@ in
installPhase = ''
mkdir $out
'';
};
})
{ };
}

View File

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