about summary refs log tree commit diff
path: root/src/libexpr/nixexpr.hh
AgeCommit message (Collapse)AuthorFilesLines
2014-05-26 Add primop ‘scopedImport’Eelco Dolstra1-1/+0
‘scopedImport’ works like ‘import’, except that it takes a set of attributes to be added to the lexical scope of the expression, essentially extending or overriding the builtin variables. For instance, the expression scopedImport { x = 1; } ./foo.nix where foo.nix contains ‘x’, will evaluate to 1. This has a few applications: * It allows getting rid of function argument specifications in package expressions. For instance, a package expression like: { stdenv, fetchurl, libfoo }: stdenv.mkDerivation { ... buildInputs = [ libfoo ]; } can now we written as just stdenv.mkDerivation { ... buildInputs = [ libfoo ]; } and imported in all-packages.nix as: bar = scopedImport pkgs ./bar.nix; So whereas we once had dependencies listed in three places (buildInputs, the function, and the call site), they now only need to appear in one place. * It allows overriding builtin functions. For instance, to trace all calls to ‘map’: let overrides = { map = f: xs: builtins.trace "map called!" (map f xs); # Ensure that our override gets propagated by calls to # import/scopedImport. import = fn: scopedImport overrides fn; scopedImport = attrs: fn: scopedImport (overrides // attrs) fn; # Also update ‘builtins’. builtins = builtins // overrides; }; in scopedImport overrides ./bla.nix * Similarly, it allows extending the set of builtin functions. For instance, during Nixpkgs/NixOS evaluation, the Nixpkgs library functions could be added to the default scope. There is a downside: calls to scopedImport are not memoized, unlike import. So importing a file multiple times leads to multiple parsings / evaluations. It would be possible to construct the AST only once, but that would require careful handling of variables/environments.
2014-04-04 Show position info in attribute selection errorsEelco Dolstra1-2/+3
2014-04-04 Show position info in Boolean operationsEelco Dolstra1-17/+3
2014-04-04 Show position info in string concatenation / addition errorsEelco Dolstra1-2/+3
2014-04-04 forceString: Show position infoEelco Dolstra1-0/+4
2014-04-04 Include position info in function applicationEelco Dolstra1-1/+17
This allows error messages like: error: the anonymous function at `/etc/nixos/configuration.nix:1:1' called without required argument `foo', at `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/lib/modules.nix:77:59'
2014-03-10 The expr of AttrNames/DynamicAttrDefs is always an ExprConcatStringsShea Levy1-5/+5
2014-01-21 Fix some clang warningsEelco Dolstra1-1/+2
2013-12-31 Don't use any syntactic sugar for dynamic attrsShea Levy1-2/+11
This doesn't change any functionality but moves some behavior out of the parser and into the evaluator in order to simplify the code. Signed-off-by: Shea Levy <shea@shealevy.com>
2013-12-31 Dynamic attrsShea Levy1-1/+9
This adds new syntax for attribute names: * attrs."${name}" => getAttr name attrs * attrs ? "${name}" => isAttrs attrs && hasAttr attrs name * attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def * { "${name}" = value; } => listToAttrs [{ inherit name value; }] Of course, it's a bit more complicated than that. The attribute chains can be arbitrarily long and contain combinations of static and dynamic parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively straightforward for the getAttrs/hasAttrs cases but is more complex for the listToAttrs case due to rules about duplicate attribute definitions. For attribute sets with dynamic attribute names, duplicate static attributes are detected at parse time while duplicate dynamic attributes are detected when the attribute set is forced. So, for example, { a = null; a.b = null; "${"c"}" = true; } will be a parse-time error, while { a = {}; "${"a"}".b = null; c = true; } will be an eval-time error (technically that case could theoretically be detected at parse time, but the general case would require full evaluation). Moreover, duplicate dynamic attributes are not allowed even in cases where they would be with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but { a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction might be relaxed in the future in cases where the static variant would not be an error, but it is not obvious that that is desirable. Finally, recursive attribute sets with dynamic attributes have the static attributes in scope but not the dynamic ones. So rec { a = true; "${"b"}" = a; } is equivalent to { a = true; b = true; } but rec { "${"a"}" = true; b = a; } would be an error or use a from the surrounding scope if it exists. Note that the getAttr, getAttr or default, and hasAttr are all implemented purely in the parser as syntactic sugar, while attribute sets with dynamic attribute names required changes to the AST to be implemented cleanly. This is an alternative solution to and closes #167 Signed-off-by: Shea Levy <shea@shealevy.com>
2013-12-31 Add the ExprBuiltin Expr type to the ASTShea Levy1-0/+7
Certain desugaring schemes may require the parser to use some builtin function to do some of the work (e.g. currently `throw` is used to lazily cause an error if a `<>`-style path is not in the search path) Unfortunately, these names are not reserved keywords, so an expression that uses such a syntactic sugar will not see the expected behavior (see tests/lang/eval-okay-redefine-builtin.nix for an example). This adds the ExprBuiltin AST type, which when evaluated uses the value from the rootmost variable scope (which of course is initialized internally and can't shadow any of the builtins). Signed-off-by: Shea Levy <shea@shealevy.com>
2013-11-18 Add a symbol __curPos that expands to the current source locationEelco Dolstra1-0/+7
I.e. an attribute set { file = <string>; line = <int>; column = <int>; }.
2013-11-12 Make function calls tail-recursiveEelco Dolstra1-1/+1
2013-10-24 Rename "attribute sets" to "sets"Eelco Dolstra1-2/+2
We don't have any other kind of sets so calling them attribute sets is unnecessarily verbose.
2013-10-08 Deduplicate filenames in PosEelco Dolstra1-3/+5
This saves ~4 MiB of RAM for NixOS system instantiation, and ~18 MiB for "nix-env -qa".
2013-10-08 Treat undefined variable errors consistentlyEelco Dolstra1-0/+1
Previously, a undefined variable inside a "with" caused an EvalError (which can be caught), while outside, it caused a ParseError (which cannot be caught). Now both cause an UndefinedVarError (which cannot be caught).
2013-10-08 Show the exact position of undefined variablesEelco Dolstra1-1/+2
In particular, undefined variable errors in a "with" previously didn't show *any* position information, so this should help a lot in those cases.
2013-10-08 Merge VarRef into ExprVarEelco Dolstra1-10/+2
2013-09-02 Fix whitespaceEelco Dolstra1-1/+1
2013-08-26 Simplify inherited attribute handlingShea Levy1-4/+2
This reduces the difference between inherited and non-inherited attribute handling to the choice of which env to use (in recs and lets) by setting the AttrDef::e to a new ExprVar in the parser rather than carrying a separate AttrDef::v VarRef member. As an added bonus, this allows inherited attributes that inherit from a with to delay forcing evaluation of the with's attributes. Signed-off-by: Shea Levy <shea@shealevy.com>
2013-08-19 Store Nix integers as longsEelco Dolstra1-2/+2
So on 64-bit systems, integers are now 64-bit. Fixes #158.
2013-05-16 Show function names in error messagesEelco Dolstra1-0/+4
Functions in Nix are anonymous, but if they're assigned to a variable/attribute, we can use the variable/attribute name in error messages, e.g. while evaluating `concatMapStrings' at `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/lib/strings.nix:18:25': ...
2013-03-08 Revert "Prevent config.h from being clobbered"Eelco Dolstra1-1/+3
This reverts commit 28bba8c44f484eae38e8a15dcec73cfa999156f6.
2013-03-07 Prevent config.h from being clobberedEelco Dolstra1-3/+1
2013-02-08 Make "${./path} ..." evaluate to a string, not a pathEelco Dolstra1-1/+3
Wacky string coercion semantics caused expressions like exec = "${./my-script} params..."; to evaluate to a path (‘/path/my-script params’), because anti-quotations are desuged to string concatenation: exec = ./my-script + " params..."; By constrast, adding a space at the start would yield a string as expected: exec = " ${./my-script} params..."; Now the first example also evaluates to a string.
2012-08-12 Add some basic profiling support to the evaluatorEelco Dolstra1-0/+9
Setting the environment variable NIX_COUNT_CALLS to 1 enables some basic profiling in the evaluator. It will count calls to functions and primops as well as evaluations of attributes. For example, to see where evaluation of a NixOS configuration spends its time: $ NIX_SHOW_STATS=1 NIX_COUNT_CALLS=1 ./src/nix-instantiate/nix-instantiate '<nixos>' -A system --readonly-mode ... calls to 39 primops: 239532 head 233962 tail 191252 hasAttr ... calls to 1595 functions: 224157 `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/lib/lists.nix:17:19' 221767 `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/lib/lists.nix:17:14' 221767 `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/lib/lists.nix:17:10' ... evaluations of 7088 attributes: 167377 undefined position 132459 `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/lib/attrsets.nix:119:41' 47322 `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/lib/attrsets.nix:13:21' ...
2012-07-18 Use "#pragma once" to prevent repeated header file inclusionEelco Dolstra1-5/+1
2012-01-07 * Don't create thunks for simple constants (integers, strings, paths)Eelco Dolstra1-3/+10
and allocate them only once. * Move Value and related functions into value.hh.
2012-01-04 * Don't use dynamic_cast, it's very slow. "nix-instantiateEelco Dolstra1-0/+2
/etc/nixos/nixos -A system" spent about 10% of its time in dynamic_cast.
2011-07-13 * Allow a default value in attribute selection by writingEelco Dolstra1-3/+3
x.y.z or default (as originally proposed in https://mail.cs.uu.nl/pipermail/nix-dev/2009-September/002989.html). For instance, an expression like stdenv.lib.attrByPath ["features" "ckSched"] false args can now be written as args.features.ckSched or false
2011-07-06 * Change the right-hand side of the ‘.’ operator from an attribute toEelco Dolstra1-2/+3
an attribute path. This is a refactoring to support default values.
2011-07-06 * In the ‘?’ operator, allow attribute paths. For instance, you canEelco Dolstra1-2/+8
write ‘attrs ? a.b’ to test whether ‘attrs’ has an attribute ‘a’ containing an attribute ‘b’. This is more convenient than ‘attrs ? a && attrs.a ? b’. Slight change in the semantics: it's no longer an error if the left-hand side of ‘?’ is not an attribute set. In that case it just returns false. So, ‘null ? foo’ no longer throws an error.
2010-10-24 * Keep attribute sets in sorted order to speed up attribute lookups.Eelco Dolstra1-7/+13
* Simplify the representation of attributes in the AST. * Change the behaviour of listToAttrs() in case of duplicate names.
2010-10-23 * Optimise string constants by putting them in the symbol table.Eelco Dolstra1-2/+2
2010-10-22 * Store Value nodes outside of attribute sets. I.e., Attr now storesEelco Dolstra1-0/+1
a pointer to a Value, rather than the Value directly. This improves the effectiveness of garbage collection a lot: if the Value is stored inside the set directly, then any live pointer to the Value causes all other attributes in the set to be live as well.
2010-10-04 * Make sure that config.h is included before the system headers,Eelco Dolstra1-2/+2
because it defines _FILE_OFFSET_BITS. Without this, on OpenSolaris the system headers define it to be 32, and then the 32-bit stat() ends up being called with a 64-bit "struct stat", or vice versa. This also ensures that we get 64-bit file sizes everywhere. * Remove the redundant call to stat() in parseExprFromFile(). The file cannot be a symlink because that's the exit condition of the loop before.
2010-06-01 * Turn build errors during evaluation into EvalErrors.Eelco Dolstra1-0/+1
2010-05-07 * Store position info for inherited attributes.Eelco Dolstra1-1/+2
2010-05-06 * Store attribute positions in the AST and report duplicate attributeEelco Dolstra1-2/+8
errors with position info. * For all positions, use the position of the first character of the first token, rather than the last character of the first token plus one.
2010-04-22 * Simplify the implementation of `with'. This gives a 7% speedup inEelco Dolstra1-1/+1
evaluating the NixOS system configuration.
2010-04-22 * Check for duplicate attribute names / function arguments. `makeEelco Dolstra1-1/+8
check' now succeeds :-) * An attribute set such as `{ foo = { enable = true; }; foo.port = 23; }' now parses. It was previously rejected, but I'm too lazy to implement the check. (The only reason to reject it is that the reverse, `{ foo.port = 23; foo = { enable = true; }; }', is rejected, which is kind of ugly.)
2010-04-14 * Implemented inherit.Eelco Dolstra1-1/+1
2010-04-14 * Refactoring: move variable uses to a separate class.Eelco Dolstra1-3/+10
2010-04-14 * Implemented withs.Eelco Dolstra1-0/+1
2010-04-14 * After parsing, compute level/displacement pairs for each variableEelco Dolstra1-9/+39
use site, allowing environments to be stores as vectors of values rather than maps. This should speed up evaluation and reduce the number of allocations.
2010-04-14 * Remove more obsolete code.Eelco Dolstra1-11/+0
2010-04-13 * Evaluate lets directly (i.e. without desugaring to `rec { attrs...;Eelco Dolstra1-0/+8
<let-body> = e; }.<let-body>). This prevents the unnecessary allocation of an attribute set.
2010-04-13 * Use a symbol table to represent identifiers and attribute namesEelco Dolstra1-13/+13
efficiently. The symbol table ensures that there is only one copy of each symbol, thus allowing symbols to be compared efficiently using a pointer equality test.
2010-04-12 * Indented strings.Eelco Dolstra1-9/+9
2010-04-12 * More missing constructs.Eelco Dolstra1-5/+33