diff --git a/README.md b/README.md index 9af78de..fe49084 100644 --- a/README.md +++ b/README.md @@ -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" ]; - }); + }; } ``` diff --git a/default.nix b/default.nix index 2bcac98..7db89c5 100644 --- a/default.nix +++ b/default.nix @@ -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,80 +40,126 @@ in }; # Pre-package Zephyr SDK. - sdk = stdenv.mkDerivation (finalAttrs: { - pname = "zephyr-sdk"; - inherit version; + 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); + srcs = [ + (fetchSDKFile "zephyr-sdk-${version}_${platform}-${arch}_minimal.tar.xz") + ] ++ 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; - dontUseCmakeConfigure = true; + dontBuild = true; + dontUseCmakeConfigure = true; - sourceRoot = "."; + sourceRoot = "."; - installPhase = '' - runHook preInstall + installPhase = '' + runHook preInstall - rm zephyr-sdk-$version/zephyr-sdk-${arch}-hosttools-standalone-*.sh - rm zephyr-sdk-$version/setup.sh; + rm zephyr-sdk-$version/zephyr-sdk-${arch}-hosttools-standalone-*.sh + rm zephyr-sdk-$version/setup.sh; - mv zephyr-sdk-$version $out - mv $(ls | grep -v env-vars) $out/ + mv zephyr-sdk-$version $out + mv $(ls | grep -v env-vars) $out/ - 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. - hosttools = stdenv.mkDerivation { - pname = "zephyr-sdk-hosttools"; - inherit version; + 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"; + src = fetchSDKFile "hosttools_${platform}-${arch}.tar.xz"; - nativeBuildInputs = [ which autoPatchelfHook ]; + nativeBuildInputs = [ which autoPatchelfHook ]; - buildInputs = [ python3 ]; + buildInputs = [ python3 ]; - dontBuild = true; + dontBuild = true; - sourceRoot = "."; + 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 - ''; - }; + 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; # A variant of hosttools, but all tools are taken from nixpkgs. - hosttools-nix = stdenv.mkDerivation { - name = "zephyr-sdk-hosttools-nix"; + hosttools-nix = callPackage + ({ stdenv + , bossa + , dtc + , nettle + , openocd + , qemu_full + , shared-mime-info + }: stdenv.mkDerivation { + name = "zephyr-sdk-hosttools-nix"; - dontUnpack = true; - dontBuild = true; + dontUnpack = true; + dontBuild = true; - propagatedBuildInputs = with pkgs; [ - bossa - dtc - nettle - openocd - qemu_full - shared-mime-info - ]; + propagatedBuildInputs = [ + bossa + dtc + nettle + openocd + qemu_full + shared-mime-info + ]; - installPhase = '' - mkdir $out - ''; - }; + installPhase = '' + mkdir $out + ''; + }) + { }; } diff --git a/flake.nix b/flake.nix index 224d0df..c995bed 100644 --- a/flake.nix +++ b/flake.nix @@ -18,16 +18,17 @@ { packages = forAllSystems - ( - system: - let - pkgs = nixpkgs.legacyPackages.${system}; - in - pkgs.callPackages ./. { - zephyr-src = zephyr; - inherit pyproject-nix; - } - ); + ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + builtins.removeAttrs + (pkgs.callPackage ./. { + zephyr-src = zephyr; + inherit pyproject-nix; + }) [ "override" "overrideDerivation" ] + ); } ); }