Files
lix/tests
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
..
2014-05-26 14:26:29 +02:00
2014-02-26 18:59:01 +01:00
2013-10-17 11:18:37 +02:00
2014-02-17 12:22:50 +01:00
2014-02-26 18:00:46 +01:00
2006-07-21 13:21:43 +00:00
2014-05-02 19:02:10 +02:00
2014-02-26 19:12:31 +01:00
2014-05-22 11:38:50 +02:00
2014-02-26 17:58:53 +01:00
2014-04-15 15:32:27 +02:00
2014-02-01 15:37:50 +01:00
2012-09-12 11:29:10 -04:00
2014-02-26 17:53:51 +01:00
2014-02-06 13:54:44 +01:00
2014-04-15 15:32:27 +02:00
Doh
2014-02-26 22:41:29 +01:00
2007-08-13 13:15:02 +00:00
2013-09-02 12:44:30 +02:00
2012-12-04 14:47:50 +01:00
2012-12-04 14:47:50 +01:00
2014-02-26 18:45:26 +01:00