zephyr-nix/default.nix

207 lines
4.6 KiB
Nix
Raw Permalink Normal View History

2024-02-06 02:52:10 +00:00
{ callPackage
, zephyr-src
, pyproject-nix
, lib
, fetchurl
, python38
2024-02-09 00:56:59 +00:00
, newScope
, openocd
, autoreconfHook
, fetchFromGitHub
2024-02-06 02:52:10 +00:00
}:
let
2024-02-06 06:25:00 +00:00
sdk' = lib.importJSON ./sdk.json;
inherit (sdk') version;
2024-02-06 02:52:10 +00:00
2024-02-06 06:25:00 +00:00
getPlatform = stdenv:
2024-02-06 02:52:10 +00:00
if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "macos"
else throw "Unsupported platform";
2024-02-06 06:25:00 +00:00
getArch = stdenv:
2024-02-06 02:52:10 +00:00
if stdenv.isLinux then stdenv.hostPlatform.linuxArch
else if stdenv.isDarwin then stdenv.hostPlatform.darwinArch
else throw "Unsupported arch";
baseURL = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}";
fetchSDKFile = file: fetchurl {
url = "${baseURL}/${file}";
2024-02-06 06:25:00 +00:00
sha256 = sdk'.files.${file};
};
sdkArgs = {
python3 = python38;
2024-02-06 02:52:10 +00:00
};
in
2024-02-09 00:56:59 +00:00
lib.makeScope newScope (self: let
inherit (self) callPackage;
in {
2024-02-06 02:52:10 +00:00
# Zephyr/west Python environment.
pythonEnv = callPackage ./python.nix {
inherit zephyr-src;
inherit pyproject-nix;
};
# Pre-package Zephyr SDK.
2024-02-06 06:25:00 +00:00
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") targets);
passthru = {
inherit platform arch targets;
};
2024-02-08 23:47:23 +00:00
nativeBuildInputs =
[ which cmake ]
++ lib.optional (!stdenv.isDarwin) autoPatchelfHook
;
2024-02-06 06:25:00 +00:00
buildInputs = [ stdenv.cc.cc python3 ];
dontBuild = true;
dontUseCmakeConfigure = true;
sourceRoot = ".";
installPhase = ''
runHook preInstall
rm zephyr-sdk-$version/zephyr-sdk-${arch}-hosttools-standalone-*.sh
rm -f env-vars
2024-02-06 06:25:00 +00:00
mv zephyr-sdk-$version $out
if [ -n "$(ls -A .)" ]; then
mv * $out
fi
mkdir -p $out/nix-support
cat <<EOF >> $out/nix-support/setup-hook
export ZEPHYR_SDK_INSTALL_DIR=$out
EOF
2024-02-06 06:25:00 +00:00
runHook postInstall
'';
})
sdkArgs;
# # SDK with all targets selected
sdkFull =
let
2024-02-09 00:56:59 +00:00
inherit (self.sdk.passthru) platform arch;
2024-02-06 06:25:00 +00:00
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
2024-02-09 00:56:59 +00:00
self.sdk.override {
2024-02-06 06:25:00 +00:00
targets = allTargets;
};
2024-02-06 02:52:10 +00:00
# Binary host tools provided by the Zephyr project.
2024-02-06 06:25:00 +00:00
hosttools = callPackage
({ stdenv
, which
, autoPatchelfHook
, python3
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
in
stdenv.mkDerivation {
pname = "zephyr-sdk-hosttools";
inherit version;
src = fetchSDKFile "hosttools_${platform}-${arch}.tar.xz";
2024-02-08 23:47:23 +00:00
nativeBuildInputs =
[ which ]
++ lib.optional (!stdenv.isDarwin) autoPatchelfHook
;
2024-02-06 06:25:00 +00:00
buildInputs = [ python3 ];
dontBuild = true;
dontFixup = true;
2024-02-06 06:25:00 +00:00
sourceRoot = ".";
installPhase = ''
runHook preInstall
mkdir -p $out/usr/share/zephyr/hosttools
./zephyr-sdk-${arch}-hosttools-standalone-*.sh -d $out/usr/share/zephyr/hosttools
ln -s $out/usr/share/zephyr/hosttools/sysroots/${arch}-pokysdk-${platform}/usr/bin $out/bin
runHook postInstall
'';
})
sdkArgs;
2024-02-06 02:52:10 +00:00
2024-02-09 00:56:59 +00:00
openocd-zephyr = openocd.overrideAttrs(old: let
pname = "openocd-zephyr";
version = "20220611";
in {
inherit pname version;
name = "${pname}-${version}";
nativeBuildInputs = old.nativeBuildInputs ++ [
autoreconfHook
];
src = fetchFromGitHub {
owner = "zephyrproject-rtos";
repo = "openocd";
rev = "b6f95a16c1360e347a06faf91befd122c0d15864";
hash = "sha256-NItD5vrFlm3vfma5DexRYpGDsrl7yLjgmskiXPpbYP8=";
};
});
2024-02-06 02:52:10 +00:00
# A variant of hosttools, but all tools are taken from nixpkgs.
2024-02-06 06:25:00 +00:00
hosttools-nix = callPackage
({ stdenv
, bossa
, dtc
, nettle
2024-02-09 00:56:59 +00:00
, openocd-zephyr
2024-02-06 06:25:00 +00:00
, qemu_full
, shared-mime-info
}: stdenv.mkDerivation {
name = "zephyr-sdk-hosttools-nix";
dontUnpack = true;
dontBuild = true;
propagatedBuildInputs = [
bossa
dtc
nettle
2024-02-09 00:56:59 +00:00
openocd-zephyr
2024-02-06 06:25:00 +00:00
qemu_full
shared-mime-info
];
installPhase = ''
mkdir $out
'';
})
{ };
2024-02-09 00:56:59 +00:00
})