Skip to content

[ros2] zenoh-cpp-vendor#558

Closed
muellerbernd wants to merge 20 commits intolopsided98:developfrom
muellerbernd:fix-zenoh
Closed

[ros2] zenoh-cpp-vendor#558
muellerbernd wants to merge 20 commits intolopsided98:developfrom
muellerbernd:fix-zenoh

Conversation

@muellerbernd
Copy link
Copy Markdown
Contributor

I have tested the somewhat new middleware rmw_zenoh in a local workspace. Now I try to get the corresponding nix packages running.
When try to build them:

nix build .\#jazzy.zenoh-cpp-vendor --accept-flake-config

Colcon gets invoked, then colcon executes cargo and cargo cannot download stuff because it is sandboxed. According to the nixpkgs manual we would have to set the cargoDeps.
I saw that in #501 was also done some work to build rust nodes.

@muellerbernd
Copy link
Copy Markdown
Contributor Author

Zenoh is now part of nixpkgs see here, maybe we can use that here. I will test this in the next days.

@muellerbernd
Copy link
Copy Markdown
Contributor Author

muellerbernd commented Feb 15, 2025

now zenoh-cpp-vendor is working but zenoh-bridge-dds does not compile completely and crashes with :

"enum_(unnamed_at_/nix/store/6aci60gk5wj4bjj1rygzbkc6ximmsm17-glibc-2_40-66-dev/include/bits/confname_h_24_1)" is not a valid Ident

@muellerbernd
Copy link
Copy Markdown
Contributor Author

muellerbernd commented Feb 18, 2025

update on zenoh-bridge-dds. If I check out version 1.2.1 from github it also builds, so I think a new release will fix that also.
zenoh-cpp-vendor and rmw-zenoh-cpp are compiling and work as expected in my test workspace.

@muellerbernd muellerbernd marked this pull request as ready for review February 18, 2025 07:46
@muellerbernd muellerbernd changed the title [jazzy] zenoh-cpp-vendor WIP [jazzy] zenoh-cpp-vendor Feb 19, 2025
@muellerbernd muellerbernd changed the title [jazzy] zenoh-cpp-vendor [ros2] zenoh-cpp-vendor Feb 20, 2025
@Cakem1x
Copy link
Copy Markdown

Cakem1x commented Feb 21, 2025

Yay, thanks for your work @muellerbernd!

I was just trying to use zenoh as RMW in my devshell (via pkg rmw-zenoh-cpp, on distro jazzy) and ran into the following error using the develop branch:

error: builder for '/nix/store/gkazl5k3qhi1slrz6g83grk0ai4nh7p1-ros-jazzy-zenoh-cpp-vendor-0.2.1-r1.drv' failed with exit code 2;
[...]
       > [  6%] Creating directories for 'zenoh_c_vendor'
       > [ 12%] Performing download step for 'zenoh_c_vendor'
       > === ./zenoh_c_vendor (git) ===
       > Could not determine ref type of version: fatal: unable to access 'https://github.com/eclipse-zenoh/zenoh-c.git/': Could not resolve host: github.com

With your branch, I can successfully use rmw zenoh.

@Pleune
Copy link
Copy Markdown

Pleune commented Feb 27, 2025

