Skip to content

Instantly share code, notes, and snippets.

@cleverca22
Created October 24, 2015 02:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleverca22/0b2069b7b2d908a10959 to your computer and use it in GitHub Desktop.
Save cleverca22/0b2069b7b2d908a10959 to your computer and use it in GitHub Desktop.
{ 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}\"
''
#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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment