| My Nix converter tool | |
| 2025-12-16 | |
| Last edit: 2025-12-16 | |
| --------------------- | |
| Last year I wanted to convert my Nix code into YAML and TOML. So I went looking… | |
| ## Presentation | |
| The tool is called | |
| nix-converter | |
| . It's a command-line interface written in Go that converts various configurati… | |
| ## How it works | |
| Let's assume we have a source language called A and a destination language call… | |
| First, I use external Go libraries to retrieve a syntactic abstract representin… | |
| The only exception to these steps is the transformation of Nix code into TOML c… | |
| ## Examples of transformations | |
| Let's take a simple example with the JSON code below. | |
| ```json | |
| { | |
| "fruits": [ | |
| "Apple", | |
| "Banana", | |
| "Orange", | |
| 1, | |
| 2, | |
| -1 | |
| ], | |
| "abc": { | |
| "c": "c", | |
| "b": "b", | |
| "a": "a" | |
| }, | |
| "": 1, | |
| "123": "", | |
| "f123": "" | |
| } | |
| ``` | |
| To convert it into Nix code, you can use the command line below, which also inc… | |
| ```bash | |
| nix-converter -f file.json -l json -unsafe-keys -sort-iterators "all" | |
| ``` | |
| Once launched, you get the Nix code below. | |
| ```nix | |
| { | |
| "" = 1; | |
| "123" = ""; | |
| abc = { | |
| a = "a"; | |
| b = "b"; | |
| c = "c"; | |
| }; | |
| f123 = ""; | |
| fruits = [ | |
| "Apple" | |
| "Banana" | |
| "Orange" | |
| (-1) | |
| 1 | |
| 2 | |
| ]; | |
| } | |
| ``` | |
| If we wish, we can convert this Nix code again, for example with the command li… | |
| ```bash | |
| nix-converter -f file.nix -l toml -from-nix | |
| ``` | |
| Here's how it would look below. | |
| ```toml | |
| "" = 1 | |
| 123 = "" | |
| f123 = "" | |
| fruits = ["Apple", "Banana", "Orange", -1, 1, 2] | |
| [abc] | |
| a = "a" | |
| b = "b" | |
| c = "c" | |
| ``` | |
| ## Testing the tool | |
| If you're curious and just want to test the tool, you can for example use the N… | |
| ```bash | |
| nix run github:theobori/nix-converter -- -help | |
| ``` | |
| ## Contributing | |
| Anyone is free to contribute to the source code. For more details, please visit… | |
| GitHub repository | |
| . | |
| ## Conclusion | |
| nix-converter | |
| is a simple and useful tool that I enjoy using when I need to convert Nix code. | |