Skip to content

Commit

Permalink
[nix] patch realm as a nodePackage for linux and darwin and use it fr…
Browse files Browse the repository at this point in the history
…om nix store, add to default nix-shell buildDependencies, regenerate nodePackages with node2nix 1.6.1
  • Loading branch information
hlolli authored and Pedro Pombeiro committed May 22, 2019
1 parent 6b1c42f commit dd4093b
Show file tree
Hide file tree
Showing 8 changed files with 1,831 additions and 100 deletions.
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -140,11 +140,11 @@ desktop-server:
# -------------
_run-%:
$(eval SYSTEM := $(word 2, $(subst -, , $@)))
react-native run-$(SYSTEM)
npx react-native run-$(SYSTEM)

run-android: export TARGET_OS ?= android
run-android: ##@run Run Android build
react-native run-android --appIdSuffix debug
npx react-native run-android --appIdSuffix debug

run-desktop: export TARGET_OS ?= $(HOST_OS)
run-desktop: _run-desktop ##@run Run Desktop build
Expand All @@ -153,9 +153,9 @@ SIMULATOR=
run-ios: export TARGET_OS ?= ios
run-ios: ##@run Run iOS build
ifneq ("$(SIMULATOR)", "")
react-native run-ios --simulator="$(SIMULATOR)"
npx react-native run-ios --simulator="$(SIMULATOR)"
else
react-native run-ios
npx react-native run-ios
endif