Here is my overlay to use zenoh, but I have to use a slightly out of date version from before the switch to zenoh-cpp

    rmw-zenoh-cpp = rosFinal.buildRosPackage {
      pname = "ros-${ros}-rmw-zenoh-cpp";
      inherit version;
      src = ./vendored/rmw_zenoh/rmw_zenoh_cpp;
      # Dep added directly, dont need vendor
      postPatch = ''
        sed -i '/zenoh_c_vendor/d' CMakeLists.txt
      '';
      buildType = "ament_cmake";
      nativeBuildInputs = with rosFinal; [ ament-cmake ];
      propagatedBuildInputs =
        with final;
        with rosFinal;
        with rosFinal.python3.pkgs;
        [
          zenoh-c
          rosidl-typesupport-fastrtps-c
          rosidl-typesupport-fastrtps-cpp
          rcutils
          rcpputils
          fastcdr
          ament-index-cpp
          rmw
        ];
    };

  zenoh-c = prev.stdenv.mkDerivation rec {
    name = "zenoh-c";
    pname = "zenoh-c";
    src = prev.fetchFromGitHub rec {
      name = "zenoh-c-${rev}"; # invalidate build if rev changes
      owner = "eclipse-zenoh";
      repo = "zenoh-c";
      # parse exact git commit from the zenoh_c_vendor package
      rev = builtins.elemAt (builtins.match ".*VCS_VERSION ([a-z0-9]+).*" (
        builtins.readFile ./vendored/rmw_zenoh/zenoh_c_vendor/CMakeLists.txt
      )) 0;
      hash = "sha256-4MAytgYjBqpNeeTEFRTzT3deIg4YFVE+YJnzFEJPpCE=";
    };
    cargoDeps = prev.rustPlatform.importCargoLock {
      lockFile = "${src}/Cargo.lock";
      outputHashes = {
        "zenoh-0.11.0-dev" = "sha256-UtOuH5CwOKj8ZSxnAwVlFfe5RrejAp87SN9xlkwam90=";
      };
    };
    nativeBuildInputs = with prev; [
      cmake
      rustPlatform.cargoSetupHook
      cargo
      rustc
    ];
    # Remove path building, as nix is already absolute paths
    postPatch = ''
      sed -i -e '/^preifx=.*/d' -e 's|[$]{prefix}/||' install/zenohc.pc.in
      sed -i -e 's|[$]{_IMPORT_PREFIX}/||' install/PackageConfig.cmake.in
    '';
  };

I hope it helps!

Edit: I specifically have commit 1607d834dba7fbbcd19cd2ea81a040c66b7dbc5e checked out from rmw_zenoh

@Pleune
Copy link
Copy Markdown

Pleune commented Feb 27, 2025

I have tried to update to newer versions of rmw_zenoh, but when building zenoh-cpp I get errors like below, even though I am building from the exact commits that are referenced in rmw_zenoh's vendors / zenoh-cpp's submodules.

error: builder for '/nix/store/dqp7haqy2vkg8dykdq90580v7wi1wnx8-ros-jazzy-rmw-zenoh-cpp-0.1.0.drv' failed with exit code 2;
       last 25 log lines:
       >   142 |   std::optional<zenoh::ext::AdvancedSubscriber<void>> sub_;
       >       |                             ^~~~~~~~~~~~~~~~~~
       > /build/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp:142:52: error: template argument 1 is invalid
       >   142 |   std::optional<zenoh::ext::AdvancedSubscriber<void>> sub_;
       >       |                                                    ^~
       > /build/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp:57:24: error: 'ShmProvider' is not a member of 'zenoh'
       >    57 |   std::optional<zenoh::ShmProvider> & shm_provider();
       >       |                        ^~~~~~~~~~~
       > /build/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp:57:35: error: template argument 1 is invalid
       >    57 |   std::optional<zenoh::ShmProvider> & shm_provider();
       >       |                                   ^
       > /build/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp:57:24: error: 'ShmProvider' is not a member of 'zenoh'
       >    57 |   std::optional<zenoh::ShmProvider> & shm_provider();
       >       |                        ^~~~~~~~~~~
       > /build/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp:57:35: error: template argument 1 is invalid
       >    57 |   std::optional<zenoh::ShmProvider> & shm_provider();
       >       |                                   ^
       > make[2]: *** [CMakeFiles/rmw_zenoh_cpp.dir/build.make:272: CMakeFiles/rmw_zenoh_cpp.dir/src/detail/rmw_service_data.cpp.o] Error 1
       > make[2]: *** [CMakeFiles/rmw_zenoh_cpp.dir/build.make:258: CMakeFiles/rmw_zenoh_cpp.dir/src/detail/rmw_node_data.cpp.o] Error 1
       > make[2]: *** [CMakeFiles/rmw_zenoh_cpp.dir/build.make:454: CMakeFiles/rmw_zenoh_cpp.dir/src/rmw_get_topic_names_and_types.cpp.o] Error 1
       > make[2]: *** [CMakeFiles/rmw_zenoh_cpp.dir/build.make:482: CMakeFiles/rmw_zenoh_cpp.dir/src/rmw_init.cpp.o] Error 1
       > [ 97%] Linking CXX executable rmw_zenohd
       > [ 97%] Built target rmw_zenohd
       > make[1]: *** [CMakeFiles/Makefile2:139: CMakeFiles/rmw_zenoh_cpp.dir/all] Error 2
       > make: *** [Makefile:146: all] Error 2
       For full logs, run 'nix log /nix/store/dqp7haqy2vkg8dykdq90580v7wi1wnx8-ros-jazzy-rmw-zenoh-cpp-0.1.0.drv'.

