From 2ed4f576883d7038fa21941f38d17fbb42262032 Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sat, 6 Jan 2024 22:17:09 +0000 Subject: [PATCH] home-assistant-chip: init --- ci-root.nix | 2 +- nix/pkgs/default.nix | 3 + nix/pkgs/home-assistant-chip/default.nix | 348 +++++++++++++++++++++ nix/pkgs/zcl-advanced-platform/default.nix | 32 ++ 4 files changed, 384 insertions(+), 1 deletion(-) create mode 100644 nix/pkgs/home-assistant-chip/default.nix create mode 100644 nix/pkgs/zcl-advanced-platform/default.nix diff --git a/ci-root.nix b/ci-root.nix index 481d68a339..864cec8cca 100644 --- a/ci-root.nix +++ b/ci-root.nix @@ -12,7 +12,7 @@ let ciBits = rec { x86_64-linux = { machine = depot.ops.nixos.systems; - pkgs = builtins.removeAttrs depot.nix.pkgs [ "grafana-plugins" "windows" "unifiHacked" "nixpkgs-mozilla" "nightlyRust" "nightlyRustPlatform" "baserow" "baserow-oss" "authentik" "factorio-mods" ]; + pkgs = builtins.removeAttrs depot.nix.pkgs [ "grafana-plugins" "windows" "unifiHacked" "nixpkgs-mozilla" "nightlyRust" "nightlyRustPlatform" "baserow" "baserow-oss" "authentik" "factorio-mods" "home-assistant-chip" ]; pkg-baserow = { inherit (depot.nix.pkgs.baserow) backend web-frontend; }; diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix index 53d8fd749a..2187fda6f1 100644 --- a/nix/pkgs/default.nix +++ b/nix/pkgs/default.nix @@ -83,4 +83,7 @@ }; freeswitch-sounds = pkgs.callPackage ./freeswitch-sounds.nix { }; + + zcl-advanced-platform = pkgs.callPackage ./zcl-advanced-platform { }; + home-assistant-chip = pkgs.python3.pkgs.callPackage ./home-assistant-chip { inherit zcl-advanced-platform; gn = pkgs.gn1924; }; } // (import ./heptapod-runner args) diff --git a/nix/pkgs/home-assistant-chip/default.nix b/nix/pkgs/home-assistant-chip/default.nix new file mode 100644 index 0000000000..9c385d5cdf --- /dev/null +++ b/nix/pkgs/home-assistant-chip/default.nix @@ -0,0 +1,348 @@ +{ lib +, stdenv +, buildPythonPackage +, fetchFromGitHub +, pythonOlder +, runCommand +, python +, git +, zcl-advanced-platform +, click +, setuptools +, lark +, jinja2 +, stringcase +, clang-tools +, gn +, pkg-config +, libnl +, openssl +, glib +, ninja +, pyproject-hooks +, autoPatchelfHook + +, aenum +, coloredlogs +, construct +, cryptography +, dacite +, ecdsa +, rich +, pyyaml +, ipdb +, deprecation +, mobly +, pygobject3 + +, ipython +, dbus-python +}: + +let + matterVersion = "1.2.0.1"; # check matter_sdk_ref in https://github.com/home-assistant-libs/chip-wheels/blob/main/.github/workflows/build.yaml + matterSDKSrc = (fetchFromGitHub { + owner = "project-chip"; + repo = "connectedhomeip"; + rev = "v${matterVersion}"; + hash = "sha256-CSOeTd0A95nePjrceCyVntRn36dz2q8Z9b8l2ehxBOQ="; + forceFetchGit = true; + }).overrideAttrs (old: { + # Checkout submodules before .git directories are removed. + nativeBuildInputs = old.nativeBuildInputs ++ [ + python + ]; + env.NIX_PREFETCH_GIT_CHECKOUT_HOOK = '' + python $dir/scripts/checkout_submodules.py --shallow --platform linux + ''; + }); + + zapPregenerated = stdenv.mkDerivation rec { + pname = "matter-sdk-zap"; + version = matterVersion; + + src = matterSDKSrc; + + nativeBuildInputs = [ + python + + click + lark + jinja2 + stringcase + + zcl-advanced-platform # zap-cli + clang-tools # clang-format + ]; + + postPatch = '' + patchShebangs --build scripts + ''; + + buildPhase = '' + runHook preBuild + # Generate ZAP code + python scripts/codepregen.py $out + runHook postBuild + ''; + }; + + baseline = rec { + pname = "home-assistant-chip-core"; + version = "2023.12.0"; + pyproject = false; + + disabled = pythonOlder "3.7"; + + src = fetchFromGitHub { + owner = "home-assistant-libs"; + repo = "chip-wheels"; + rev = version; + hash = "sha256-/FUyP+DASaXSRFWuSnhSv+kWFR2bQATJr7QdeMV6kdg="; + }; + + inherit matterSDKSrc zapPregenerated; + + prePatch = '' + cp -R $matterSDKSrc project-chip + chmod -R +w project-chip + ''; + + postPatch = '' + pushd project-chip + rm ../*-Use-data-as-platform-storage-location.patch + for patch in ../*.patch; do + patch -p1 < "$patch" + done + patchShebangs --build scripts + sed -i 's,[>=]=[0-9][^;]*,,g' \ + third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/virtualenv_setup/python_base_requirements.txt + popd + ''; + + buildInputs = [ + libnl + openssl + glib + ]; + + nativeBuildInputs = [ + gn # gn + ninja # ninja + pkg-config # pkg-config + ]; + + gnArgs = []; + + configurePhase = '' + runHook preConfigure + + pushd project-chip + + # Empty pigweed_environment + echo "# Empty pigweed environment" > build_overrides/pigweed_environment.gni + + # Generate gn build scripts + gn gen --check --fail-on-unused-args out --args=" \ + enable_rtti=true \ + enable_pylib=true \ + chip_config_memory_debug_checks=false \ + chip_config_memory_debug_dmalloc=false \ + chip_mdns=\"minimal\" \ + chip_minmdns_default_policy=\"libnl\" \ + chip_python_version=\"${version}\" \ + chip_python_package_prefix=\"home-assistant-chip\" \ + chip_python_platform_tag=\"manylinux_2_31\" \ + chip_code_pre_generated_directory=\"$zapPregenerated\" \ + $gnArgs + " + + popd + + runHook postConfigure + ''; + }; + + src-wheels = stdenv.mkDerivation (baseline // { + pname = "${baseline.pname}-src-wheels"; + + buildPhase = '' + runHook preBuild + + pushd project-chip + + export HOME=$NIX_BUILD_TOP/home + mkdir $HOME + ninja -C out $(gn ls out --as=output '//:matter_build_venv.vendor_wheels') + + popd + + runHook postBuild + ''; + + nativeBuildInputs = baseline.nativeBuildInputs ++ [ + setuptools + ]; + + installPhase = '' + runHook preInstall + + cp -R ./project-chip/out/python/gen/matter_build_venv.vendor_wheels/wheels $out + + runHook postInstall + ''; + + outputHashMode = "recursive"; + outputHashAlgo = "sha256"; + outputHash = "sha256:1gndaagj9l5a4xdh5i24dg64qd97qixym6rqc30l32fjdf9rbnhf"; + }); + + out-wheels = stdenv.mkDerivation (baseline // { + pname = "${baseline.pname}-wheels"; + + env.NIX_CFLAGS_COMPILE = "-Wno-error=cpp -Wno-cpp"; + + gnArgs = [ + "pw_build_PYTHON_PIP_INSTALL_OFFLINE=true" + "pw_build_PYTHON_PIP_INSTALL_DISABLE_CACHE=true" + ''pw_build_PYTHON_PIP_INSTALL_FIND_LINKS=["//wheels"]'' + ]; + + nativeBuildInputs = baseline.nativeBuildInputs ++ [ + setuptools + glib # gdbus-codegen + + pyproject-hooks + ]; + + buildPhase = '' + runHook preBuild + + pushd project-chip + + ln -s ${src-wheels} wheels + + export HOME=$NIX_BUILD_TOP/home + mkdir $HOME + ninja -C out chip-repl + + popd + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + mkdir $out + + mkdir $out/wheels + cp project-chip/out/controller/python/*.whl $out/wheels + + mkdir $out/wheels-short + for f in $out/wheels/*.whl; do + basef="''${f##*/}" + ln -s "$f" $out/wheels-short/''${basef%%-*}.whl + done + + runHook postInstall + ''; + }); + + outputWheel = name: overrides: buildPythonPackage ({ + pname = name; + inherit (baseline) version; + src = out-wheels; + format = "wheel"; + + preUnpack = '' + src="$(readlink -f "$src/wheels-short/${lib.replaceStrings ["-"] ["_"] name}.whl")" + ''; + } // overrides); +in +rec { + wheels = out-wheels; + core = outputWheel "home-assistant-chip-core" { + propagatedBuildInputs = [ + aenum + coloredlogs + construct + cryptography + dacite + ecdsa + rich + pyyaml + ipdb + deprecation + mobly + pygobject3 + + clusters + ]; + + buildInputs = [ + libnl + ]; + + nativeBuildInputs = [ + autoPatchelfHook + ]; + + pythonNamespaces = [ + "chip" + "chip.clusters" + ]; + + pythonImportsCheck = [ + "chip" + "chip.ble" + "chip.configuration" + "chip.clusters" + "chip.discovery" + "chip.exceptions" + "chip.native" + "chip.storage" + ]; + + doCheck = false; # no tests + }; + clusters = outputWheel "home-assistant-chip-clusters" { + propagatedBuildInputs = [ + aenum + dacite + ]; + + pythonNamespaces = [ + "chip" + ]; + + pythonImportsCheck = [ + "chip.clusters.ClusterObjects" + "chip.clusters.enum" + "chip.clusters.Objects" + "chip.clusters.TestObjects" + "chip.clusters.Types" + "chip.tlv" + ]; + }; + repl = outputWheel "home-assistant-chip-repl" { + propagatedBuildInputs = [ + core + clusters + + coloredlogs + ipython + rich + dbus-python + pygobject3 + ]; + + pythonNamespaces = [ + "chip" + ]; + + pythonImportsCheck = [ + "chip.ChipBluezMgr" + ]; + }; +} diff --git a/nix/pkgs/zcl-advanced-platform/default.nix b/nix/pkgs/zcl-advanced-platform/default.nix new file mode 100644 index 0000000000..44c990c390 --- /dev/null +++ b/nix/pkgs/zcl-advanced-platform/default.nix @@ -0,0 +1,32 @@ +{ lib +, buildNpmPackage +, fetchFromGitHub +, python3 +}: + +buildNpmPackage rec { + pname = "zcl-advanced-platform"; + version = "2023.12.07"; + + src = fetchFromGitHub { + owner = "project-chip"; + repo = "zap"; + rev = "v${version}"; + hash = "sha256-vQ4tlpfh2CUl58l3fhduEK9Gdajf+P28+j/gZPGMcps="; + }; + + nativeBuildInputs = [ + python3 + ]; + + npmDepsHash = "sha256-L7y49Jr9JukHbuzhe3JqpAlKqQYHXKax2uM+RnNiF2o="; + + postInstall = '' + ln -s zap $out/bin/zap-cli + echo '{"hash": "5f1f3a62b27db9f5293f2bfef3ab01e603751a37", "date": "2023-12-07", "zapVersion": "2023.12.07"}' > $out/lib/node_modules/zap/.version.json + ''; + + env.ZAP_SKIP_REAL_VERSION = true; + env.ELECTRON_SKIP_BINARY_DOWNLOAD = true; + env.CYPRESS_INSTALL_BINARY = 0; +}