about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-09-04T21·06+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-09-04T21·06+0000
commit75068e7d753cf6cbe45a4bf294000dca9bd41d8b (patch)
treec6274cc10caab08349b5585206034f41ca4a575f
parentaab88127321344d5818d823bff515d127108d058 (diff)
* Use a proper namespace.
* Optimise header file usage a bit.
* Compile the parser as C++.

-rwxr-xr-xsrc/aterm-helper.pl10
-rw-r--r--src/libexpr/Makefile.am8
-rw-r--r--src/libexpr/attr-path.cc7
-rw-r--r--src/libexpr/attr-path.hh6
-rw-r--r--src/libexpr/eval.cc10
-rw-r--r--src/libexpr/eval.hh16
-rw-r--r--src/libexpr/expr-to-xml.cc9
-rw-r--r--src/libexpr/expr-to-xml.hh6
-rw-r--r--src/libexpr/get-drvs.cc9
-rw-r--r--src/libexpr/get-drvs.hh10
-rw-r--r--src/libexpr/lexer.l8
-rw-r--r--src/libexpr/nixexpr.cc8
-rw-r--r--src/libexpr/nixexpr.hh8
-rw-r--r--src/libexpr/parser.cc40
-rw-r--r--src/libexpr/parser.hh6
-rw-r--r--src/libexpr/parser.y47
-rw-r--r--src/libexpr/primops.cc16
-rw-r--r--src/libmain/shared.cc27
-rw-r--r--src/libmain/shared.hh14
-rw-r--r--src/libstore/build.cc53
-rw-r--r--src/libstore/build.hh9
-rw-r--r--src/libstore/db.cc18
-rw-r--r--src/libstore/db.hh16
-rw-r--r--src/libstore/derivations.cc7
-rw-r--r--src/libstore/derivations.hh16
-rw-r--r--src/libstore/gc.cc13
-rw-r--r--src/libstore/gc.hh8
-rw-r--r--src/libstore/globals.cc11
-rw-r--r--src/libstore/globals.hh11
-rw-r--r--src/libstore/misc.cc9
-rw-r--r--src/libstore/misc.hh6
-rw-r--r--src/libstore/pathlocks.cc11
-rw-r--r--src/libstore/pathlocks.hh10
-rw-r--r--src/libstore/references.cc15
-rw-r--r--src/libstore/references.hh6
-rw-r--r--src/libstore/store.cc46
-rw-r--r--src/libstore/store.hh10
-rw-r--r--src/libutil/Makefile.am3
-rw-r--r--src/libutil/archive.cc5
-rw-r--r--src/libutil/archive.hh14
-rw-r--r--src/libutil/aterm-map.cc13
-rw-r--r--src/libutil/aterm-map.hh6
-rw-r--r--src/libutil/aterm.cc14
-rw-r--r--src/libutil/aterm.hh15
-rw-r--r--src/libutil/hash.cc11
-rw-r--r--src/libutil/hash.hh8
-rw-r--r--src/libutil/types.hh73
-rw-r--r--src/libutil/util.cc26
-rw-r--r--src/libutil/util.hh59
-rw-r--r--src/libutil/xml-writer.cc8
-rw-r--r--src/libutil/xml-writer.hh14
-rw-r--r--src/nix-env/main.cc19
-rw-r--r--src/nix-env/names.cc7
-rw-r--r--src/nix-env/names.hh9
-rw-r--r--src/nix-env/profiles.cc8
-rw-r--r--src/nix-env/profiles.hh8
-rw-r--r--src/nix-hash/nix-hash.cc9
-rw-r--r--src/nix-instantiate/main.cc15
-rw-r--r--src/nix-store/dotgraph.cc14
-rw-r--r--src/nix-store/dotgraph.hh6
-rw-r--r--src/nix-store/main.cc10
61 files changed, 648 insertions, 266 deletions
diff --git a/src/aterm-helper.pl b/src/aterm-helper.pl
index 3bb466fe3bb8..54eb33abda8e 100755
--- a/src/aterm-helper.pl
+++ b/src/aterm-helper.pl
@@ -40,6 +40,11 @@ my $initFun = "init";
 open HEADER, ">$ARGV[0]";
 open IMPL, ">$ARGV[1]";
 
