Files
logos-messaging-module/flake.nix

72 lines
3.2 KiB
Nix

{
description = "Logos Delivery Module";
inputs = {
logos-module-builder.url = "github:logos-co/logos-module-builder";
nix-bundle-lgx.url = "github:logos-co/nix-bundle-lgx";
logos-delivery.url = "git+https://github.com/logos-messaging/logos-delivery?submodules=1";
# Pin to the same zerokit logos-delivery uses so librln.dylib versions match
zerokit.follows = "logos-delivery/zerokit";
};
outputs = inputs@{ logos-module-builder, ... }:
logos-module-builder.lib.mkLogosModule {
src = ./.;
configFile = ./metadata.json;
flakeInputs = inputs;
externalLibInputs = {
logosdelivery = {
input = inputs.logos-delivery;
packages.default = "liblogosdelivery";
};
# Bundle librln.dylib alongside liblogosdelivery.dylib so the transitive
# dep resolves at runtime (and during logos-cpp-generator dlopen).
rln = {
input = inputs.zerokit;
packages.default = "rln";
};
};
postInstall = ''
# liblogosdelivery.dylib has a sandbox-baked absolute path for librln.dylib
# (Cargo bakes the build-time path as the install name). Rewrite it to
# @rpath/librln.dylib so the dynamic linker finds it via @loader_path.
if [ -f "$out/lib/liblogosdelivery.dylib" ]; then
OLD_RLN=$(otool -L "$out/lib/liblogosdelivery.dylib" | awk '/librln/{print $1}')
if [ -n "$OLD_RLN" ]; then
echo "Fixing librln rpath in liblogosdelivery.dylib: $OLD_RLN -> @rpath/librln.dylib"
install_name_tool -change "$OLD_RLN" "@rpath/librln.dylib" \
"$out/lib/liblogosdelivery.dylib"
fi
# Add @loader_path/. as an rpath so that Nim's runtime dlopen("libpq.dylib")
# finds the bundled libpq in the same directory as liblogosdelivery.dylib.
if ! otool -l "$out/lib/liblogosdelivery.dylib" | awk '
$1 == "cmd" && $2 == "LC_RPATH" { in_rpath = 1; next }
in_rpath && $1 == "path" { print $2; in_rpath = 0 }
' | grep -Fxq "@loader_path/."; then
install_name_tool -add_rpath "@loader_path/." \
"$out/lib/liblogosdelivery.dylib"
fi
fi
# Use pkg-config to locate the exact libpq from the build environment
LIBPQ_LIBDIR=$(pkg-config --variable=libdir libpq 2>/dev/null || true)
if [ -n "$LIBPQ_LIBDIR" ] && [ -d "$LIBPQ_LIBDIR" ]; then
for f in "$LIBPQ_LIBDIR"/libpq.*; do
[ -f "$f" ] && cp -L "$f" $out/lib/ 2>/dev/null || true
done
fi
# libpq is loaded at runtime via dlopen/dlsym (not a linked dependency),
# so install_name_tool has no effect on macOS — otool -L won't show libpq.
# On Linux, dlopen with a bare name searches the calling library's DT_RUNPATH,
# so setting $ORIGIN makes libpq.so discoverable from the same directory.
if [ -f "$out/lib/liblogosdelivery.so" ]; then
echo "Fixing rpath in liblogosdelivery.so: adding \$ORIGIN for dlopen libpq resolution"
chmod u+w "$out/lib/liblogosdelivery.so"
patchelf --set-rpath '$ORIGIN' "$out/lib/liblogosdelivery.so"
fi
'';
};
}