How to share compiler installation between switches?

In my default switch, I have current stable version of OCaml compiler, version 5.1.1 .
Now when sandboxing my projects with opam switch create, I’d like to reuse that existing installation of compiler, instead of rebuilding it yet again and again for each case. Is there a way to configure opam for such behavior?

Similar like rustup for Rust or ghcup/stack for Haskell, which allow only rebuild per-project dependencies sans the compiler chain itself.

To do so simply (e.g. with hardlinks) would require the relocatable compiler unfortunately, so at the moment you’ll probably need to keep rebuilding compilers unfortunately.

If you don’t mind depending on Nix being installed, I believe you can have this kind of sharing using GitHub - Julow/nix-opam-switch: Create Opam switches where the compiler and tools are built by Nix. by @Juloo.

I though this is the case. Good to know this issue is already addressed.

Interesting. For Haskell’s cabal-install is Nix also the only (sane) way to go.

If you use the same compiler for multiple projects, why do you need more than one switch? Serious question, not trolling.

you might depend on different incompatible versions of libs/tools. For example I have some public projects that are using a given version of ocamlformat, and some private projects that are using a different one. Those 2 ocamlformat can’t be installed at the same time.

When creating multiple projects based on various and version constrained dependencies, one does not want to pollute the default switch and those can be easily and safely removed when they are no longer needed. Seriously…