Opam lock file mechanism?

Hi,

is there a mechanism like npms package.json?
So I can define which packages need to be installed and excure that installation in 1 go?

1 Like

Check out the little known opam switch (export | import) <file> commands.

The command opam switch export <file> will create a file that lists all the opam packages installed into current switch with all transitive dependencies and versions included. I usually call this file by the OCaml version, so that information is also preserved. Here’s an example of such file:

You can then later reproduce the same setup with opam switch import <file> which will create a new switch with same packages and versions.

So this “switch” file corresponds to a lock file like package-lock.json, while the opam file corresponds to package.json.

Don’t know if opam 2.0 brings any changes in this area.

1 Like

WIth opam 2.0, a similar effect can be achieved with opam list --columns=name,version.

$ opam list --columns=name,version
# Packages matching: installed
# Name              # Version   
base-bytes          base        
base-unix           base        
biniou              1.2.0       
cmdliner            1.0.2       
conf-m4             1           
conf-which          1           
cppo                1.6.4       
dune                1.0.0       
easy-format         1.3.1       
jbuilder            transition  
merlin              3.1.0       
ocaml               4.07.0      
ocaml-base-compiler 4.07.0      
ocamlbuild          0.12.0      
ocamlfind           1.8.0       
ocp-build           1.99.20-beta
ocp-indent          1.6.1       
result              1.3         
topkg               0.9.1       
yojson              1.4.1  

Not sure if the question has been answered, but assuming that OP is looking for a similar workflow as [defining project deps in package.json > npm install] to have dependencies installed without having to do opam install package_a package_b etc., then there is a way for that using opam file and pin.

Create an opam file with the depends field:

opam-version: "1.2"
name: "my_project"
version: "1.0.0"
maintainer: "Your Name <email@example.com>"

depends: [
  "dune" {build}
  "lwt"
  "lwt_ppx"
  "caqti"
  "caqti-lwt"
  "caqti-driver-postgresql"
]

Pin the project without installing:

$ opam pin add -yn my_project .

Install the dependencies only:

$ opam install --deps-only todolist

You can also use --with-test flag to install test dependencies (noted by {with-test} flag in depends), at least in opam2.0, not sure about 1.2.

1 Like

@lindig can this file be then “imported” like in opam 1.x?

Related trick entry.

I don’t think so. But it would be easy to create a script that installs precisely these packages.