Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
eval-settings.hh
Go to the documentation of this file.
1#pragma once
3
8
9namespace nix {
10
11class EvalState;
12struct PrimOp;
13
19class DeprecatedWarnSetting : public BaseSetting<bool>
20{
21 Setting<Diagnose> & target;
22 const char * targetName;
23
24public:
25 DeprecatedWarnSetting(
26 Config * options,
27 Setting<Diagnose> & target,
28 const char * targetName,
29 const std::string & name,
30 const std::string & description,
31 const StringSet & aliases = {})
32 : BaseSetting<bool>(false, true, name, description, aliases, std::nullopt)
33 , target(target)
34 , targetName(targetName)
35 {
36 options->addSetting(this);
37 }
38
39 void assign(const bool & v) override;
40 void appendOrSet(bool newValue, bool append) override;
41 void override(const bool & v) override;
42
43 bool excludedFromFullSerialisation() const override
44 {
45 return true;
46 }
47};
48
49struct EvalSettings : Config
50{
51private:
52 void anchor() override;
53
54public:
64 using LookupPathHook = std::optional<SourcePath>(EvalState & state, std::string_view);
65
79 using LookupPathHooks = std::map<std::string, fun<LookupPathHook>>;
80
81 EvalSettings(bool & readOnlyMode, LookupPathHooks lookupPathHooks = {});
82
83 /* FIXME: This really shouldn't be public. The C API should have non-global settings instead. */
84 bool * readOnlyMode = nullptr;
85
86 bool isReadOnly() const
87 {
88 assert(readOnlyMode);
89 return *readOnlyMode;
90 }
91
92 static Strings getDefaultNixPath();
93
94 static bool isPseudoUrl(std::string_view s);
95
96 static Strings parseNixPath(const std::string & s);
97
98 static std::string resolvePseudoUrl(std::string_view url);
99
100 LookupPathHooks lookupPathHooks;
101
102 std::vector<PrimOp> extraPrimOps;
103
104 Setting<bool> enableNativeCode{this, false, "allow-unsafe-native-code-during-evaluation", R"(
105 Enable built-in functions that allow executing native code.
106
107 In particular, this adds:
108 - `builtins.importNative` *path* *symbol*
109
110 Opens dynamic shared object (DSO) at *path*, loads the function with the symbol name *symbol* from it and runs it.
111 The loaded function must have the following signature:
112 ```cpp
113 extern "C" typedef void (*ValueInitialiser) (EvalState & state, Value & v);
114 ```
115
116 The [Nix C++ API documentation](@docroot@/development/documentation.md#api-documentation) has more details on evaluator internals.
117
118 - `builtins.exec` *arguments*
119
120 Execute a program, where *arguments* are specified as a list of strings, and parse its output as a Nix expression.
121 )"};
122
123 Setting<Strings> nixPath{
124 this,
125 {},
126 "nix-path",
127 R"(
128 List of search paths to use for [lookup path](@docroot@/language/constructs/lookup-path.md) resolution.
129 This setting determines the value of
130 [`builtins.nixPath`](@docroot@/language/builtins.md#builtins-nixPath) and can be used with [`builtins.findFile`](@docroot@/language/builtins.md#builtins-findFile).
131
132 - The configuration setting is overridden by the [`NIX_PATH`](@docroot@/command-ref/env-common.md#env-NIX_PATH)
133 environment variable.
134 - `NIX_PATH` is overridden by [specifying the setting as the command line flag](@docroot@/command-ref/conf-file.md#command-line-flags) `--nix-path`.
135 - Any current value is extended by the [`-I` option](@docroot@/command-ref/opt-common.md#opt-I) or `--extra-nix-path`.
136
137 If the respective paths are accessible, the default values are:
138
139 - `$HOME/.nix-defexpr/channels`
140
141 The [user channel link](@docroot@/command-ref/files/default-nix-expression.md#user-channel-link), pointing to the current state of [channels](@docroot@/command-ref/files/channels.md) for the current user.
142
143 - `nixpkgs=$NIX_STATE_DIR/profiles/per-user/root/channels/nixpkgs`
144
145 The current state of the `nixpkgs` channel for the `root` user.
146
147 - `$NIX_STATE_DIR/profiles/per-user/root/channels`
148
149 The current state of all channels for the `root` user.
150
151 These files are set up by the [Nix installer](@docroot@/installation/installing-binary.md).
152 See [`NIX_STATE_DIR`](@docroot@/command-ref/env-common.md#env-NIX_STATE_DIR) for details on the environment variable.
153
154 > **Note**
155 >
156 > If [restricted evaluation](@docroot@/command-ref/conf-file.md#conf-restrict-eval) is enabled, the default value is empty.
157 >
158 > If [pure evaluation](#conf-pure-eval) is enabled, `builtins.nixPath` *always* evaluates to the empty list `[ ]`.
159 )",
160 {},
161 false};
162
163 Setting<std::string> currentSystem{
164 this,
165 "",
166 "eval-system",
167 R"(
168 This option defines
169 [`builtins.currentSystem`](@docroot@/language/builtins.md#builtins-currentSystem)
170 in the Nix language if it is set as a non-empty string.
171 Otherwise, if it is defined as the empty string (the default), the value of the
172 [`system` ](#conf-system)
173 configuration setting is used instead.
174
175 Unlike `system`, this setting does not change what kind of derivations can be built locally.
176 This is useful for evaluating Nix code on one system to produce derivations to be built on another type of system.
177 )"};
178
183 const std::string & getCurrentSystem() const;
184
185 Setting<bool> restrictEval{
186 this,
187 false,
188 "restrict-eval",
189 R"(
190 If set to `true`, the Nix evaluator doesn't allow access to any
191 files outside of
192 [`builtins.nixPath`](@docroot@/language/builtins.md#builtins-nixPath),
193 or to URIs outside of
194 [`allowed-uris`](@docroot@/command-ref/conf-file.md#conf-allowed-uris).
195 )"};
196
197 Setting<bool> pureEval{
198 this,
199 false,
200 "pure-eval",
201 R"(
202 Pure evaluation mode ensures that the result of Nix expressions is fully determined by explicitly declared inputs, and not influenced by external state:
203
204 - Restrict file system and network access to files specified by cryptographic hash
205 - Disable impure constants:
206 - [`builtins.currentSystem`](@docroot@/language/builtins.md#builtins-currentSystem)
207 - [`builtins.currentTime`](@docroot@/language/builtins.md#builtins-currentTime)
208 - [`builtins.nixPath`](@docroot@/language/builtins.md#builtins-nixPath)
209 - [`builtins.storePath`](@docroot@/language/builtins.md#builtins-storePath)
210
211 As a result, every fetch must be a []{#pure-fetch}*pure fetch* — one that references immutable content:
212 [`fetchTree`](@docroot@/language/builtins.md#builtins-fetchTree) and [`fetchGit`](@docroot@/language/builtins.md#builtins-fetchGit) require a locked revision, and [`fetchTarball`](@docroot@/language/builtins.md#builtins-fetchTarball) and [`fetchurl`](@docroot@/language/builtins.md#builtins-fetchurl) require a `sha256` hash.
213 A mutable reference, such as a Git branch or tag without a revision, is rejected, since its result could otherwise change over time.
214 )"};
215
216 Setting<bool> traceImportFromDerivation{
217 this,
218 false,
219 "trace-import-from-derivation",
220 R"(
221 By default, Nix allows [Import from Derivation](@docroot@/language/import-from-derivation.md).
222
223 When this setting is `true`, Nix logs a warning indicating that it performed such an import.
224 This option has no effect if `allow-import-from-derivation` is disabled.
225 )"};
226
227 Setting<bool> enableImportFromDerivation{
228 this,
229 true,
230 "allow-import-from-derivation",
231 R"(
232 By default, Nix allows [Import from Derivation](@docroot@/language/import-from-derivation.md).
233
234 With this option set to `false`, Nix throws an error when evaluating an expression that uses this feature,
235 even when the required store object is readily available.
236 This ensures that evaluation doesn't require any builds to take place,
237 regardless of the state of the store.
238 )"};
239
240 Setting<Strings> allowedUris{
241 this,
242 {},
243 "allowed-uris",
244 R"(
245 A list of URI prefixes to which access is allowed in restricted
246 evaluation mode. For example, when set to
247 `https://github.com/NixOS`, builtin functions such as `fetchGit` are
248 allowed to access `https://github.com/NixOS/patchelf.git`.
249
250 Access is granted when
251 - the URI is equal to the prefix,
252 - or the URI is a subpath of the prefix,
253 - or the prefix is a URI scheme ended by a colon `:` and the URI has the same scheme.
254 )"};
255
256 Setting<bool> traceFunctionCalls{
257 this,
258 false,
259 "trace-function-calls",
260 R"(
261 If set to `true`, the Nix evaluator traces every function call.
262 Nix prints a log message at the "vomit" level for every function
263 entrance and function exit.
264
265 function-trace entered undefined position at 1565795816999559622
266 function-trace exited undefined position at 1565795816999581277
267 function-trace entered /nix/store/.../example.nix:226:41 at 1565795253249935150
268 function-trace exited /nix/store/.../example.nix:226:41 at 1565795253249941684
269
270 The `undefined position` means the function call is a builtin.
271
272 Use the `contrib/stack-collapse.py` script distributed with the Nix
273 source code to convert the trace logs in to a format suitable for
274 `flamegraph.pl`.
275 )"};
276
277 Setting<EvalProfilerMode> evalProfilerMode{
278 this,
279 EvalProfilerMode::disabled,
280 "eval-profiler",
281 R"(
282 Enables evaluation profiling. The following modes are supported:
283
284 * `flamegraph` stack sampling profiler. Outputs folded format, one line per stack (suitable for `flamegraph.pl` and compatible tools).
285
286 Use [`eval-profile-file`](#conf-eval-profile-file) to specify where the profile is saved.
287
288 See [Using the `eval-profiler`](@docroot@/advanced-topics/eval-profiler.md).
289 )"};
290
291 Setting<std::filesystem::path> evalProfileFile{
292 this,
293 "nix.profile",
294 "eval-profile-file",
295 R"(
296 Specifies the file where [evaluation profile](#conf-eval-profiler) is saved.
297 )"};
298
299 Setting<uint32_t> evalProfilerFrequency{
300 this,
301 99,
302 "eval-profiler-frequency",
303 R"(
304 Specifies the sampling rate in hertz for sampling evaluation profilers.
305 Use `0` to sample the stack after each function call.
306 See [`eval-profiler`](#conf-eval-profiler).
307 )"};
308
309 Setting<bool> useEvalCache{
310 this,
311 true,
312 "eval-cache",
313 R"(
314 Whether to use the flake evaluation cache.
315 Certain commands won't have to evaluate when invoked for the second time with a particular version of a flake.
316 Intermediate results are not cached.
317 )"};
318
319 Setting<bool> ignoreExceptionsDuringTry{
320 this,
321 false,
322 "ignore-try",
323 R"(
324 If set to true, ignore exceptions inside 'tryEval' calls when evaluating Nix expressions in
325 debug mode (using the --debugger flag). By default the debugger pauses on all exceptions.
326 )"};
327
328 Setting<bool> traceVerbose{
329 this,
330 false,
331 "trace-verbose",
332 "Whether `builtins.traceVerbose` should trace its first argument when evaluated."};
333
334 Setting<unsigned int> maxCallDepth{
335 this, 10000, "max-call-depth", "The maximum function call depth to allow before erroring."};
336
337 Setting<bool> builtinsTraceDebugger{
338 this,
339 false,
340 "debugger-on-trace",
341 R"(
342 If set to true and the `--debugger` flag is given, the following functions
343 enter the debugger like [`builtins.break`](@docroot@/language/builtins.md#builtins-break):
344
345 * [`builtins.trace`](@docroot@/language/builtins.md#builtins-trace)
346 * [`builtins.traceVerbose`](@docroot@/language/builtins.md#builtins-traceVerbose)
347 if [`trace-verbose`](#conf-trace-verbose) is set to true.
348 * [`builtins.warn`](@docroot@/language/builtins.md#builtins-warn)
349
350 This is useful for debugging warnings in third-party Nix code.
351 )"};
352
353 Setting<bool> builtinsDebuggerOnWarn{
354 this,
355 false,
356 "debugger-on-warn",
357 R"(
358 If set to true and the `--debugger` flag is given, [`builtins.warn`](@docroot@/language/builtins.md#builtins-warn)
359 will enter the debugger like [`builtins.break`](@docroot@/language/builtins.md#builtins-break).
360
361 This is useful for debugging warnings in third-party Nix code.
362
363 Use [`debugger-on-trace`](#conf-debugger-on-trace) to also enter the debugger on legacy warnings that are logged with [`builtins.trace`](@docroot@/language/builtins.md#builtins-trace).
364 )"};
365
366 Setting<bool> builtinsAbortOnWarn{
367 this,
368 false,
369 "abort-on-warn",
370 R"(
371 If set to true, [`builtins.warn`](@docroot@/language/builtins.md#builtins-warn) throws an error when logging a warning.
372
373 This will give you a stack trace that leads to the location of the warning.
374
375 This is useful for finding information about warnings in third-party Nix code when you can not start the interactive debugger, such as when Nix is called from a non-interactive script. See [`debugger-on-warn`](#conf-debugger-on-warn).
376
377 Currently, a stack trace can only be produced when the debugger is enabled, or when evaluation is aborted.
378
379 This option can be enabled by setting `NIX_ABORT_ON_WARN=1` in the environment.
380 )"};
381
382 Setting<Diagnose> lintShortPathLiterals{
383 this,
384 Diagnose::Ignore,
385 "lint-short-path-literals",
386 R"(
387 Controls handling of relative path literals that don't start with `./` or `../`.
388
389 - `ignore`: Ignore without warning (default)
390 - `warn`: Emit a warning suggesting to use `./` prefix
391 - `fatal`: Treat as a parse error
392
393 For example, with this setting set to `warn` or `fatal`, `foo/bar` would
394 suggest using `./foo/bar` instead.
395
396 This is useful for improving code readability and making path literals
397 more explicit.
398 )",
399 };
400
401 DeprecatedWarnSetting warnShortPathLiterals{
402 this,
403 lintShortPathLiterals,
404 "lint-short-path-literals",
405 "warn-short-path-literals",
406 R"(
407 Deprecated. Use [`lint-short-path-literals`](#conf-lint-short-path-literals)` = warn` instead.
408 )",
409 };
410
411 Setting<Diagnose> lintAbsolutePathLiterals{
412 this,
413 Diagnose::Ignore,
414 "lint-absolute-path-literals",
415 R"(
416 Controls handling of absolute path literals (paths starting with `/`) and home path literals (paths starting with `~/`).
417
418 - `ignore`: Ignore without warning (default)
419 - `warn`: Emit a warning about non-portability
420 - `fatal`: Treat as a parse error
421
422 It is true that some files are more difficult to reference with relative paths,
423 because they would require lots of `../../..` upward traversing to reach them.
424 But firstly, it is probably not a good idea to reference these files ---
425 such paths often make Nix expressions less portable and reproducible,
426 as they depend on the file system layout of the machine evaluating the expression.
427
428 Secondly, with [pure evaluation mode](#conf-pure-eval), most such files are prohibited to access anyway,
429 whether by absolute or relative paths.
430 In that case, enabling this lint in fatal mode is less disruptive,
431 because the paths pure eval allows are usually not the ones that would be ergonomically expressed with absolute paths anyway.
432 )",
433 };
434
435 Setting<Diagnose> lintUrlLiterals{
436 this,
437 Diagnose::Ignore,
438 "lint-url-literals",
439 R"(
440 Controls handling of unquoted URLs as part of the Nix language syntax.
441 The Nix language allows for URL literals, like so:
442
443 ```
444 $ nix repl
445 nix-repl> http://foo
446 "http://foo"
447 ```
448
449 Setting this to `warn` or `fatal` will cause the Nix parser to
450 warn or throw an error when encountering a URL literal:
451
452 ```
453 $ nix repl --lint-url-literals fatal
454 nix-repl> http://foo
455 error: URL literal 'http://foo' is deprecated
456 at «string»:1:1:
457
458 1| http://foo
459 | ^
460 ```
461
462 Unquoted URLs are being deprecated and their usage is discouraged.
463
464 The reason is that, as opposed to path literals, URLs have no
465 special properties that distinguish them from regular strings, URLs
466 containing query parameters have to be quoted anyway, and unquoted URLs
467 may confuse external tooling.
468 )",
469 };
470
471 Setting<unsigned> bindingsUpdateLayerRhsSizeThreshold{
472 this,
473 sizeof(void *) == 4 ? 8192 : 16,
474 "eval-attrset-update-layer-rhs-threshold",
475 R"(
476 Tunes the maximum size of an attribute set that, when used
477 as a right operand in an [attribute set update expression](@docroot@/language/operators.md#update),
478 uses a more space-efficient linked-list representation of attribute sets.
479
480 Setting this to larger values generally leads to less memory allocations,
481 but may lead to worse evaluation performance.
482
483 A value of `0` disables this optimization completely.
484
485 This is an advanced performance tuning option and typically should not be changed.
486 The default value is chosen to balance performance and memory usage. On 32 bit systems
487 where memory is scarce, the default is a large value to reduce the amount of allocations.
488 )"};
489};
490
494std::filesystem::path getNixDefExpr();
495
496} // namespace nix
Definition configuration.hh:147
Definition eval.hh:420
Definition configuration.hh:476
SourcePath.
std::optional< SourcePath >(EvalState &state, std::string_view) LookupPathHook
Definition eval-settings.hh:64
std::map< std::string, fun< LookupPathHook > > LookupPathHooks
Definition eval-settings.hh:79
const std::string & getCurrentSystem() const
Definition eval-settings.cc:125
Definition eval.hh:111
std::set< std::string, std::less<> > StringSet
Definition types.hh:45