From 4e624849b686894f8a4c23a56098f4d47634a85c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 17 Jan 2012 23:07:22 +0000 Subject: [PATCH] =?UTF-8?q?*=20Added=20a=20command=20=E2=80=98nix-store=20?= =?UTF-8?q?--print-env=20$drvpath=E2=80=99=20that=20prints=20out=20the=20?= =?UTF-8?q?=20=20environment=20of=20the=20given=20derivation=20in=20a=20fo?= =?UTF-8?q?rmat=20that=20can=20be=20sourced=20=20=20by=20the=20shell,=20e.?= =?UTF-8?q?g.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit $ eval "$(nix-store --print-env $(nix-instantiate /etc/nixos/nixpkgs -A pkg))" $ NIX_BUILD_TOP=/tmp $ source $stdenv/setup This is especially useful to reproduce the environment used to build a package outside of its builder for development purposes. TODO: add a nix-build option to do the above and fetch the dependencies of the derivation as well. --- doc/manual/release-notes.xml | 4 ++++ src/nix-store/nix-store.cc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/doc/manual/release-notes.xml b/doc/manual/release-notes.xml index 3712a8b91..7ef25401b 100644 --- a/doc/manual/release-notes.xml +++ b/doc/manual/release-notes.xml @@ -44,6 +44,10 @@ TODO: nix-store --verify-path command. + + TODO: nix-store --print-env command. + + diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index b9f8d927a..2e1d3f044 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -382,6 +382,39 @@ static void opQuery(Strings opFlags, Strings opArgs) } +static string shellEscape(const string & s) +{ + string r; + foreach (string::const_iterator, i, s) + if (*i == '\'') r += "'\\''"; else r += *i; + return r; +} + + +static void opPrintEnv(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() != 1) throw UsageError("`--print-env' requires one derivation store path"); + + Path drvPath = opArgs.front(); + Derivation drv = derivationFromPath(*store, drvPath); + + /* Print each environment variable in the derivation in a format + that can be sourced by the shell. */ + foreach (StringPairs::iterator, i, drv.env) + cout << format("export %1%; %1%='%2%'\n") % i->first % shellEscape(i->second); + + /* Also output the arguments. This doesn't preserve whitespace in + arguments. */ + cout << "export _args; _args='"; + foreach (Strings::iterator, i, drv.args) { + if (i != drv.args.begin()) cout << ' '; + cout << shellEscape(*i); + } + cout << "'\n"; +} + + static void opReadLog(Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); @@ -754,6 +787,8 @@ void run(Strings args) op = opDelete; else if (arg == "--query" || arg == "-q") op = opQuery; + else if (arg == "--print-env") + op = opPrintEnv; else if (arg == "--read-log" || arg == "-l") op = opReadLog; else if (arg == "--dump-db")