opam-nix icon indicating copy to clipboard operation
opam-nix copied to clipboard

Initializing a project from flake template fails

Open eureka-cpu opened this issue 1 year ago • 4 comments

Describe the bug A clear and concise description of what the bug is.

I just wanted to start a new ocaml project but following the guide in the readme throws an error.

To Reproduce Steps to reproduce the behavior.

nix flake init -t github:tweag/opam-nix
git init .
git add -A
nix develop
error:
       … while evaluating the attribute 'legacyPackages.aarch64-linux.ocaml-test'
         at /nix/store/01x5k4nlxcpyd85nnr0b9gm89rm8ff4x-source/lib.nix:44:11:
           43|         ${key} = (attrs.${key} or { }) // {
           44|           ${system} = ret.${key};
             |           ^
           45|         };

       … while evaluating the attribute 'legacyPackages'
         at /nix/store/jfa59xcdf7fjyijd4q166qdikishkc0y-source/flake.nix:29:9:
           28|       {
           29|         legacyPackages = scope.overrideScope overlay;
             |         ^
           30|

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: attribute 'ocaml-test' missing
       at /nix/store/n4ch79chp544barfjwxmnyq1b9397sxy-source/src/opam.nix:759:16:
          758|       latestVersions = mapAttrs (_: last) (listRepo repo);
          759|       pkgdef = repo.passthru.pkgdefs.${name}.${latestVersions.${name}};
             |                ^
          760|

Expected behavior A clear and concise description of what you expected to happen.

Expected to enter a devShell as stated in the article in the readme for getting started

Environment

  • OS name + version: aarch64-linux
  • Version of the code:

Additional context Add any other context about the problem here.

eureka-cpu avatar Mar 10 '25 03:03 eureka-cpu

You also need to have an actual OCaml project in that directory as well, so that opam-nix has something to build. If you don't have any yet and want to initialize one, try:

nix-shell -p dune_3 --run "dune init proj ocaml_test ."

But be aware that for now (due to #115) you also have to disable subst:

echo "(subst disabled)" >> dune-project

balsoft avatar Mar 10 '25 09:03 balsoft

It would be nice if there was a default where if a project doesn't yet exist you still get a devShell with the tools to create a project, similar to crane. Perhaps I should reference this in a new issue, though.

eureka-cpu avatar Mar 10 '25 19:03 eureka-cpu

I think it would be better addressed in documentation. You don't need much tooling besides some version of dune, and it doesn't make sense (at least to me) for opam-nix to explicitly provide it, especially given how it doesn't even save much typing (nix-shell -p dune_3 vs nix develop). I'll keep this issue open until I can update the documentation to reflect this.

balsoft avatar Mar 12 '25 07:03 balsoft

I think my only argument would be discover-ability, but documented is better than not 🙂

eureka-cpu avatar Mar 13 '25 03:03 eureka-cpu

I'll add that on initial setup, you also need to:

  • Have ocaml in the path (either with nix shell etc or installed system-wide)

  • Run dune build at least once, otherwise you'll have an empty .opam file, and nix develop etc will fail

snrubthinks avatar Jul 25 '25 10:07 snrubthinks

Thanks @snrubthinks , I'll address this in the docs

balsoft avatar Jul 25 '25 10:07 balsoft