@Pleune
Copy link
Copy Markdown

Pleune commented Feb 27, 2025

Related to my progress getting my overlay to work with updated rmw_zenoh ros2/rmw_zenoh#482

@muellerbernd
Copy link
Copy Markdown
Contributor Author

Related to my progress getting my overlay to work with updated rmw_zenoh ros2/rmw_zenoh#482

I have updated this PR to use updated version of rmw_zenoh

@Pleune
Copy link
Copy Markdown

Pleune commented Feb 27, 2025

Okay, I got this to work even with the newer versions of rmw_zenoh_cpp. Here is a complete overlay:

let
  addRosPackages = ros: final: prev: rosFinal: rosPrev: {
    rmw-zenoh-cpp = rosFinal.buildRosPackage {
      pname = "ros-${ros}-rmw-zenoh-cpp";
      version = "0.2.2";
      src = ./vendored/rmw_zenoh/rmw_zenoh_cpp;
      # Dep added directly, dont need vendor
      postPatch = ''
        substituteInPlace CMakeLists.txt --replace-fail \
          "find_package(zenoh_cpp_vendor REQUIRED)" \
          "find_package(zenohc REQUIRED)
          find_package(zenohcxx REQUIRED)"
      '';
      cmakeArgs = [
        "-DZENOHC_BUILD_WITH_UNSTABLE_API=TRUE"
        "-DZENOHC_CARGO_FLAGS=--features=shared-memory,zenoh/transport_compression,zenoh/transport_tcp,zenoh/transport_tls"
      ];
      buildType = "ament_cmake";
      nativeBuildInputs = with rosFinal; [ ament-cmake ];
      propagatedBuildInputs =
        with final;
        with rosFinal;
        with rosFinal.python3.pkgs;
        [
          ament-index-cpp
          fastcdr
          rcpputils
          rcutils
          rmw
          rosidl-typesupport-fastrtps-c
          rosidl-typesupport-fastrtps-cpp
          tracetools
          zenoh-cpp
          zenoh-c
        ];
    };
  };