+print HEADER "#ifdef __cplusplus\n";
+print HEADER "namespace nix {\n";
+print HEADER "#endif\n\n\n";
+print IMPL "namespace nix {\n";
+
 while (<STDIN>) {
     next if (/^\s*$/);
     
@@ -162,5 +167,10 @@ print IMPL "void $initFun() {\n";
 print IMPL "$init";
 print IMPL "}\n";
 
+print HEADER "#ifdef __cplusplus\n";
+print HEADER "}\n";
+print HEADER "#endif\n\n\n";
+print IMPL "}\n";
+
 close HEADER;
 close IMPL;
diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am
index f0850425994f..3696e99d54f6 100644
--- a/src/libexpr/Makefile.am
+++ b/src/libexpr/Makefile.am
@@ -2,13 +2,13 @@ pkglib_LTLIBRARIES = libexpr.la
 
 libexpr_la_SOURCES = nixexpr.cc nixexpr.hh parser.cc parser.hh \
  eval.cc eval.hh primops.cc \
- lexer-tab.c lexer-tab.h parser-tab.c parser-tab.h \
+ lexer-tab.c lexer-tab.h parser-tab.cc parser-tab.hh \
  get-drvs.cc get-drvs.hh \
  attr-path.cc attr-path.hh \
  expr-to-xml.cc expr-to-xml.hh 
 
 BUILT_SOURCES = nixexpr-ast.cc nixexpr-ast.hh \
- parser-tab.h lexer-tab.h parser-tab.c lexer-tab.c
+ parser-tab.hh lexer-tab.h parser-tab.cc lexer-tab.c
 
 EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc
 
@@ -21,8 +21,8 @@ AM_CFLAGS = \
 
 # Parser generation.
 
-parser-tab.c parser-tab.h: parser.y
-	$(bison) -v -o parser-tab.c $(srcdir)/parser.y -d
+parser-tab.cc parser-tab.hh: parser.y
+	$(bison) -v -o parser-tab.cc $(srcdir)/parser.y -d
 
 lexer-tab.c lexer-tab.h: lexer.l
 	$(flex) --outfile lexer-tab.c --header-file=lexer-tab.h $(srcdir)/lexer.l 
diff --git a/src/libexpr/attr-path.cc b/src/libexpr/attr-path.cc
index 7228adf95d01..8606da559771 100644
--- a/src/libexpr/attr-path.cc
+++ b/src/libexpr/attr-path.cc
@@ -1,5 +1,9 @@
 #include "attr-path.hh"
 #include "nixexpr-ast.hh"
+#include "util.hh"
+
+
+namespace nix {
 
 
 bool isAttrs(EvalState & state, Expr e, ATermMap & attrs)
@@ -73,3 +77,6 @@ Expr findAlongAttrPath(EvalState & state, const string & attrPath,
     
     return e;
 }
+
+ 
+}
diff --git a/src/libexpr/attr-path.hh b/src/libexpr/attr-path.hh
index 0797ecc58218..7abaa83a0167 100644
--- a/src/libexpr/attr-path.hh
+++ b/src/libexpr/attr-path.hh
@@ -7,8 +7,14 @@
 #include "eval.hh"
 
 
+namespace nix {
+
+    
 Expr findAlongAttrPath(EvalState & state, const string & attrPath,
     const ATermMap & autoArgs, Expr e);
 
+    
+}
+
 
 #endif /* !__ATTR_PATH_H */
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index aacdc1c8ebf1..02df4a4a325a 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -1,8 +1,13 @@
 #include "eval.hh"
 #include "parser.hh"
+#include "hash.hh"
+#include "util.hh"
 #include "nixexpr-ast.hh"
 
 
+namespace nix {
+    
+
 EvalState::EvalState()
     : normalForms(32768), primOps(128)
 {
@@ -271,7 +276,7 @@ Expr wrapInContext(ATermList context, Expr e)
 static ATerm concatStrings(EvalState & state, const ATermVector & args)
 {
     ATermList context = ATempty;
-    ostringstream s;
+    std::ostringstream s;
     bool isPath = false;
 
     for (ATermVector::const_iterator i = args.begin(); i != args.end(); ++i) {
@@ -666,3 +671,6 @@ void printEvalStats(EvalState & state)
     if (showStats)
         printATermMapStats();
 }
+
+ 
+}
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index c95a309359af..b34e91055a37 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -4,16 +4,21 @@
 #include <map>
 
 #include "aterm.hh"
-#include "hash.hh"
 #include "nixexpr.hh"
 
 
-typedef map<Path, PathSet> DrvRoots;
-typedef map<Path, Hash> DrvHashes;
+namespace nix {
+
+
+class Hash;
+    
+
+typedef std::map<Path, PathSet> DrvRoots;
+typedef std::map<Path, Hash> DrvHashes;
 
 /* Cache for calls to addToStore(); maps source paths to the store
    paths. */
-typedef map<Path, Path> SrcToStore;
+typedef std::map<Path, Path> SrcToStore;
 
 struct EvalState;
 
@@ -74,5 +79,8 @@ Expr autoCallFunction(Expr e, const ATermMap & args);
 /* Print statistics. */
 void printEvalStats(EvalState & state);
 
+ 
+}
+
 
 #endif /* !__EVAL_H */
diff --git a/src/libexpr/expr-to-xml.cc b/src/libexpr/expr-to-xml.cc
index cd9112a425e2..b84c0058e236 100644
--- a/src/libexpr/expr-to-xml.cc
+++ b/src/libexpr/expr-to-xml.cc
@@ -1,10 +1,12 @@
 #include "expr-to-xml.hh"
-
 #include "xml-writer.hh"
 #include "nixexpr-ast.hh"
 #include "aterm.hh"
 
 
+namespace nix {
+
+    
 static XMLAttrs singletonAttrs(const string & name, const string & value)
 {
     XMLAttrs attrs;
@@ -84,9 +86,12 @@ static void printTermAsXML(Expr e, XMLWriter & doc)
 }
 
 
-void printTermAsXML(Expr e, ostream & out)
+void printTermAsXML(Expr e, std::ostream & out)
 {
     XMLWriter doc(true, out);
     XMLOpenElement root(doc, "expr");
     printTermAsXML(e, doc);
 }
+
+ 
+}
diff --git a/src/libexpr/expr-to-xml.hh b/src/libexpr/expr-to-xml.hh
index 56f947e520de..6b95c88f5909 100644
--- a/src/libexpr/expr-to-xml.hh
+++ b/src/libexpr/expr-to-xml.hh
@@ -6,8 +6,10 @@
 
 #include "nixexpr.hh"
 
+namespace nix {
 
-void printTermAsXML(Expr e, ostream & out);
-
+void printTermAsXML(Expr e, std::ostream & out);
+    
+}
 
 #endif /* !__EXPR_TO_XML_H */
diff --git a/src/libexpr/get-drvs.cc b/src/libexpr/get-drvs.cc
index 07dd88e4c7b8..808e12ffd541 100644
--- a/src/libexpr/get-drvs.cc
+++ b/src/libexpr/get-drvs.cc
@@ -1,5 +1,9 @@
 #include "get-drvs.hh"
 #include "nixexpr-ast.hh"
+#include "util.hh"
+
+
+namespace nix {
 
 
 string DrvInfo::queryDrvPath(EvalState & state) const
@@ -66,7 +70,7 @@ static bool getDerivation(EvalState & state, Expr e,
         e = evalExpr(state, e);
         if (!matchAttrs(e, es)) return true;
 
-        shared_ptr<ATermMap> attrs(new ATermMap(32)); /* !!! */
+        boost::shared_ptr<ATermMap> attrs(new ATermMap(32)); /* !!! */
         queryAllAttrs(e, *attrs, false);
     
         Expr a = attrs->get(toATerm("type"));
@@ -183,3 +187,6 @@ void getDerivations(EvalState & state, Expr e, const string & pathPrefix,
     Exprs doneExprs;
     getDerivations(state, e, pathPrefix, autoArgs, drvs, doneExprs);
 }
+
+ 
+}
diff --git a/src/libexpr/get-drvs.hh b/src/libexpr/get-drvs.hh
index 75f6bcf9d796..3dac56a4f301 100644
--- a/src/libexpr/get-drvs.hh
+++ b/src/libexpr/get-drvs.hh
@@ -9,7 +9,10 @@
 #include "eval.hh"
 
 
-typedef map<string, string> MetaInfo;
+namespace nix {
+
+
+typedef std::map<string, string> MetaInfo;
 
 
 struct DrvInfo
@@ -23,7 +26,7 @@ public:
     string attrPath; /* path towards the derivation */
     string system;
 
-    shared_ptr<ATermMap> attrs;
+    boost::shared_ptr<ATermMap> attrs;
 
     string queryDrvPath(EvalState & state) const;
     string queryOutPath(EvalState & state) const;
@@ -52,5 +55,8 @@ bool getDerivation(EvalState & state, Expr e, DrvInfo & drv);
 void getDerivations(EvalState & state, Expr e, const string & pathPrefix,
     const ATermMap & autoArgs, DrvInfos & drvs);
 
+ 
+}
+
 
 #endif /* !__GET_DRVS_H */
diff --git a/src/libexpr/lexer.l b/src/libexpr/lexer.l
index fb19e16b457c..bbf872ff6ec8 100644
--- a/src/libexpr/lexer.l
+++ b/src/libexpr/lexer.l
@@ -9,7 +9,7 @@
 %{
 #include <string.h>
 #include <aterm2.h>
-#include "parser-tab.h"
+#include "parser-tab.hh"
 
 static void initLoc(YYLTYPE * loc)
 {
@@ -35,7 +35,11 @@ static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
     }
 }
 
-ATerm toATerm(const char * s);
+ATerm toATerm(const char * s)
+{
+    return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));
+}
+
 ATerm unescapeStr(const char * s);
 
 #define YY_USER_INIT initLoc(yylloc)
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index fac2c0e5c0b7..b7ccb656cf35 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -1,11 +1,14 @@
 #include "nixexpr.hh"
 #include "derivations.hh"
-
+#include "util.hh"
 
 #include "nixexpr-ast.hh"
 #include "nixexpr-ast.cc"
 
 
+namespace nix {
+    
+
 string showPos(ATerm pos)
 {
     ATerm path;
@@ -332,3 +335,6 @@ string showValue(Expr e)
     /* !!! incomplete */
     return "<unknown>";
 }
+
+ 
+}
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index 3eb4d4cb2bec..af39997c5ec1 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -6,7 +6,10 @@
 #include <aterm2.h>
 
 #include "aterm-map.hh"
-#include "util.hh"
+#include "types.hh"
+
+
+namespace nix {
 
 
 MakeError(EvalError, Error)
@@ -95,5 +98,8 @@ string showType(Expr e);
 
 string showValue(Expr e);
 
+ 
+}
+
 
 #endif /* !__NIXEXPR_H */
diff --git a/src/libexpr/parser.cc b/src/libexpr/parser.cc
index fa6c4e2f3225..20a6c9be2efd 100644
--- a/src/libexpr/parser.cc
+++ b/src/libexpr/parser.cc
@@ -1,3 +1,8 @@
+#include "parser.hh"
+#include "aterm.hh"
+#include "util.hh"
+#include "nixexpr-ast.hh"
+    
 #include <sstream>
 
 #include <sys/types.h>
@@ -5,9 +10,15 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-#include "aterm.hh"
-#include "parser.hh"
-#include "nixexpr-ast.hh"
+
+extern "C" {
+
+#include "parser-tab.hh"
+#include "lexer-tab.h"
+    
+}
+
+namespace nix {
 
 
 struct ParseData 
@@ -17,16 +28,15 @@ struct ParseData
     Path path;
     string error;
 };
+ 
+}
 
 
-extern "C" {
+int yyparse(yyscan_t scanner, nix::ParseData * data);
+
+
+namespace nix {
 
-#include "parser-tab.h"
-#include "lexer-tab.h"
-    
-/* Callbacks for getting from C to C++.  Due to a (small) bug in the
-   GLR code of Bison we cannot currently compile the parser as C++
-   code. */
 
 void setParseResult(ParseData * data, ATerm t)
 {
@@ -71,6 +81,7 @@ const char * getPath(ParseData * data)
     return data->path.c_str();
 }
 
+extern "C" {
 Expr unescapeStr(const char * s)
 {
     string t;
@@ -93,11 +104,7 @@ Expr unescapeStr(const char * s)
     }
     return makeStr(toATerm(t));
 }
-
-int yyparse(yyscan_t scanner, ParseData * data);
-
-
-} /* end of C functions */
+}
 
 
 static void checkAttrs(ATermMap & names, ATermList bnds)
@@ -232,3 +239,6 @@ Expr parseExprFromString(EvalState & state,
 {
     return parse(state, s.c_str(), "(string)", basePath);
 }
+
+ 
+}
diff --git a/src/libexpr/parser.hh b/src/libexpr/parser.hh
index 2af5385f6275..334822b5e0e4 100644
--- a/src/libexpr/parser.hh
+++ b/src/libexpr/parser.hh
@@ -4,6 +4,9 @@
 #include "eval.hh"
 
 
+namespace nix {
+
+
 /* Parse a Nix expression from the specified file.  If `path' refers
    to a directory, the "/default.nix" is appended. */
 Expr parseExprFromFile(EvalState & state, Path path);
@@ -13,4 +16,7 @@ Expr parseExprFromString(EvalState & state, const string & s,
     const Path & basePath);
 
 
+}
+
+
 #endif /* !__PARSER_H */
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index c1e3b48a79a1..7714a5911165 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -3,7 +3,7 @@
 %locations
 %error-verbose
 %parse-param { yyscan_t scanner }
-%parse-param { void * data }
+%parse-param { ParseData * data }
 %lex-param { yyscan_t scanner }
 
 %{
@@ -12,34 +12,47 @@
 #include <string.h>
 #include <aterm2.h>
 
-#include "parser-tab.h"
+#include "parser-tab.hh"
+extern "C" {
 #include "lexer-tab.h"
+}
 
-typedef ATerm Expr;
-typedef ATerm ValidValues;
-typedef ATerm DefaultValue;
-typedef ATerm Pos;
+#include "aterm.hh"
     
+#include "nixexpr.hh"
 #include "nixexpr-ast.hh"
 
-void setParseResult(void * data, ATerm t);
-void parseError(void * data, char * error, int line, int column);
-ATerm absParsedPath(void * data, ATerm t);
-ATerm fixAttrs(int recursive, ATermList as);
-const char * getPath(void * data);
-void backToString(yyscan_t scanner);
+using namespace nix;
 
-void yyerror(YYLTYPE * loc, yyscan_t scanner, void * data, char * s)
+namespace nix {
+ 
+struct ParseData 
 {
-    parseError(data, s, loc->first_line, loc->first_column);
+    Expr result;
+    Path basePath;
+    Path path;
+    string error;
+};
+
+void setParseResult(ParseData * data, ATerm t);
+void parseError(ParseData * data, char * error, int line, int column);
+ATerm absParsedPath(ParseData * data, ATerm t);
+ATerm fixAttrs(int recursive, ATermList as);
+const char * getPath(ParseData * data);
+Expr unescapeStr(const char * s);
+ 
+extern "C" {
+    void backToString(yyscan_t scanner);
+}
+ 
 }
 
-ATerm toATerm(const char * s)
+void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, char * s)
 {
-    return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));
+    parseError(data, s, loc->first_line, loc->first_column);
 }
 
