There are tools available through opam which are useful as a developer but not tied to specific OCaml versions. For example, ocp-indent and ocamlformat.
By default, at least under opam 1.2.2, these tools need to be installed under each switch separately to keep them available in $PATH. A user can manually maintain symlinks but it would be really nice to avoid that if at all possible.
Is there a proper/accepted way to share these tools across switches?
I’d like something like this in general - maybe not system-wide, but maybe opam-wide. So an installation of ocamlformat could be available once the opam environment is setup, regardless of the switch in use.
This likely becomes more complicated with tools like ocp-index, merlin and refmt which may be more tightly coupled to internal data representations.
I use ocamlformat from the CLI at times to clean up an entire repo at once. That admittedly a more limited use-case though so it doesn’t need such special treatment.
direnv is on the list of tools I keep meaning to try but never get around to. I’ll give it a shot, thanks!
I have a really lightweight solution to this: when my shell starts up, I evaluate just the first command in opam-init/variables.sh, which gives me a $OPAM_SWITCH_PREFIX for the default opam switch; then I manually add that "$OPAM_SWITCH_PREFIX/bin" to my $PATH:
This allows me to have the command-line invocations of, say, ocamlformat available system-wide, but still have them overridden by anything installed in a directory-local opam switch (which I use heavily). For instance, this also means the installation of ocamlmerlin in my ‘global’ opam switch ensures I have completion, type-interrogation, etc, even in a blank Vim buffer with no associated directory … but when I switch to a directory with a very specific, custom build of Merlin for BuckleScript, then my Vim instance automatically loads that plugin instead.