in
(final: prev: rec {
  zenoh-c = prev.stdenv.mkDerivation rec {
    name = "${pname}-${src.rev}";
    pname = "zenoh-c";
    src = prev.fetchFromGitHub rec {
      name = "${pname}-${rev}"; # invalidate build if rev changes
      owner = "eclipse-zenoh";
      repo = "zenoh-c";
      # parse exact git commit from the zenoh_c_vendor package
      rev = builtins.elemAt (builtins.match ".*zenoh-c\.git\n[ ]*VCS_VERSION ([a-z0-9]+).*" (builtins.readFile ./vendored/rmw_zenoh/zenoh_cpp_vendor/CMakeLists.txt)) 0;
      hash = "ecf21d45edb870ca2d3e24d38503842081420766";
    };
    cargoDeps = prev.rustPlatform.importCargoLock {
      lockFile = "${src}/Cargo.lock";
      outputHashes = {
        "zenoh-1.2.1" = "sha256-jtslgZxKKIhqNHOwZ38pjOn+zbnJSFnWejMnOQa8lv4";
      };
    };
    cmakeFlags =
      let
        # Parse needed cargo feature flags from source, too
        cargoFlags = builtins.elemAt (builtins.match ".*ZENOHC_CARGO_FLAGS \"(.+)\"\\).*" (builtins.readFile ./vendored/rmw_zenoh/zenoh_cpp_vendor/CMakeLists.txt)) 0;
      in
      [
        "-DZENOHC_BUILD_WITH_UNSTABLE_API=TRUE"
        "-DZENOHC_CARGO_FLAGS=${cargoFlags}"
      ];
    nativeBuildInputs = with prev; [
      cmake
      rustPlatform.cargoSetupHook
      cargo
      rustc
    ];
    # Remove path building, as nix is already absolute paths
    postPatch = ''
      sed -i -e '/^preifx=.*/d' -e 's|[$]{prefix}/||' install/zenohc.pc.in
      sed -i -e 's|[$]{_IMPORT_PREFIX}/||' install/PackageConfig.cmake.in
    '';
  };

  zenoh-cpp = prev.stdenv.mkDerivation rec {
    name = "${pname}-${src.rev}";
    pname = "zenoh-cpp";
    src = prev.fetchFromGitHub rec {
      name = "zenoh-cpp-${rev}"; # invalidate build if rev changes
      owner = "eclipse-zenoh";
      repo = "zenoh-cpp";
      # parse exact git commit from the zenoh_cpp_vendor package
      rev = builtins.elemAt (builtins.match ".*zenoh-cpp\n[ ]*VCS_VERSION ([a-z0-9]+).*" (builtins.readFile ./vendored/rmw_zenoh/zenoh_cpp_vendor/CMakeLists.txt)) 0;
      hash = "sha256-8tuVs3NgUPmFuswFrOF8hZZWaOGRt2nwzoNScIWk9vE=";
    };
    nativeBuildInputs = with prev; [
      cmake
    ];
    buildInputs = [ zenoh-c ];
    postPatch = ''
      sed -i -e '/^preifx=.*/d' -e 's|[$]{prefix}/||' install/zenohcxx.pc.in
      sed -i -e 's|[$]{_IMPORT_PREFIX}/||' install/PackageConfig.cmake.in
    '';
  };

  rosPackages.jazzy = prev.rosPackages.jazzy.overrideScope (addRosPackages "jazzy" final prev);
  rosPackages.rolling = prev.rosPackages.rolling.overrideScope (addRosPackages "rolling" final prev);
})

Sorry, I ran into some issues testing this specifically with jazzy, which is what our team uses, and had to switch rmw_zenoh now to commit ecf21d45edb870ca2d3e24d38503842081420766 (newest jazzy commit as of this morning).

This could be a little hard to bundle into this repo, because there will have to be a set different zenoh-c and zenoh-cpp and related src and outputHashes attrsets (zenoh-* version can change too) updated for each ros2 distro.

Maybe @lopsided98 has some input on the best way to handle this. I think you do the most work in the overlays. But I do think it would be worth fixing the rmw_zenoh functionality in this repo even if it is a little messy, since some usecases are significantly improved by Zenoh over the alternatives.

muellerbernd added a commit to muellerbernd/nix-ros-overlay that referenced this pull request Mar 4, 2025
@muellerbernd muellerbernd marked this pull request as draft April 15, 2025 13:21
@muellerbernd muellerbernd marked this pull request as ready for review April 15, 2025 13:54
@muellerbernd
Copy link
Copy Markdown
Contributor Author

@wentasah @lopsided98 I had to reintroduce the older version of patchAmentVendorGit especially for zenoh-cpp-vendor because zenoh-cpp-vendor has multiple occurrences of amend_vendor and as far as I understand the new patchAmendVendor function does not work for that use case.

@YuanYuYuan
Copy link
Copy Markdown

Hi @muellerbernd! I just came across your work and wanted to express my gratitude for the great contributions you've made. I can confirm that everything works perfectly on my NixOS machine. 😊

wentasah added a commit to wentasah/nix-ros-overlay that referenced this pull request Apr 16, 2025
…t_vendor calls

This is needed at least for zenoh-cpp-vendor, which is being prepared
in lopsided98#558.

