-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nix recipe to build Nimbus wrappers
- Loading branch information
Showing
4 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ lib }: | ||
|
||
# This Nix expression allows filtering a local directory by specifying dirRootsToInclude, dirsToExclude and filesToInclude. | ||
# It also filters out symlinks to result folders created by nix-build, as well as backup/swap/generated files | ||
|
||
let | ||
inherit (lib) | ||
any compare compareLists elem elemAt hasPrefix length min splitString take; | ||
|
||
isPathAllowed = allowedPath: path: | ||
let | ||
count = min (length allowedPathElements) (length pathElements); | ||
pathElements = splitString "/" path; | ||
allowedPathElements = splitString "/" allowedPath; | ||
pathElementsSubset = take count pathElements; | ||
allowedPathElementsSubset = take count allowedPathElements; | ||
in (compareLists compare allowedPathElementsSubset pathElementsSubset) == 0; | ||
|
||
mkFilter = { dirRootsToInclude, # Relative paths of directories to include | ||
dirsToExclude ? [ ], # Base names of directories to exclude | ||
filesToInclude ? [ ], # Relative path of files to include | ||
filesToExclude ? [ ], # Relative path of files to exclude | ||
root }: | ||
path: type: | ||
let | ||
baseName = baseNameOf (toString path); | ||
subpath = elemAt (splitString "${toString root}/" path) 1; | ||
spdir = elemAt (splitString "/" subpath) 0; | ||
|
||
in lib.cleanSourceFilter path type && ( | ||
(type != "directory" && (elem spdir filesToInclude) && !(elem spdir filesToExclude)) || | ||
# check if any part of the directory path is described in dirRootsToInclude | ||
((any (dirRootToInclude: isPathAllowed dirRootToInclude subpath) dirRootsToInclude) && ! ( | ||
# Filter out version control software files/directories | ||
(type == "directory" && (elem baseName dirsToExclude)) | ||
))); | ||
|
||
in mkFilter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{ stdenv, callPackage, fetchFromGitHub, clang, go, nim, sqlite, pcre, rocksdb }: | ||
|
||
{ buildSamples ? true }: | ||
|
||
let | ||
inherit (stdenv.lib) concatMapStringsSep makeLibraryPath optional optionalString; | ||
|
||
mkFilter = callPackage ./mkFilter.nix { inherit (stdenv) lib; }; | ||
vendorDeps = [ | ||
"nim-chronicles" "nim-faststreams" "nim-json-serialization" "nim-chronos" "nim-eth" "nim-json" | ||
"nim-metrics" "nim-secp256k1" "nim-serialization" "nim-stew" "nim-stint" "nimcrypto" | ||
]; | ||
|
||
in | ||
|
||
stdenv.mkDerivation rec { | ||
name = "nimbus-${version}"; | ||
version = "0.0.1"; | ||
|
||
src = | ||
let path = ./..; # Import the root /android and /mobile/js_files folders clean of any build artifacts | ||
in builtins.path { # We use builtins.path so that we can name the resulting derivation, otherwise the name would be taken from the checkout directory, which is outside of our control | ||
inherit path; | ||
name = "nimbus-sources"; | ||
filter = | ||
# Keep this filter as restrictive as possible in order to avoid unnecessary rebuilds and limit closure size | ||
mkFilter { | ||
dirRootsToInclude = [ | ||
"vendor" "wrappers" | ||
]; | ||
dirsToExclude = [ ".git" ".svn" "CVS" ".hg" "nimbus-build-system" "tests" ] | ||
++ (builtins.map (dep: "vendor/${dep}") vendorDeps); | ||
filesToInclude = [ ]; | ||
filesToExclude = [ "VERSION" "android/gradlew" ]; | ||
root = path; | ||
}; | ||
}; | ||
nativeBuildInputs = optional buildSamples go; | ||
buildInputs = [ clang nim rocksdb pcre sqlite ]; | ||
LD_LIBRARY_PATH = "${makeLibraryPath buildInputs}"; | ||
|
||
buildPhase = '' | ||
mkdir -p $TMPDIR/.nimcache ./build | ||
BUILD_MSG="\\e[92mBuilding:\\e[39m" | ||
export CC="${clang}/bin/clang" | ||
ln -s nimbus.nimble nimbus.nims | ||
vendorPathOpts="${concatMapStringsSep " " (dep: "--path:./vendor/${dep}") vendorDeps}" | ||
echo -e $BUILD_MSG "build/libnimbus.so" && \ | ||
${nim}/bin/nim c --app:lib --noMain --nimcache:$TMPDIR/.nimcache -d:release ''${vendorPathOpts} -o:./build/libnimbus.so wrappers/libnimbus.nim | ||
# For some reason nim doesn't output anything if we pass -o option when building the static lib | ||
# so we just move it from the default location after it's built | ||
echo -e $BUILD_MSG "build/libnimbus.a" && \ | ||
${nim}/bin/nim c --app:staticlib --noMain --nimcache:$TMPDIR/.nimcache -d:release ''${vendorPathOpts} wrappers/libnimbus.nim && \ | ||
mv libnimbus build/libnimbus.a | ||
rm -rf $TMPDIR/.nimcache | ||
'' + | ||
optionalString buildSamples '' | ||
mkdir -p $TMPDIR/.home/.cache | ||
export HOME=$TMPDIR/.home | ||
echo -e $BUILD_MSG "build/C_wrapper_example" && \ | ||
$CC wrappers/wrapper_example.c -Wl,-rpath,'$$ORIGIN' -Lbuild -lnimbus -lm -g -o build/C_wrapper_example | ||
echo -e $BUILD_MSG "build/go_wrapper_example" && \ | ||
${go}/bin/go build -linkshared -o build/go_wrapper_example wrappers/wrapper_example.go wrappers/cfuncs.go | ||
echo -e $BUILD_MSG "build/go_wrapper_whisper_example" && \ | ||
${go}/bin/go build -linkshared -o build/go_wrapper_whisper_example wrappers/wrapper_whisper_example.go wrappers/cfuncs.go | ||
rm -rf $TMPDIR/.home/.cache | ||
''; | ||
installPhase = '' | ||
mkdir -p $out/{include,lib} | ||
cp ./wrappers/libnimbus.h $out/include/ | ||
cp ./build/libnimbus.{a,so} $out/lib/ | ||
'' + | ||
optionalString buildSamples '' | ||
mkdir -p $out/samples | ||
cp ./build/{C_wrapper_example,go_wrapper_example,go_wrapper_whisper_example} $out/samples | ||
''; | ||
|
||
meta = with stdenv.lib; { | ||
description = "A C wrapper of the Nimbus Ethereum 2.0 Sharding Client for Resource-Restricted Devices"; | ||
homepage = https://github.com/status-im/nimbus; | ||
license = with licenses; [ asl20 ]; | ||
platforms = with platforms; unix ++ windows; | ||
}; | ||
} |