Skip to content

Instantly share code, notes, and snippets.

@mkg20001
Created February 14, 2021 17:30
Show Gist options
  • Save mkg20001/87cfc8fe7ef40e49cf94f2d6ce4c56c1 to your computer and use it in GitHub Desktop.
Save mkg20001/87cfc8fe7ef40e49cf94f2d6ce4c56c1 to your computer and use it in GitHub Desktop.
nix derivation to build filecoin lotus
with (import ./. {});
let
lotusSrc = fetchFromGitHub {
owner = "filecoin-project";
repo = "lotus";
# master (nix-prefetch-github filecoin-project lotus --fetch-submodules --nix)
rev = "95e47cf9982c1c3538f4de8037ca5476cbaedab0";
sha256 = "lZmVYqs6wVzT0om8fvpDrP5pFDIYJZnZmQHM+RsP25I=";
fetchSubmodules = true;
};
filcrypto = rustPlatform.buildRustPackage rec {
pname = "filecrypto";
version = "0.0.0";
doCheck = false; # requires elevated permissions
src = "${lotusSrc}/extern/filecoin-ffi/rust";
cargoSha256 = "sha256-L+hEfWmRDsLyJyeA1aDdCa0IvXBCfXln4v6yghHMSvg=";
nativeBuildInputs = [
pkg-config
];
preBuild = ''
LOGFILE=$(mktemp)
exec \
1> >(tee >(awk '{ system(""); print $0; system(""); }' >> "$LOGFILE")) \
2> >(tee >(awk '{ system(""); print $0; system(""); }' >> "$LOGFILE") >&2)
export OPENSSL_LIB_DIR=${openssl.out}/lib
export OPENSSL_INCLUDE_DIR=${openssl.dev}/include
export OPENSSL_NO_VENDOR=true
export OUT_DIR=$out
'';
RUSTFLAGS = "--print native-static-libs";
postInstall = ''
# install -D include/filcrypto.h $out/include
local PRIVATE_LIBS=$(cat ''${LOGFILE} \
| grep native-static-libs\: \
| head -n 1 \
| cut -d ':' -f 3)
mkdir -p $out/lib/pkgconfig
echo 'prefix='"''${out}"'
exec_prefix=''${prefix}
libdir=''${exec_prefix}/lib
includedir=''${prefix}/include' > $out/lib/pkgconfig/filcrypto.pc
cat filcrypto.pc.template | sed "s|@VERSION@|0.0.0|" | sed "s|@PRIVATE_LIBS@|$PRIVATE_LIBS|" >> $out/lib/pkgconfig/filcrypto.pc
mkdir -p $out/include
find -L . -type f -name filcrypto.h -exec cp -- "{}" $out/include/ \;
'';
buildInputs = [
opencl-headers opencl-icd openssl.out openssl.dev hwloc
];
};
ffi = buildGoModule {
pname = "filecoin-ffi";
version = "0.0.0";
doCheck = false;
src = "${lotusSrc}/extern/filecoin-ffi";
vendorSha256 = "sha256-09I6RF+jWLNyfiNEci0C8+kq647kG7uiwk3iXx3A8Jc=";
preBuild = ''
ls
find ${filcrypto} -type f -exec ln -s {} . \;
'';
nativeBuildInputs = [
pkg-config
];
buildInputs = [
filcrypto
] ++ filcrypto.buildInputs;
};
ipsnsecp = fetchFromGitHub {
owner = "ipsn";
repo = "go-secp256k1";
rev = "9d62b9f0bc52d16160f79bfb84b2bbf0f6276b03";
sha256 = "P0DVsWbuQ2BrHGX3eVvM0ucn9YM4XgP1j5vqNDhukr4=";
fetchSubmodules = true;
};
hidlib = fetchFromGitHub {
owner = "zondax";
repo = "hid";
rev = "302fd402163c34626286195dfa9adac758334acc";
sha256 = "cGJobeveSyJfWDDPBtU2RvnwbwdkvZa2+Nagtoh11/I=";
fetchSubmodules = true;
};
lotus = buildGoModule {
pname = "lotus";
version = "0.0.0";
src = lotusSrc;
doCheck = false;
vendorSha256 = "sha256-SpuwrILNIxVFc6hboppNeT8QAjCbzSblSPiPFILjuAo=";
subPackages = [
"cmd/lotus*"
];
nativeBuildInputs = [
pkg-config
go-rice
];
buildInputs = [
ffi
] ++ filcrypto.buildInputs;
preBuild = ''
cp -r ${ipsnsecp}/libsecp256k1 /build/source/vendor/github.com/ipsn/go-secp256k1/libsecp256k1
cp -r ${hidlib}/{libusb,hidapi} /build/source/vendor/github.com/zondax/hid/
mkdir -p /build/source/vendor/github.com/filecoin-project/filecoin-ffi
find ${filcrypto} -type f -exec ln -s {} /build/source/vendor/github.com/filecoin-project/filecoin-ffi/ \;
'';
postInstall = ''
mkdir -p $out/share/bash-completion/completions $out/share/zsh/site-functions/
install -C ./scripts/bash-completion/lotus $out/share/bash-completion/completions/lotus
install -C ./scripts/bash-completion/lotus-miner $out/share/bash-completion/completions/lotus-miner
install -C ./scripts/zsh-completion/lotus $out/share/zsh/site-functions/_lotus
install -C ./scripts/zsh-completion/lotus-miner $out/share/zsh/site-functions/_lotus-miner
'';
postFixup = ''
for l in $out/bin/lotus*; do
rice append --verbose --exec "$l" -i ./build
done
'';
# preBuild = ''
# export RUSTFLAGS="-C target-cpu=native -g"
# export FFI_BUILD_FROM_SOURCE=1
#'';
};
in
lotus
@elliot-u410
Copy link

Thank for publishing this. I've been working off of it for more recent versions: https://gist.github.com/noch3/28218ab5bc75bf93698c0bf0e497691a

@mkg20001
Copy link
Author

mkg20001 commented Sep 11, 2021

I also have this repo https://github.com/mkg20001/lotus-flake with a improved version of this gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment