summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/home-assistant-chip-wheels/default.nix
blob: dc7b30c4aca94bc2785489d00d5238f5e8fd019f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{
  aiohttp,
  appdirs,
  appnope,
  black,
  build,
  clang-tools,
  click,
  colorama,
  coloredlogs,
  coverage,
  cryptography,
  diskcache,
  fetchFromGitHub,
  glib,
  gn,
  googleapis-common-protos,
  google-cloud-storage,
  ipython,
  jinja2,
  json5,
  jsonschema,
  lark,
  lib,
  libnl,
  mobly,
  mypy,
  mypy-extensions,
  mypy-protobuf,
  ninja,
  openssl,
  packaging,
  parameterized,
  pip-tools,
  pkg-config,
  pkgconfig,
  prompt-toolkit,
  protobuf,
  psutil,
  ptpython,
  pyelftools,
  pygments,
  pykwalify,
  pylint,
  pyperclip,
  pyserial,
  python,
  python-daemon,
  pythonOlder,
  pyyaml,
  requests,
  setuptools,
  six,
  sphinx,
  sphinx-argparse,
  sphinx-design,
  stdenv,
  stringcase,
  toml,
  tornado,
  types-protobuf,
  types-pyyaml,
  types-requests,
  types-setuptools,
  watchdog,
  websockets,
  wheel,
  yapf,
  zap-chip,
}:

