Merge branch 'master' of lassulus/techblog into master
This commit is contained in:
commit
b6b3eeb9a7
|
@ -13,19 +13,19 @@ summary: >
|
||||||
# nix-instantiate
|
# nix-instantiate
|
||||||
|
|
||||||
I like NixOS and the way modules work.
|
I like NixOS and the way modules work.
|
||||||
I miss it when I do tasks in other languages,
|
I miss them when I do tasks in other languages,
|
||||||
which have less power than NixOS, for example
|
which have less power than NixOS, for example
|
||||||
[Ansible](https://www.ansible.com) and [Terraform](https://www.terraform.io).
|
[Ansible](https://www.ansible.com) and [Terraform](https://www.terraform.io).
|
||||||
|
|
||||||
Luckily all these tools can be configured via JSON,
|
Luckily all these tools can be configured via JSON,
|
||||||
and Nix can easily create JSON.
|
and Nix can easily create JSON.
|
||||||
The tool go to tool for that job is
|
The goto tool for that job is
|
||||||
[nix-instantiate](https://nixos.org/nix/manual/#sec-nix-instantiate)
|
[nix-instantiate](https://nixos.org/nix/manual/#sec-nix-instantiate)
|
||||||
which every NixOS has installed (yeye!).
|
which every NixOS has installed (yeye!).
|
||||||
|
|
||||||
## Small overview
|
## Small overview
|
||||||
|
|
||||||
I will show you how easy it is, with a few lines of nix
|
I will show you how easy it is, with a few lines of nix,
|
||||||
to create a JSON renderer for terraform configuration files.
|
to create a JSON renderer for terraform configuration files.
|
||||||
But this is only done in a sketchy way,
|
But this is only done in a sketchy way,
|
||||||
to inspire you to create your own setup for
|
to inspire you to create your own setup for
|
||||||
|
@ -127,9 +127,9 @@ we get the following json.
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is almost what we like to see. the `_module` value is not wanted.
|
This is almost what we want to see. the `_module` value is not needed.
|
||||||
so lets remove it with a sanitize function, and move the content path
|
So lets remove it with a sanitize function, and move the content path
|
||||||
in a different file called config.nix
|
to a different file called config.nix
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
# file test3.nix
|
# file test3.nix
|
||||||
|
@ -159,7 +159,7 @@ in
|
||||||
(sanitize result.config)
|
(sanitize result.config)
|
||||||
```
|
```
|
||||||
|
|
||||||
in `config.nix` we can now focus on the configuration content. And it we write it
|
in `config.nix` we can now focus on the configuration content. And we write it
|
||||||
just like we would write a NixOS module.
|
just like we would write a NixOS module.
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
|
@ -215,12 +215,12 @@ or hiding complexity.
|
||||||
|
|
||||||
## a simple example
|
## a simple example
|
||||||
|
|
||||||
Let's make an example a none NixOS veteran can see
|
Let's make an example so a none NixOS veteran can see
|
||||||
how you would start using this modules system.
|
how to start using this modules system.
|
||||||
|
|
||||||
### hcloud.nix
|
### hcloud.nix
|
||||||
|
|
||||||
The following file should show is a module that let's us create
|
The following file is a module that let's us create
|
||||||
resource entries to create a
|
resource entries to create a
|
||||||
[hcloud server](https://www.terraform.io/docs/providers/hcloud/r/server.html).
|
[hcloud server](https://www.terraform.io/docs/providers/hcloud/r/server.html).
|
||||||
But it has one parameter `additionalFileSize`
|
But it has one parameter `additionalFileSize`
|
||||||
|
@ -344,12 +344,11 @@ $> nix-instantiate --eval --strict --json test3.nix --show-trace | jq
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The output is quite like we expected it to be.
|
The output is like we expected it to be.
|
||||||
|
|
||||||
The `hcloud` parameter should be removed, but for now it I leave it here,
|
The `hcloud` parameter should be removed, but for now I will leave it here,
|
||||||
to see the original configuration.
|
to see the original configuration.
|
||||||
To make this work in terraform, you have to remove everything of course,
|
To make this work in terraform, you have to remove everything except `resource`.
|
||||||
except `resource`.
|
|
||||||
|
|
||||||
#### with additionalFileSize
|
#### with additionalFileSize
|
||||||
|
|
||||||
|
@ -415,7 +414,7 @@ properly removed from `resource.hcloud_server.test`.
|
||||||
|
|
||||||
You could also create this very simple example with
|
You could also create this very simple example with
|
||||||
`variables`, `locals` and `count`.
|
`variables`, `locals` and `count`.
|
||||||
But doing that you already reached the limits of
|
By doing that you already reached the limits of
|
||||||
`HCL` but in Nix this is a very simple example.
|
`HCL` but in Nix this is a very simple example.
|
||||||
|
|
||||||
## A more complex Example
|
## A more complex Example
|
||||||
|
|
Loading…
Reference in a new issue