tweag / opam-nix

Turn opam-based OCaml projects into Nix derivations
MIT License
111 stars 33 forks source link

Unable to statically build package that depends on `dllunix` #47

Closed solomon-b closed 1 year ago

solomon-b commented 1 year ago

Describe the bug I'm trying to statically build a project that has a transitive dependency on dllunix.so and am getting the following error:

error: builder for '/nix/store/x4jq8kzc7m7awl74pxyib11sn92fy1lc-jsonrpc-static-x86_64-unknown-linux-musl-1.15.1-5.0.drv' failed with exit code 2;
       last 10 log lines:
       > building
       > File "_none_", line 1:
       > Alert ocaml_deprecated_auto_include:
       > OCaml's lib directory layout changed in 5.0. The unix subdirectory has been
       > automatically added to the search path, but you should add -I +unix to the
       > command-line to silence this alert (e.g. by adding unix to the list of
       > libraries in your dune file, or adding use_unix to your _tags file for
       > ocamlbuild, or using -package unix for ocamlfind).
       > Cannot load required shared library dllunix.
       > Reason: dllunix.so: dynamic loading not supported on this platform.
       For full logs, run 'nix log /nix/store/x4jq8kzc7m7awl74pxyib11sn92fy1lc-jsonrpc-static-x86_64-unknown-linux-musl-1.15.1-5.0.drv'.
error: 1 dependencies of derivation '/nix/store/ym9m7ipv68y04vmw6a9008290y9i4js6-lsp-static-x86_64-unknown-linux-musl-1.15.1-5.0.drv' failed to build
error (ignored): error: cannot unlink '/tmp/nix-build-menhirLib-static-x86_64-unknown-linux-musl-20230415.drv-2': Directory not empty
error: 1 dependencies of derivation '/nix/store/7gv25hzwjg801axwzpm5n6l728x0qxar-asai-static-x86_64-unknown-linux-musl-0.1.0_dev.drv' failed to build
error: 1 dependencies of derivation '/nix/store/18d0sz41m84cliq8w84waj5116rgg32x-polytt-static-x86_64-unknown-linux-musl-dev-env.drv' failed to build

To Reproduce My project is not public but here is my flake.nix:

{
  inputs = {
    opam-nix.url = "github:tweag/opam-nix";
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.follows = "opam-nix/nixpkgs";
  };
  outputs = { self, flake-utils, opam-nix, nixpkgs }@inputs:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        on = opam-nix.lib.${system};

        localPackagesQuery = builtins.mapAttrs (_: pkgs.lib.last)
          (on.listRepo (on.makeOpamRepo ./.));

        query = {
          ocaml-base-compiler = "*";
        };

        scope = on.buildDuneProject { pkgs = pkgs.pkgsStatic; } "polytt" ./. query;

        overlay = self: super: {
          # Prevent unnecessary dependencies on the resulting derivation
          polytt = super.polytt.overrideAttrs (_: {
            removeOcamlReferences = true;
            postFixup = "rm -rf $out/nix-support";
          });
        };

        scope' = scope.overrideScope' overlay;

        # Packages in this workspace
        packages =
          pkgs.lib.getAttrs (builtins.attrNames localPackagesQuery) scope';
      in
      {
        legacyPackages = scope';

        packages = packages // { default = packages.polytt; };
      });
}

This should fail on any package that depends on, for example, the ocaml lsp package.

Expected behavior I would expect a successful static build.

Environment

Additional context Add any other context about the problem here.

balsoft commented 1 year ago

This is not an opam-nix problem, and unfortunately there's little we can do from our side. See similar issues: 1 2 3 .You can try an override like this:

jsonrpc = super.jsonrpc.overrideAttrs (oa: { buildPhase = "dune build -p $pname @install"; });

Which skips the part of the build that required unixdll.so (ocaml unix.cma unvendor.ml) but it's not clear to me whether the resulting package will work after that.

balsoft commented 1 year ago

Can we close this issue, as there's nothing that can be done on opam-nix side?

solomon-b commented 1 year ago

Hi. Yes consider this issue closed. To make sure I understand the issue correctly, the problem is that jsonrpc depends on dllunix.so for which there is not a static build available?

balsoft commented 1 year ago

To make sure I understand the issue correctly, the problem is that jsonrpc depends on dllunix.so for which there is not a static build available?

Yes, pretty much. dllunix relies on dynamic loading (it uses dlopen), so it can't be built statically.