Skip to content

Instantly share code, notes, and snippets.

@elliot-u410
Last active September 11, 2021 04:29
Show Gist options
  • Save elliot-u410/28218ab5bc75bf93698c0bf0e497691a to your computer and use it in GitHub Desktop.
Save elliot-u410/28218ab5bc75bf93698c0bf0e497691a to your computer and use it in GitHub Desktop.
lotus.nix
let
nixpkgs = builtins.fetchTarball {
url = "https://releases.nixos.org/nixpkgs/nixpkgs-21.11pre313691.ccb19cf7d00/nixexprs.tar.xz";
sha256 = "sha256:10qkfx7p9jy8az1lcbn731nbzwi38228xkxbza0lbmmlwhm8i8jw";
};
in
{ nixpkgsFunc ? import nixpkgs
, enableRustNativeCpu ? true
}:
with (nixpkgsFunc {
overlays = [
(self: super: {
ocl-icd = self.callPackage ./ocl-icd.nix {}; # Won't be necessary once https://github.com/NixOS/nixpkgs/pull/137342 lands
})
];
});
let
version = "1.11.2";
versionShas = {
"1.11.2" = "sha256:0cbmgkdqakkwcy4qbsnmqdk75cc89mjcfbmf26fygwrflnhm25mn";
"1.11.1" = "sha256:0kvs9b3nzbkkrinrx9mnw0n41f32mcbd0bix3v98ng2h7lc3zj8a";
"1.11.0" = "sha256:0zvy0d9wfqsbcwzgkvb79gav3xadn9wp0nzq5jnnqh07y2338fg3";
};
lotusRawSrc = fetchFromGitHub {
owner = "filecoin-project";
repo = "lotus";
# master (nix-prefetch-github filecoin-project lotus --fetch-submodules --nix --rev VERSION)
rev = "v${version}";
sha256 = versionShas.${version};
fetchSubmodules = true;
};
# Lotus has an unnecessary entry in go.mod that triggers a bug in vend to loop infinitely.
# We do it here instead of in the patch phase of the derivation because vend runs before the patch phase.
# NOTE: New versions of lotus have removed this, but this patch is kept for backwards compat.
patchSource = src: pkgs.runCommand "patch-lotus-source-pre-vend" {} ''
cp -a '${src}' "$out"
chmod -R +w "$out"
sed -i 's|replace github.com/filecoin-project/lotus => ./||g' "$out/go.mod"
'';
lotusSrc = patchSource lotusRawSrc;
filcrypto = rustPlatform.buildRustPackage rec {
pname = "filecrypto";
version = "0.0.0";
doCheck = false; # requires elevated permissions
src = "${lotusSrc}/extern/filecoin-ffi/rust";
cargoSha256 = "sha256:0bir5y05fhkl43hdb5f0b3davkhw1f506yfd3kv2yf2dbvkckh0j";
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 = lib.concatStringsSep " " ([
"--print native-static-libs"
] ++ (if enableRustNativeCpu then ["-C target-cpu=native -g"] else []));
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 rec {
pname = "filecoin-ffi";
version = "0.0.0";
doCheck = false;
src = "${lotusSrc}/extern/filecoin-ffi";
vendorSha256 = "0wp5xbmrc49n85rdrc0wj9svv3kahnyqqhbjfcwp6al2r2zqlxks";
# Replicate the placement of the filcrypto outputs so ffi can find them.
# pkgconfig about these missing when not configured correctly.
preBuild = ''
ln -s "${filcrypto}/lib/pkgconfig/filcrypto.pc" .
ln -s "${filcrypto}/include/filcrypto.h" .
ln -s "${filcrypto}/lib/libfilcrypto.a" .
'';
nativeBuildInputs = [
pkg-config
];
buildInputs = [
filcrypto
pkgs.opencl-clang
] ++ filcrypto.buildInputs
++ lib.optional stdenv.isDarwin darwin.apple_sdk.frameworks.OpenCL;
};
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 = network: buildGoModule {
inherit version;
pname = "lotus-${network}";
src = lotusSrc;
doCheck = false;
vendorSha256 = "sha256:0d6ndk0md0kkwdfsvyfx5wrspv56d8x1i3kcimanlk6vb0by6kpp";
runVend = true;
subPackages = [
"cmd/lotus*"
];
nativeBuildInputs = [
pkg-config
go-rice
];
buildInputs = [
ffi
] ++ filcrypto.buildInputs
++ lib.optional stdenv.isDarwin darwin.apple_sdk.frameworks.OpenCL;
preBuild = ''
chmod -R +w vendor
ln -s "${filcrypto}/lib/pkgconfig/filcrypto.pc" vendor/github.com/filecoin-project/filecoin-ffi
ln -s "${filcrypto}/include/filcrypto.h" vendor/github.com/filecoin-project/filecoin-ffi
ln -s "${filcrypto}/lib/libfilcrypto.a" vendor/github.com/filecoin-project/filecoin-ffi
'';
tags = if network == "mainnet" then [] else [network];
# 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/zsh-completion/lotus $out/share/zsh/site-functions/_lotus
'';
# install -C ./scripts/bash-completion/lotus-miner $out/share/bash-completion/completions/lotus-miner
# 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 {
inherit ffi hidlib filcrypto;
lotus-mainnet = lotus "mainnet";
lotus-calibnet = lotus "calibnet";
lotus-nerpanet = lotus "nerpanet";
}
{ pkgs, lib, stdenv, fetchFromGitHub, ruby, opencl-headers, addOpenGLRunpath }:
stdenv.mkDerivation rec {
pname = "ocl-icd";
version = "2.3.1";
src = fetchFromGitHub {
owner = "OCL-dev";
repo = "ocl-icd";
rev = "v2.3.1";
sha256 = "sha256:1km2rqc9pw6xxkqp77a22pxfsb5kgw95w9zd15l5jgvyjb6rqqad";
};
nativeBuildInputs = [ ruby pkgs.autoreconfHook ];
buildInputs = [ opencl-headers ];
meta = with lib; {
description = "OpenCL ICD Loader for ${opencl-headers.name}";
homepage = "https://forge.imag.fr/projects/ocl-icd/";
license = licenses.bsd2;
platforms = platforms.linux ++ platforms.darwin;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment