logos-messaging-nim/nix/default.nix
Prem Chaitanya Prathi 161bc02ad4
fix: make Android SDK env vars conditional on abidir
ANDROID_SDK_ROOT, ANDROID_NDK_HOME, and androidManifest are only
needed for Android builds (when abidir is set). Making them
unconditional causes evaluation failures on aarch64-darwin since
the Android SDK overlay doesn't support that platform.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 13:12:24 +05:30

146 lines
4.5 KiB
Nix

{
config ? {},
pkgs ? import <nixpkgs> { },
src ? ../.,
targets ? ["libwaku-android-arm64"],
verbosity ? 2,
useSystemNim ? true,
quickAndDirty ? true,
stableSystems ? [
"x86_64-linux" "aarch64-linux"
],
abidir ? null,
zerokitRln,
}:
assert pkgs.lib.assertMsg ((src.submodules or true) == true)
"Unable to build without submodules. Append '?submodules=1#' to the URI.";
let
inherit (pkgs) stdenv lib writeScriptBin callPackage;
revision = lib.substring 0 8 (src.rev or "dirty");
androidManifest = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.example.mylibrary\" />";
in stdenv.mkDerivation (rec {
pname = "logos-messaging-nim";
version = "1.0.0-${revision}";
inherit src;
buildInputs = with pkgs; [
openssl
gmp
zip
];
# Dependencies that should only exist in the build environment.
nativeBuildInputs = let
# Fix for Nim compiler calling 'git rev-parse' and 'lsb_release'.
fakeGit = writeScriptBin "git" "echo ${version}";
# Fix for the zerokit package that is built with cargo/rustup/cross.
fakeCargo = writeScriptBin "cargo" "echo ${version}";
# Fix for the zerokit package that is built with cargo/rustup/cross.
fakeRustup = writeScriptBin "rustup" "echo ${version}";
# Fix for the zerokit package that is built with cargo/rustup/cross.
fakeCross = writeScriptBin "cross" "echo ${version}";
in
with pkgs; [
cmake
which
] ++ lib.optionals stdenv.hostPlatform.isLinux [
lsb-release
] ++ lib.optionals stdenv.hostPlatform.isDarwin [
# libtool is needed by libnatpmp to create static archives on macOS
pkgs.darwin.cctools
] ++ [
zerokitRln
nim-unwrapped-2_0
fakeGit
fakeCargo
fakeRustup
fakeCross
];
NIMFLAGS = "-d:disableMarchNative -d:git_revision_override=${revision}";
XDG_CACHE_HOME = "/tmp";
makeFlags = targets ++ [
"V=${toString verbosity}"
"QUICK_AND_DIRTY_COMPILER=${if quickAndDirty then "1" else "0"}"
"QUICK_AND_DIRTY_NIMBLE=${if quickAndDirty then "1" else "0"}"
"USE_SYSTEM_NIM=${if useSystemNim then "1" else "0"}"
"LIBRLN_FILE=${zerokitRln}/target/release/librln.a"
] ++ lib.optionals stdenv.hostPlatform.isDarwin [
# nim-libbacktrace's Makefile hardcodes --host=arm which breaks on aarch64-darwin
"USE_LIBBACKTRACE=0"
];
configurePhase = ''
patchShebangs . vendor/nimbus-build-system > /dev/null
# build_nim.sh guards "rm -rf dist/checksums" with NIX_BUILD_TOP != "/build",
# but on macOS the nix sandbox uses /private/tmp/... so the check fails and
# dist/checksums (provided via preBuild) gets deleted. Fix the check to skip
# the removal whenever NIX_BUILD_TOP is set (i.e. any nix build).
substituteInPlace vendor/nimbus-build-system/scripts/build_nim.sh \
--replace 'if [[ "''${NIX_BUILD_TOP}" != "/build" ]]; then' \
'if [[ -z "''${NIX_BUILD_TOP}" ]]; then'
make nimbus-build-system-paths
make nimbus-build-system-nimble-dir
'';
# For the Nim v2.2.4 built with NBS we added sat and zippy
preBuild = ''
ln -s waku.nimble waku.nims
${lib.optionalString (!useSystemNim) ''
pushd vendor/nimbus-build-system/vendor/Nim
mkdir dist
mkdir -p dist/nimble/vendor/sat
mkdir -p dist/nimble/vendor/checksums
mkdir -p dist/nimble/vendor/zippy
cp -r ${callPackage ./nimble.nix {}}/. dist/nimble
cp -r ${callPackage ./checksums.nix {}}/. dist/checksums
cp -r ${callPackage ./csources.nix {}}/. csources_v2
cp -r ${callPackage ./sat.nix {}}/. dist/nimble/vendor/sat
cp -r ${callPackage ./checksums.nix {}}/. dist/nimble/vendor/checksums
cp -r ${callPackage ./zippy.nix {}}/. dist/nimble/vendor/zippy
chmod -R u+w dist/ csources_v2
popd
''}
'';
installPhase = if abidir != null then ''
mkdir -p $out/jni
cp -r ./build/android/${abidir}/* $out/jni/
echo '${androidManifest}' > $out/jni/AndroidManifest.xml
cd $out && zip -r libwaku.aar *
'' else ''
mkdir -p $out/bin $out/include
# Copy library files
cp build/* $out/bin/ 2>/dev/null || true
# Copy the header file
cp library/libwaku.h $out/include/
'';
meta = with pkgs.lib; {
description = "NWaku derivation to build libwaku for mobile targets using Android NDK and Rust.";
homepage = "https://github.com/status-im/nwaku";
license = licenses.mit;
platforms = stableSystems;
};
} // lib.optionalAttrs (abidir != null) {
ANDROID_SDK_ROOT = "${pkgs.androidPkgs.sdk}";
ANDROID_NDK_HOME = "${pkgs.androidPkgs.ndk}";
})