Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/manual/rl-next/nix-instantiate-raw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
synopsis: "`nix-instantiate --eval` now supports `--raw`"
prs: [12119]
---

The `nix-instantiate --eval` command now supports a `--raw` flag, when used
the evaluation result must be a string, which is printed verbatim without
quotation marks or escaping.
2 changes: 1 addition & 1 deletion doc/manual/source/command-ref/nix-copy-closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ When using public key authentication, you can avoid typing the passphrase with `
> Copy GNU Hello from a remote machine using a known store path, and run it:
>
> ```shell-session
> $ storePath="$(nix-instantiate --eval '<nixpkgs>' -I nixpkgs=channel:nixpkgs-unstable -A hello.outPath | tr -d '"')"
> $ storePath="$(nix-instantiate --eval --raw '<nixpkgs>' -I nixpkgs=channel:nixpkgs-unstable -A hello.outPath)"
> $ nix-copy-closure --from alice@itchy.example.org "$storePath"
> $ "$storePath"/bin/hello
> Hello, world!
Expand Down
7 changes: 6 additions & 1 deletion doc/manual/source/command-ref/nix-instantiate.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Synopsis

`nix-instantiate`
[`--parse` | `--eval` [`--strict`] [`--json`] [`--xml`] ]
[`--parse` | `--eval` [`--strict`] [`--raw` | `--json` | `--xml`] ]
[`--read-write-mode`]
[`--arg` *name* *value*]
[{`--attr`| `-A`} *attrPath*]
Expand Down Expand Up @@ -102,6 +102,11 @@ standard input.
> This option can cause non-termination, because lazy data
> structures can be infinitely large.

- `--raw`

When used with `--eval`, the evaluation result must be a string,
which is printed verbatim, without quoting, escaping or trailing newline.

- `--json`

When used with `--eval`, print the resulting value as an JSON
Expand Down
10 changes: 8 additions & 2 deletions src/nix-instantiate/nix-instantiate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static Path gcRoot;
static int rootNr = 0;


enum OutputKind { okPlain, okXML, okJSON };
enum OutputKind { okPlain, okRaw, okXML, okJSON };

void processExpr(EvalState & state, const Strings & attrPaths,
bool parseOnly, bool strict, Bindings & autoArgs,
Expand All @@ -50,7 +50,11 @@ void processExpr(EvalState & state, const Strings & attrPaths,
vRes = v;
else
state.autoCallFunction(autoArgs, v, vRes);
if (output == okXML)
if (output == okRaw)
std::cout << *state.coerceToString(noPos, vRes, context, "while generating the nix-instantiate output");
// We intentionally don't output a newline here. The default PS1 for Bash in NixOS starts with a newline
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline

This should be documented in the manual, perhaps without the reasoning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idea (no action required)
--lines to allow multiple arguments and stream them to stdout, each on their own line. This would always add a newline, so --raw --lines would output a line. Perhaps also implicitly flattening lists as well, printing a line for each item.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned the absence of a trailing newline in the manual.

// and other interactive shells like Zsh are smart enough to print a missing newline before the prompt.
else if (output == okXML)
printValueAsXML(state, strict, location, vRes, std::cout, context, noPos);
else if (output == okJSON) {
printValueAsJSON(state, strict, vRes, v.determinePos(noPos), std::cout, context);
Expand Down Expand Up @@ -132,6 +136,8 @@ static int main_nix_instantiate(int argc, char * * argv)
gcRoot = getArg(*arg, arg, end);
else if (*arg == "--indirect")
;
else if (*arg == "--raw")
outputKind = okRaw;
else if (*arg == "--xml")
outputKind = okXML;
else if (*arg == "--json")
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/eval.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
int = 123;
str = "foo";
str = "foo\nbar";
attr.foo = "bar";
}
7 changes: 4 additions & 3 deletions tests/functional/eval.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ EOF
nix eval --expr 'assert 1 + 2 == 3; true'

[[ $(nix eval int -f "./eval.nix") == 123 ]]
[[ $(nix eval str -f "./eval.nix") == '"foo"' ]]
[[ $(nix eval str --raw -f "./eval.nix") == 'foo' ]]
[[ $(nix eval str -f "./eval.nix") == '"foo\nbar"' ]]
[[ $(nix eval str --raw -f "./eval.nix") == $'foo\nbar' ]]
[[ "$(nix eval attr -f "./eval.nix")" == '{ foo = "bar"; }' ]]
[[ $(nix eval attr --json -f "./eval.nix") == '{"foo":"bar"}' ]]
[[ $(nix eval int -f - < "./eval.nix") == 123 ]]
Expand All @@ -28,7 +28,8 @@ nix eval --expr 'assert 1 + 2 == 3; true'

nix-instantiate --eval -E 'assert 1 + 2 == 3; true'
[[ $(nix-instantiate -A int --eval "./eval.nix") == 123 ]]
[[ $(nix-instantiate -A str --eval "./eval.nix") == '"foo"' ]]
[[ $(nix-instantiate -A str --eval "./eval.nix") == '"foo\nbar"' ]]
[[ $(nix-instantiate -A str --raw --eval "./eval.nix") == $'foo\nbar' ]]
[[ "$(nix-instantiate -A attr --eval "./eval.nix")" == '{ foo = "bar"; }' ]]
[[ $(nix-instantiate -A attr --eval --json "./eval.nix") == '{"foo":"bar"}' ]]
[[ $(nix-instantiate -A int --eval - < "./eval.nix") == 123 ]]
Expand Down
Loading