Is there a way to lock/freeze an installed package?

Hello,

If I install an opam package, and I don’t want to accidentally
remove it in the future, is there some command for this use case?
I also don’t want its version to change; in any direction.
I.e. I’d like to “freeze” the package that was installed.

In Debian/Ubuntu, the command would be:

sudo apt-mark hold <PACKAGE_NAME>

Regards,
F.

I think opam pin PKG.X.Y.Z does what you want.

1 Like

I do not think opam pin prevents you in any way from doing an opam remove later on for the same package.

Well I guess you may still want an explicit opam remove to work. For the rest it does what you want (it will not upgrade/downgrade or be removed as the result of a removal of another dependency).

pin does not guaranty that i don’t think, although in general opam tried to avoid this as much as possible.

Otherwise to answer to the original question, i think what you want is:

opam pin add --current <pkg>
opam switch set-invariant --formula "$(opam switch invariant | sed 's/]/ \"<pkg>\"]/')"

The first command will avoid any change, even opam-repository fixes (if you want those fixes then use opam pin add --kind version <pkg> <version> instead)
And the second will add the package you want to ensure always stays installed to the current switch invariant. The only way to remove the package is for the invariant to be changed again or to use opam install --update-invariant

2 Likes

If it does not then FIY my mental model was:

  1. opam doesn’t remove packages that I explicitely opam install (it is a root).
  2. opam pin XXX is equivalent to do an explicit opam install.

The current mental model to have is:

  • opam install <pkg> adds <pkg> to the list of root packages and installs the package
  • root packages are packages that have a stronger priority to stay installed when using the default solver, but they can be removed if necessary
  • opam pin add <pkg>/<url>/<version>/... sets that package at that version, state or location and make it the only version of that package to be installable in that switch. Every other versions are as if they were hidden.
  • opam install <directory>/<file> is equivalent to opam pin add <directory>/<file> (except when using --deps-only)
  • The switch invariant is the list of packages (in a form of a formula) that should always stay installed at all time.
4 Likes