buildRustCrate + crate2nix + glib resources

I have a GTK app which uses glib resources. It compiles without a problem with Cargo, but then my build.rs fails when I run the build via Nix. I made a minimal repository that demonstrates this. It’s so minimal that it’s still the “hello world” app, because the problem manifests in the build script, before we get that far.

If you grab the repository, you can build it with a simple nix build at the root.

Anyway, the error I’m getting is…

rust_resources_demo> 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" --out-dir target/build/resources_demo --emit=dep-info,link -L dependency=target/buildDeps --extern glib_build_tools=/nix/store/fbbij8hkl76h7886x0an3m8zv6gdd8y2-rust_glib-build-tools-0.18.0-lib/lib/libglib_build_tools-62d120e43c.rlib --cap-lints allow --color always
rust_resources_demo> thread 'main' panicked at /glib-build-tools-0.18.0/src/lib.rs:33:10:
rust_resources_demo> called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }

I’ve done some digging. It appears that the build script itself is fine, and the failure I’m getting actually comes from glib-compile-resources itself. This is not very informative, so I took most of the code out of compile_resources and just put it right into my build script with some extra debugging. You’ll find that at

So, for this, after building the command, I then ask the command to print out all of the arguments, and I get this:

rust_resources_demo> [Program    ] glib-compile-resources
rust_resources_demo> [arg        ] --sourcedir
rust_resources_demo> [arg        ] /build/source
rust_resources_demo> [arg        ] --target
rust_resources_demo> [arg        ] /build/source/target/build/resources_demo.out/com.luminescent-dreams.resources-demo
rust_resources_demo> [arg        ] /build/source/gresources.xml
rust_resources_demo> [current dir] None

When I dig into the build directory, I can see that all of the relevant paths exist. So, I actually really don’t know that glib-compile-resources is complaining about.

(I’ve tried a couple of different source and individual file options.)

So, if you are familiar with buildRustCrate, crate2nix, and glib, could you take a look at this and see if you can tell what I’m missing?

Do you have glib in nativeBuildInputs for resources_demo? Maybe it is failing to find glib-compile-resources?

oh

Yes, that apparently is it. A fact that was confusing because just adding it didn’t help.

I had to add it to the overrides for the final application:

    gtkNativeInputs = [
      pkgs.pkg-config
      pkgs.gtk4
      pkgs.lidadwaita
    ];

    cargoOverrides = pkgs: pkgs.buildRustCrate.override {
      defaultCrateOverrides = pkgs.defaultCrateOverrides // {
        ...
        resources_demo = attrs: { nativeBuildInputs = gtkNativeInputs; };
      };
    };

So, yeah, have to add the dependencies to the final app, along with everything else.

Incidentally, if you want to see an existing monorepo, which provides a flake that installs a couple of different Rust apps:

Probably in a month or two I’m going to be back on the app that is Rust + Typescript, and we will see how that goes in the flake world.

1 Like