<commit_before>#include <config.h>
#include <primops.hh>
#include <globals.hh>
#include <eval-inline.hh>
#include <dlfcn.h>

#include "nix-plugins-config.h"

#if HAVE_BOEHMGC

#include <gc/gc.h>
#include <gc/gc_cpp.h>

#endif


using namespace nix;

struct ExtraBuiltinsSettings : Config {
    Setting<Path> extraBuiltinsFile{this,
        settings.nixConfDir + "/extra-builtins.nix",
            "extra-builtins-file",
            "The path to a nix expression defining extra expression-language level builtins."};
};

static ExtraBuiltinsSettings extraBuiltinsSettings;

static GlobalConfig::Register rp(&extraBuiltinsSettings);

static void extraBuiltins(EvalState & state, const Pos & _pos,
    Value ** _args, Value & v)
{
    static auto extraBuiltinsFile = extraBuiltinsSettings.extraBuiltinsFile;
    if (state.allowedPaths)
        state.allowedPaths->insert(extraBuiltinsFile);

    try {
        auto fun = state.allocValue();
        state.evalFile(extraBuiltinsFile, *fun);
        Value * arg;
        if (evalSettings.enableNativeCode) {
            arg = state.baseEnv.values[0];
        } else {
            arg = state.allocValue();
            state.mkAttrs(*arg, 2);

            auto sExec = state.symbols.create("exec");
            auto vExec = state.allocAttr(*arg, sExec);
            vExec->mkPrimOp(new PrimOp { .fun = prim_exec, .arity = 1, .name = sExec });

            auto sImportNative = state.symbols.create("importNative");
            auto vImportNative = state.allocAttr(*arg, sImportNative);
            vImportNative->mkPrimOp(new PrimOp { .fun = prim_importNative, .arity = 2, .name = sImportNative });

            arg->attrs->sort();
        }
        mkApp(v, *fun, *arg);
    } catch (SysError & e) {
        if (e.errNo != ENOENT)
            throw;
        mkNull(v);
    }
}

static RegisterPrimOp rp1("__extraBuiltins", 0,
    extraBuiltins);

static void cflags(EvalState & state, const Pos & _pos,
    Value ** _args, Value & v)
{
    state.mkAttrs(v, 3);
    state.allocAttr(v, state.symbols.create("NIX_INCLUDE_DIRS"))->mkString(NIX_INCLUDE_DIRS);
    state.allocAttr(v, state.symbols.create("NIX_CFLAGS_OTHER"))->mkString(NIX_CFLAGS_OTHER);
    state.allocAttr(v, state.symbols.create("BOOST_INCLUDE_DIR"))->mkString(BOOST_INCLUDE_DIR);
    v.attrs->sort();
}

static RegisterPrimOp rp2("__nix-cflags", 0,
    cflags);<commit_msg>extra-builtins: Force the application of the builtins file to its arguments.<commit_after>#include <config.h>
#include <primops.hh>
#include <globals.hh>
#include <eval-inline.hh>
#include <dlfcn.h>

#include "nix-plugins-config.h"

#if HAVE_BOEHMGC

#include <gc/gc.h>
#include <gc/gc_cpp.h>

#endif


using namespace nix;

struct ExtraBuiltinsSettings : Config {
    Setting<Path> extraBuiltinsFile{this,
        settings.nixConfDir + "/extra-builtins.nix",
            "extra-builtins-file",
            "The path to a nix expression defining extra expression-language level builtins."};
};

static ExtraBuiltinsSettings extraBuiltinsSettings;

static GlobalConfig::Register rp(&extraBuiltinsSettings);

static void extraBuiltins(EvalState & state, const Pos & _pos,
    Value ** _args, Value & v)
{
    static auto extraBuiltinsFile = extraBuiltinsSettings.extraBuiltinsFile;
    if (state.allowedPaths)
        state.allowedPaths->insert(extraBuiltinsFile);

    try {
        auto fun = state.allocValue();
        state.evalFile(extraBuiltinsFile, *fun);
        Value * arg;
        if (evalSettings.enableNativeCode) {
            arg = state.baseEnv.values[0];
        } else {
            arg = state.allocValue();
            state.mkAttrs(*arg, 2);

            auto sExec = state.symbols.create("exec");
            auto vExec = state.allocAttr(*arg, sExec);
            vExec->mkPrimOp(new PrimOp { .fun = prim_exec, .arity = 1, .name = sExec });

            auto sImportNative = state.symbols.create("importNative");
            auto vImportNative = state.allocAttr(*arg, sImportNative);
            vImportNative->mkPrimOp(new PrimOp { .fun = prim_importNative, .arity = 2, .name = sImportNative });

            arg->attrs->sort();
        }
        mkApp(v, *fun, *arg);
        state.forceValue(v);
    } catch (SysError & e) {
        if (e.errNo != ENOENT)
            throw;
        mkNull(v);
    }
}

static RegisterPrimOp rp1("__extraBuiltins", 0,
    extraBuiltins);

static void cflags(EvalState & state, const Pos & _pos,
    Value ** _args, Value & v)
{
    state.mkAttrs(v, 3);
    state.allocAttr(v, state.symbols.create("NIX_INCLUDE_DIRS"))->mkString(NIX_INCLUDE_DIRS);
    state.allocAttr(v, state.symbols.create("NIX_CFLAGS_OTHER"))->mkString(NIX_CFLAGS_OTHER);
    state.allocAttr(v, state.symbols.create("BOOST_INCLUDE_DIR"))->mkString(BOOST_INCLUDE_DIR);
    v.attrs->sort();
}

static RegisterPrimOp rp2("__nix-cflags", 0,
    cflags);
{'fraction_non_alphanumeric': 0.12947715109098396, 'fraction_numerical': 0.0037052284890901604, 'mean_word_length': 3.079764903442485, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5652702', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1556, 'n_words': 318}