Can I use an "opam switch" as an immutable base of another one?

This probably will not help the majority of users, but if you can use a rather sophisticated copy-on-write filesystem like btrfs on Linux, you can actually achieve this: Working with Btrfs - Snapshots - Fedora Magazine

Btrfs snapshots work different than file copies: They keep references to current and past inodes instead. When you appended the change to the file, under the hood Btrfs allocated some more space to store the changes in and added a reference to this new data to the original inode. The previous contents remain untouched.

So basically,

  • Create a new btrfs subvolume. This appears on the system like a directory.
  • Set up the base switch inside this directory. Install whatever packages you want in this base switch.
  • Create snapshots of this subvolume for each new project. Each snapshot will start off with the exact same installed packages. Then install anything else you want in the individual projects. The original subvolume remains unchanged but they all share most of the disk space because it’s a copy-on-write filesystem.

EDIT: yeah, I forgot that the OCaml compiler is not relocatable, so the above probably won’t work :sweat_smile: Afair it’s mostly because the full path to the compiler is hardcoded into bytecode executables.

1 Like