Interpret Nix from Python?

Is there a way I can use Nix for a config file for a python program I’m writing?

I’d like the python program to somehow interpret the file, outputting a static config that the rest of the system can use.

I understand that I could have nix installed alongside and call it through the shell, but I’d prefer to somehow package together the nix interpreter I use with my python program.

1 Like

Do you have a particular need for it to be Nix specifically, or do you just want a configuration file with the power of Nix?

There’s a language called Dhall which is designed for configuration files, it’s guaranteed to terminate (and therefore isn’t Turing-complete), but includes lots of features like string interpolations, optional types, user-defined functions, imports (including from URLs), etc. And it can even take your input Dhall file(s) and “normalize” it to an output file that strips all the programming, or even to JSON and YAML.

There’s even a Dhall-to-Nix compiler, if you want to construct Nix expressions using Dhall.

2 Likes

Yes, there are bindings to evaluate Nix expressions. The package is called pythonix. To get a Python env, python3.withPackages(ps: with ps; [ pythonix ]).

1 Like