1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! String builtins: stringLength, substring, replaceStrings, hasPrefix, hasSuffix,
//! toLower, toUpper, match, split, concatStringsSep, concatStrings.
use std::cell::RefCell;
use std::collections::HashMap;
use super::*;
thread_local! {
/// LRU-ish regex cache: avoids recompiling the same pattern on every
/// `builtins.match` / `builtins.split` call. Nix expressions like
/// `map (builtins.match "([0-9]+)") list` hit the same pattern
/// thousands of times during nixpkgs evaluation.
static REGEX_CACHE: RefCell<HashMap<String, regex::Regex>> = RefCell::new(HashMap::new());
}
fn cached_regex(pattern: &str) -> Result<regex::Regex, EvalError> {
REGEX_CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
if let Some(re) = cache.get(pattern) {
return Ok(re.clone());
}
let re = regex::Regex::new(pattern)
.map_err(|e| EvalError::TypeError(format!("invalid regex '{pattern}': {e}")))?;
cache.insert(pattern.to_string(), re.clone());
Ok(re)
})
}
/// Register a curried string predicate builtin (first arg is a pattern, second is the subject).
macro_rules! register_string_predicate {
($builtins:expr, $name:expr, $partial_name:expr, $check:expr) => {
register_builtin($builtins, $name, |args| {
let pattern = args[0].as_string()?.to_string();
Ok(Value::Builtin(Box::new(BuiltinFn {
name: $partial_name,
func: Rc::new(move |args2| {
let s = args2[0].as_string()?;
Ok(Value::Bool($check(&pattern, s)))
}),
})))
});
};
}
pub(crate) fn register(builtins: &mut NixAttrs) {
register_builtin(builtins, "toString", |args| {
let val = &args[0];
let (s, ctx) = val.coerce_to_string()?;
Ok(Value::String(Rc::new(NixString::with_context(s, ctx))))
});
register_builtin(builtins, "stringLength", |args| {
Ok(Value::Int(args[0].as_string()?.len() as i64))
});
register_builtin(builtins, "substring", |args| {
// Mirror the VM's substring — see sui-bytecode/src/builtins.rs
// for the rationale. CppNix uses negative length to mean
// "to end of string" (core idiom in lib.strings.removePrefix);
// negative start yields an empty string.
let start_i = args[0].as_int()?;
Ok(Value::Builtin(Box::new(BuiltinFn {
name: "substring<p1>",
func: Rc::new(move |args2| {
let len_i = args2[0].as_int()?;
Ok(Value::Builtin(Box::new(BuiltinFn {
name: "substring<p2>",
func: Rc::new(move |args3| {
// nix: `substring` is context-PRESERVING — the slice
// carries the input string's context (a store-path dep
// survives a substring). Preserve it for the String
// case (verified `nix eval` hasContext=true); a coerced
// path arg keeps prior behavior.
let (s, ctx) = match &args3[0] {
Value::String(ns) => {
(ns.chars.to_string(), Some(ns.context.clone()))
}
_ => (args3[0].as_string()?.to_string(), None),
};
if start_i < 0 {
return Err(EvalError::TypeError(
"substring: negative start position".into(),
));
}
let s_len = s.len();
let start = (start_i as usize).min(s_len);
let end = if len_i < 0 {
s_len
} else {
start.saturating_add(len_i as usize).min(s_len)
};
let slice = s[start..end].to_string();
match ctx {
Some(ctx) => Ok(Value::String(Rc::new(
NixString::with_context(slice, ctx),
))),
None => Ok(Value::string(slice)),
}
}),
})))
}),
})))
});
// Case conversion (context-preserving)
register_builtin(builtins, "toLower", |args| {
let ns = args[0].as_nix_string()?;
Ok(Value::String(Rc::new(NixString::with_context(
ns.chars.to_lowercase(),
ns.context.clone(),
))))
});
register_builtin(builtins, "toUpper", |args| {
let ns = args[0].as_nix_string()?;
Ok(Value::String(Rc::new(NixString::with_context(
ns.chars.to_uppercase(),
ns.context.clone(),
))))
});
register_builtin(builtins, "replaceStrings", |args| {
let from = args[0].to_list()?.iter()
.map(|v| v.to_str())
.collect::<Result<Vec<_>, _>>()?;
Ok(Value::Builtin(Box::new(BuiltinFn {
name: "replaceStrings<p1>",
func: Rc::new(move |args2| {
// Keep each replacement's STRING + its CONTEXT. CppNix's
// `prim_replaceStrings` accumulates the context of the
// subject string PLUS the context of every replacement
// that actually fires — dropping either loses input-drv
// references and diverges the drv (e.g. `escapeShellArg`
// uses replaceStrings; a `-B${gcc}/bin` value stopped
// registering gcc, so `replaceVars`-built patches were
// missing their toolchain inputDrvs).
let to: Vec<(String, StringContext)> = args2[0]
.to_list()?
.iter()
.map(|v| {
let forced = crate::eval::force_value(v)?;
let (s, c) = forced.coerce_to_string()?;
Ok::<_, EvalError>((s, c))
})
.collect::<Result<Vec<_>, _>>()?;
let from2 = from.clone();
Ok(Value::Builtin(Box::new(BuiltinFn {
name: "replaceStrings<p2>",
func: Rc::new(move |args3| {
// Subject string WITH its context.
let forced = crate::eval::force_value(&args3[0])?;
let (subject, subject_ctx) = forced.coerce_to_string()?;
let mut ctx = subject_ctx;
// Left-to-right scan matching CppNix: at each
// position, try each `from[i]` in order; the first
// match wins, emits `to[i]` (accumulating its
// context), and advances past the match. Empty
// `from` entries match the empty string at every
// position (CppNix semantics), so handle them the
// same way — but never loop forever.
let bytes = subject.as_bytes();
let mut result = String::with_capacity(subject.len());
let mut i = 0usize;
while i < bytes.len() {
let mut matched = false;
for (idx, f) in from2.iter().enumerate() {
if !f.is_empty() && subject[i..].starts_with(f.as_str()) {
result.push_str(&to[idx].0);
ctx.merge(&to[idx].1);
i += f.len();
matched = true;
break;
}
}
if !matched {
// CppNix also matches an EMPTY `from` at the
// current position before copying the char.
if let Some(empty_idx) =
from2.iter().position(|f| f.is_empty())
{
result.push_str(&to[empty_idx].0);
ctx.merge(&to[empty_idx].1);
}
let ch_len = subject[i..]
.chars()
.next()
.map_or(1, char::len_utf8);
result.push_str(&subject[i..i + ch_len]);
i += ch_len;
}
}
// Trailing empty-`from` match at end-of-string.
if let Some(empty_idx) = from2.iter().position(|f| f.is_empty()) {
result.push_str(&to[empty_idx].0);
ctx.merge(&to[empty_idx].1);
}
Ok(Value::String(Rc::new(NixString::with_context(
result, ctx,
))))
}),
})))
}),
})))
});
register_builtin(builtins, "concatStringsSep", |args| {
let sep = args[0].as_string()?.to_string();
Ok(Value::Builtin(Box::new(BuiltinFn {
name: "concatStringsSep<partial>",
func: Rc::new(move |args2| {
// CppNix's prim_concatStringsSep coerces each element to a
// string and ACCUMULATES its string context into the result
// (`v.mkString(res, context)`). Dropping the element context —
// as the old `to_str()`/`Value::string` path did — loses every
// input-drv reference a coerced derivation output carried, so a
// `${pkg.out}/lib` element stopped registering `pkg`'s `out`
// output. That silently diverged `lib.makeLibraryPath` /
// xgcc's `LIBRARY_PATH` (zlib requested `["dev"]` not
// `["dev","out"]`) → a different hashDerivationModulo →
// divergent drvPath. Merge the context like CppNix.
let list = args2[0].to_list()?;
let mut result = String::new();
let mut ctx = StringContext::new();
for (i, v) in list.iter().enumerate() {
if i > 0 { result.push_str(&sep); }
let (s, c) = v.coerce_to_string()?;
result.push_str(&s);
ctx.merge(&c);
}
Ok(Value::String(Rc::new(NixString::with_context(result, ctx))))
}),
})))
});
register_string_predicate!(builtins, "hasPrefix", "hasPrefix<partial>",
|prefix: &str, s: &str| s.starts_with(prefix));
register_string_predicate!(builtins, "hasSuffix", "hasSuffix<partial>",
|suffix: &str, s: &str| s.ends_with(suffix));
// concatStrings — concat without separator (= concatStringsSep "").
// Same context-accumulation contract as concatStringsSep above.
register_builtin(builtins, "concatStrings", |args| {
let list = args[0].to_list()?;
let mut result = String::new();
let mut ctx = StringContext::new();
for v in list.iter() {
let (s, c) = v.coerce_to_string()?;
result.push_str(&s);
ctx.merge(&c);
}
Ok(Value::String(Rc::new(NixString::with_context(result, ctx))))
});
// Regex: hashString, match, split
register_curried(builtins, "hashString", |algo, s| {
let algo_str = algo.as_string()?;
let input = s.as_string()?;
// CppNix supports md5, sha1, sha256, sha512 at minimum (plus
// blake3/blake2b/etc. on newer versions via `builtins.convertHash`
// — which routes elsewhere). Adding the missing three; found
// by probing `builtins.hashString "md5" "hello"` which CppNix
// returns `"5d41402abc4b2a76b9719d911017c592"` and sui was
// rejecting with 'unsupported algorithm'.
let hex = match algo_str {
"md5" => {
use md5::{Md5, Digest};
format!("{:x}", Md5::digest(input.as_bytes()))
}
"sha1" => {
use sha1::{Sha1, Digest};
format!("{:x}", Sha1::digest(input.as_bytes()))
}
"sha256" => {
use sha2::{Sha256, Digest};
format!("{:x}", Sha256::digest(input.as_bytes()))
}
"sha512" => {
use sha2::{Sha512, Digest};
format!("{:x}", Sha512::digest(input.as_bytes()))
}
_ => return Err(EvalError::TypeError(format!("hashString: unsupported algorithm: {algo_str}"))),
};
Ok(Value::string(hex))
});
register_curried(builtins, "match", |pattern, s| {
let pat = pattern.as_string()?;
let input = s.as_string()?;
let re = cached_regex(&format!("^{pat}$"))?;
match re.captures(input) {
Some(caps) => {
let groups: Vec<Value> = (1..caps.len())
.map(|i| match caps.get(i) {
Some(m) => Value::string(m.as_str()),
None => Value::Null,
})
.collect();
Ok(Value::List(Rc::new(NixList::new(groups))))
}
None => Ok(Value::Null),
}
});
// Regex-based split per Nix spec: alternates non-match strings and match group lists.
register_curried(builtins, "split", |pattern, s| {
let pat = pattern.as_string()?;
let input = s.as_string()?;
let re = cached_regex(pat)?;
let mut result: Vec<Value> = Vec::new();
let mut last_end = 0;
for m in re.find_iter(input) {
// Add the non-matching part before this match
result.push(Value::string(&input[last_end..m.start()]));
// Add the match groups as a list.
// Per Nix spec: when the regex has no capture groups,
// the separator position gets an empty list [].
// When it has capture groups, those captures are the list elements.
if let Some(caps) = re.captures(&input[m.start()..]) {
let groups: Vec<Value> = (1..caps.len())
.map(|i| match caps.get(i) {
Some(g) => Value::string(g.as_str()),
None => Value::Null,
})
.collect();
result.push(Value::List(Rc::new(NixList::new(groups))));
}
last_end = m.end();
}
// Add trailing non-matching part
result.push(Value::string(&input[last_end..]));
Ok(Value::List(Rc::new(NixList::new(result))))
});
}