-static Pos makeCurPos(YYLTYPE * loc, void * data)
+static Pos makeCurPos(YYLTYPE * loc, ParseData * data)
 {
     return makePos(toATerm(getPath(data)),
         loc->first_line, loc->first_column);
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 5dfe90a8f96c..3f915fc232a9 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -1,11 +1,16 @@
-#include <algorithm>
-
 #include "build.hh"
 #include "misc.hh"
 #include "eval.hh"
 #include "globals.hh"
-#include "nixexpr-ast.hh"
+#include "store.hh"
+#include "util.hh"
 #include "expr-to-xml.hh"
+#include "nixexpr-ast.hh"
+
+#include <algorithm>
+
+
+namespace nix {
 
 
 static Expr primBuiltins(EvalState & state, const ATermVector & args)
@@ -472,7 +477,7 @@ static Expr primToString(EvalState & state, const ATermVector & args)
    be sensibly or completely represented (e.g., functions). */
 static Expr primToXML(EvalState & state, const ATermVector & args)
 {
-    ostringstream out;
+    std::ostringstream out;
     printTermAsXML(strictEvalExpr(state, args[0]), out);
     return makeStr(toATerm(out.str()));
 }
@@ -746,3 +751,6 @@ void EvalState::addPrimOps()
     addPrimOp("removeAttrs", 2, primRemoveAttrs);
     addPrimOp("relativise", 2, primRelativise);
 }
+
+ 
+}
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index 71be0dd5d0ef..1987050dd510 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -1,3 +1,11 @@
+#include "shared.hh"
+#include "globals.hh"
+#include "gc.hh"
+#include "store.hh"
+#include "util.hh"
+
+#include "config.h"
+
 #include <iostream>
 #include <cctype>
 
@@ -12,12 +20,8 @@ extern "C" {
 #include <aterm2.h>
 }
 
-#include "globals.hh"
-#include "gc.hh"
-#include "store.hh"
-#include "shared.hh"
 
-#include "config.h"
+namespace nix {
 
 
 volatile sig_atomic_t blockInt = 0;
@@ -173,7 +177,7 @@ static void initAndRun(int argc, char * * argv)
             return;
         }
         else if (arg == "--version") {
-            cout << format("%1% (Nix) %2%") % programId % NIX_VERSION << endl;
+            std::cout << format("%1% (Nix) %2%") % programId % NIX_VERSION << std::endl;
             return;
         }
         else if (arg == "--keep-failed" || arg == "-K")
@@ -338,10 +342,15 @@ void switchToNixUser()
 }
 
 
+}
+
+
 static char buf[1024];
 
 int main(int argc, char * * argv)
 {
+    using namespace nix;
+    
     /* If we are setuid root, we have to get rid of the excess
        privileges ASAP. */
     switchToNixUser();
@@ -352,7 +361,7 @@ int main(int argc, char * * argv)
 
     /* Turn on buffering for cerr. */
 #if HAVE_PUBSETBUF
-    cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
+    std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
 #endif
 
     try {
@@ -377,10 +386,12 @@ int main(int argc, char * * argv)
     } catch (Error & e) {
         printMsg(lvlError, format("error: %1%") % e.msg());
         return 1;
-    } catch (exception & e) {
+    } catch (std::exception & e) {
         printMsg(lvlError, format("error: %1%") % e.what());
         return 1;
     }
 
     return 0;
 }
+
+ 
diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh
index 82da7550626f..16162604a914 100644
--- a/src/libmain/shared.hh
+++ b/src/libmain/shared.hh
@@ -1,9 +1,7 @@
 #ifndef __SHARED_H
 #define __SHARED_H
 
-#include <string>
-
-#include "util.hh"
+#include "types.hh"
 
 
 /* These are not implemented here, but must be implemented by a
@@ -12,17 +10,21 @@
 /* Main program.  Called by main() after the ATerm library has been
    initialised and some default arguments have been processed (and
    removed from `args').  main() will catch all exceptions. */
-void run(Strings args);
+void run(nix::Strings args);
 
 /* Should print a help message to stdout and return. */
 void printHelp();
 
+extern std::string programId;
+
+
+namespace nix {
+
 /* Ugh.  No better place to put this. */
 Path makeRootName(const Path & gcRoot, int & counter);
 void printGCWarning();
 
-
-extern string programId;
+}
 
 
 #endif /* !__SHARED_H */
diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index c953707a5126..647671036de1 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -1,3 +1,13 @@
+#include "build.hh"
+#include "references.hh"
+#include "pathlocks.hh"
+#include "misc.hh"
+#include "globals.hh"
+#include "gc.hh"
+#include "store.hh"
+#include "db.hh"
+#include "util.hh"
+
 #include <map>
 #include <iostream>
 #include <sstream>
@@ -15,13 +25,11 @@
 #include <pwd.h>
 #include <grp.h>
 
-#include "build.hh"
-#include "references.hh"
-#include "pathlocks.hh"
-#include "misc.hh"
-#include "globals.hh"
-#include "gc.hh"
 
+namespace nix {
+
+using std::map;
+    
 
 /* !!! TODO derivationFromPath shouldn't be used here */
 
@@ -38,8 +46,8 @@ class Worker;
 
 /* A pointer to a goal. */
 class Goal;
-typedef shared_ptr<Goal> GoalPtr;
-typedef weak_ptr<Goal> WeakGoalPtr;
+typedef boost::shared_ptr<Goal> GoalPtr;
+typedef boost::weak_ptr<Goal> WeakGoalPtr;
 
 /* Set of goals. */
 typedef set<GoalPtr> Goals;
@@ -50,7 +58,7 @@ typedef map<Path, WeakGoalPtr> WeakGoalMap;
 
 
 
-class Goal : public enable_shared_from_this<Goal>
+class Goal : public boost::enable_shared_from_this<Goal>
 {
 public:
     typedef enum {ecBusy, ecSuccess, ecFailed} ExitCode;
@@ -447,8 +455,8 @@ static void killUser(uid_t uid)
             if (kill(-1, SIGKILL) == -1)
                 throw SysError(format("cannot kill processes for UID `%1%'") % uid);
         
-        } catch (exception & e) {
-            cerr << format("build error: %1%\n") % e.what();
+        } catch (std::exception & e) {
+            std::cerr << format("build error: %1%\n") % e.what();
             quickExit(1);
         }
         quickExit(0);
@@ -930,8 +938,8 @@ DerivationGoal::HookReply DerivationGoal::tryBuildHook()
             
             throw SysError(format("executing `%1%'") % buildHook);
             
-        } catch (exception & e) {
-            cerr << format("build error: %1%\n") % e.what();
+        } catch (std::exception & e) {
+            std::cerr << format("build error: %1%\n") % e.what();
         }
         quickExit(1);
     }
@@ -1326,8 +1334,8 @@ void DerivationGoal::startBuilder()
             throw SysError(format("executing `%1%'")
                 % drv.builder);
             
-        } catch (exception & e) {
-            cerr << format("build error: %1%\n") % e.what();
+        } catch (std::exception & e) {
+            std::cerr << format("build error: %1%\n") % e.what();
         }
         quickExit(1);
     }
@@ -1593,7 +1601,7 @@ private:
     Pid pid;
 
     /* Lock on the store path. */
-    shared_ptr<PathLocks> outputLock;
+    boost::shared_ptr<PathLocks> outputLock;
     
     typedef void (SubstitutionGoal::*GoalState)();
     GoalState state;
@@ -1719,7 +1727,7 @@ void SubstitutionGoal::tryToRun()
     }
 
     /* Acquire a lock on the output path. */
