1diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc
2index 3ed19497..f9534bd0 100644
3--- a/passes/cmds/plugin.cc
4+++ b/passes/cmds/plugin.cc
5@@ -75,8 +75,27 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
6 #endif
7
8 void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
9- if (hdl == NULL && orig_filename.find('/') == std::string::npos)
10- hdl = dlopen((proc_share_dirname() + "plugins/" + orig_filename + ".so").c_str(), RTLD_LAZY|RTLD_LOCAL);
11+ if (hdl == NULL && orig_filename.find('/') == std::string::npos) {
12+ std::string install_dir = proc_share_dirname() + "plugins";
13+
14+ vector<string> all_dirs;
15+ all_dirs.push_back(install_dir);
16+
17+ char* plugin_dirs = getenv("NIX_YOSYS_PLUGIN_DIRS");
18+ if (plugin_dirs != NULL) {
19+ std::string p(plugin_dirs), t;
20+ std::stringstream ss(p);
21+
22+ while(std::getline(ss, t, ':')) {
23+ all_dirs.push_back(t);
24+ }
25+ }
26+
27+ for (auto dir : all_dirs) {
28+ hdl = dlopen((dir + "/" + orig_filename + ".so").c_str(), RTLD_LAZY|RTLD_LOCAL);
29+ if (hdl != NULL) break;
30+ }
31+ }
32 if (hdl == NULL)
33 log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
34 loaded_plugins[orig_filename] = hdl;