The forgotten gem that is environment hooks and DSV

Greetings fellow robotics enthusiasts :rocket::heart:,

In the good old days of ROS 1, we had a useful little cmake command catkin_add_env_hooks where you could provide a list of shell scripts for different shells (bash, sh, zsh, …) to be sourced when the ROS workspace is sourced.
You could use this to register your library in an environment variable, e.g., to make a plugin findable, to create your own environment variables or helper functions, or whatever else you needed to do when sourcing the workspace.

This command was improved (as many things were) for ROS2 which now provides the ament_environment_hooks command.
In addition to specific shell scripts, you could also use DSV files, which is a simple format that allows you to modify environment variables.
For example, you can prepend or append your library path to a plugin environment variable.
The advantage here is that it’s supposedly faster, but most importantly, it is cross-platform.

Unfortunately, this feature is very well hidden and apparently, only a select few seem to know about it judging from the number of READMEs I’ve seen that include:

Then you have to make this library findable by running export RANDOM_LIB_PLUGIN_PATH=$(instruction that some people will certainly have issues with)/somepath/lib:${RANDOM_LIB_PLUGIN_PATH}

So, this post hopefully provides someone (maybe you) with the information that this gem exists and might be helpful to you.
I’m pretty sure I have seen documentation for DSV and environment hooks at some point but even with the flaky knowledge that it should exist, I couldn’t find it on Google.
Here’s the closest thing that I could find:
ros_gz_project_template/ros_gz_example_gazebo/hooks at main · gazebosim/ros_gz_project_template · GitHub
(See @gavanderhoorn’s comment)

The only downside I know of is that the ROS binary workspace is currently broken and ignores DSV files, so they only work in the source workspace :person_shrugging:
See DSV files are not sourced in binary workspace · Issue #1613 · ros2/ros2 · GitHub
However, you can also register sh or bash scripts and they should work in both binary and source workspaces.

Some examples of my own:

Here I register my QML ROS 2 plugin to be found by QML. Please ignore the .in ending, I just noticed that I changed the path and don’t need any template processing anymore, so it could just be a .dsv.

In this WIP package, it’s used to register a command that makes working with a ROS 2 source workspace and robots a bit more comfortable:

This just serves to show that you can also use environment hooks for more complex functionality. Please ignore the current documentation status, which may be a bit outdated. You can expect a release post sometime in the future with more polished documentation.

I hope this information is useful of you.
If not, here’s an AI-generated cat riding on an AI-knockoff Spot to make up for the time you took to read this:

6 Likes

https://colcon.readthedocs.io/en/released/developer/environment.html#dsv-files

?

1 Like

Yes, thank you!
That’s the one.
Hopefully, now this post may be found on Google so it can also be found by looking for ament_environment_hooks dsv.

@rgariepy I found your next avatar.

2 Likes

Apparently, this was fixed in rolling. I’m not sure why it’s not backported yet, though.