-    outputLock = shared_ptr<PathLocks>(new PathLocks);
+    outputLock = boost::shared_ptr<PathLocks>(new PathLocks);
     outputLock->lockPaths(singleton<PathSet>(storePath),
         (format("waiting for lock on `%1%'") % storePath).str());
 
@@ -1767,8 +1775,8 @@ void SubstitutionGoal::tryToRun()
             
             throw SysError(format("executing `%1%'") % sub.program);
             
-        } catch (exception & e) {
-            cerr << format("substitute error: %1%\n") % e.what();
+        } catch (std::exception & e) {
+            std::cerr << format("substitute error: %1%\n") % e.what();
         }
         quickExit(1);
     }
@@ -1930,8 +1938,8 @@ static void removeGoal(GoalPtr goal, WeakGoalMap & goalMap)
 
 void Worker::removeGoal(GoalPtr goal)
 {
-    ::removeGoal(goal, derivationGoals);
-    ::removeGoal(goal, substitutionGoals);
+    nix::removeGoal(goal, derivationGoals);
+    nix::removeGoal(goal, substitutionGoals);
     if (topGoals.find(goal) != topGoals.end()) {
         topGoals.erase(goal);
         /* If a top-level goal failed, then kill all other goals
@@ -2160,3 +2168,6 @@ void ensurePath(const Path & path)
     if (goal->getExitCode() != Goal::ecSuccess)
         throw Error(format("path `%1%' does not exist and cannot be created") % path);
 }
+
+ 
+}
diff --git a/src/libstore/build.hh b/src/libstore/build.hh
index 489a6cabc8a5..c90c126769ef 100644
--- a/src/libstore/build.hh
+++ b/src/libstore/build.hh
@@ -1,8 +1,13 @@
 #ifndef __BUILD_H
 #define __BUILD_H
 
-#include "derivations.hh"
 
+#include "types.hh"
+
+
+namespace nix {
+
+    
 /* Ensure that the output paths of the derivation are valid.  If they
    are already valid, this is a no-op.  Otherwise, validity can
    be reached in two ways.  First, if the output paths have
@@ -16,5 +21,7 @@ void buildDerivations(const PathSet & drvPaths);
 void ensurePath(const Path & storePath);
 
     
+}
+
 
 #endif /* !__BUILD_H */
diff --git a/src/libstore/db.cc b/src/libstore/db.cc
index 7f428dc488e6..ca5d0582a099 100644
--- a/src/libstore/db.cc
+++ b/src/libstore/db.cc
@@ -1,3 +1,7 @@
+#include "db.hh"
+#include "util.hh"
+#include "pathlocks.hh"
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -7,9 +11,8 @@
 
 #include <db_cxx.h>
 
-#include "db.hh"
-#include "util.hh"
-#include "pathlocks.hh"
+
+namespace nix {
 
 
 /* Wrapper class to ensure proper destruction. */
@@ -112,7 +115,7 @@ Db * Database::getDb(TableId table)
     if (table == 0)
         throw Error("database table is not open "
             "(maybe you don't have sufficient permission?)");
-    map<TableId, Db *>::iterator i = tables.find(table);
+    std::map<TableId, Db *>::iterator i = tables.find(table);
     if (i == tables.end())
         throw Error("unknown table id");
     return i->second;
@@ -263,10 +266,10 @@ void Database::close()
 
     try {
 
-        for (map<TableId, Db *>::iterator i = tables.begin();
+        for (std::map<TableId, Db *>::iterator i = tables.begin();
              i != tables.end(); )
         {
-            map<TableId, Db *>::iterator j = i;
+            std::map<TableId, Db *>::iterator j = i;
             ++j;
             closeTable(i->first);
             i = j;
@@ -433,3 +436,6 @@ void Database::enumTable(const Transaction & txn, TableId table,
 
     } catch (DbException e) { rethrow(e); }
 }
+
+ 
+}
diff --git a/src/libstore/db.hh b/src/libstore/db.hh
index 0315728de597..54f490f885d5 100644
--- a/src/libstore/db.hh
+++ b/src/libstore/db.hh
@@ -1,13 +1,9 @@
 #ifndef __DB_H
 #define __DB_H
 
-#include <string>
-#include <list>
-#include <map>
-
-#include "util.hh"
+#include "types.hh"
 
-using namespace std;
+#include <map>
 
 
 /* Defined externally. */
@@ -16,6 +12,9 @@ class DbEnv;
 class Db;
 
 
+namespace nix {
+
+
 class Database;
 
 
@@ -53,7 +52,7 @@ private:
     DbEnv * env;
 
     TableId nextId;
-    map<TableId, Db *> tables;
+    std::map<TableId, Db *> tables;
 
     void requireEnv();
 
@@ -99,5 +98,8 @@ public:
     DbNoPermission(const format & f) : Error(f) { };
 };
 
+ 
+}
+
 
 #endif /* !__DB_H */
diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc
index e2d81bda83e7..d6291cd45ac3 100644
--- a/src/libstore/derivations.cc
+++ b/src/libstore/derivations.cc
@@ -1,11 +1,13 @@
 #include "derivations.hh"
-#include "globals.hh"
 #include "store.hh"
 
 #include "derivations-ast.hh"
 #include "derivations-ast.cc"
 
 
+namespace nix {
+
+
 Hash hashTerm(ATerm t)
 {
     return hashString(htSHA256, atPrint(t));
@@ -170,3 +172,6 @@ bool isDerivation(const string & fileName)
         fileName.size() >= drvExtension.size() &&
         string(fileName, fileName.size() - drvExtension.size()) == drvExtension;
 }
+
+ 
+}
diff --git a/src/libstore/derivations.hh b/src/libstore/derivations.hh
index 9358db2edae0..d1c9db50849f 100644
--- a/src/libstore/derivations.hh
+++ b/src/libstore/derivations.hh
@@ -2,7 +2,12 @@
 #define __DERIVATIONS_H
 
 #include "aterm.hh"
-#include "store.hh"
+#include "hash.hh"
+
+#include <map>
+
+
+namespace nix {
 
 
 /* Extension of derivations in the Nix store. */
@@ -27,13 +32,13 @@ struct DerivationOutput
     }
 };
 
-typedef map<string, DerivationOutput> DerivationOutputs;
+typedef std::map<string, DerivationOutput> DerivationOutputs;
 
 /* For inputs that are sub-derivations, we specify exactly which
    output IDs we are interested in. */
-typedef map<Path, StringSet> DerivationInputs;
+typedef std::map<Path, StringSet> DerivationInputs;
 
-typedef map<string, string> StringPairs;
+typedef std::map<string, string> StringPairs;
 
 struct Derivation
 {
@@ -63,5 +68,8 @@ ATerm unparseDerivation(const Derivation & drv);
    derivations. */
 bool isDerivation(const string & fileName);
 
+ 
+}
+
 
 #endif /* !__DERIVATIONS_H */
diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc
index 37fde29ca035..59e71daa0b46 100644
--- a/src/libstore/gc.cc
+++ b/src/libstore/gc.cc
@@ -1,7 +1,10 @@
-#include "globals.hh"
 #include "gc.hh"
+#include "globals.hh"
 #include "misc.hh"
 #include "pathlocks.hh"
+#include "store.hh"
+#include "db.hh"
+#include "util.hh"
 
 #include <boost/shared_ptr.hpp>
 
@@ -17,6 +20,9 @@
 #endif
 
 
+namespace nix {
+
+
 static string gcLockName = "gc.lock";
 static string tempRootsDir = "temproots";
 static string gcRootsDir = "gcroots";
@@ -192,7 +198,7 @@ void removeTempRoots()
 }
 
 
-typedef shared_ptr<AutoCloseFD> FDPtr;
+typedef boost::shared_ptr<AutoCloseFD> FDPtr;
 typedef list<FDPtr> FDs;
 
 
@@ -558,3 +564,6 @@ void collectGarbage(GCAction action, const PathSet & pathsToDelete,
         }
     }
 }
+
+ 
+}
diff --git a/src/libstore/gc.hh b/src/libstore/gc.hh
index c6b13bc471ac..54fc4dec4beb 100644
--- a/src/libstore/gc.hh
+++ b/src/libstore/gc.hh
@@ -1,7 +1,10 @@
 #ifndef __GC_H
 #define __GC_H
 
-#include "util.hh"
+#include "types.hh"
+
+
+namespace nix {
 
 
 /* Garbage collector operation. */
@@ -39,4 +42,7 @@ Path addPermRoot(const Path & storePath, const Path & gcRoot,
     bool indirect);
 
 
+}
+
+
 #endif /* !__GC_H */
diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc
index f61fe167d870..466d0e0b26d7 100644
--- a/src/libstore/globals.cc
+++ b/src/libstore/globals.cc
@@ -1,9 +1,13 @@
 #include "globals.hh"
+#include "util.hh"
 
 #include <map>
 #include <algorithm>
 
 
