dart.flutter_webrtc: init

this migrates what was already done in the fluffychat derivation, and
once commet-chat updates to 0.5.0 and thus to flutter_webrtc >= 1.5,
(which is underway in #566626), it can also apply to it.

i also threw together a small, hacky script for easily fetching new
hashes of libwebrtc.
This commit is contained in:
blokyk
2026-10-17 21:51:35 +02:00
parent 00045e9c8e
commit d034dd2cb1
6 changed files with 174 additions and 85 deletions

View File

@@ -88,6 +88,8 @@ flutter341.buildFlutterApplication (finalAttrs: {
targetFlutterPlatform = "linux";
customSourceBuilders = {
# fixme: once commet-chat updates to flutter_webrtc >= 1.5 (in the next releases),
# this can be removed, tho you'll need to update the libwebrtc-info.json file
flutter_webrtc =
{ version, src, ... }:
stdenv.mkDerivation {

View File

@@ -2,12 +2,8 @@
lib,
stdenv,
fetchFromGitHub,
fetchzip,
imagemagick,
libgbm,
libdrm,
flutter344,
pulseaudio,
imagemagick,
webkitgtk_4_1,
copyDesktopItems,
makeDesktopItem,
@@ -17,24 +13,7 @@
}:
let
libwebrtcRpath = lib.makeLibraryPath [
libgbm
libdrm
];
pubspecLock = lib.importJSON ./pubspec.lock.json;
libwebrtc_version = "m144.7559.09";
libwebrtc =
{
x86_64-linux = fetchzip {
url = "https://github.com/webrtc-sdk/libwebrtc/releases/download/libwebrtc.${libwebrtc_version}/libwebrtc-linux-x64-release.zip";
sha256 = "sha256-uzS07voCGM1zs663UalYpb8pWiYpkrKMxKt/wB4rcB4=";
};
aarch64-linux = fetchzip {
url = "https://github.com/webrtc-sdk/libwebrtc/releases/download/libwebrtc.${libwebrtc_version}/libwebrtc-linux-arm64-release.zip";
sha256 = "sha256-nMMU/HrCN4zSB4vO1O4TfJRBtK87OX+zYbxZRq8Q4Us=";
};
}
.${stdenv.hostPlatform.system};
in
flutter344.buildFlutterApplication (
rec {
@@ -74,9 +53,6 @@ flutter344.buildFlutterApplication (
webkitgtk_4_1
];
runtimeDependencies = [ pulseaudio ];
env.NIX_LDFLAGS = "-rpath-link ${libwebrtcRpath}";
env.CXXFLAGS = "-include cstdint";
desktopItems = [
@@ -95,64 +71,6 @@ flutter344.buildFlutterApplication (
})
];
customSourceBuilders = {
flutter_webrtc =
{ version, src, ... }:
stdenv.mkDerivation {
pname = "flutter_webrtc";
inherit version src;
inherit (src) passthru;
postPatch = ''
# Since we directly supply the binary distribution of libwebrtc instead of letting the builder download it we need to check that we still provide the correct version.
# nixpkgs forbids import from derivation, so we cannot simply read the contents to fetch the correct version
if ! grep -q "binary_version = libwebrtc.${libwebrtc_version}" third_party/libwebrtc_version.ini; then
echo -en "\nWrong libwebrtc version is in use!\n\t'libwebrtc.${libwebrtc_version}' was supplied, but cannot be found in 'third_party/libwebrtc_version.ini'.\nflutter_webrtc expects the following version:\n\t"
grep "binary_version" third_party/libwebrtc_version.ini; echo
false # triggers the build failure
fi
substituteInPlace third_party/CMakeLists.txt \
--replace-fail "\''${LIBWEBRTC_THIRD_PARTY_DIR}/downloads/\''${LIBWEBRTC_ASSET}.zip" ${libwebrtc}
ln -s ${libwebrtc} third_party/libwebrtc
'';
installPhase = ''
runHook preInstall
mkdir $out
cp -r ./* $out/
runHook postInstall
'';
};
}
// lib.optionalAttrs (stdenv.hostPlatform.system == "aarch64-linux") {
native_toolchain_cmake =
{ version, src, ... }:
stdenv.mkDerivation {
pname = "native_toolchain_cmake";
inherit version src;
inherit (src) passthru;
postPatch = ''
substituteInPlace cmake/aarch64-linux-gnu.toolchain.cmake \
--replace-fail "aarch64-linux-gnu-gcc" "gcc"
substituteInPlace cmake/aarch64-linux-gnu.toolchain.cmake \
--replace-fail "aarch64-linux-gnu-g++" "g++"
'';
installPhase = ''
runHook preInstall
mkdir $out
cp -r ./* $out/
runHook postInstall
'';
};
};
postPatch = ''
substituteInPlace linux/CMakeLists.txt \
--replace-fail \
@@ -165,8 +83,6 @@ flutter344.buildFlutterApplication (
mkdir -p $ICO/hicolor/scalable/apps
cp $src/assets/logo/vector/logo_standalone.svg $ICO/hicolor/scalable/apps/fluffychat.png
patchelf --add-rpath ${libwebrtcRpath} $out/app/fluffychat-linux/lib/libwebrtc.so
'';
}
// lib.optionalAttrs (targetFlutterPlatform == "web") {

View File

@@ -6,6 +6,7 @@
flutter_secure_storage_linux = callPackage ./flutter-secure-storage-linux { };
flutter_vodozemac = callPackage ./flutter_vodozemac { };
flutter_volume_controller = callPackage ./flutter_volume_controller { };
flutter_webrtc = callPackage ./flutter_webrtc { };
fvp = callPackage ./fvp { };
handy_window = callPackage ./handy-window { };
hotkey_manager_linux = callPackage ./hotkey_manager_linux { };

View File

@@ -0,0 +1,97 @@
{
applyPatches,
fetchzip,
lib,
libdrm,
libgbm,
pulseaudio,
stdenvNoCC,
}:
{ version, src, ... }:
let
# if nixpkgs allowed IFD, we could just read the necessary libwebrtc directly
# from flutter-webrtc's sources, but unfortunately we can't... so we manually
# record that info (libwebrtc version but also hashes of each) into one JSON file
libwebrtc-info = lib.importJSON ./libwebrtc-info.json;
# note: we can't use pkgs.livekit-libwebrtc because it might not be
# the version wanted by this flutter_webrtc version, so we
# instead have to download the vendor-compiled versions. sucks :(
libwebrtc =
let
info =
libwebrtc-info.${version} or (throw ''
Unsupported version of pub package 'flutter_webrtc': ${version}
Please add the corresponding libwebrtc version and hashes to:
pkgs/development/compilers/dart/package-source-builders/flutter_webrtc/libwebrtc-info.json
To do it semi-automatically, you can use the adjacent 'fetch-webrtc-version.sh' script.
You can use like this, to fetch the new version's info and then update the libwebrtc-info file:
jq -s add ./libwebrtc-info.json <(./fetch-webrtc-version.sh '${version}') | sponge ./libwebrtc-info.json
'');
# note: if you add a new platform here, you'll need to add it to the list in fetch-webrtc-version
# (and also re-gen the JSON file to support it) ((and update meta.platforms))
arch =
{
x86_64-linux = "linux-x64";
aarch64-linux = "linux-arm64";
}
.${stdenvNoCC.hostPlatform.system} or (throw ''
Package 'flutter_webrtc' is not supported on ${stdenvNoCC.hostPlatform} platforms.
'');
url = "https://github.com/webrtc-sdk/libwebrtc/releases/download/${info.version}/libwebrtc-${arch}-release.zip";
hash = info.hashes.${arch};
in
applyPatches {
pname = "libwebrtc";
inherit version;
src = fetchzip {
inherit version url hash;
meta = {
platforms = lib.intersectLists lib.platforms.linux (lib.platforms.x86_64 ++ lib.platforms.aarch64);
sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
};
};
postPatch = ''
patchelf lib/libwebrtc.so --add-rpath ${
lib.makeLibraryPath [
libdrm
libgbm
]
}
'';
};
in
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "flutter_webrtc";
inherit version src;
passthru = src.passthru // {
inherit libwebrtc;
};
propagatedBuildInputs = [ pulseaudio.dev ];
postPatch = ''
substituteInPlace third_party/CMakeLists.txt \
--replace-fail "\''${LIBWEBRTC_THIRD_PARTY_DIR}/downloads/\''${LIBWEBRTC_ASSET}.zip" ${finalAttrs.passthru.libwebrtc}
ln -s ${finalAttrs.passthru.libwebrtc} third_party/libwebrtc
'';
installPhase = ''
runHook preInstall
cp -r ./ -T "$out/"
runHook postInstall
'';
# we only patch the linux cmake (and currently libwebrtc is only vendored for linux anyway)
meta.platforms = lib.platforms.linux;
})

View File

@@ -0,0 +1,64 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p yq-go curl
set +e
if ! [[ -v 1 ]] || [[ "$1" = "-h" ]] || [[ "$1" = "--help" ]]; then
echo "usage: fetch-webrtc-version.sh <version>"
echo ' ~-------~'
echo ' this should be the *flutter_webrtc* version'
exit 1
fi
main() {
# remove the 'v' from the version string if it's there, so v1.5.0 -> 1.5.0 but 1.6.2 -> 1.6.2
local flutter_webrtc_version="${1#v}"
local webrtc_ver
webrtc_ver="$(get_libwebrtc_version "$flutter_webrtc_version")"
echo "flutter_webrtc v$flutter_webrtc_version uses $webrtc_ver" >&2
cat <<EOF
{
"$flutter_webrtc_version": {
"version": "$webrtc_ver",
"hashes": $(get_all_libwebrtc_hashes "$webrtc_ver")
}
}
EOF
}
# args: <libwebrtc-version>
get_all_libwebrtc_hashes() {
local version="$1"
cat <<EOF
{
"linux-x64": "sha256-$(get_libwebrtc_hash "linux-x64" "$version")",
"linux-arm64": "sha256-$(get_libwebrtc_hash "linux-arm64" "$version")"
}
EOF
}
# args: <flutter_webrtc_version>
get_libwebrtc_version() {
local version="$1"
local url="https://raw.githubusercontent.com/flutter-webrtc/flutter-webrtc/refs/tags/v${version}/third_party/libwebrtc_version.ini"
curl -L "$url" | yq --input-format ini '.libwebrtc.binary_version'
}
# args: <platform> <libwebrtc-version>
get_libwebrtc_hash() {
local platform="$1"
local version="$2"
local url="https://github.com/webrtc-sdk/libwebrtc/releases/download/${version}/libwebrtc-${platform}-release.zip"
local hash
hash=$(nix-prefetch-url --unpack --name "$(basename "$url")" "$url")
nix-hash --to-base64 --type sha256 "$hash"
}
main "$@"

View File

@@ -0,0 +1,9 @@
{
"1.5.2": {
"version": "libwebrtc.m144.7559.09",
"hashes": {
"linux-x64": "sha256-uzS07voCGM1zs663UalYpb8pWiYpkrKMxKt/wB4rcB4=",
"linux-arm64": "sha256-nMMU/HrCN4zSB4vO1O4TfJRBtK87OX+zYbxZRq8Q4Us="
}
}
}