Access opam package variable in dune rule

I have a dune rule that needs to be conditionally enabled based on the value of an opam package variable. I’ve tried the following setup, but it results in a dependency cycle:

(rule
 (with-stdout-to conf-ios-arch.txt
  (system "opam var conf-ios:arch")))

(rule
 (enabled_if (= %{read:conf-ios-arch.txt} "arm64")
 ...)

Can you suggest another solution?

This seems fine. Just a detail: (system ...) invokes the command through the system shell; in your case you can just do (run opam var conf-ios:arch) instead to avoid the indirection.

Cheers,
Nicolas

I should add that the second rule is inside a subdir stanza:

(subdir DemoFramework.xcframework/ios-arm64
 (rule
  (enabled_if (= %{read:conf-ios-arch.txt} "arm64")
  ...)

Maybe I’m misusing this feature. The error I see is the following:

Error: Dependency cycle between:  
   Computing directory contents of
   _build/simulator.ios/DemoFramework.xcframework/ios-arm64
-> %{read:conf-ios-arch.txt} at dune:64
-> Computing directory contents of
   _build/simulator.ios/DemoFramework.xcframework/ios-arm64
-> required by alias default (context simulator.ios)

I would assume that if there was a more direct way to read the package variable without using an intermediate file there would be no dependency cycle. I haven’t found such a way though.

Shouldn’t you have to use %{read:../conf-ios-arch.txt} instead of %{read:conf-ios-arch.txt}, since the stanza is evaluated in the subdirectory rather than the parent directory?

Cheers,
Nicolas

You are absolutely right. Thank you.