Nix-ify CloudFormation?


So I am still pretty new to nix, but I am loving it so far.

At work, I was working on some AWS stuff, and I had to set up a cloudformation template. As I was doing that, I was struck by how similar the cloudformation JSON template is to the Nix expression language. Both are declarative and describe a set of resources (software on a machine on one hand, and AWS resources on the other hand).

The next thought I had was: would it be possible to use nix to do cloudformation? That would be pretty neat, instead of this DSL-JSON-frankenstein that is cloudformation.

I’m still a newbie so I couldn’t really think beyond there. I thought I would ask on here. Am I incorrect in identifying the similarity? Would it be possible to nix-ify cloudformation?

Sure. You can write a Nix module that produces a JSON file. If you want added benefits beyond simple type-checking, you should have a look at Nickel: Nickel 1.0 release


Hey, that’s really cool, thanks for sharing. Will definitely look into Nickel.

I was facing similar problems, so I created a project to make my life harder.

Then I discovered Nix, had same thought as you.

That is why I created modules to devshell to generate files (devshell-files). You could create a similar project with devenv, but PR with modules to CloudFormation would be welcome.

Both with python and CUE failed to read CloudFormation YAML or some especial features of YAML. I think they use some kind of extended YAML. But you can read a JSON and YAML from Nix with:

  my_conf_json = builtins.fromJSON (builtins.fromFile ./my_conf.json);
  my_conf_yaml = builtins.fromJSON(
    builtins.readFile (
      runCommandNoCC "blaq" {} ''
        ca ${./my_conf.yml}|${pkgs.yj}/bin/yj >$out

There is also a PR to add fromYAML.

fwiw such a thing already exists for terraform:

Not aware of any equivalent for CloudFormation, but that could still be a good inspiration :slight_smile:

1 Like