+namespace nix {
+
+
 string nixStore = "/UNINIT";
 string nixDataDir = "/UNINIT";
 string nixLogDir = "/UNINIT";
@@ -23,7 +27,7 @@ string thisSystem = "unset";
 
 static bool settingsRead = false;
 
-static map<string, Strings> settings;
+static std::map<string, Strings> settings;
 
 
 string & at(Strings & ss, unsigned int n)
@@ -72,7 +76,7 @@ static void readSettings()
 Strings querySetting(const string & name, const Strings & def)
 {
     if (!settingsRead) readSettings();
-    map<string, Strings>::iterator i = settings.find(name);
+    std::map<string, Strings>::iterator i = settings.find(name);
     return i == settings.end() ? def : i->second;
 }
 
@@ -98,3 +102,6 @@ bool queryBoolSetting(const string & name, bool def)
     else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'")
         % name % v);
 }
+
+ 
+}
diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh
index 03aaa13d8917..9441dc344a73 100644
--- a/src/libstore/globals.hh
+++ b/src/libstore/globals.hh
@@ -1,11 +1,11 @@
 #ifndef __GLOBALS_H
 #define __GLOBALS_H
 
-#include <string>
-#include <set>
-#include "util.hh"
+#include "types.hh"
+
+
+namespace nix {
 
-using namespace std;
 
 /* Path names. */
 
@@ -67,5 +67,8 @@ string querySetting(const string & name, const string & def);
 
 bool queryBoolSetting(const string & name, bool def);
 
+    
+}
+
 
 #endif /* !__GLOBALS_H */
diff --git a/src/libstore/misc.cc b/src/libstore/misc.cc
index 91cf25f27187..156d059b6f30 100644
--- a/src/libstore/misc.cc
+++ b/src/libstore/misc.cc
@@ -1,4 +1,10 @@
+#include "misc.hh"
+#include "store.hh"
 #include "build.hh"
+#include "db.hh"
+
+
+namespace nix {
 
 
 Derivation derivationFromPath(const Path & drvPath)
@@ -81,3 +87,6 @@ void queryMissing(const PathSet & targets,
         }
     }
 }
+
+ 
+}
diff --git a/src/libstore/misc.hh b/src/libstore/misc.hh
index f758f5bfd58e..ad1ad80eb186 100644
--- a/src/libstore/misc.hh
+++ b/src/libstore/misc.hh
@@ -4,6 +4,9 @@
 #include "derivations.hh"
 
 
+namespace nix {
+
+
 /* Read a derivation, after ensuring its existence through
    ensurePath(). */
 Derivation derivationFromPath(const Path & drvPath);
@@ -29,4 +32,7 @@ void queryMissing(const PathSet & targets,
     PathSet & willBuild, PathSet & willSubstitute);
 
 
+}
+
+
 #endif /* !__MISC_H */
diff --git a/src/libstore/pathlocks.cc b/src/libstore/pathlocks.cc
index 8d0e6c3299ac..03ba4f212439 100644
--- a/src/libstore/pathlocks.cc
+++ b/src/libstore/pathlocks.cc
@@ -1,17 +1,21 @@
+#include "pathlocks.hh"
+#include "util.hh"
+
 #include <cerrno>
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#include "pathlocks.hh"
-
 #ifdef __CYGWIN__
 #include <windows.h>
 #include <sys/cygwin.h>
 #endif
 
 
+namespace nix {
+
+
 int openLockFile(const Path & path, bool create)
 {
     AutoCloseFD fd;
@@ -220,3 +224,6 @@ void PathLocks::setDeletion(bool deletePaths)
 {
     this->deletePaths = deletePaths;
 }
+
+ 
+}
diff --git a/src/libstore/pathlocks.hh b/src/libstore/pathlocks.hh
index 911fe4579f28..87bb7bf3ef71 100644
--- a/src/libstore/pathlocks.hh
+++ b/src/libstore/pathlocks.hh
@@ -1,7 +1,10 @@
 #ifndef __PATHLOCKS_H
 #define __PATHLOCKS_H
 
-#include "util.hh"
+#include "types.hh"
+
+
+namespace nix {
 
 
 /* Open (possibly create) a lock file and return the file descriptor.
@@ -22,7 +25,7 @@ bool lockFile(int fd, LockType lockType, bool wait);
 class PathLocks 
 {
 private:
-    typedef pair<int, Path> FDPair;
+    typedef std::pair<int, Path> FDPair;
     list<FDPair> fds;
     bool deletePaths;
 
@@ -37,4 +40,7 @@ public:
 };
 
 
+}
+
+
 #endif /* !__PATHLOCKS_H */
diff --git a/src/libstore/references.cc b/src/libstore/references.cc
index d67e5b9dcc82..aba4ef01072f 100644
--- a/src/libstore/references.cc
+++ b/src/libstore/references.cc
@@ -1,3 +1,7 @@
+#include "references.hh"
+#include "hash.hh"
+#include "util.hh"
+
 #include <cerrno>
 #include <map>
 
@@ -7,8 +11,8 @@
 #include <dirent.h>
 #include <fcntl.h>
 
-#include "references.hh"
-#include "hash.hh"
+
+namespace nix {
 
 
 static unsigned int refLength = 32; /* characters */
@@ -90,7 +94,7 @@ void checkPath(const string & path,
 
 PathSet scanForReferences(const string & path, const PathSet & paths)
 {
-    map<string, Path> backMap;
+    std::map<string, Path> backMap;
     StringSet ids;
     StringSet seen;
 
@@ -114,10 +118,13 @@ PathSet scanForReferences(const string & path, const PathSet & paths)
 
     PathSet found;
     for (StringSet::iterator i = seen.begin(); i != seen.end(); i++) {
-        map<string, Path>::iterator j;
+        std::map<string, Path>::iterator j;
         if ((j = backMap.find(*i)) == backMap.end()) abort();
         found.insert(j->second);
     }
 
     return found;
 }
+
+ 
+}
diff --git a/src/libstore/references.hh b/src/libstore/references.hh
index 4b1299e7a352..76a7ee166dbe 100644
--- a/src/libstore/references.hh
+++ b/src/libstore/references.hh
@@ -1,10 +1,12 @@
 #ifndef __REFERENCES_H
 #define __REFERENCES_H
 
-#include "util.hh"
+#include "types.hh"
 
+namespace nix {
 
 PathSet scanForReferences(const Path & path, const PathSet & refs);
-
+    
+}
 
 #endif /* !__REFERENCES_H */
diff --git a/src/libstore/store.cc b/src/libstore/store.cc
index 2f9193749347..f8441af9cb42 100644
--- a/src/libstore/store.cc
+++ b/src/libstore/store.cc
@@ -1,3 +1,13 @@
+#include "store.hh"
+#include "util.hh"
+#include "globals.hh"
+#include "db.hh"
+#include "archive.hh"
+#include "pathlocks.hh"
+#include "gc.hh"
+#include "aterm.hh"
+#include "derivations-ast.hh"
+    
 #include <iostream>
 #include <algorithm>
 
@@ -6,14 +16,9 @@
 #include <unistd.h>
 #include <utime.h>
 
-#include "store.hh"
-#include "globals.hh"
-#include "db.hh"
-#include "archive.hh"
-#include "pathlocks.hh"
-#include "gc.hh"
-
+namespace nix {
 
+    
 /* Nix database. */
 static Database nixDB;
 
@@ -956,10 +961,6 @@ void verifyStore(bool checkContents)
 }
 
 
-#include "aterm.hh"
-#include "derivations-ast.hh"
-
-
 /* Upgrade from schema 1 (Nix <= 0.7) to schema 2 (Nix >= 0.8). */
 static void upgradeStore07()
 {
@@ -971,7 +972,7 @@ static void upgradeStore07()
     nixDB.enumTable(txn, dbValidPaths, validPaths2);
     PathSet validPaths(validPaths2.begin(), validPaths2.end());
 
-    cerr << "hashing paths...";
+    std::cerr << "hashing paths...";
     int n = 0;
     for (PathSet::iterator i = validPaths.begin(); i != validPaths.end(); ++i) {
         checkInterrupt();
@@ -980,20 +981,20 @@ static void upgradeStore07()
         if (s == "") {
             Hash hash = hashPath(htSHA256, *i);
             setHash(txn, *i, hash);
-            cerr << ".";
+            std::cerr << ".";
             if (++n % 1000 == 0) {
                 txn.commit();
                 txn.begin(nixDB);
             }
         }
     }
-    cerr << "\n";
+    std::cerr << std::endl;
 
     txn.commit();
 
     txn.begin(nixDB);
     
-    cerr << "processing closures...";
+    std::cerr << "processing closures...";
     for (PathSet::iterator i = validPaths.begin(); i != validPaths.end(); ++i) {
         checkInterrupt();
         if (i->size() > 6 && string(*i, i->size() - 6) == ".store") {
@@ -1037,10 +1038,10 @@ static void upgradeStore07()
                     setReferences(txn, path, references);
             }
             
-            cerr << ".";
+            std::cerr << ".";
         }
     }
-    cerr << "\n";
+    std::cerr << std::endl;
 
     /* !!! maybe this transaction is way too big */
     txn.commit();
@@ -1061,7 +1062,7 @@ static void upgradeStore09()
 
     Transaction txn(nixDB);
 
-    cerr << "converting referers to referrers...";
+    std::cerr << "converting referers to referrers...";
 
     TableId dbReferers = nixDB.openTable("referers"); /* sic! */
 
@@ -1080,16 +1081,19 @@ static void upgradeStore09()
         if (++n % 1000 == 0) {
             txn.commit();
             txn.begin(nixDB);
-            cerr << "|";
+            std::cerr << "|";
         }
-        cerr << ".";
+        std::cerr << ".";
     }
 
     txn.commit();
     
-    cerr << "\n";
+    std::cerr << std::endl;
 
     nixDB.closeTable(dbReferers);
 
     nixDB.deleteTable("referers");
 }
+
+ 
+}
diff --git a/src/libstore/store.hh b/src/libstore/store.hh
index a170c69e7477..7b18871e4f27 100644
--- a/src/libstore/store.hh
+++ b/src/libstore/store.hh
@@ -4,9 +4,12 @@
 #include <string>
 
 #include "hash.hh"