The format of vendored-source.json is changed from a single object to
an array of objects. The order of objects corresponds to the order of
ament_vendor calls in the patched CMakeLists.txt.

ament_cmake_vendor_packageConfig.cmake was updated to generate such a
format automatically and patchAmentVendorGit was updated to understand
that format and patch all call sites in the correct order.

The vendored-source.json files are regenerated automatically and there
are no changes except addition of extra brackets.
@wentasah
Copy link
Copy Markdown
Collaborator

@muellerbernd Sorry for breaking your PR. I tried to extend the new patchAmentVendor logic to also work for zenoh-cpp-vendor. Can you try building this PR on top of #620?

When I tried override like this, automatic patching worked:

zenoh-cpp-vendor = lib.patchAmentVendorGit rosSuper.zenoh-cpp-vendor { };

It will be needed to further override the result to patch the other things that you do in this PR. If you need the git hashes, you should be able to read them from zenoh-cpp-vendor/vendored-source.json, however you will need to use numerical indices to choose between zenoh-c and zenoh-cpp. Let me know if you encounter any problems.

@muellerbernd
Copy link
Copy Markdown
Contributor Author

@muellerbernd Sorry for breaking your PR. I tried to extend the new patchAmentVendor logic to also work for zenoh-cpp-vendor. Can you try building this PR on top of #620?

When I tried override like this, automatic patching worked:

zenoh-cpp-vendor = lib.patchAmentVendorGit rosSuper.zenoh-cpp-vendor { };

It will be needed to further override the result to patch the other things that you do in this PR. If you need the git hashes, you should be able to read them from zenoh-cpp-vendor/vendored-source.json, however you will need to use numerical indices to choose between zenoh-c and zenoh-cpp. Let me know if you encounter any problems.

I have tried to get it building in a testing branch but it is still WIP.

@muellerbernd muellerbernd marked this pull request as draft April 17, 2025 07:29
@wentasah
Copy link
Copy Markdown
Collaborator

I'm getting CMake Error: The source directory "/build/rmw_zenoh-release-release-jazzy-zenoh_cpp_vendor-0.2.3-1/build/zenoh_cpp_vendor-prefix/src/zenoh_cpp_vendor" does not appear to contain CMakeLists.txt.

@muellerbernd
Copy link
Copy Markdown
Contributor Author

I'm getting CMake Error: The source directory "/build/rmw_zenoh-release-release-jazzy-zenoh_cpp_vendor-0.2.3-1/build/zenoh_cpp_vendor-prefix/src/zenoh_cpp_vendor" does not appear to contain CMakeLists.txt.

I get the same error. Right now I have no clue what's causing this.

wentasah added a commit to wentasah/nix-ros-overlay that referenced this pull request Apr 19, 2025
…t_vendor calls

This is needed at least for zenoh-cpp-vendor, which is being prepared
in lopsided98#558.

The format of vendored-source.json is changed from a single object to
an array of objects. The order of objects corresponds to the order of
ament_vendor calls in the patched CMakeLists.txt.

ament_cmake_vendor_packageConfig.cmake was updated to generate such a
format automatically and patchAmentVendorGit was updated to understand
that format and patch all call sites in the correct order.

The vendored-source.json files are regenerated automatically and there
are no changes except addition of extra brackets.
@wentasah
Copy link
Copy Markdown
Collaborator

Hmm, it was my fault. I updated #620. With it, your overrides work.

wentasah added a commit to wentasah/nix-ros-overlay that referenced this pull request Jun 4, 2025
…t_vendor calls

This is needed at least for zenoh-cpp-vendor, which is being prepared
in lopsided98#558.

The format of vendored-source.json is changed from a single object to
an array of objects. The order of objects corresponds to the order of
ament_vendor calls in the patched CMakeLists.txt.

ament_cmake_vendor_packageConfig.cmake was updated to generate such a
format automatically and patchAmentVendorGit was updated to understand
that format and patch all call sites in the correct order.

