cleverca22
10/24/2015 - 2:08 AM

default.nix

#include <iostream>

#include "shared.hh"
#include "eval.hh"
#include "common-opts.hh"
#include "eval-inline.hh"

using namespace nix;

typedef set<Value*> ValuesSeen;

int displ = 0;

void addVarToScope(const Symbol &name, Value &v, Env *env, StaticEnv &staticEnv) {
  staticEnv.vars[name] = displ;
  env->values[displ++] = &v;
}

Expr *parseString(string s, EvalState &state, StaticEnv &staticEnv) {
  Expr *e = state.parseExprFromString(s,".",staticEnv);
  return e;
}

void addAttrsToScope(Value &attrs, EvalState &state, Env *env, StaticEnv &staticEnv) {
  state.forceAttrs(attrs);
  for (auto & i : *attrs.attrs) addVarToScope(i.name, *i.value, env, staticEnv);
}

std::ostream &printValue(std::ostream &str, Value &v, unsigned int maxDepth, ValuesSeen &seen, EvalState &state) {
  str.flush();
  state.forceValue(v);
  switch (v.type) {
  case tAttrs: {
    seen.insert(&v);
    bool isDrv = state.isDerivation(v);
    if (isDrv) {
      str << "<<derivation ";
      Bindings::iterator i = v.attrs->find(state.sDrvPath);
      PathSet context;
      Path drvPath = i != v.attrs->end() ? state.coerceToPath(*i->pos, *i->value, context) : "???";
      str << drvPath << ">>";
    }
  }
  }
}

int main(int argc, char **argv) {
  initNix();
  initGC();
  Strings searchPath;
  EvalState state(searchPath);
  StaticEnv staticEnv(false, &state.staticBaseEnv);
  Env *env;
  int envSize = 32768;

  env = &state.allocEnv(envSize);
  env->up = &state.baseEnv;
  
  Value v1,v2;
  state.evalFile(lookupFileArg(state,"<nixpkgs>"),v1);
  Bindings &bindings(*state.allocBindings(0));
  state.autoCallFunction(bindings,v1,v2);
  addAttrsToScope(v2, state, env, staticEnv);

  Value v;
  Expr *e = parseString("firefox", state, staticEnv);
  e->eval(state, *env, v);
  state.forceValue(v);
  ValuesSeen seen;
  printValue(std::cout,v,2,seen,state);
}
{ nixpkgs ? <nixpkgs>, system ? builtins.currentSystem }:

with import nixpkgs { inherit system; };

let nix = enableDebugging nixUnstable; in

runCommand "test"
  { buildInputs = [ nix boehmgc ]; dontStrip = true; }
  ''
    mkdir -p $out/bin/
    g++ -g -O3 -Wall -std=c++0x -o $out/bin/test1 ${./input.c} -I${nix}/include/nix -lnixformat -lnixutil -lnixstore -lnixexpr -lnixmain -lgc -DNIX_VERSION=\"${(builtins.parseDrvName nix.name).version}\"
  ''