devenv icon indicating copy to clipboard operation
devenv copied to clipboard

config.languages.rust.import when dependency needs pkg-config

Open devurandom opened this issue 3 months ago • 3 comments

I have a Rust dependency that needs pkg-config to build. With the Cargo.toml, devenv.nix and devenv.yaml below, I get this error (full logs below):

thread 'main' panicked at build.rs:10:47:
called `Result::unwrap()` on an `Err` value: Could not run `PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --libs --cflags wayland-client`
The pkg-config command could not be found.

How do I teach devenv / crate2nix / nix that rust_wayland-sys-0.31.7 needs pkg-config installed?

Cargo.toml (excerpt)

[dependencies]
bevy = "0.17"

devenv.nix

{ pkgs, lib, config, ... }:
let
  myapp = config.languages.rust.import ./. {};
in
{
  languages.rust = {
    enable = true;
    mold.enable = true;
    components = [ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" ];
  };

  packages = with pkgs; [
    pkg-config
    alsa-lib.dev
    systemd.dev
    wayland.dev
    myapp
  ];

  outputs ={
    inherit myapp;
  };

  git-hooks.hooks = {
    rustfmt.enable = true;
    clippy.enable = true;
  };

  enterShell = ''
    echo "Linking rust source for stable access"
    ln -sfn ${config.env.RUST_SRC_PATH} ${config.env.DEVENV_STATE}/rust-lib-src
  '';
}

devenv.yaml

inputs:
  crate2nix:
    url: github:nix-community/crate2nix
    inputs:
      nixpkgs:
        follows: nixpkgs
  nixpkgs:
    url: github:cachix/devenv-nixpkgs/rolling

nix log

Running phase: unpackPhase
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking source archive /nix/store/i22ybrphbdprjl2jn693az2z9p25c4g0-wayland-sys-0.31.7.tar.gz
source root is wayland-sys-0.31.7
setting SOURCE_DATE_EPOCH to timestamp 1153704088 of file "wayland-sys-0.31.7/src/server.rs"
Running phase: patchPhase
@nix { "action": "setPhase", "phase": "patchPhase" }
Running phase: updateAutotoolsGnuConfigScriptsPhase
@nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" }
Running phase: configurePhase
@nix { "action": "setPhase", "phase": "configurePhase" }
[0;1;32mRunning [0mcd .
[0;1;32mBuilding build.rs (wayland_sys)[0m
[0;1;32mRunning [0mrustc --crate-name build_script_build build.rs --crate-type bin -C opt-level=3 -C codegen-units=1 --edition 2021 --cfg feature="client" --cfg feature="default" --cfg feature="dlib" --cfg feature="log" --out-dir target/build/wayland-sys --emit=dep-info,link -L dependency=target/buildDeps --extern pkg_config=/nix/store/qqgi6qm5s435hvfv62wx9h5pw1k7b9sm-rust_pkg-config-0.3.32-lib/lib/libpkg_config-a43828a6b1.rlib --cap-lints allow --color always

thread 'main' panicked at build.rs:10:47:
called `Result::unwrap()` on an `Err` value: Could not run `PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --libs --cflags wayland-client`
The pkg-config command could not be found.

Most likely, you need to install a pkg-config package for your OS.
Try `apt install pkg-config`, or `yum install pkg-config`, or `brew install pkgconf`
or `pkg install pkg-config`, or `apk add pkgconfig` depending on your distribution.

If you've already installed it, ensure the pkg-config command is one of the
directories in the PATH environment variable.

If you did not expect this build to link to a pre-installed system library,
then check documentation of the wayland-sys crate for an option to
build the library from source, or disable features or dependencies
that require pkg-config.
stack backtrace:
cargo:rerun-if-env-changed=WAYLAND_CLIENT_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=WAYLAND_CLIENT_STATIC
cargo:rerun-if-env-changed=WAYLAND_CLIENT_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=WAYLAND_CLIENT_STATIC
cargo:rerun-if-env-changed=WAYLAND_CLIENT_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: build_script_build::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

devurandom avatar Nov 11 '25 23:11 devurandom

We should allow telling the import to override some thing like adding tools.

Ideally, we'd offer a way to specify these in Cargo.toml and then keep a central registry for packages that don't specify it in Cargo.toml.

domenkozar avatar Nov 12 '25 15:11 domenkozar

We should allow telling the import to override some thing like adding tools.

You mean putting something like the following in devenv.nix?

{ pkgs, lib, config, ... }:
let
  myapp = config.languages.rust.import ./. {
    extraPackages = [
      pkgs.pkg-config
    ];
  };
in
{
  ...
}

That'd get the job done for me and I'd be OK figuring out the list of packages on my own based on the software I'm trying to build.

devurandom avatar Nov 12 '25 17:11 devurandom

Yeah something like this should work, we need to figure out the correct interface here.

domenkozar avatar Nov 13 '25 18:11 domenkozar