stdenv.mkDerivation rec {
  pname = "home-assistant-chip-wheels";
  version = "2025.4.0";
  src = fetchFromGitHub {
    owner = "home-assistant-libs";
    repo = "chip-wheels";
    tag = version;
    fetchSubmodules = false;
    leaveDotGit = true;
    hash = "sha256-20dqVXHPgSxBveTxlbHEjTtp9NI1oVCVpBTDbjDI2QA=";
    postFetch = ''
      cd $out
      # Download connectedhomeip.
      git fetch
      git reset --hard HEAD
      git submodule update --init --depth 1 connectedhomeip

      # Initialize only necessary submodules.
      cd connectedhomeip
      ${python.interpreter} scripts/checkout_submodules.py --platform linux --shallow

      # Keep the output deterministic.
      cd $out
      # in case python decided to leave a .pyc file, for example
      git clean -fxd
      rm -rf .git/
    '';
  };

  strictDeps = true;

  nativeBuildInputs = [
    gn
    pkg-config
    ninja
    clang-tools
    zap-chip
    # gdbus-codegen
    glib
    pkgconfig
    python
    # dependencies of build scripts
    click
    jinja2
    lark
    setuptools
    stringcase
    build
    pip-tools
    black
    yapf
  ];

  propagatedBuildInputs = [
    openssl
    glib
    libnl
  ];

  postPatch = ''
    cd connectedhomeip
    export HOME=$(mktemp -d)

    patchShebangs --build scripts

    for patch in ../*.patch; do
      patch -p1 < $patch
    done

    # unpin dependencies
    # there are many files to modify, in different formats
    sed -i 's/==.*$//' third_party/pigweed/repo/pw_env_setup/py/pw_env_setup/virtualenv_setup/python_base_requirements.txt
    sed -i 's/==[^;]*//' scripts/setup/constraints.txt
    sed -i 's/\(^ \+[a-zA-Z0-9-]*\)[=~><]=[^;]*/\1/' third_party/pigweed/repo/pw_protobuf_compiler/py/setup.cfg third_party/pigweed/repo/pw_protobuf/py/setup.cfg third_party/pigweed/repo/pw_protobuf_compiler/py/setup.cfg
    # remove a few dependencies not packaged in nixpkgs and which are apparently
    # not needed to build the python bindings of chip
    sed -i -e '/sphinxcontrib-mermaid/d' -e '/types-six/d' -e '/types-pygment/d' -e '/types-pyserial/d' third_party/pigweed/repo/*/py/setup.cfg

    # obtained by running a build in nix-shell with internet access
    cp ${./pigweed_environment.gni} build_overrides/pigweed_environment.gni

    # some code is generated by a templating tool (zap-cli)
    scripts/codepregen.py ./zzz_pregenerated/
  '';

  # the python parts of the build system work as follows
  # gn calls pigweed to read a dozen different files to generate
  # a file looking like requirements.txt. It then calls pip
  # to install this computed list of dependencies into a virtualenv.
  # Of course, pip fails in the sandbox, because it cannot download
  # the python packages.
  # The documented way of doing offline builds is to create a folder
  # with wheel files for all dependencies and point pip to it
  # via its configuration file or environment variables.
  # https://pigweed.dev/python_build.html#installing-offline
  # The wheel of a python package foo is available as foo.dist.
  # So that would be easy, but we also need wheels for transitive dependencies.
  # the function saturateDependencies below computes this transitive closure.
  #
  # yes this list of dependencies contains both build tools and proper dependencies.
  env.PIP_NO_INDEX = "1";
  env.PIP_FIND_LINKS =
    let
      dependencies = [
        aiohttp
        appdirs
        appnope
        black
        build
        colorama
        coloredlogs
        coverage
        click
        cryptography
        diskcache
        googleapis-common-protos
        google-cloud-storage
        ipython
        jinja2
        json5
        jsonschema
        lark
        mobly
        mypy
        mypy-extensions
        mypy-protobuf
        packaging
        parameterized
        pip-tools
        pkgconfig
        prompt-toolkit
        protobuf
        psutil
        ptpython
        pyelftools
        pygments
        pykwalify
        (pylint.overridePythonAttrs { doCheck = pythonOlder "3.13"; })
        pyperclip
        pyserial
        python-daemon
        pyyaml
        requests
        setuptools
        six
        sphinx
        sphinx-argparse
        sphinx-design
        stringcase
        toml
        tornado
        types-protobuf
        types-pyyaml
        types-requests
        types-setuptools
        watchdog
        websockets
        wheel
        yapf
      ];
      filterNull = list: lib.filter (dep: dep != null) list;
      toItem = dep: {
        inherit dep;
        key = dep.name;
      };
      saturatedDependencies = lib.genericClosure {
        startSet = map toItem (filterNull dependencies);
        operator = item: map toItem (filterNull ((item.dep).propagatedBuildInputs or [ ]));
      };
      saturatedDependencyList = lib.filter (dep: dep ? dist && dep != null) (
        map (item: item.dep) saturatedDependencies
      );
    in
    lib.concatMapStringsSep " " (dep: "file://${dep.dist}") saturatedDependencyList;

  gnFlags = [
    ''chip_project_config_include_dirs=["//.."]''
    ''chip_crypto="openssl"''
    ''enable_rtti=true''
    ''chip_config_memory_debug_checks=false''
    ''chip_config_memory_debug_dmalloc=false''
    ''chip_mdns="minimal"''
    ''chip_minmdns_default_policy="libnl"''
    ''chip_python_version="${lib.versions.majorMinor python.version}"''
    ''chip_python_platform_tag="any"''
    ''chip_python_package_prefix="home-assistant-chip"''
    ''custom_toolchain="custom"''
    ''target_cc="${stdenv.cc.targetPrefix}cc"''
    ''target_cxx="${stdenv.cc.targetPrefix}c++"''
    ''target_ar="${stdenv.cc.targetPrefix}ar"''
  ];

  preBuild = ''
    export NIX_CFLAGS_COMPILE="$($PKG_CONFIG --cflags glib-2.0) -O2 -Wno-error"
    export NIX_CFLAGS_LINK="$($PKG_CONFIG --libs gio-2.0) $($PKG_CONFIG --libs gobject-2.0) $($PKG_CONFIG --libs glib-2.0)"
  '';

  ninjaFlags = [ "chip-repl" ];

  installPhase = ''
    runHook preInstall

    cp -r controller/python $out

    runHook postInstall
  '';

  meta = {
    description = "Python wheels for APIs and tools related to CHIP";
    homepage = "https://github.com/home-assistant-libs/chip-wheels";
    changelog = "https://github.com/home-assistant-libs/chip-wheels/releases/tag/${src.tag}";
    license = lib.licenses.asl20;
    maintainers = lib.teams.home-assistant.members;
  };

}