diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 21ff23c..483b3ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,3 +50,21 @@ jobs: key: ${{ runner.os }}-nbs-${{ steps.nbs.outputs.hash }} - run: make update - run: make tests + + nix-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + with: + submodules: recursive + - uses: cachix/install-nix-action@v27 + with: + nix_path: nixpkgs=channel:nixos-unstable + extra_nix_config: | + experimental-features = nix-command flakes + - run: nix build ".?submodules=1#" --print-build-logs + - name: Verify outputs + run: | + test -f result/lib/liblogoschat.so + test -f result/include/liblogoschat.h + diff --git a/Makefile b/Makefile index db259d3..55ed376 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,13 @@ clean: # must be included after the default target -include $(BUILD_SYSTEM_DIR)/makefiles/targets.mk +### Create nimble links (used when building with Nix) +.PHONY: nimbus-build-system-nimble-dir +nimbus-build-system-nimble-dir: + NIMBLE_DIR="$(CURDIR)/$(NIMBLE_DIR)" \ + PWD_CMD="$(PWD)" \ + $(CURDIR)/vendor/nwaku/scripts/generate_nimble_links.sh + ## Possible values: prod; debug TARGET ?= prod @@ -131,6 +138,12 @@ liblogoschat: | build-waku-librln build-waku-nat build-libchat logos_chat.nims echo -e "\n\x1B[92mLibrary built successfully:\x1B[39m" && \ echo " $(shell pwd)/$(LIBLOGOSCHAT)" +### Nix-only: dependencies are pre-built Nix derivations, not built here +.PHONY: liblogoschat-nix +liblogoschat-nix: | logos_chat.nims + echo -e $(BUILD_MSG) "$(LIBLOGOSCHAT)" && \ + $(ENV_SCRIPT) nim liblogoschat $(NIM_PARAMS) --path:src logos_chat.nims + endif diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b26615a --- /dev/null +++ b/flake.lock @@ -0,0 +1,82 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1771369470, + "narHash": "sha256-0NBlEBKkN3lufyvFegY4TYv5mCNHbi5OmBDrzihbBMQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0182a361324364ae3f436a63005877674cf45efb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1771642886, + "narHash": "sha256-4zOvSi0WkS2WAaoJtM28wECtS9S+L38CPYbhF+wINDA=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "85078369717bdbe1f266c9eaad5e66956fb6feea", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..160f089 --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "logos-chat shared library"; + + nixConfig = { + extra-substituters = [ "https://nix-cache.status.im/" ]; + extra-trusted-public-keys = [ "nix-cache.status.im-1:x/93lOfLU+duPplwMSBR+OlY4+mo+dCN7n0mr4oPwgY=" ]; + }; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, rust-overlay, flake-utils }: + flake-utils.lib.eachSystem [ + "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" + ] (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ rust-overlay.overlays.default ]; + }; + libchatDrv = pkgs.callPackage ./nix/libchat.nix {}; + librln = pkgs.callPackage ./nix/librln.nix {}; + in { + packages.default = pkgs.callPackage ./nix/default.nix { + src = self; + inherit libchatDrv librln; + }; + devShells.default = pkgs.callPackage ./nix/shell.nix { + inherit libchatDrv; + }; + } + ); +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..0e8b1cb --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,93 @@ +{ lib, stdenv, nim, which, pkg-config, writeScriptBin, + openssl, miniupnpc, libnatpmp, darwin ? null, + src, # logos-chat source (self from flake, with submodules=1) + libchatDrv, # result of libchat.nix + librln }: # result of librln.nix + +# NOTE: this build requires git submodules to be present in src. +# When fetching from GitHub use '?submodules=1#', e.g.: +# nix build "github:logos-messaging/logos-chat?submodules=1#" +# For local builds use: nix build ".?submodules=1#" + +assert lib.assertMsg ((src.submodules or false) == true) + "Unable to build without submodules. Append '?submodules=1#' to the URI."; + +let + revision = lib.substring 0 8 (src.rev or "dirty"); + libExt = if stdenv.isDarwin then "dylib" else "so"; + libchatPath = "${libchatDrv}/lib/liblogos_chat.${libExt}"; +in stdenv.mkDerivation { + pname = "liblogoschat"; + version = "0.1.0"; + inherit src; + + NIMFLAGS = lib.concatStringsSep " " [ + "--passL:vendor/nwaku/librln_v0.7.0.a" + "--passL:-lm" + "-d:miniupnpcUseSystemLibs" + "-d:libnatpmpUseSystemLibs" + "--passL:-lminiupnpc" + "--passL:-lnatpmp" + "-d:CONVERSATIONS_LIB:${libchatPath}" + "-d:git_version=${revision}" + ]; + + nativeBuildInputs = let + fakeGit = writeScriptBin "git" '' + #!${stdenv.shell} + echo "${revision}" + ''; + in [ nim which pkg-config fakeGit ]; + + # Nim defaults to $HOME/.cache/nim; the sandbox maps $HOME to /homeless-shelter + # which doesn't exist. Point XDG_CACHE_HOME at /tmp so Nim writes its cache there. + XDG_CACHE_HOME = "/tmp"; + + buildInputs = [ openssl miniupnpc libnatpmp ] + ++ lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [ + Security + CoreFoundation + SystemConfiguration + ]); + + configurePhase = '' + runHook preConfigure + patchShebangs . vendor/nimbus-build-system > /dev/null 2>&1 || true + # Create logos_chat.nims symlink (if not already a real file) + if [ ! -e logos_chat.nims ]; then + ln -sf logos_chat.nimble logos_chat.nims + fi + # Regenerate nimble-link files with sandbox-correct absolute paths. + # vendor/.nimble/pkgs contains paths baked in at `nimble develop` time on a + # developer's machine; they won't resolve inside the Nix sandbox. Running + # the same generate_nimble_links.sh that logos-delivery uses re-creates them + # from the current $PWD without requiring git. + make nimbus-build-system-nimble-dir + runHook postConfigure + ''; + + preBuild = '' + # Place pre-built librln where the Nim linker expects it + cp ${librln}/lib/librln_v0.7.0.a vendor/nwaku/librln_v0.7.0.a + mkdir -p build + ''; + + makeFlags = [ "liblogoschat-nix" ]; + + installPhase = '' + runHook preInstall + mkdir -p $out/lib $out/include + cp build/liblogoschat.so $out/lib/ 2>/dev/null || true + cp build/liblogoschat.dylib $out/lib/ 2>/dev/null || true + ls $out/lib/liblogoschat.* > /dev/null + cp library/liblogoschat.h $out/include/ + runHook postInstall + ''; + + meta = with lib; { + description = "Logos-Chat shared library (C FFI)"; + homepage = "https://github.com/logos-messaging/logos-chat"; + license = licenses.mit; + platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + }; +} diff --git a/nix/libchat.nix b/nix/libchat.nix new file mode 100644 index 0000000..1d71ef8 --- /dev/null +++ b/nix/libchat.nix @@ -0,0 +1,41 @@ +{ lib, stdenv, rust-bin, makeRustPlatform, perl, pkg-config, cmake, + darwin ? null }: + +let + rustToolchain = rust-bin.fromRustupToolchainFile ../vendor/libchat/rust_toolchain.toml; + rPlatform = makeRustPlatform { + cargo = rustToolchain; + rustc = rustToolchain; + }; +in rPlatform.buildRustPackage { + pname = "liblogos-chat"; + version = "0.1.0"; + src = ../vendor/libchat; + + cargoLock = { + lockFile = ../vendor/libchat/Cargo.lock; + outputHashes = { + "chat-proto-0.1.0" = "sha256-aCl80VOIkd/GK3gnmRuFoSAvPBfeE/FKCaNlLt5AbUU="; + }; + }; + + nativeBuildInputs = [ perl pkg-config cmake ]; + buildInputs = lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [ + Security + SystemConfiguration + ]); + doCheck = false; + + installPhase = '' + runHook preInstall + mkdir -p $out/lib + cp target/*/release/liblogos_chat.so $out/lib/ 2>/dev/null || true + cp target/*/release/liblogos_chat.dylib $out/lib/ 2>/dev/null || true + cp target/*/release/liblogos_chat.a $out/lib/ 2>/dev/null || true + # fallback: non-cross build path + cp target/release/liblogos_chat.so $out/lib/ 2>/dev/null || true + cp target/release/liblogos_chat.dylib $out/lib/ 2>/dev/null || true + cp target/release/liblogos_chat.a $out/lib/ 2>/dev/null || true + runHook postInstall + ''; +} diff --git a/nix/librln.nix b/nix/librln.nix new file mode 100644 index 0000000..21923ee --- /dev/null +++ b/nix/librln.nix @@ -0,0 +1,31 @@ +{ lib, stdenv, fetchurl }: + +let + version = "v0.7.0"; + triplets = { + "x86_64-linux" = "x86_64-unknown-linux-gnu"; + "aarch64-linux" = "aarch64-unknown-linux-gnu"; + "x86_64-darwin" = "x86_64-apple-darwin"; + "aarch64-darwin" = "aarch64-apple-darwin"; + }; + hashes = { + "x86_64-linux" = "sha256-FVXW7HHbmxxp6vy7Ji5iy0Y483P9JJXUnkyE84j3gZk="; + "aarch64-linux" = "sha256-E5dir4E/UT0XiaKJxnEXRz9pIGRwWHr5fHkRPYN6T80="; + "x86_64-darwin" = "sha256-FyuXn7hlecZMDhghE6CknVWNN9JMOADzVUVIUxknO78="; + "aarch64-darwin" = "sha256-8buoDdGtPDtrVQh7oIpmd9OYqTkS5byGgjo7zhNLn84="; + }; + triplet = triplets.${stdenv.hostPlatform.system}; + tarball = fetchurl { + url = "https://github.com/vacp2p/zerokit/releases/download/${version}/${triplet}-arkzkey-rln.tar.gz"; + hash = hashes.${stdenv.hostPlatform.system}; + }; +in stdenv.mkDerivation { + pname = "librln"; + inherit version; + src = tarball; + unpackPhase = "tar -xzf $src"; + installPhase = '' + mkdir -p $out/lib + cp release/librln.a $out/lib/librln_${version}.a + ''; +} diff --git a/nix/shell.nix b/nix/shell.nix new file mode 100644 index 0000000..97c9f27 --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,21 @@ +{ pkgs, lib, stdenv, libchatDrv }: + +let + libExt = if stdenv.isDarwin then "dylib" else "so"; + darwinDeps = lib.optionals stdenv.isDarwin (with pkgs.darwin.apple_sdk.frameworks; [ + Security CoreFoundation SystemConfiguration + ]); +in pkgs.mkShell { + buildInputs = with pkgs; [ + nim cargo rustc clippy rustfmt + git cmake gnumake which pkg-config + openssl miniupnpc libnatpmp + ] ++ darwinDeps; + + shellHook = '' + export CONVERSATIONS_LIB="${libchatDrv}/lib/liblogos_chat.${libExt}" + echo "logos-chat dev shell. CONVERSATIONS_LIB=$CONVERSATIONS_LIB" + echo "Build: make liblogoschat" + echo "Nix build: nix build '.?submodules=1#'" + ''; +}