Interpret Nix expresssion files

I’m trying to learn Nix language and I’m curious if there’s an easy way to interpret Nix expression without the nix repl.

If I have a custom.nix file with a simple string “Hello!” inside, I would like to have the option to interpret it with nix. I didn’t manage to find that in any language tutorial I looked at.

Using nix-instantiate or nix-build doesn’t help because the I get expression does not evaluate to a derivation (or a set or list of those) error.

I know the purpose of Nix is to write in the end a derivation, but it would be nice to have a collection of scripts to run them locally while I learn Nix.

1 Like

Use nix-instantiate --eval.

I have a new Nix language tutorial in the making - feedback is highly appreciated.

3 Likes

you can have fun without creating derivations

let
  variable = "something";
in "this is the content of ${variable}"

using nix eval (requires experimental features nix command)

~> nix eval --raw -f test.nix
this is the content of something⏎ 

using nix-instantiate + jq (note this adds a return character to the string, it doesn’t contain \n so there shouldn’t be a newline, on the previous example, my shell shows there were no newline by adding a )

nix-instantiate --eval --strict test.nix | jq -r .
this is the content of something

You can have fun crafting strings like this, and redirect them to file or to a pipe.

2 Likes

Thanks, both!

@fricklerhandwerk In case I have feedback what’s the best place to send it? Comment on the PR?

Yes, please add comments to the PR, ideally inline and if possible with suggestions so I can accommodate them quickly.