Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/nixify terra #10

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# legacy nix
use_nix

# 👇 uncomment when you want to use flake
# use flake

export TF_VAR_do_token=
export TF_VAR_linode_token=
export TF_VAR_namecheap_username=
Expand Down
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.envrc
.terraform
terraform.tfstate
terraform.tfstate.*
Expand All @@ -7,6 +6,12 @@ terraform.tfstate.*
# Edit at https://www.toptal.com/developers/gitignore?templates=direnv

### direnv ###
.envrc
.direnv

# End of https://www.toptal.com/developers/gitignore/api/direnv
result
config.tf.json

# ignored generated precommit config by nix-precommit
/.pre-commit-config.yaml
255 changes: 255 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";

# terranix modules
terranix = {
url = "github:terranix/terranix";
inputs.nixpkgs.follows = "nixpkgs";
};

# Other sources / nix utilities

# pre-commit-hooks
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = { url = "github:edolstra/flake-compat"; flake = false; };
flake-utils.url = "github:numtide/flake-utils";
nix-filter.url = "github:numtide/nix-filter";
};

outputs = { self, nixpkgs, flake-utils, terranix, flake-compat, nix-filter, pre-commit-hooks }:
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
terraform = pkgs.terraform;
terraformConfiguration = terranix.lib.terranixConfiguration {
inherit system;
modules = [
# TODO rewrite *.tf to .nix
# see https://terranix.org/documentation/terranix-vs-hcl/
];
};
sources.nix = nix-filter.lib {
root = ./.;
include = [
(nix-filter.lib.matchExt "nix")
];
};
in
{
defaultPackage = terraformConfiguration;

# nix develop
devShells.default = pkgs.mkShell {
inherit (self.checks.${system}.pre-commit-check) shellHook;
buildInputs = with pkgs;[
terraform
terranix.defaultPackage.${system}

tfsec
terrascan

ripgrep
bat
];
};

# nix run ".#apply"
apps.apply = {
type = "app";
program = toString (pkgs.writers.writeBash "apply" ''
if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi
cp ${terraformConfiguration} config.tf.json \
&& ${terraform}/bin/terraform init \
&& ${terraform}/bin/terraform apply
'');
};

# nix run ".#destroy"
apps.destroy = {
type = "app";
program = toString (pkgs.writers.writeBash "destroy" ''
if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi
cp ${terraformConfiguration} config.tf.json \
&& ${terraform}/bin/terraform init \
&& ${terraform}/bin/terraform destroy
'');
};

# nix flake check
checks = {
pre-commit-check = pre-commit-hooks.lib.${system}.run {
Copy link
Member Author

@r17x r17x Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example pre-commit @faultables

image

src = ./.;
hooks = {
nixpkgs-fmt.enable = true;
terraform-format = true;
};
};
};


# nix run
# every run will be generated config.tf.json
defaultApp = self.apps.${system}.apply;
});
}
Loading