Conditional dependencies in dune-project

Hi, how do I add a condition dependency in a package in dune-project? E.g. a platform-specific one?

I foolishly asked this question on Stackoverflow so there are some more details about what I mean there: https://stackoverflow.com/q/79761212/265521

Following your example on StackOverflow, what you are looking for is:

(package
  (name foo)
  (depends
    (linenoise (and (>= 1.1.0) (= :os "linux")))))
  ;;                              ^^^^
  ;;                           Here is the correct name

or if you just want not to support windows:

(package
  (name foo)
  (depends
    (linenoise (and (>= 1.1.0) (<> :os "windows")))))

IIRC, in the dependency specification language for packages, it doesn’t use variable expansion but S-expression atoms. When dune generates the opam file, it turns :os to the variable os.

An nice that worked, thanks! But how did you know about it?

The spec for those filters does not list :os

Also how do I know what the possible values of :os are?

1 Like

Oh wait it has a caveat:

Filters will expand to any opam variable name if prefixed by :, not just the ones listed in filter.

I wish they’d have put that in the grammar!

And :os is defined here. Thanks for the help!

2 Likes

The possible values are whatever OPAM determines your OS to be. Dune does not limit the possible values. You can find the implementation here.

Hi, related question:

i tried

(conf-libgl (and (<> :os "macos") (<> :os "windows"))

in this project

but in the ocaml ci, when testing Windows, they still try to install the conf-libgl library, see here.

what else could I try?

I suggest

   (conf-libgl (and (<> :os "macos") (<> :os "win32"))

as “windows” is not what opam reports for the os variable under Windows

1 Like