Skip to content

Commit

Permalink
Move plugins infra to libnixmain
Browse files Browse the repository at this point in the history
They are not actually part of the store layer, but instead part of the
Nix executable infra (libraries don't need plugins, executables do).

This is part of a larger project of moving all of our legacy settings
infra to libmain, and having the underlying libraries just have plain
configuration structs detached from any settings infra / UI layer.

Progress on NixOS#5638
  • Loading branch information
Ericson2314 committed Jul 2, 2024
1 parent f8372cb commit 679ed2b
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 126 deletions.
1 change: 1 addition & 0 deletions src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "machines.hh"
#include "shared.hh"
#include "plugin.hh"
#include "pathlocks.hh"
#include "globals.hh"
#include "serialise.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libmain/common-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "logging.hh"
#include "loggers.hh"
#include "util.hh"
#include "plugin.hh"

namespace nix {

Expand Down
117 changes: 117 additions & 0 deletions src/libmain/plugin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#ifndef _WIN32
# include <dlfcn.h>
#endif

#include "config-global.hh"
#include "signals.hh"

namespace nix {

struct PluginFilesSetting : public BaseSetting<Paths>
{
bool pluginsLoaded = false;

PluginFilesSetting(
Config * options,
const Paths & def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<Paths>(def, true, name, description, aliases)
{
options->addSetting(this);
}

Paths parse(const std::string & str) const override;
};

Paths PluginFilesSetting::parse(const std::string & str) const
{
if (pluginsLoaded)
throw UsageError(
"plugin-files set after plugins were loaded, you may need to move the flag before the subcommand");
return BaseSetting<Paths>::parse(str);
}

struct PluginSettings : Config
{
PluginFilesSetting pluginFiles{
this,
{},
"plugin-files",
R"(
A list of plugin files to be loaded by Nix. Each of these files will
be dlopened by Nix. If they contain the symbol `nix_plugin_entry()`,
this symbol will be called. Alternatively, they can affect execution
through static initialization. In particular, these plugins may construct
static instances of RegisterPrimOp to add new primops or constants to the
expression language, RegisterStoreImplementation to add new store
implementations, RegisterCommand to add new subcommands to the `nix`
command, and RegisterSetting to add new nix config settings. See the
constructors for those types for more details.
Warning! These APIs are inherently unstable and may change from
release to release.
Since these files are loaded into the same address space as Nix
itself, they must be DSOs compatible with the instance of Nix
running at the time (i.e. compiled against the same headers, not
linked to any incompatible libraries). They should not be linked to
any Nix libs directly, as those will be available already at load
time.
If an entry in the list is a directory, all files in the directory
are loaded as plugins (non-recursively).
)"};
};

static PluginSettings pluginSettings;

static GlobalConfig::Register rPluginSettings(&pluginSettings);

void initPlugins()
{
assert(!pluginSettings.pluginFiles.pluginsLoaded);
for (const auto & pluginFile : pluginSettings.pluginFiles.get()) {
std::vector<std::filesystem::path> pluginFiles;
try {
auto ents = std::filesystem::directory_iterator{pluginFile};
for (const auto & ent : ents) {
checkInterrupt();
pluginFiles.emplace_back(ent.path());
}
} catch (std::filesystem::filesystem_error & e) {
if (e.code() != std::errc::not_a_directory)
throw;
pluginFiles.emplace_back(pluginFile);
}
for (const auto & file : pluginFiles) {
checkInterrupt();
/* handle is purposefully leaked as there may be state in the
DSO needed by the action of the plugin. */
#ifndef _WIN32 // TODO implement via DLL loading on Windows
void * handle = dlopen(file.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (!handle)
throw Error("could not dynamically open plugin file '%s': %s", file, dlerror());

/* Older plugins use a statically initialized object to run their code.
Newer plugins can also export nix_plugin_entry() */
void (*nix_plugin_entry)() = (void (*)()) dlsym(handle, "nix_plugin_entry");
if (nix_plugin_entry)
nix_plugin_entry();
#else
throw Error("could not dynamically open plugin file '%s'", file);
#endif
}
}

/* Since plugins can add settings, try to re-apply previously
unknown settings. */
globalConfig.reapplyUnknownSettings();
globalConfig.warnUnknownSettings();

/* Tell the user if they try to set plugin-files after we've already loaded */
pluginSettings.pluginFiles.pluginsLoaded = true;
}

}
12 changes: 12 additions & 0 deletions src/libmain/plugin.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
///@file

namespace nix {

/**
* This should be called after settings are initialized, but before
* anything else
*/
void initPlugins();

}
10 changes: 0 additions & 10 deletions src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ nix_err nix_libstore_init_no_load_config(nix_c_context * context)
NIXC_CATCH_ERRS
}

nix_err nix_init_plugins(nix_c_context * context)
{
if (context)
context->last_err_code = NIX_OK;
try {
nix::initPlugins();
}
NIXC_CATCH_ERRS
}

Store * nix_store_open(nix_c_context * context, const char * uri, const char *** params)
{
if (context)
Expand Down
11 changes: 0 additions & 11 deletions src/libstore-c/nix_api_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ nix_err nix_libstore_init(nix_c_context * context);
*/
nix_err nix_libstore_init_no_load_config(nix_c_context * context);

/**
* @brief Loads the plugins specified in Nix's plugin-files setting.
*
* Call this once, after calling your desired init functions and setting
* relevant settings.
*
* @param[out] context Optional, stores error information
* @return NIX_OK if the initialization was successful, an error code otherwise.
*/
nix_err nix_init_plugins(nix_c_context * context);

/**
* @brief Open a nix store.
*
Expand Down
55 changes: 0 additions & 55 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <nlohmann/json.hpp>

#ifndef _WIN32
# include <dlfcn.h>
# include <sys/utsname.h>
#endif

Expand Down Expand Up @@ -333,60 +332,6 @@ unsigned int MaxBuildJobsSetting::parse(const std::string & str) const
}


Paths PluginFilesSetting::parse(const std::string & str) const
{
if (pluginsLoaded)
throw UsageError("plugin-files set after plugins were loaded, you may need to move the flag before the subcommand");
return BaseSetting<Paths>::parse(str);
}


void initPlugins()
{
assert(!settings.pluginFiles.pluginsLoaded);
for (const auto & pluginFile : settings.pluginFiles.get()) {
std::vector<std::filesystem::path> pluginFiles;
try {
auto ents = std::filesystem::directory_iterator{pluginFile};
for (const auto & ent : ents) {
checkInterrupt();
pluginFiles.emplace_back(ent.path());
}
} catch (std::filesystem::filesystem_error & e) {
if (e.code() != std::errc::not_a_directory)
throw;
pluginFiles.emplace_back(pluginFile);
}
for (const auto & file : pluginFiles) {
checkInterrupt();
/* handle is purposefully leaked as there may be state in the
DSO needed by the action of the plugin. */
#ifndef _WIN32 // TODO implement via DLL loading on Windows
void *handle =
dlopen(file.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (!handle)
throw Error("could not dynamically open plugin file '%s': %s", file, dlerror());

/* Older plugins use a statically initialized object to run their code.
Newer plugins can also export nix_plugin_entry() */
void (*nix_plugin_entry)() = (void (*)())dlsym(handle, "nix_plugin_entry");
if (nix_plugin_entry)
nix_plugin_entry();
#else
throw Error("could not dynamically open plugin file '%s'", file);
#endif
}
}

/* Since plugins can add settings, try to re-apply previously
unknown settings. */
globalConfig.reapplyUnknownSettings();
globalConfig.warnUnknownSettings();

/* Tell the user if they try to set plugin-files after we've already loaded */
settings.pluginFiles.pluginsLoaded = true;
}

static void preloadNSS()
{
/* builtin:fetchurl can trigger a DNS lookup, which with glibc can trigger a dynamic library load of
Expand Down
50 changes: 0 additions & 50 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ struct MaxBuildJobsSetting : public BaseSetting<unsigned int>
unsigned int parse(const std::string & str) const override;
};

struct PluginFilesSetting : public BaseSetting<Paths>
{
bool pluginsLoaded = false;

PluginFilesSetting(Config * options,
const Paths & def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<Paths>(def, true, name, description, aliases)
{
options->addSetting(this);
}

Paths parse(const std::string & str) const override;
};

const uint32_t maxIdsPerBuild =
#if __linux__
1 << 16
Expand Down Expand Up @@ -1158,33 +1141,6 @@ public:
Setting<uint64_t> minFreeCheckInterval{this, 5, "min-free-check-interval",
"Number of seconds between checking free disk space."};

PluginFilesSetting pluginFiles{
this, {}, "plugin-files",
R"(
A list of plugin files to be loaded by Nix. Each of these files will
be dlopened by Nix. If they contain the symbol `nix_plugin_entry()`,
this symbol will be called. Alternatively, they can affect execution
through static initialization. In particular, these plugins may construct
static instances of RegisterPrimOp to add new primops or constants to the
expression language, RegisterStoreImplementation to add new store
implementations, RegisterCommand to add new subcommands to the `nix`
command, and RegisterSetting to add new nix config settings. See the
constructors for those types for more details.
Warning! These APIs are inherently unstable and may change from
release to release.
Since these files are loaded into the same address space as Nix
itself, they must be DSOs compatible with the instance of Nix
running at the time (i.e. compiled against the same headers, not
linked to any incompatible libraries). They should not be linked to
any Nix libs directly, as those will be available already at load
time.
If an entry in the list is a directory, all files in the directory
are loaded as plugins (non-recursively).
)"};

Setting<size_t> narBufferSize{this, 32 * 1024 * 1024, "nar-buffer-size",
"Maximum size of NARs before spilling them to disk."};

Expand Down Expand Up @@ -1278,12 +1234,6 @@ public:
// FIXME: don't use a global variable.
extern Settings settings;

/**
* This should be called after settings are initialized, but before
* anything else
*/
void initPlugins();

/**
* Load the configuration (from `nix.conf`, `NIX_CONFIG`, etc.) into the
* given configuration object.
Expand Down

0 comments on commit 679ed2b

Please sign in to comment.