Opam: a way to freeze a switch state in full (to later unpack it quickly)?

I searched around, and couldn’t find this, so I figured I’d ask here.

I’d like a way to do the following:

  1. create a switch
  2. install a bunch of packages
  3. then freeze it in binary form to a file
  4. later, blow away the installed switch
  5. and then reinstall from #3, so I can quickly restore the state as it was before step #3 was done.

A sort of “checkpoint”. I have a vague memory that there’s some way to do this, b/c I remember it being mentioned in various error-messages, but I can’t jog my memory enough to figure out what it was. Is there a way to do this ? A reading of the opam-switch manpage doesn’t yield anything promising: “opam export” looks like a way to freeze the metadata (package names/versions/commitids) and not the actual binary files as-found-on-the-disk.

I’m a bit lazy to look at the man pages in detail and the exact structure of the files on disk, so please excuse the blurry idea, but it might be a way to get it to work.

What you want probably requires 2 step.

First one is related to the “content” of the switch. It usually lives in ~/.opam/NAME_OF_THE_SWITCH or in _opam if you are using a local switch. It contains all the installed packages AND the state of the switch (the list of installed packages). Saving or restoring this content is fairly simple. You can zip/tar/archive this folder to save it. And the reverse operation to restore.

Second operation is to “register” the switch in the opam root during restoration. The very simple solution might be to just add the name of the switch being restored in the installed-switches value of ~/.opam/config. I’ve never tried unfortunately, so can’t confirm that it is enough. What I would go for instinctively is to create an empty switch. It’s fairly fast and easy. opam sw create SWITCH_NAME --empty should do the job.

To summarize, freezing the global switch named example:

tar cvf my-pretty-switch.tar ~/.opam/example

Then before the restoration I assumed that opam sw remove example was launched.

Restoration is then

opam sw create example --empty
rm -r ~/.opam/example
tar xvf my-pretty-switch.tar
mv example ~/.opam/example

Hopefully this gives you a decent start into which direction to look at.

1 Like

Thank you! I try not to inquire into the guts of opam’s on-disk datastructures, but your response confirms that maybe I’ll have to, in order to get what I want. So … time to do it grin.