From 92a1367216e9e7b2da9abdd83633352ea8cf14dc Mon Sep 17 00:00:00 2001 From: Sebastian Wendel Date: Mon, 11 Sep 2023 17:33:19 +0200 Subject: [PATCH] first commit --- .editorconfig | 10 ++ .envrc | 4 + .gitea/actions/setup-nix/action.yaml | 14 +++ .gitea/workflows/build.yaml | 30 +++++ .gitignore | 18 +++ .reuse/dep5 | 17 +++ LICENSES/MIT.txt | 9 ++ README.md | 67 ++++++++++++ default.nix | 14 +++ flake.lock | 158 +++++++++++++++++++++++++++ flake.nix | 61 +++++++++++ packages/zephyr-sdk/default.nix | 136 +++++++++++++++++++++++ packages/zephyr-sdk/setup-hook.sh | 12 ++ shells/nix.nix | 18 +++ shells/zephyr.nix | 36 ++++++ 15 files changed, 604 insertions(+) create mode 100644 .editorconfig create mode 100644 .envrc create mode 100644 .gitea/actions/setup-nix/action.yaml create mode 100644 .gitea/workflows/build.yaml create mode 100644 .gitignore create mode 100644 .reuse/dep5 create mode 100644 LICENSES/MIT.txt create mode 100644 README.md create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 packages/zephyr-sdk/default.nix create mode 100644 packages/zephyr-sdk/setup-hook.sh create mode 100644 shells/nix.nix create mode 100644 shells/zephyr.nix diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d235eec --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 + +[*.nix] +indent_size = 2 diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4fd71b3 --- /dev/null +++ b/.envrc @@ -0,0 +1,4 @@ +use flake + +watch_file flake.* +watch_file **/*.nix diff --git a/.gitea/actions/setup-nix/action.yaml b/.gitea/actions/setup-nix/action.yaml new file mode 100644 index 0000000..ae271f8 --- /dev/null +++ b/.gitea/actions/setup-nix/action.yaml @@ -0,0 +1,14 @@ +--- +name: Install Nix +description: Install Nix package manager using the Determinante Systems installer +runs: + using: composite + steps: + - uses: https://github.com/DeterminateSystems/nix-installer-action@v4 + with: + init: none + planner: linux + github-token: ${{ secrets.GH_TOKEN }} + extra-conf: | + accept-flake-config = true + experimental-features = nix-command flakes recursive-nix diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml new file mode 100644 index 0000000..a5bbe69 --- /dev/null +++ b/.gitea/workflows/build.yaml @@ -0,0 +1,30 @@ +--- +name: build +on: push + +jobs: + check: + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Install Nix + uses: ./.gitea/actions/setup-nix + + - name: Nix check + run: nix flake check + + build: + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Install Nix + uses: ./.gitea/actions/setup-nix + + - name: Nix build + run: nix build .# diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ca8b14 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# General +*~ +*.dll +*.log +*.so +*.swp +*.000 +*.bak +*.bck +*.tmp + +# macOS +.DS_Store + +# Nix +.direnv +.pre-commit-config.yaml +result diff --git a/.reuse/dep5 b/.reuse/dep5 new file mode 100644 index 0000000..d431fc2 --- /dev/null +++ b/.reuse/dep5 @@ -0,0 +1,17 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: nix-zephyr-sdk +Upstream-Contact: Sebastian Wendel +Source: https://code.srx.dev/srx/nix-zephyr-sdk + +Files: + .gitignore + .editorconfig + .envrc + .gitea + packages/* + shells/* + *.lock + *.nix + *.md +Copyright: 2023 Sebastian Wendel +License: MIT diff --git a/LICENSES/MIT.txt b/LICENSES/MIT.txt new file mode 100644 index 0000000..2b64cd3 --- /dev/null +++ b/LICENSES/MIT.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 Sebastian Wendel + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf277fe --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# Nix Zephyr SDK + +[Nix Flake](https://zero-to-nix.com/concepts/flakes) Collection for [Zephyr SDK](https://docs.zephyrproject.org/latest/develop/toolchains/zephyr_sdk.html) Toolchain + +## Description + +This repository provides a [Nix Flake](https://zero-to-nix.com/concepts/flakes) collection for the Zephyr SDK toolchain. The [Zephyr SDK](https://docs.zephyrproject.org/latest/develop/toolchains/zephyr_sdk.html) is a collection of tools and libraries for developing applications for the [Zephyr RTOS](https://zephyrproject.org/). This [Nix Flake](https://zero-to-nix.com/concepts/flakes) makes it easy to install and manage the Zephyr SDK toolchain in a reproducible manner across different systems. + +## Features + +- Easy installation of Zephyr SDK toolchain using Nix. +- Reproducible builds to ensure consistent behavior across different systems. +- Flexible configurations to suit various development environments. +- Ability to pin specific versions of Zephyr SDK. +- Support for multiple platforms including Linux and macOS. + +## Prerequisites + +- [Nix package manager](https://nixos.org/download.html) +- [Basic knowledge of Zephyr SDK and RTOS development](https://docs.zephyrproject.org/latest/index.html) + +## Installation + +Firstly, make sure you have [Nix with flakes support enabled](https://nixos.wiki/wiki/Flakes#Enable_flakes). Then you can add this flake as an input to your own flake, or install the tools globally. + +### Activating the Flake + +To use this flake in your own project: + +```nix +{ + inputs = { + nixpkgs.url = "nixpkgs/nixos-unstable"; + zephyr-sdk.url = "git+https://code.srx.dev/srx/nix-zephyr-sdk"; + }; + + outputs = { self, zephyr-sdk, nixpkgs }: { + devShells.default = zephyr-sdk.devShells.zephyr; + }; +} +``` + +### Direct Installation + +To install the Zephyr SDK globally: + +```sh +nix profile install git+https://code.srx.dev/srx/nix-zephyr-sdk +``` + +## Usage + +Once installed, the Zephyr SDK tools should be available in your shell. For example, to build a Zephyr application: + +```sh +west build -b +``` + +Please consult the [Zephyr documentation](https://docs.zephyrproject.org/latest/index.html) for full usage instructions. + +## Contributing + +Contributions are welcome! Please submit Pull Requests or Issues to help improve the project. + +## License + +This project follows the [REUSE Specification](https://reuse.software/spec/) and is licensed under the [MIT License - see the LICENSE file](./LICENSES/MIT.txt) for details. diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..0205bc8 --- /dev/null +++ b/default.nix @@ -0,0 +1,14 @@ +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { + src = ./.; + }) +.defaultNix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e2d8dc5 --- /dev/null +++ b/flake.lock @@ -0,0 +1,158 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1692799911, + "narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660459072, + "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1694183432, + "narHash": "sha256-YyPGNapgZNNj51ylQMw9lAgvxtM2ai1HZVUu3GS8Fng=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "db9208ab987cdeeedf78ad9b4cf3c55f5ebd269b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1685801374, + "narHash": "sha256-otaSUoFEMM+LjBI1XL/xGB5ao6IwnZOXc47qhIgJe8U=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c37ca420157f4abc31e26f436c1145f8951ff373", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat_2", + "flake-utils": [ + "flake-utils" + ], + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1694364351, + "narHash": "sha256-oadhSCqopYXxURwIA6/Anpe5IAG11q2LhvTJNP5zE6o=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "4f883a76282bc28eb952570afc3d8a1bf6f481d7", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..50334bc --- /dev/null +++ b/flake.nix @@ -0,0 +1,61 @@ +{ + description = "Nix Flake Collection for Zephyr SDK Toolchain"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + pre-commit-hooks = { + url = "github:cachix/pre-commit-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.flake-utils.follows = "flake-utils"; + }; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + pre-commit-hooks, + ... + } @ inputs: + flake-utils.lib.eachSystem [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-linux" + "aarch64-darwin" + ] (system: { + pkgs = import nixpkgs { + inherit system; + config = {allowUnfree = true;}; + }; + + packages = { + zephyr-sdk = self.pkgs.${system}.callPackage ./packages/zephyr-sdk inputs; + default = self.packages.${system}.zephyr-sdk; + }; + + devShells = { + zephyr = import ./shells/zephyr.nix inputs system; + nix = import ./shells/nix.nix inputs system; + default = self.devShells.${system}.nix; + }; + + checks = { + pre-commit-check = + pre-commit-hooks.lib.${system}.run + { + src = self.pkgs.${system}.lib.cleanSource ./.; + hooks = { + editorconfig-checker.enable = true; + statix.enable = true; + deadnix.enable = true; + alejandra.enable = true; + }; + }; + }; + }); +} diff --git a/packages/zephyr-sdk/default.nix b/packages/zephyr-sdk/default.nix new file mode 100644 index 0000000..e121935 --- /dev/null +++ b/packages/zephyr-sdk/default.nix @@ -0,0 +1,136 @@ +{ + autoPatchelfHook, + gmp, + gnutls, + hidapi, + libftdi1, + libusb, + libxml2, + pixman, + python38, + readline, + cmake, + dtc, + fetchurl, + glib, + glibcLocales, + lib, + libfaketime, + nettle, + python3, + stdenv, + unzip, + which, + xz, + ... +}: let + hosttools.version = "0.9"; +in + stdenv.mkDerivation rec { + pname = "zephyr-sdk"; + version = "0.16.1"; + + src = let + getHash = arch: + { + "linux-x86_64" = "sha256-UTONUapM6iUWZBzg2dwLUbdjd58A3EVkorwN1xPfIsc="; + "linux-aarch64" = "sha256-BiuytcR8pW3Sm3+S3X8Hpc4iulE3WdK2lgvGWFMesAw="; + "macos-x86_64" = "sha256-sEbHviuQ7qMPH9kHr+DwgCFnAw62NWCxKfJj2aV1Tdo="; + "macos-aarch64" = "sha256-Vf/Gtzdv21x7+20zyEYEnmwPitYmL4ZClPxC1AWbGiY="; + } + .${arch}; + getArch = + { + "x86_64-linux" = "linux-x86_64"; + "aarch64-linux" = "linux-aarch64"; + "x86_64-darwin" = "macos-x86_64"; + "aarch64-darwin" = "macos-aarch64"; + } + .${stdenv.system} + or (throw "${pname}-${version}: ${stdenv.system} is unsupported."); + getUrl = arch: "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/${pname}-${version}_${arch}.tar.xz"; + in + fetchurl { + url = getUrl getArch; + sha256 = getHash getArch; + }; + + nativeBuildInputs = [ + autoPatchelfHook + glib + glibcLocales + libfaketime + python3.pythonForBuild + unzip + which + xz + ]; + + buildInputs = [ + cmake + dtc + gmp + gnutls + hidapi + libftdi1 + libusb + libxml2 + nettle + pixman + python38 + readline + ]; + + postUnpack = '' + $sourceRoot/zephyr-sdk-$(uname -m)-hosttools-standalone-${hosttools.version}.sh \ + -y -p -d $sourceRoot + + rm -f $sourceRoot/{setup,zephyr-sdk-$(uname -m)-hosttools-standalone-*}.sh \ + $sourceRoot/{version,environment-setup}-$(uname -m)-pokysdk-linux + ''; + + patchPhase = '' + patchShebangs . + + substituteInPlace cmake/zephyr/host-tools.cmake \ + --replace "/usr/share" "/share" \ + --replace "/sysroots/\*-pokysdk-linux" "" + ''; + + dontConfigure = true; + dontBuild = true; + + installPhase = '' + runHook preInstall + + mkdir -p $out/{share,lib} + + # remove deprecated symbolic links + find ./sysroots/$(uname -m)-pokysdk-linux/usr/bin -type l -exec unlink {} \; + + # add zephyr-sdk standalone hosttools + mv ./sysroots/$(uname -m)-pokysdk-linux/usr/{bin,libexec,share} $out/ + mv ./sysroots/$(uname -m)-pokysdk-linux/usr/synopsys/bin/qemu-system-* $out/bin/ + mv ./sysroots/$(uname -m)-pokysdk-linux/usr/xilinx/bin/qemu-system-aarch64 \ + $out/bin/qemu-system-xilinx-aarch64 + mv ./sysroots/$(uname -m)-pokysdk-linux/usr/xilinx/bin/qemu-system-microblazeel \ + $out/bin/qemu-system-xilinx-microblazeel + + # add zephyr-sdk cmake modules + mv ./cmake $out/lib/cmake + + # add zephyr-sdk cross compilers + mv ./*zephyr*/bin/* $out/bin/ + + runHook postInstall + ''; + + setupHook = ./setup-hook.sh; + + meta = with lib; { + description = "Zephyr SDK (Toolchains, Development Tools)"; + homepage = "https://github.com/zephyrproject-rtos/sdk-ng/"; + license = licenses.asl20; + platforms = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"]; + }; + } diff --git a/packages/zephyr-sdk/setup-hook.sh b/packages/zephyr-sdk/setup-hook.sh new file mode 100644 index 0000000..6f4d703 --- /dev/null +++ b/packages/zephyr-sdk/setup-hook.sh @@ -0,0 +1,12 @@ +addZephyrSDKCEnvVars() { + cmakeFlagsArray+=(-DCMAKE_MODULE_PATH=@out@/lib/cmake) + cmakeFlagsArray+=(-DZEPHYR_TOOLCHAIN_VARIANT=zephyr) + cmakeFlagsArray+=(-DZEPHYR_SDK_INSTALL_DIR=@out@) + cmakeFlagsArray+=(-DHOST_TOOLS_HOME=@out@) + + export ZEPHYR_TOOLCHAIN_VARIANT=zephyr + export ZEPHYR_SDK_INSTALL_DIR=@out@ + export HOST_TOOLS_HOME=@out@ +} + +addEnvHooks "$hostOffset" addZephyrSDKCEnvVars diff --git a/shells/nix.nix b/shells/nix.nix new file mode 100644 index 0000000..91893a9 --- /dev/null +++ b/shells/nix.nix @@ -0,0 +1,18 @@ +{self, ...}: system: +with self.pkgs.${system}; + mkShell { + name = "nix-dev"; + buildInputs = [ + alejandra + deadnix + git + nil + nixUnstable + pre-commit + reuse + statix + ]; + shellHook = '' + ${self.checks.${system}.pre-commit-check.shellHook} + ''; + } diff --git a/shells/zephyr.nix b/shells/zephyr.nix new file mode 100644 index 0000000..10b0982 --- /dev/null +++ b/shells/zephyr.nix @@ -0,0 +1,36 @@ +{self, ...}: system: +with self.pkgs.${system}; +with self.packages.${system}; + mkShell { + name = "zephyr-dev"; + buildInputs = [ + # C/CPP + bear + bison + ccache + clang + clang-tools + clangStdenv + cmake + cmake-format + cmake-language-server + dfu-util + flex + gdb + gnumake + gperf + gtest + libffi + libusb + ncurses + ninja + + # # Zephyr + python310Packages.west + zephyr-sdk + + # ESP + espup + esptool + ]; + }