The vendored-source.json files are regenerated automatically and there
are no changes except addition of extra brackets.
wentasah added a commit to wentasah/nix-ros-overlay that referenced this pull request Jun 5, 2025
…t_vendor calls

This is needed at least for zenoh-cpp-vendor, which is being prepared
in lopsided98#558, but it also improves the robustness of the whole patching
machanism.

The format of vendored-source.json is changed from a single object to
object of objects, one for each call to ament_vendor. The key in the
top-level object is the target name specified in the ament_vendor call
in CMakeLists.txt.

The principle of operation is slightly different from the previous
version. Instead of introducing "fake" ament_cmake_vendor_package just
for creation of vendored-source.json, we introduce a "wrapped" version
of ament_cmake_vendor_package, which is used both for creation of
vendored-source.json as well as for compiling the package. When
compiling the package, the wrapped version captures ament_vendor calls
and modifies the arguments to use the prefetched data from the Nix
store instead of downloading them directly.

patchAmentVendorGit was updated to understand the new format and to
invoke CMake with the wrapped package and information from
vendored-source.json.

The vendored-source.json files will be changed to the new format in
the next commit.
@wentasah
Copy link
Copy Markdown
Collaborator

wentasah commented Jul 4, 2025

@muellerbernd I've extracted overrides from your test branch and added them in #643 as af4ee83. I simplified the Nix expression a bit to take advantage of the fact that vendored-source.json is an object rather than an array (as before).

I've also removed overrides for zenoh-bridge-dds as they don't work with the latest overlay (in my PR). Is zenoh-bridge-dds needed nowadays? The README says that zenoh-plugin-ros2dds is preferable.

@muellerbernd
Copy link
Copy Markdown
Contributor Author

@muellerbernd I've extracted overrides from your test branch and added them in #643 as af4ee83. I simplified the Nix expression a bit to take advantage of the fact that vendored-source.json is an object rather than an array (as before).

I've also removed overrides for zenoh-bridge-dds as they don't work with the latest overlay (in my PR). Is zenoh-bridge-dds needed nowadays? The README says that zenoh-plugin-ros2dds is preferable.

@wentasah looks good to me. Thanks for your effort on getting also Kilted working with this overlay. @lopsided98 would be nice if you could give some feedback on this and the other mentioned PRs.

self.cargo
self.rustc
];
postPatch = postPatch + ''
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this currently works for me but to get it to cross-compile you need to set the define ZENOHC_CUSTOM_TARGET in the cmake flags. Something like this:

cmakeFlags = cmakeFlags ++
      lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform)
        [ "-DZENOHC_CUSTOM_TARGET=${stdenv.hostPlatform.config}" ];

See: https://github.com/eclipse-zenoh/zenoh-c?tab=readme-ov-file#cross-compilation

wentasah added a commit that referenced this pull request Sep 8, 2025
…t_vendor calls

This is needed at least for zenoh-cpp-vendor, which is being prepared
in #558, but it also improves the robustness of the whole patching
machanism.

The format of vendored-source.json is changed from a single object to
object of objects, one for each call to ament_vendor. The key in the
top-level object is the target name specified in the ament_vendor call
in CMakeLists.txt.

The principle of operation is slightly different from the previous
version. Instead of introducing "fake" ament_cmake_vendor_package just
for creation of vendored-source.json, we introduce a "wrapped" version
of ament_cmake_vendor_package, which is used both for creation of
vendored-source.json as well as for compiling the package. When
compiling the package, the wrapped version captures ament_vendor calls
and modifies the arguments to use the prefetched data from the Nix
store instead of downloading them directly.

patchAmentVendorGit was updated to understand the new format and to
invoke CMake with the wrapped package and information from
vendored-source.json.

The vendored-source.json files will be changed to the new format in
the next commit.
@wentasah wentasah mentioned this pull request Sep 13, 2025
@muellerbernd
Copy link
Copy Markdown
Contributor Author

closing in favor of #666

@muellerbernd muellerbernd deleted the fix-zenoh branch September 14, 2025 06:16
YuanYuYuan pushed a commit to YuanYuYuan/nix-ros-overlay that referenced this pull request Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants