incus: fix container tests from image rename (#360305)
[NixPkgs.git] / pkgs / tools / audio / yabridge / libyabridge-from-nix-profiles.patch
blobbcc55d48f0aaa60a604dbc754b2b5fa4e7056298
1 diff --git a/src/chainloader/utils.cpp b/src/chainloader/utils.cpp
2 index fa90b8f7..bd44d0ea 100644
3 --- a/src/chainloader/utils.cpp
4 +++ b/src/chainloader/utils.cpp
5 @@ -29,8 +29,10 @@
6 namespace fs = ghc::filesystem;
8 void* find_plugin_library(const std::string& name) {
9 + Logger logger = Logger::create_exception_logger();
11 // Just using a goto for this would probably be cleaner, but yeah...
12 - const auto impl = [&name]() -> void* {
13 + const auto impl = [&name, &logger]() -> void* {
14 // If `name` exists right next to the Wine plugin host binary, then
15 // we'll try loading that. Otherwise we'll fall back to regular
16 // `dlopen()` for distro packaged versions of yabridge
17 @@ -52,27 +54,28 @@ void* find_plugin_library(const std::string& name) {
21 - if (void* handle = dlopen(name.c_str(), RTLD_LAZY | RTLD_LOCAL)) {
22 - return handle;
23 + auto nix_profiles = getenv("NIX_PROFILES");
24 + if (!nix_profiles || nix_profiles[0] == '\0') {
25 + logger.log("");
26 + logger.log("ERROR: 'NIX_PROFILES' environment variable is not set");
27 + logger.log("");
28 + return nullptr;
31 - // One last Hail Mary, in case ldconfig was not set up correctly. This
32 - // might be relevant for some of the `/usr/local/*` locations (although
33 - // you really, really shouldn't install yabridge there, please, thank
34 - // you). Yabridgectl searches through these same directories.
35 - for (const auto& lib_dir : {
36 - "/usr/lib",
37 - "/usr/lib/x86_64-linux-gnu",
38 - "/usr/lib64",
39 - "/usr/local/lib",
40 - "/usr/local/lib/x86_64-linux-gnu",
41 - "/usr/local/lib64",
42 - }) {
43 - const fs::path candidate = fs::path(lib_dir) / name;
44 - if (void* handle =
45 - dlopen(candidate.c_str(), RTLD_LAZY | RTLD_LOCAL)) {
46 + // NIX_PROFILES is iterated in reverse from the most specific (the
47 + // user profile) to the least specific (the system profile).
48 + const std::string_view nix_profiles_view = nix_profiles;
49 + auto segment_end = nix_profiles_view.size();
50 + while (segment_end != std::string::npos) {
51 + const auto next_segment_end = nix_profiles_view.rfind(' ', segment_end - 1);
52 + const auto segment_begin = next_segment_end + 1;
53 + const auto profile = nix_profiles_view.substr(segment_begin, segment_end - segment_begin);
54 + const auto candidate = fs::path(profile) / "lib" / name;
55 + if (auto handle = dlopen(candidate.c_str(), RTLD_LAZY | RTLD_LOCAL)) {
56 return handle;
59 + segment_end = next_segment_end;
62 return nullptr;
63 @@ -82,8 +85,6 @@ void* find_plugin_library(const std::string& name) {
64 if (!handle) {
65 const fs::path this_plugin_path = get_this_file_location();
67 - Logger logger = Logger::create_exception_logger();
69 logger.log("");
70 logger.log("Could not find '" + name + "'");
71 logger.log("");