#--------------
Expand Down
4 changes: 2 additions & 2 deletions derivation.nix
Expand Up @@ -8,12 +8,12 @@ let
# TODO: Try to use stdenv for iOS. The problem is with building iOS as the build is trying to pass parameters to Apple's ld that are meant for GNU's ld (e.g. -dynamiclib)
stdenv' = pkgs.stdenvNoCC;
gradle = pkgs.gradle_4_10;
statusDesktop = pkgs.callPackage ./nix/desktop { inherit target-os status-go; inherit (pkgs) darwin; stdenv = stdenv'; };
statusDesktop = pkgs.callPackage ./nix/desktop { inherit target-os status-go; inherit (pkgs) darwin; nodejs = nodejs'; stdenv = stdenv'; };
statusMobile = pkgs.callPackage ./nix/mobile { inherit target-os config status-go gradle; inherit (pkgs.xcodeenv) composeXcodeWrapper; stdenv = stdenv'; };
status-go = pkgs.callPackage ./nix/status-go { inherit target-os; inherit (pkgs.xcodeenv) composeXcodeWrapper; inherit (statusMobile) xcodewrapperArgs; androidPkgs = statusMobile.androidComposition; };
nodejs' = pkgs.nodejs-10_x;
yarn' = pkgs.yarn.override { nodejs = nodejs'; };
nodeInputs = import ./nix/global-node-packages/output {
nodeInputs = import ./nix/global-node-packages {
# The remaining dependencies come from Nixpkgs
inherit pkgs;
nodejs = nodejs';
Expand Down
2 changes: 1 addition & 1 deletion nix/desktop/default.nix
@@ -1,6 +1,6 @@
{ stdenv, callPackage, target-os,
cmake, extra-cmake-modules, file, status-go,
darwin }:
darwin, nodejs }:

with stdenv;

Expand Down
30 changes: 30 additions & 0 deletions nix/global-node-packages/default.nix
@@ -0,0 +1,30 @@
{ pkgs, nodejs }:

let nodePackages = import ./output { inherit pkgs; inherit nodejs; };
realm-version = "5.12.1";
realm-patched-name = "realm-git+https://github.com/status-im/realm-js.git#heads/v2.20.1";
realm-core-src = pkgs.fetchurl (
if builtins.currentSystem == "x86_64-darwin" then {
url = "https://static.realm.io/downloads/core/realm-core-Release-v${realm-version}-Darwin-devel.tar.gz";
sha256 = "05ji1zyskwjj8p5i01kcg7h1cxdjj62fcsp6haf2f65qshp6r44d";
} else {
url = "https://static.realm.io/downloads/core/realm-core-Release-v${realm-version}-Linux-devel.tar.gz";
sha256 = "02pvi28qnvzdv7ghqzf79bxn8id9s7mpp3g2ambxg8jrcrkqfvr1";
}
);
realm-dest-dir = if builtins.currentSystem == "x86_64-darwin" then
"$out/lib/node_modules/realm/vendor/realm-darwin-x64" else
"$out/lib/node_modules/realm/vendor/realm-linux-x64";
in nodePackages // {
${realm-patched-name} = nodePackages.${realm-patched-name}.override(oldAttrs: {
buildInputs = oldAttrs.buildInputs ++ [ pkgs.nodePackages.node-pre-gyp ];
reconstructLock = true;
preRebuild = ''
# Do not attempt to do any http calls!
substituteInPlace $out/lib/node_modules/realm/scripts/download-realm.js \
--replace "return acquire(requirements, realmDir)" ""
mkdir -p ${realm-dest-dir}
tar -xzf ${realm-core-src} -C ${realm-dest-dir}
'';
});
}
6 changes: 4 additions & 2 deletions nix/global-node-packages/generate-nix.sh
Expand Up @@ -11,13 +11,15 @@ rm -rf $output_dir && mkdir -p $output_dir
react_native_cli_required_version=$($toolversion react_native_cli)
cat << EOF > $input
[
{ "react-native-cli": "${react_native_cli_required_version}" }
{ "react-native-cli": "${react_native_cli_required_version}",
"realm": "git+https://github.com/status-im/realm-js.git#heads/v2.20.1" }
]
EOF

node_required_version=$($toolversion node)
node_major_version=$(echo $node_required_version | cut -d. -f1,1)
node2nix --nodejs-${node_major_version} --bypass-cache -i $input \

node2nix --nodejs-${node_major_version} -i $input \
-o $output_dir/node-packages.nix \
-c $output_dir/default.nix \
-e $output_dir/node-env.nix
Expand Down
2 changes: 1 addition & 1 deletion nix/global-node-packages/output/default.nix
@@ -1,4 +1,4 @@
# This file has been generated by node2nix 1.6.0. Do not edit!
# This file has been generated by node2nix 1.6.1. Do not edit!

{pkgs ? import <nixpkgs> {
inherit system;
Expand Down
137 changes: 63 additions & 74 deletions nix/global-node-packages/output/node-env.nix
Expand Up @@ -72,7 +72,7 @@ let
packageDir="$(find . -maxdepth 1 -type d | tail -1)"
# Restore write permissions to make building work
find "$packageDir" -type d -print0 | xargs -0 chmod u+x
find "$packageDir" -type d -exec chmod u+x {} \;
chmod -R u+w "$packageDir"
# Move the extracted tarball into the output folder
Expand Down Expand Up @@ -308,6 +308,61 @@ let
'';
};

prepareAndInvokeNPM = {packageName, bypassCache, reconstructLock, npmFlags, production}:
let
forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com";
in
''
# Pinpoint the versions of all dependencies to the ones that are actually being used
echo "pinpointing versions of dependencies..."
source $pinpointDependenciesScriptPath
# Patch the shebangs of the bundled modules to prevent them from
# calling executables outside the Nix store as much as possible
patchShebangs .
# Deploy the Node.js package by running npm install. Since the
# dependencies have been provided already by ourselves, it should not
# attempt to install them again, which is good, because we want to make
# it Nix's responsibility. If it needs to install any dependencies
# anyway (e.g. because the dependency parameters are
# incomplete/incorrect), it fails.
#
# The other responsibilities of NPM are kept -- version checks, build
# steps, postprocessing etc.
export HOME=$TMPDIR
cd "${packageName}"
runHook preRebuild
${stdenv.lib.optionalString bypassCache ''
${stdenv.lib.optionalString reconstructLock ''
if [ -f package-lock.json ]
then
echo "WARNING: Reconstruct lock option enabled, but a lock file already exists!"
echo "This will most likely result in version mismatches! We will remove the lock file and regenerate it!"
rm package-lock.json
else
echo "No package-lock.json file found, reconstructing..."
fi
node ${reconstructPackageLock}
''}
node ${addIntegrityFieldsScript}
''}
npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
if [ "$dontNpmInstall" != "1" ]
then
# NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
rm -f npm-shrinkwrap.json
npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
fi
'';

# Builds and composes an NPM package including all its dependencies
buildNodePackage =
{ name
Expand All @@ -319,14 +374,14 @@ let
, npmFlags ? ""
, dontNpmInstall ? false
, bypassCache ? false
, reconstructLock ? false
, preRebuild ? ""
, dontStrip ? true
, unpackPhase ? "true"
, buildPhase ? "true"
, ... }@args:

let
forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com";
extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" "dontStrip" "dontNpmInstall" "preRebuild" "unpackPhase" "buildPhase" ];
in
stdenv.mkDerivation ({
Expand All @@ -352,47 +407,7 @@ let
# Compose the package and all its dependencies
source $compositionScriptPath
# Pinpoint the versions of all dependencies to the ones that are actually being used
echo "pinpointing versions of dependencies..."
source $pinpointDependenciesScriptPath
# Patch the shebangs of the bundled modules to prevent them from
# calling executables outside the Nix store as much as possible
patchShebangs .
# Deploy the Node.js package by running npm install. Since the
# dependencies have been provided already by ourselves, it should not
# attempt to install them again, which is good, because we want to make
# it Nix's responsibility. If it needs to install any dependencies
# anyway (e.g. because the dependency parameters are
# incomplete/incorrect), it fails.
#
# The other responsibilities of NPM are kept -- version checks, build
# steps, postprocessing etc.
export HOME=$TMPDIR
cd "${packageName}"
runHook preRebuild
${stdenv.lib.optionalString bypassCache ''
if [ ! -f package-lock.json ]
then
echo "No package-lock.json file found, reconstructing..."
node ${reconstructPackageLock}
fi
node ${addIntegrityFieldsScript}
''}
npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
if [ "$dontNpmInstall" != "1" ]
then
# NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
rm -f npm-shrinkwrap.json
npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
fi
${prepareAndInvokeNPM { inherit packageName bypassCache reconstructLock npmFlags production; }}
# Create symlink to the deployed executable folder, if applicable
if [ -d "$out/lib/node_modules/.bin" ]
Expand Down Expand Up @@ -431,14 +446,13 @@ let
, npmFlags ? ""
, dontNpmInstall ? false
, bypassCache ? false
, reconstructLock ? false
, dontStrip ? true
, unpackPhase ? "true"
, buildPhase ? "true"
, ... }@args:

let
forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com";

extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" ];

nodeDependencies = stdenv.mkDerivation ({
Expand Down Expand Up @@ -473,39 +487,13 @@ let
fi
''}
# Pinpoint the versions of all dependencies to the ones that are actually being used
echo "pinpointing versions of dependencies..."
# Go to the parent folder to make sure that all packages are pinpointed
cd ..
${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."}
source $pinpointDependenciesScriptPath
cd ${packageName}
# Patch the shebangs of the bundled modules to prevent them from
# calling executables outside the Nix store as much as possible
patchShebangs .
export HOME=$PWD
${stdenv.lib.optionalString bypassCache ''
if [ ! -f package-lock.json ]
then
echo "No package-lock.json file found, reconstructing..."
node ${reconstructPackageLock}
fi
node ${addIntegrityFieldsScript}
''}
npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
${stdenv.lib.optionalString (!dontNpmInstall) ''
# NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
rm -f npm-shrinkwrap.json
npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
''}
${prepareAndInvokeNPM { inherit packageName bypassCache reconstructLock npmFlags production; }}
# Expose the executables that were installed
cd ..
${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."}
Expand All @@ -532,6 +520,7 @@ let
inherit nodeDependencies;
shellHook = stdenv.lib.optionalString (dependencies != []) ''
export NODE_PATH=$nodeDependencies/lib/node_modules
export PATH="$nodeDependencies/bin:$PATH"
'';
};
in
Expand Down

0 comments on commit dd4093b

Please sign in to comment.