Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@

configureFlags =
lib.optionals stdenv.isLinux [
"--with-boost=${boost}/lib"
"--with-boost=${boost-nix}/lib"
"--with-sandbox-shell=${sh}/bin/busybox"
]
++ lib.optionals (stdenv.isLinux && !(isStatic && stdenv.system == "aarch64-linux")) [
Expand Down Expand Up @@ -202,7 +202,7 @@
version = libgit2.lastModifiedDate;
cmakeFlags = (attrs.cmakeFlags or []) ++ ["-DUSE_SSH=exec"];
}))
boost
boost-nix
lowdown-nix
libsodium
]
Expand Down Expand Up @@ -423,14 +423,14 @@

propagatedBuildInputs = propagatedDeps;

disallowedReferences = [ boost ];
disallowedReferences = [ boost-nix ];

preConfigure = lib.optionalString (! currentStdenv.hostPlatform.isStatic)
''
# Copy libboost_context so we don't get all of Boost in our closure.
# https://github.com/NixOS/nixpkgs/issues/45462
mkdir -p $out/lib
cp -pd ${boost}/lib/{libboost_context*,libboost_thread*,libboost_system*} $out/lib
cp -pd ${boost-nix}/lib/{libboost_context*,libboost_thread*,libboost_system*,libboost_regex*} $out/lib
rm -f $out/lib/*.a
${lib.optionalString currentStdenv.hostPlatform.isLinux ''
chmod u+w $out/lib/*.so.*
Expand All @@ -440,9 +440,9 @@
for LIB in $out/lib/*.dylib; do
chmod u+w $LIB
install_name_tool -id $LIB $LIB
install_name_tool -delete_rpath ${boost}/lib/ $LIB || true
install_name_tool -delete_rpath ${boost-nix}/lib/ $LIB || true
done
install_name_tool -change ${boost}/lib/libboost_system.dylib $out/lib/libboost_system.dylib $out/lib/libboost_thread.dylib
install_name_tool -change ${boost-nix}/lib/libboost_system.dylib $out/lib/libboost_system.dylib $out/lib/libboost_thread.dylib
''}
'';

Expand Down Expand Up @@ -470,9 +470,13 @@
''}
${lib.optionalString currentStdenv.isDarwin ''
install_name_tool \
-change ${boost}/lib/libboost_context.dylib \
-change ${boost-nix}/lib/libboost_context.dylib \
$out/lib/libboost_context.dylib \
$out/lib/libnixutil.dylib
install_name_tool \
-change ${boost-nix}/lib/libboost_regex.dylib \
$out/lib/libboost_regex.dylib \
$out/lib/libnixexpr.dylib
''}
'';

Expand All @@ -495,6 +499,10 @@
meta.mainProgram = "nix";
});

boost-nix = final.boost.override {
enableIcu = false;
};

lowdown-nix = with final; currentStdenv.mkDerivation rec {
name = "lowdown-0.9.0";

Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ libexpr_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/lib

libexpr_LIBS = libutil libstore libfetchers

libexpr_LDFLAGS += -lboost_context -pthread
libexpr_LDFLAGS += -lboost_context -lboost_regex -pthread
ifdef HOST_LINUX
libexpr_LDFLAGS += -ldl
endif
Expand Down
35 changes: 23 additions & 12 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "primops.hh"

#include <boost/container/small_vector.hpp>
#include <boost/regex.hpp>
#include <nlohmann/json.hpp>

#include <sys/types.h>
Expand All @@ -24,7 +25,6 @@

#include <algorithm>
#include <cstring>
#include <regex>
#include <dlfcn.h>

#include <cmath>
Expand Down Expand Up @@ -3885,19 +3885,30 @@ static RegisterPrimOp primop_convertHash({
.fun = prim_convertHash,
});

// regex aliases, switch between boost and std
using regex = boost::regex;
using regex_error = boost::regex_error;
using cmatch = boost::cmatch;
using cregex_iterator = boost::cregex_iterator;
namespace regex_constants = boost::regex_constants;
// overloaded function alias
constexpr auto regex_match = [] (auto &&...args) {
return boost::regex_match(std::forward<decltype(args)>(args)...);
};

struct RegexCache
{
// TODO use C++20 transparent comparison when available
std::unordered_map<std::string_view, std::regex> cache;
std::unordered_map<std::string_view, regex> cache;
std::list<std::string> keys;

std::regex get(std::string_view re)
regex get(std::string_view re)
{
auto it = cache.find(re);
if (it != cache.end())
return it->second;
keys.emplace_back(re);
return cache.emplace(keys.back(), std::regex(keys.back(), std::regex::extended)).first->second;
return cache.emplace(keys.back(), regex(keys.back(), regex::extended)).first->second;
}
};

Expand All @@ -3917,8 +3928,8 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
NixStringContext context;
const auto str = state.forceString(*args[1], context, pos, "while evaluating the second argument passed to builtins.match");

std::cmatch match;
if (!std::regex_match(str.begin(), str.end(), match, regex)) {
cmatch match;
if (!regex_match(str.begin(), str.end(), match, regex)) {
v.mkNull();
return;
}
Expand All @@ -3933,8 +3944,8 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
(v.listElems()[i] = state.allocValue())->mkString(match[i + 1].str());
}

} catch (std::regex_error & e) {
if (e.code() == std::regex_constants::error_space) {
} catch (regex_error & e) {
if (e.code() == regex_constants::error_space) {
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
state.debugThrowLastTrace(EvalError({
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
Expand Down Expand Up @@ -3997,8 +4008,8 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
NixStringContext context;
const auto str = state.forceString(*args[1], context, pos, "while evaluating the second argument passed to builtins.split");

auto begin = std::cregex_iterator(str.begin(), str.end(), regex);
auto end = std::cregex_iterator();
auto begin = cregex_iterator(str.begin(), str.end(), regex);
auto end = cregex_iterator();

// Any matches results are surrounded by non-matching results.
const size_t len = std::distance(begin, end);
Expand Down Expand Up @@ -4037,8 +4048,8 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)

assert(idx == 2 * len + 1);

} catch (std::regex_error & e) {
if (e.code() == std::regex_constants::error_space) {
} catch (regex_error & e) {
if (e.code() == regex_constants::error_space) {
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
state.debugThrowLastTrace(EvalError({
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
Expand Down