I’ve been recently been plaing around with Nix my new Macbook Pro, and have been interested in experimenting with OCaml. As part of this I’ve made an over-engineered Hello World example project, demonstrating how to set up OCaml with Nix flakes and Dune. I’ve got no idea of it’s useful to anybody, but I thought I’d publish it in the off-chance it is.
Let me know if I’ve done anything particularly silly, or if I’m missing anything in the documentation that might be worth mentioning. I’m neither an expert in Nix, nor an expert in OCaml or Dune, so this is very likely! It’s released under MIT-0 so feel free to do whatever you want with it.
this repo is amazing! And I’m learning so much just from reading the file! So thank-you so much for writing this. My question is: how do you get dependencies needed? I have my dependencies listed in the .opam file, and usually I would do opam install . --deps-only, but it feels wrong to have that in the preBuild, and it also feels wrong to repeat the dependencies in the nativeBuildInputs
Thanks, glad to hear that you’ve found it useful! A big goal was to attempt to keep things relatively straightforward and easy to follow. From what I’ve seen nix files often tend to end up as big blobs of spaghetti, so I was doing my best to avoid that fate.
My question is: how do you get dependencies needed? I have my dependencies listed in the .opam file, and usually I would do opam install . --deps-only , but it feels wrong to have that in the preBuild , and it also feels wrong to repeat the dependencies in the nativeBuildInputs
Yeah I’d like to have an example of this at some stage. I think if you want to do this without the duplication you might need to use something like opam-nix. I tried this a while back, but found the documentation, API, and examples confusing and failed to make much progress. The API also seemed to result in somewhat ugly looking nix expressions which was kind of disappointing, so I’ve shelved this for now. If you can figure it out though I’d love to learn from you.
yeah the errors I’m getting is that dune can’t find the dependencies (even though I added them to the list in the flake.nix). If you clone the repo you can try nix run, but tbh I think I should just spend the time to understand this opam-nix.
The libraries also need to be in buildInputs not nativeBuildInputs. IIUC OCaml links to these dynamically, so they need to remain on the host platform.
The order of the outputs seems significant too, where the first one is the default package. You could probably just remove the stuff related to documentation generation though.
I’m not sure if you need a .opam file right now, as this is not something that I imagine will be distributed on the package manager? The main utility of that would be collaborating with non-nix developers (which is really important to support), but seeing this is a project all about Nix I’m not sure how useful that is.