nix-community / crate2nix

rebuild only changed crates in CI with crate2nix and nix
https://nix-community.github.io/crate2nix/
Apache License 2.0
385 stars 89 forks source link

Building a GTK application fails without an error message #286

Closed savannidgerinel closed 1 year ago

savannidgerinel commented 1 year ago

I have a GTK application that I am trying to build with crate2nix, and I am consistently getting this sort of error message on three different crates that the app depends upon.

The repository in question is https://git.luminescent-dreams.com/savanni/tools . I am working on the feature/build-tools branch.

The following error occurs for gobject-sys, graphene-sys, and cairo-sys. Which one appears in any given build attempt is random.

error: builder for '/nix/store/fdli8kfywpnh5clb73y5axcgmkksxiw1-rust_cairo-sys-rs-0.17.0.drv' failed with exit code 1;
       last 9 log lines:
       > unpacking sources
       > unpacking source archive /nix/store/w5qls7d9lcmz309qhk08pjrrrsbzkk7n-cairo-sys-rs-0.17.0.tar.gz
       > source root is cairo-sys-rs-0.17.0
       > setting SOURCE_DATE_EPOCH to timestamp 1153704088 of file cairo-sys-rs-0.17.0/src/lib.rs
       > patching sources
       > configuring
       > Running cd .
       > Building build.rs (cairo_sys)
       > Running rustc --crate-name build_script_build build.rs --crate-type bin -C opt-level=3 -C codegen-units=1 --edition 2021 --cfg feature="default" --cfg feature="glib" --cfg feature="glib-sys" --cfg feature="use_glib" --out-dir target/build/cairo-sys-rs --emit=dep-info,link -L dependency=target/buildDeps --extern system_deps=/nix/store/qsh8cnpjr3ysgk7f2lgbgkj2mwinp4mw-rust_system-deps-6.0.3-lib/lib/libsystem_deps-3e490eaafe.rlib --cap-lints allow --color always
       For full logs, run 'nix log /nix/store/fdli8kfywpnh5clb73y5axcgmkksxiw1-rust_cairo-sys-rs-0.17.0.drv'.
error: 1 dependencies of derivation '/nix/store/8ifrbgqj4cjzfk84j5akpnv5g71kvnsq-rust_kifu-gtk-0.1.0.drv' failed to build

Here is the full log from the build:

   1   │ @nix { "action": "setPhase", "phase": "unpackPhase" }
   2   │ unpacking sources
   3   │ unpacking source archive /nix/store/w5qls7d9lcmz309qhk08pjrrrsbzkk7n-cairo-sys-rs-0.17.0.tar.gz
   4   │ source root is cairo-sys-rs-0.17.0
   5   │ setting SOURCE_DATE_EPOCH to timestamp 1153704088 of file cairo-sys-rs-0.17.0/src/lib.rs
   6   │ @nix { "action": "setPhase", "phase": "patchPhase" }
   7   │ patching sources
   8   │ @nix { "action": "setPhase", "phase": "configurePhase" }
   9   │ configuring
  10   │ Running cd .
  11   │ Building build.rs (cairo_sys)
  12   │ Running rustc --crate-name build_script_build build.rs --crate-type bin -C opt-level=3 -C codegen-units=1 --edition 2021 --cfg feature="default" --cf
       │ g feature="glib" --cfg feature="glib-sys" --cfg feature="use_glib" --out-dir target/build/cairo-sys-rs --emit=dep-info,link -L dependency=target/buil
       │ dDeps --extern system_deps=/nix/store/qsh8cnpjr3ysgk7f2lgbgkj2mwinp4mw-rust_system-deps-6.0.3-lib/lib/libsystem_deps-3e490eaafe.rlib --cap-lints allo
       │ w --color always

I get the same error with graphene-sys and gobject-sys. The order of the build execution is non-deterministic, so I may hit any one of these on any given build.

I'm building from a flake. Eliding the entire devShell, I'm down to just this:

{
  description = "Luminescent Dreams Tools";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-22.11";
    unstable.url = "nixpkgs/nixos-unstable";
    pkgs-cargo2nix.url = "github:cargo2nix/cargo2nix";
  };

  outputs = { self, nixpkgs, unstable, pkgs-cargo2nix, ... }:
    let 
      version = builtins.string 0 8 self.lastModifiedDate;
      supportedSystems = [ "x86_64-linux" ];
    in
    {
        packages."x86_64-linux" =
          let
            pkgs = import nixpkgs { system = "x86_64-linux"; };
            customBuildInfo = pkgs: pkgs.buildRustCrate.override {
              defaultCrateOverrides = pkgs.defaultCrateOverrides // {
                gobject-sys = attrs: {
                  nativeBuildInputs = [
                    pkgs.pkg-config
                    pkgs.wrapGAppsHook4
                    pkgs.gtk4
                    pkgs.glib
                    pkgs.clang
                  ];
                };
                graphene-sys = attrs: {
                  nativeBuildInputs = [
                    pkgs.pkg-config
                    pkgs.wrapGAppsHook4
                    pkgs.gtk4
                    pkgs.glib
                    pkgs.clang
                  ];
                };
                cairo-sys = attrs: {
                  nativeBuildInputs = [
                    pkgs.pkg-config
                    pkgs.wrapGAppsHook4
                    pkgs.gtk4
                    pkgs.glib
                    pkgs.clang
                  ];
                };
              };
            };
          in {
            kifu-gtk = (import ./kifu/kifu-gtk/Cargo.nix {
              inherit pkgs;
              buildRustCrateForPkgs = customBuildInfo;
            }).rootCrate.build;
          };
    };
}

I run the build from the root of my repository with this:

nix build .#kifu-gtk
savannidgerinel commented 1 year ago

Having now banged my head against this for many hours, I have found a solution. Maybe not the intended one.

Even with the documentation in the readme I had trouble setting up the crate overrides to add the relevant native builds. I would like to submit some documentation illustrating the problem above and the solution.

Which, btw, looks like this:

        packages."x86_64-linux" =
          let
            pkgs = import nixpkgs { system = "x86_64-linux"; };
            standardOverride = attrs: {
                nativeBuildInputs = [
                  pkgs.pkg-config
                  pkgs.gtk4
                  pkgs.glib
                ];
                verbose = true;
            };
            customBuildInfo = pkgs: pkgs.buildRustCrate.override {
              defaultCrateOverrides = pkgs.defaultCrateOverrides // {
                cairo-sys-rs = standardOverride;
                graphene-sys = standardOverride;
                gobject-sys = standardOverride;
                pango-sys = standardOverride;
                gio-sys = standardOverride;
                gdk-pixbuf-sys = standardOverride;
                gdk4-sys = standardOverride;
                gsk4-sys = standardOverride;
                gtk4-sys = standardOverride;
              };
            };
          in {
            # gobject-sys = pkgs.buildRustCrate cargo.internal.crates.gobject-sys;
            kifu-gtk = (import ./kifu/kifu-gtk/Cargo.nix {
              inherit pkgs;
              buildRustCrateForPkgs = customBuildInfo;
            }).rootCrate.build;
          };
savannidgerinel commented 1 year ago

I think that, unless there is something wrong in what I wrote, this is the final solution.

FireFragment commented 5 months ago

Shouldn't this be reopened to at least provide some meaningful error message or link to this issue?