lix/src/libexpr
Eelco Dolstra c273c15cb1 Add primop ‘scopedImport’
‘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-05-26 14:26:29 +02:00
..
attr-path.cc Support quoted attribute names in -A 2013-11-18 10:21:12 +00:00
attr-path.hh Work on Values instead of Exprs 2013-09-03 13:17:51 +00:00
common-opts.cc Warn about missing -I paths 2014-02-26 15:21:56 +01:00
common-opts.hh Use "#pragma once" to prevent repeated header file inclusion 2012-07-18 14:59:03 -04:00
eval-inline.hh forceAttrs: Show position info 2014-04-04 19:11:40 +02:00
eval.cc Provide a more useful error message when a dynamic attr lookup fails 2014-05-15 17:56:24 +02:00
eval.hh Add primop ‘scopedImport’ 2014-05-26 14:26:29 +02:00
get-drvs.cc Show position info in string concatenation / addition errors 2014-04-04 22:19:33 +02:00
get-drvs.hh Drop pointless #include 2014-03-29 22:20:33 +01:00
lexer.l Allow "bare" dynamic attrs 2014-01-14 14:00:15 +01:00
local.mk lexer-tab.o and parser-tab.o require each other's headers 2014-02-18 17:31:30 +01:00
names.cc nix-env: Use wildcard match by default 2013-09-03 16:35:49 +02:00
names.hh Use "#pragma once" to prevent repeated header file inclusion 2012-07-18 14:59:03 -04:00
nix.sdf First attempt to update Nix SDF grammar to match the actual bison grammar 2008-06-04 14:36:46 +00:00
nixexpr.cc forceString: Show position info 2014-04-04 21:14:11 +02:00
nixexpr.hh Add primop ‘scopedImport’ 2014-05-26 14:26:29 +02:00
parser.y Add primop ‘scopedImport’ 2014-05-26 14:26:29 +02:00
primops.cc Add primop ‘scopedImport’ 2014-05-26 14:26:29 +02:00
symbol-table.hh Rename "attribute sets" to "sets" 2013-10-24 16:41:04 +02:00
value-to-json.cc Refactor JSON output 2013-11-19 00:33:06 +01:00
value-to-json.hh Refactor JSON output 2013-11-19 00:33:06 +01:00
value-to-xml.cc Fix compilation on FreeBSD 2012-03-05 22:04:40 +01:00
value-to-xml.hh Use "#pragma once" to prevent repeated header file inclusion 2012-07-18 14:59:03 -04:00
value.hh Fix some clang warnings 2014-01-21 18:29:55 +01:00