From c62abf10c186d90cab578af78d22f68653337e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Wed, 2 Dec 2020 13:40:23 +0100 Subject: [PATCH] nix: add docs about NodeJS modules, small refactor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub Sokołowski --- nix/deps/nodejs-patched/README.md | 18 +++++++++ nix/deps/nodejs-patched/default.nix | 42 ++++++++++++--------- nix/deps/nodejs/README.md | 58 +++++++++++++++++++++++++++++ nix/deps/nodejs/default.nix | 14 +++---- 4 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 nix/deps/nodejs-patched/README.md create mode 100644 nix/deps/nodejs/README.md diff --git a/nix/deps/nodejs-patched/README.md b/nix/deps/nodejs-patched/README.md new file mode 100644 index 0000000000..1fcf73c9c1 --- /dev/null +++ b/nix/deps/nodejs-patched/README.md @@ -0,0 +1,18 @@ +# Description + +This Nix derivation takes the result of the [`nix/deps/nodejs/default.nix`](../nodejs/default.nix) derivation and adjusts it for use with Gradle. + +# Details + +Modules provided by `yarn2nix` are normally fine, but we use `react-native-*` packages which have their own `gradle.build` files that reference external Maven repositories: +```js +repositories { + google() + jcenter() +} +``` +And these need to be patched and replaced with `mavenLocal()` to make sure Gradle doesn't try to fetch dependencies from remote repos. + +This derivation symlinks most of the modules found in the `yarn2nix` result and copies the ones that require patching of `gradle.build` files in `patchGradlePhase`. + +It also applies other fixes like making `BuildId` static in `patchBuildIdPhase` and fixing a Hermes bug in `patchHermesPhase`. diff --git a/nix/deps/nodejs-patched/default.nix b/nix/deps/nodejs-patched/default.nix index 349ea5787a..2cecbf1c11 100644 --- a/nix/deps/nodejs-patched/default.nix +++ b/nix/deps/nodejs-patched/default.nix @@ -6,8 +6,14 @@ stdenv.mkDerivation { name = "${deps.nodejs.name}-patched"; - - phases = [ "unpackPhase" "patchPhase" "installPhase" ]; + phases = [ + "unpackPhase" + "patchGradlePhase" + "patchBuildIdPhase" + "patchHermesPhase" + "patchJavaPhase" + "installPhase" + ]; # First symlink all modules as is # WARNING: Metro has issues when dealing with symlinks! @@ -19,10 +25,10 @@ stdenv.mkDerivation { cp -r ${deps.nodejs}/node_modules/.bin ./node_modules/ ''; - # Then patch the modules that have build.gradle files - patchPhase = '' - # Patch maven and google central repositories with our own local directories. - # This prevents the builder from downloading Maven artifacts + # Patch build.gradle files in 'react-native-*' dependencies to replace + # maven and google central repositories with our own local directories. + # This prevents the builder from downloading Maven artifacts + patchGradlePhase = '' for modBuildGradle in $(find -L ./node_modules -name build.gradle); do relativeToNode=''${modBuildGradle#*node_modules/} moduleName=''${relativeToNode%%/*} @@ -33,26 +39,26 @@ stdenv.mkDerivation { fi ${patchMavenSources} $modBuildGradle done - - patchShebangs ./node_modules - - # Do not add a BuildId to the generated libraries, for reproducibility + ''; + # Do not add a BuildId to the generated libraries, for reproducibility + patchBuildIdPhase = '' substituteInPlace ./node_modules/react-native/ReactAndroid/src/main/jni/Application.mk --replace \ '-Wl,--build-id' \ '-Wl,--build-id=none' - - # Fix bugs in Hermes usage: - # https://github.com/facebook/react-native/issues/25601#issuecomment-510856047 - # - Make PR builds also count as release builds - # - Fix issue where hermes command is being called with same input/output file + ''; + # Fix bugs in Hermes usage: + # https://github.com/facebook/react-native/issues/25601#issuecomment-510856047 + # - Make PR builds also count as release builds + # - Fix issue where hermes command is being called with same input/output file + patchHermesPhase = '' substituteInPlace ./node_modules/react-native/react.gradle --replace \ 'targetName.toLowerCase().contains("release")' \ '!targetName.toLowerCase().contains("debug")' - - # Patch Java files in modules which are not yet ported to AndroidX + ''; + # Patch Java files in modules which are not yet ported to AndroidX + patchJavaPhase = '' ${nodejs}/bin/node ./node_modules/jetifier/bin/jetify ''; - installPhase = '' mkdir -p $out cp -R node_modules $out/ diff --git a/nix/deps/nodejs/README.md b/nix/deps/nodejs/README.md new file mode 100644 index 0000000000..9cfb780c4f --- /dev/null +++ b/nix/deps/nodejs/README.md @@ -0,0 +1,58 @@ +# Description + +This derivation uses a [yarn2nix](https://github.com/nix-community/yarn2nix) to generate a Nix store package containing all NodeJS dependencies. + +# Details + +If you look at [`nix/deps/nodejs/default.nix`](./default.nix) you'll see that it it loads `package.json` and `yarn.lock` files and uses them to generate the derivation: +```sh + > make nix-repl +Welcome to Nix version 2.3.9. Type :? for help. + +nix-repl> pkgs.deps.nodejs +«derivation /nix/store/23mpmpjjnq7miclv6bc6ilgypy8wz69p-status-react-node-deps-1.8.0.drv» +``` +You can build the derivation: +``` +nix-repl> :b pkgs.deps.nodejs + +this derivation produced the following outputs: + out -> /nix/store/dqb2cjyz1g6n7jic07058y26lnmgaaz9-status-react-node-deps-1.8.0 +``` +And look inside: +``` + > cd /nix/store/dqb2cjyz1g6n7jic07058y26lnmgaaz9-status-react-node-deps-1.8.0 + > ls +deps node_modules + > ls node_modules | grep react-native-image +react-native-image-crop-picker +react-native-image-resizer +react-native-image-viewing +``` + +# Known Issues + +It's important that dependencies from GitHub are added in a correct format: +``` +git+https://github.com/status-im/bignumber.js.git#v4.0.2-status +``` +Notice three things: + +* `git+` prefix for the `https://` URL +* Repository name ends with `.git` +* Tag or branch is added after `#` character + +__WARNING__: Using branches can cause dependencies to change their hash and cause builds to fail. Using them during development is fine, but tags should be used in the final version. + +If this format is not used correctly you can see issues like: +``` +fatal: unable to access 'https://github.com/status-im/bignumber.js.git/': Could not resolve host: github.com +``` +or +``` +error Couldn't find any versions for "bignumber.js" that matches "github.com/status-im/bignumber.js.git#v4.0.2-status" in our cache (possible versions are ""). This is usually caused by a missing entry in the lockfile, running Yarn without the --offline flag may help fix this issue. +``` +or +``` +error Can't make a request in offline mode ("https://codeload.github.com/status-im/bignumber.js/tar.gz/f322b670969512a35c84441036a0ba4836a96428") +``` diff --git a/nix/deps/nodejs/default.nix b/nix/deps/nodejs/default.nix index 7d99c95335..088ad4e449 100644 --- a/nix/deps/nodejs/default.nix +++ b/nix/deps/nodejs/default.nix @@ -1,14 +1,10 @@ { lib, yarn2nix-moretea }: -let +# Create a yarn package for our project that contains all the dependecies. +yarn2nix-moretea.mkYarnModules rec { + pname = "status-react"; + name = "${pname}-node-deps-${version}"; version = lib.fileContents ../../../VERSION; yarnLock = ../../../yarn.lock; packageJSON = ../../../package.json; - packageJSONContent = lib.importJSON packageJSON; -in - # Create a yarn package for our project that contains all the dependecies. - yarn2nix-moretea.mkYarnModules rec { - pname = "status-react"; - name = "${pname}-node-deps-${version}"; - inherit version packageJSON yarnLock; - } +}