How to get an .ocamlinit file per [ switch | project ]?



How do you manage your ~/.ocamlinit file with several switches?

In other words, in the context of a growing number of opam switches with very different sets of libraries within a same machine, how can we get something like an .ocamlinit file related to one switch?

Of course, one solution is to create a dedicated toplevel with all libraries with ocamlmktop or to use dune utop


See and related

Is there something simpler?



Happy New Year to all OCamlers!

In fact, this simple and stdlib-based solution works:

#use "/home/test/dev/stuff/stuff.ocamlinit"

File is named stuff.ocamlinit instead of .ocamlinit to keep clear (and avoid a possible confusion with ~/.ocamlinit).
Is there any convention?

And its functional equivalent:

Topdirs.dir_use Format.std_formatter
( Filename.concat (Sys.getcwd ())  ".ocamlinit" )

…assuming we launch the Toplevel from the stuff/ directory where we have the stuff.ocamlinit file (with a test on Sys.getcwd () and a Sys.chdir if needed).

BTW, it may be useful to use ~/.ocamlinit as well, assuming it has only some common setup and no specific OCaml directives:

 #use "/home/test/.ocamlinit"