-#include "db.hh"
 
-using namespace std;
+
+namespace nix {
+
+
+class Transaction;
 
 
 /* Nix store and database schema version.  Version 1 (or 0) was Nix <=
@@ -168,5 +171,8 @@ void deleteFromStore(const Path & path, unsigned long long & bytesFreed);
 
 void verifyStore(bool checkContents);
 
+ 
+}
+
 
 #endif /* !__STORE_H */
diff --git a/src/libutil/Makefile.am b/src/libutil/Makefile.am
index 7798ac2bb1ca..dc514d55fa1b 100644
--- a/src/libutil/Makefile.am
+++ b/src/libutil/Makefile.am
@@ -3,7 +3,8 @@ pkglib_LTLIBRARIES = libutil.la
 libutil_la_SOURCES = util.cc util.hh hash.cc hash.hh \
  archive.cc archive.hh aterm.cc aterm.hh \
  aterm-map.cc aterm-map.hh \
- xml-writer.cc xml-writer.hh
+ xml-writer.cc xml-writer.hh \
+ types.hh
 
 if !HAVE_OPENSSL
 libutil_la_SOURCES += \
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc
index 25deccc09c15..32c75fee5d40 100644
--- a/src/libutil/archive.cc
+++ b/src/libutil/archive.cc
@@ -12,6 +12,9 @@
 #include "util.hh"
 
 
+namespace nix {
+
+
 static string archiveVersion1 = "nix-archive-1";
 
 
@@ -319,3 +322,5 @@ void restorePath(const Path & path, RestoreSource & source)
     restore(path, source);
 }
 
+ 
+}
diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh
index b498c95c1436..f85d589c6b2c 100644
--- a/src/libutil/archive.hh
+++ b/src/libutil/archive.hh
@@ -1,6 +1,10 @@
-#include <string>
+#ifndef __ARCHIVE_H
+#define __ARCHIVE_H
 
-#include "util.hh"
+#include "types.hh"
+
+
+namespace nix {
 
 
 /* dumpPath creates a Nix archive of the specified path.  The format
@@ -61,3 +65,9 @@ struct RestoreSource
 };
 
 void restorePath(const Path & path, RestoreSource & source);
+
+ 
+}
+
+
+#endif /* !__ARCHIVE_H */
diff --git a/src/libutil/aterm-map.cc b/src/libutil/aterm-map.cc
index 33388e148a65..6c53e07c4605 100644
--- a/src/libutil/aterm-map.cc
+++ b/src/libutil/aterm-map.cc
@@ -1,9 +1,14 @@
 #include "aterm-map.hh"
 
+#include <iostream>
+
 #include <assert.h>
 #include <stdlib.h>
 
 
+namespace nix {
+
+
 static const unsigned int maxLoadFactor = /* 1 / */ 3;
 static unsigned int nrResizes = 0;
 static unsigned int sizeTotalAlloc = 0;
@@ -214,10 +219,11 @@ unsigned int ATermMap::size()
 }
 
 
-#include <iostream>
-
 void printATermMapStats()
 {
+    using std::cerr;
+    using std::endl;
+    
     cerr << "RESIZES: " << nrResizes << " "
          << sizeTotalAlloc << " "
          << sizeCurAlloc << " "
@@ -319,3 +325,6 @@ int main(int argc, char * * argv)
     printATermMapStats();
 }
 #endif
+
+ 
+}
diff --git a/src/libutil/aterm-map.hh b/src/libutil/aterm-map.hh
index 115ed36cd53a..293df1eb596a 100644
--- a/src/libutil/aterm-map.hh
+++ b/src/libutil/aterm-map.hh
@@ -4,7 +4,8 @@
 #include <aterm2.h>
 #include <assert.h>
 
-using namespace std;
+
+namespace nix {
 
 
 class ATermMap
@@ -121,5 +122,8 @@ private:
 /* Hack. */
 void printATermMapStats();
 
+ 
+}
+
 
 #endif /* !__ATERM_MAP_H */
diff --git a/src/libutil/aterm.cc b/src/libutil/aterm.cc
index 6ca4521909ea..bb6e33ce92c0 100644
--- a/src/libutil/aterm.cc
+++ b/src/libutil/aterm.cc
@@ -1,7 +1,9 @@
 #include "aterm.hh"
 
+using std::string;
 
-string atPrint(ATerm t)
+
+string nix::atPrint(ATerm t)
 {
     if (!t) throw Error("attempt to print null aterm");
     char * s = ATwriteToString(t);
@@ -10,13 +12,13 @@ string atPrint(ATerm t)
 }
 
 
-ostream & operator << (ostream & stream, ATerm e)
+std::ostream & operator << (std::ostream & stream, ATerm e)
 {
-    return stream << atPrint(e);
+    return stream << nix::atPrint(e);
 }
 
 
-Error badTerm(const format & f, ATerm t)
+nix::Error nix::badTerm(const format & f, ATerm t)
 {
     char * s = ATwriteToString(t);
     if (!s) throw Error("cannot print term");
@@ -29,13 +31,13 @@ Error badTerm(const format & f, ATerm t)
 }
 
 
-ATerm toATerm(const char * s)
+ATerm nix::toATerm(const char * s)
 {
     return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));
 }
 
 
-ATerm toATerm(const string & s)
+ATerm nix::toATerm(const string & s)
 {
     return toATerm(s.c_str());
 }
diff --git a/src/libutil/aterm.hh b/src/libutil/aterm.hh
index 22364ba0118f..431c67d130ec 100644
--- a/src/libutil/aterm.hh
+++ b/src/libutil/aterm.hh
@@ -5,15 +5,15 @@ extern "C" {
 #include <aterm2.h>
 }
 
-#include "util.hh"
+#include "types.hh"
+
+
+namespace nix {
 
 
 /* Print an ATerm. */
 string atPrint(ATerm t);
 
-/* Write an ATerm to an output stream. */
-ostream & operator << (ostream & stream, ATerm e);
-
 class ATermIterator
 {
     ATermList t;
@@ -45,5 +45,12 @@ Error badTerm(const format & f, ATerm t);
 ATerm toATerm(const char * s);
 ATerm toATerm(const string & s);
 
+ 
+}
+
+
+/* Write an ATerm to an output stream. */
+std::ostream & operator << (std::ostream & stream, ATerm e);
+
 
 #endif /* !__ATERM_H */
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index b69c14832877..16597fd47cbf 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -15,12 +15,16 @@ extern "C" {
 
 #include "hash.hh"
 #include "archive.hh"
+#include "util.hh"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
 
+namespace nix {
+
+
 Hash::Hash()
 {
     type = htUnknown;
@@ -89,9 +93,9 @@ Hash parseHash(HashType ht, const string & s)
         string s2(s, i * 2, 2);
         if (!isxdigit(s2[0]) || !isxdigit(s2[1])) 
             throw Error(format("invalid hash `%1%'") % s);
-        istringstream str(s2);
+        std::istringstream str(s2);
         int n;
-        str >> hex >> n;
+        str >> std::hex >> n;
         hash.hash[i] = n;
     }
     return hash;
@@ -313,3 +317,6 @@ HashType parseHashType(const string & s)
     else if (s == "sha256") return htSHA256;
     else return htUnknown;
 }
+
+ 
+}
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index 97466cf9ef1a..95629bc9e5ba 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -1,11 +1,10 @@
 #ifndef __HASH_H
 #define __HASH_H
 
-#include <string>
+#include "types.hh"
 
-#include "util.hh"
 
-using namespace std;
+namespace nix {
 
 
 typedef enum { htUnknown, htMD5, htSHA1, htSHA256 } HashType;
@@ -76,5 +75,8 @@ Hash compressHash(const Hash & hash, unsigned int newSize);
 /* Parse a string representing a hash type. */
 HashType parseHashType(const string & s);
 
+ 
+}
 
+    
 #endif /* !__HASH_H */
diff --git a/src/libutil/types.hh b/src/libutil/types.hh
new file mode 100644
index 000000000000..1de378961e46
--- /dev/null
+++ b/src/libutil/types.hh
@@ -0,0 +1,73 @@
+#ifndef __TYPES_H
+#define __TYPES_H
+
+#include <string>
+#include <list>
+#include <set>
+
+#include <boost/format.hpp>
+
+
+namespace nix {
+
+
+/* Inherit some names from other namespaces for convenience. */
+using std::string;
+using std::list;
+using std::set;
+using std::vector;
+using boost::format;
+
+
+class Error : public std::exception
+{
+protected:
+    string err;
+public:
+    Error(const format & f);
+    ~Error() throw () { };
+    const char * what() const throw () { return err.c_str(); }
+    const string & msg() const throw () { return err; }
+    Error & addPrefix(const format & f);
+};
+
+class SysError : public Error
+{
+public:
+    SysError(const format & f);
+};
+
+#define MakeError(newClass, superClass) \
+    class newClass : public superClass                  \
+    {                                                   \
+    public:                                             \
+        newClass(const format & f) : superClass(f) { }; \
+    };
+
+MakeError(UsageError, Error)
+
+
+typedef list<string> Strings;
+typedef set<string> StringSet;
+
+
+/* Paths are just strings. */
+typedef string Path;
+typedef list<Path> Paths;
+typedef set<Path> PathSet;
+
+ 
+typedef enum { 
+    lvlError,
+    lvlInfo,
+    lvlTalkative,
+    lvlChatty,
+    lvlDebug,
+    lvlVomit
+} Verbosity;
+
+
+}
+
+
+#endif /* !__TYPES_H */
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 54cfc6c7fded..44b39f8c623a 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -9,17 +9,16 @@
 #include <cstdio>
 #include <sstream>
 
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
-#include <unistd.h>
-#include <dirent.h>
 #include <fcntl.h>
-#include <signal.h>
 
 #include "util.hh"
 
 
+namespace nix {
+
+
 Error::Error(const format & f)
 {
     err = f.str();
@@ -368,8 +367,8 @@ void Nest::open(Verbosity level, const format & f)
 {
     if (level <= verbosity) {
         if (logType == ltEscapes)
-            cerr << "\033[" << escVerbosity(level) << "p"
-                 << f.str() << "\n";
+            std::cerr << "\033[" << escVerbosity(level) << "p"
+                      << f.str() << "\n";
         else
             printMsg_(level, f);
         nest = true;
@@ -383,7 +382,7 @@ void Nest::close()
     if (nest) {
         nestingLevel--;
         if (logType == ltEscapes)
-            cerr << "\033[q";
+            std::cerr << "\033[q";
         nest = false;
     }
 }
@@ -697,8 +696,8 @@ string runProgram(Path program)
             execl(program.c_str(), program.c_str(), (char *) 0);
             throw SysError(format("executing `%1%'") % program);
             
-        } catch (exception & e) {
-            cerr << "error: " << e.what() << endl;
+        } catch (std::exception & e) {
+            std::cerr << "error: " << e.what() << std::endl;
         }
         quickExit(1);
     }
@@ -743,7 +742,7 @@ void _interrupted()
     /* Block user interrupts while an exception is being handled.
        Throwing an exception while another exception is being handled
        kills the program! */
-    if (!uncaught_exception()) {
+    if (!std::uncaught_exception()) {
         _isInterrupted = 0;
         throw Error("interrupted by the user");
     }
@@ -837,7 +836,7 @@ bool statusOk(int status)
 
 string int2String(int n)
 {
-    ostringstream str;
+    std::ostringstream str;
     str << n;
     return str.str();
 }
@@ -845,7 +844,10 @@ string int2String(int n)
 
 bool string2Int(const string & s, int & n)
 {
-    istringstream str(s);
+    std::istringstream str(s);
     str >> n;
     return str && str.get() == EOF;
 }
+
+ 
+}
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 125ba2695c2c..b4a61ae04a0d 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -1,58 +1,15 @@
 #ifndef __UTIL_H
 #define __UTIL_H
 
-#include <string>
-#include <list>
-#include <set>
-#include <sstream>
+#include "types.hh"
 
 #include <sys/types.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <signal.h>
 
-#include <boost/format.hpp>
 
-using namespace std;
-using namespace boost;
-
-
-class Error : public exception
-{
-protected:
-    string err;
-public:
-    Error(const format & f);
-    ~Error() throw () { };
-    const char * what() const throw () { return err.c_str(); }
-    const string & msg() const throw () { return err; }
-    Error & addPrefix(const format & f);
-};
-
-class SysError : public Error
-{
-public:
-    SysError(const format & f);
-};
-
-#define MakeError(newClass, superClass) \
-    class newClass : public superClass                  \
-    {                                                   \
-    public:                                             \
-        newClass(const format & f) : superClass(f) { }; \
-    };
-
-MakeError(UsageError, Error)
-
-
-typedef list<string> Strings;
-typedef set<string> StringSet;
-
-
-/* Paths are just strings. */
-typedef string Path;
-typedef list<Path> Paths;
-typedef set<Path> PathSet;
+namespace nix {
 
 
 /* Return an environment variable. */
@@ -138,15 +95,6 @@ typedef enum {
     ltFlat      /* no nesting */
 } LogType;
 
-typedef enum { 
-    lvlError,
-    lvlInfo,
-    lvlTalkative,
-    lvlChatty,
-    lvlDebug,
-    lvlVomit
-} Verbosity;
-
 extern LogType logType;
 extern Verbosity verbosity; /* suppress msgs > this */
 
@@ -307,5 +255,8 @@ struct SwitchToOriginalUser
     ~SwitchToOriginalUser();
 };
 
+ 
+}
+
 
 #endif /* !__UTIL_H */
diff --git a/src/libutil/xml-writer.cc b/src/libutil/xml-writer.cc
index 27235933e7fb..20351e2c30ac 100644
--- a/src/libutil/xml-writer.cc
+++ b/src/libutil/xml-writer.cc
@@ -3,7 +3,10 @@
 #include "xml-writer.hh"
 
 
-XMLWriter::XMLWriter(bool indent, ostream & output)
+namespace nix {
+    
+
+XMLWriter::XMLWriter(bool indent, std::ostream & output)
     : output(output), indent(indent)
 {
     output << "<?xml version='1.0' encoding='utf-8'?>\n";
@@ -122,3 +125,6 @@ int main(int argc, char * * argv)
     return 0;
 }
 #endif
+
+ 
+}
diff --git a/src/libutil/xml-writer.hh b/src/libutil/xml-writer.hh
index 8c203a3486f0..e5cc5f8c5417 100644
--- a/src/libutil/xml-writer.hh
+++ b/src/libutil/xml-writer.hh
@@ -6,7 +6,12 @@
 #include <list>
 #include <map>
 
-using namespace std;
+
+namespace nix {
+
+using std::string;
+using std::map;
+using std::list;
 
 
 typedef map<string, string> XMLAttrs;
@@ -16,7 +21,7 @@ class XMLWriter
 {
 private:
     
-    ostream & output;
+    std::ostream & output;
 
     bool indent;
     bool closed;
@@ -25,7 +30,7 @@ private:
 
 public:
 
-    XMLWriter(bool indent, ostream & output);
+    XMLWriter(bool indent, std::ostream & output);
     ~XMLWriter();
 
     void close();
@@ -63,5 +68,8 @@ public:
     }
 };
 
+ 
+}
+
 
 #endif /* !__XML_WRITER_H */
diff --git a/src/nix-env/main.cc b/src/nix-env/main.cc
index b3caaeac8277..7a6824b63acf 100644
--- a/src/nix-env/main.cc
+++ b/src/nix-env/main.cc
@@ -13,6 +13,9 @@
 #include "attr-path.hh"
 #include "pathlocks.hh"
 #include "xml-writer.hh"
+#include "store.hh"
+#include "db.hh"
+#include "util.hh"
 
 #include <cerrno>
 #include <ctime>
@@ -23,6 +26,10 @@
 #include <unistd.h>
 
 
