lix/src
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
..
boost boost::shared_ptr -> std::shared_ptr 2014-03-30 00:49:23 +01:00
bsdiff-4.3 Fix "make dist" 2014-02-01 14:38:12 +01:00
download-via-ssh Simplify getting use-ssh-substituter from untrusted users 2014-02-26 13:58:46 +01:00
libexpr Add primop ‘scopedImport’ 2014-05-26 14:26:29 +02:00
libmain Ugly hack to allow --argstr values starting with a dash 2014-05-23 14:43:58 +02:00
libstore nix-store -l: Fetch build logs from the Internet 2014-05-21 17:19:36 +02:00
libutil nix-store -l: Fetch build logs from the Internet 2014-05-21 17:19:36 +02:00
nix-daemon Fix compile errors on Illumos 2014-04-03 17:39:57 +02:00
nix-env nix-env: Minor change to '--delete-generations Nd' semantics 2014-04-15 15:34:58 +02:00
nix-hash Remove Automakefiles 2014-02-01 13:54:38 +01:00
nix-instantiate nix-instantiate --eval: Apply auto-arguments if the result is a function 2014-05-13 12:56:48 +02:00
nix-log2xml Remove Automakefiles 2014-02-01 13:54:38 +01:00
nix-store nix-store -l: Fetch build logs from the Internet 2014-05-21 17:19:36 +02:00