+using namespace nix;
+using std::cout;
+
+
 typedef enum {
     srcNixExprDrvs,
     srcNixExprs,
@@ -224,7 +231,7 @@ static DrvInfos filterBySelector(EvalState & state,
     for (DrvNames::iterator i = selectors.begin();
          i != selectors.end(); ++i)
     {
-        typedef list<pair<DrvInfo, unsigned int> > Matches;
+        typedef list<std::pair<DrvInfo, unsigned int> > Matches;
         Matches matches;
         unsigned int n = 0;
         for (DrvInfos::const_iterator j = allElems.begin();
@@ -233,7 +240,7 @@ static DrvInfos filterBySelector(EvalState & state,
             DrvName drvName(j->name);
             if (i->matches(drvName)) {
                 i->hits++;
-                matches.push_back(pair<DrvInfo, unsigned int>(*j, n));
+                matches.push_back(std::pair<DrvInfo, unsigned int>(*j, n));
             }
         }
 
@@ -244,7 +251,7 @@ static DrvInfos filterBySelector(EvalState & state,
         if (newestOnly) {
 
             /* Map from package names to derivations. */
-            typedef map<string, pair<DrvInfo, unsigned int> > Newest;
+            typedef map<string, std::pair<DrvInfo, unsigned int> > Newest;
             Newest newest;
             StringSet multiple;
 
@@ -350,7 +357,7 @@ static void queryInstSources(EvalState & state,
                 assertStorePath(*i);
 
                 DrvInfo elem;
-                elem.attrs = shared_ptr<ATermMap>(new ATermMap(0)); /* ugh... */
+                elem.attrs = boost::shared_ptr<ATermMap>(new ATermMap(0)); /* ugh... */
                 string name = baseNameOf(*i);
                 string::size_type dash = name.find('-');
                 if (dash != string::npos)
@@ -667,7 +674,7 @@ void printTable(Table & table)
             if (column < nrColumns - 1)
                 cout << string(widths[column] - j->size() + 2, ' ');
         }
-        cout << endl;
+        cout << std::endl;
     }
 }
 
@@ -801,7 +808,7 @@ static void opQuery(Globals & globals,
     
     /* Print the desired columns, or XML output. */
     Table table;
-    ostringstream dummy;
+    std::ostringstream dummy;
     XMLWriter xml(true, *(xmlOutput ? &cout : &dummy));
     XMLOpenElement xmlRoot(xml, "items");
     
diff --git a/src/nix-env/names.cc b/src/nix-env/names.cc
index 93bcfda24e60..a425b8d0762c 100644
--- a/src/nix-env/names.cc
+++ b/src/nix-env/names.cc
@@ -1,4 +1,8 @@
 #include "names.hh"
+#include "util.hh"
+
+
+namespace nix {
 
 
 DrvName::DrvName()
@@ -115,3 +119,6 @@ DrvNames drvNamesFromArgs(const Strings & opArgs)
         result.push_back(DrvName(*i));
     return result;
 }
+
+ 
+}
diff --git a/src/nix-env/names.hh b/src/nix-env/names.hh
index aeb923546fd8..e189302d6d94 100644
--- a/src/nix-env/names.hh
+++ b/src/nix-env/names.hh
@@ -1,10 +1,10 @@
 #ifndef __NAMES_H
 #define __NAMES_H
 
-#include <string>
-#include <list>
+#include "types.hh"
 
-#include "util.hh"
+
+namespace nix {
 
 
 struct DrvName
@@ -27,4 +27,7 @@ int compareVersions(const string & v1, const string & v2);
 DrvNames drvNamesFromArgs(const Strings & opArgs);
 
 
+}
+
+
 #endif /* !__NAMES_H */
diff --git a/src/nix-env/profiles.cc b/src/nix-env/profiles.cc
index 6ec12e604c73..0db5bda5e9b6 100644
--- a/src/nix-env/profiles.cc
+++ b/src/nix-env/profiles.cc
@@ -1,4 +1,5 @@
 #include "profiles.hh"
+#include "util.hh"
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -7,6 +8,9 @@
 #include <stdio.h>
 
 
+namespace nix {
+
+
 static bool cmpGensByNumber(const Generation & a, const Generation & b)
 {
     return a.number < b.number;
@@ -126,3 +130,7 @@ void switchLink(Path link, Path target)
     if (rename(tmp.c_str(), link.c_str()) != 0)
         throw SysError(format("renaming `%1%' to `%2%'") % tmp % link);
 }
+
+ 
+}
+
diff --git a/src/nix-env/profiles.hh b/src/nix-env/profiles.hh
index dc27d1c83de8..2bd05045eafb 100644
--- a/src/nix-env/profiles.hh
+++ b/src/nix-env/profiles.hh
@@ -1,9 +1,10 @@
 #ifndef __PROFILES_H
 #define __PROFILES_H
 
-#include <string>
+#include "types.hh"
 
-#include "util.hh"
+
+namespace nix {
 
 
 struct Generation
@@ -35,4 +36,7 @@ void deleteGeneration(const Path & profile, unsigned int gen);
 void switchLink(Path link, Path target);
 
 
+}
+
+
 #endif /* !__PROFILES_H */
diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc
index dd2ed8d4f4fa..1282af070e62 100644
--- a/src/nix-hash/nix-hash.cc
+++ b/src/nix-hash/nix-hash.cc
@@ -5,9 +5,12 @@
 #include "help.txt.hh"
 
 
+using namespace nix;
+
+
 void printHelp()
 {
-    cout << string((char *) helpText, sizeof helpText);
+    std::cout << string((char *) helpText, sizeof helpText);
 }
 
 
@@ -43,7 +46,7 @@ void run(Strings args)
         for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
             Hash h = flat ? hashFile(ht, *i) : hashPath(ht, *i);
             if (truncate && h.hashSize > 20) h = compressHash(h, 20);
-            cout << format("%1%\n") %
+            std::cout << format("%1%\n") %
                 (base32 ? printHash32(h) : printHash(h));
         }
     }
@@ -51,7 +54,7 @@ void run(Strings args)
     else {
         for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) {
             Hash h = op == opTo16 ? parseHash32(ht, *i) : parseHash(ht, *i);
-            cout << format("%1%\n") %
+            std::cout << format("%1%\n") %
                 (op == opTo16 ? printHash(h) : printHash32(h));
         }
     }
diff --git a/src/nix-instantiate/main.cc b/src/nix-instantiate/main.cc
index eb5c1ce8552d..91720b1d55ff 100644
--- a/src/nix-instantiate/main.cc
+++ b/src/nix-instantiate/main.cc
@@ -10,12 +10,17 @@
 #include "get-drvs.hh"
 #include "attr-path.hh"
 #include "expr-to-xml.hh"
+#include "util.hh"
+#include "store.hh"
 #include "help.txt.hh"
 
 
+using namespace nix;
+
+
 void printHelp()
 {
-    cout << string((char *) helpText, sizeof helpText);
+    std::cout << string((char *) helpText, sizeof helpText);
 }
 
 
@@ -23,7 +28,7 @@ static Expr parseStdin(EvalState & state)
 {
     startNest(nest, lvlTalkative, format("parsing standard input"));
     string s, s2;
-    while (getline(cin, s2)) s += s2 + "\n";
+    while (getline(std::cin, s2)) s += s2 + "\n";
     return parseExprFromString(state, s, absPath("."));
 }
 
@@ -38,9 +43,9 @@ static void printResult(EvalState & state, Expr e,
 {
     if (evalOnly)
         if (xmlOutput)
-            printTermAsXML(e, cout);
+            printTermAsXML(e, std::cout);
         else
-            cout << format("%1%\n") % e;
+            std::cout << format("%1%\n") % e;
     
     else {
         DrvInfos drvs;
@@ -53,7 +58,7 @@ static void printResult(EvalState & state, Expr e,
                 drvPath = addPermRoot(drvPath,
                     makeRootName(gcRoot, rootNr),
                     indirectRoot);
-            cout << format("%1%\n") % drvPath;
+            std::cout << format("%1%\n") % drvPath;
         }
     }
 }
diff --git a/src/nix-store/dotgraph.cc b/src/nix-store/dotgraph.cc
index 053358e3ad85..4b068514e4e5 100644
--- a/src/nix-store/dotgraph.cc
+++ b/src/nix-store/dotgraph.cc
@@ -1,7 +1,14 @@
+#include "dotgraph.hh"
+#include "util.hh"
+#include "store.hh"
+#include "db.hh"
+
 #include <iostream>
 
-#include "dotgraph.hh"
-#include "build.hh"
+
+using std::cout;
+
+namespace nix {
 
 
 static string dotQuote(const string & s)
@@ -151,3 +158,6 @@ void printDotGraph(const PathSet & roots)
 
     cout << "}\n";
 }
+
+ 
+}
diff --git a/src/nix-store/dotgraph.hh b/src/nix-store/dotgraph.hh
index 9effa5a884bd..2318e2fde48d 100644
--- a/src/nix-store/dotgraph.hh
+++ b/src/nix-store/dotgraph.hh
@@ -1,8 +1,12 @@
 #ifndef __DOTGRAPH_H
 #define __DOTGRAPH_H
 
-#include "util.hh"
+#include "types.hh"
+
+namespace nix {
 
 void printDotGraph(const PathSet & roots);
 
+}
+
 #endif /* !__DOTGRAPH_H */
diff --git a/src/nix-store/main.cc b/src/nix-store/main.cc
index fc458a246c4a..616cb8310631 100644
--- a/src/nix-store/main.cc
+++ b/src/nix-store/main.cc
@@ -8,9 +8,17 @@
 #include "archive.hh"
 #include "shared.hh"
 #include "dotgraph.hh"
+#include "store.hh"
+#include "db.hh"
+#include "util.hh"
 #include "help.txt.hh"
 
 
+using namespace nix;
+using std::cin;
+using std::cout;
+
+
 typedef void (* Operation) (Strings opFlags, Strings opArgs);
 
 
@@ -528,7 +536,7 @@ static void opGC(Strings opFlags, Strings opArgs)
 
     if (action != gcDeleteDead) {
         for (PathSet::iterator i = result.begin(); i != result.end(); ++i)
-            cout << *i << endl;
+            cout << *i << std::endl;
     }
 }