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 2 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
4 changes: 3 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,9 @@ 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
146 changes: 146 additions & 0 deletions flake.lock

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

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

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

# Other sources / nix utilities
flake-compat = { url = "github:edolstra/flake-compat"; flake = false; };
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, terranix, flake-compat }:
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/
];
};
in
{
defaultPackage = terraformConfiguration;

# nix develop
devShell = pkgs.mkShell {
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 run
# every run will be generated config.tf.json
defaultApp = self.apps.${system}.apply;
});
}
27 changes: 14 additions & 13 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
with import <nixpkgs> {};

pkgs.mkShell {
name = "area13";

buildInputs = [
terraform
tfsec
terrascan
ripgrep
bat
];
}
# See https://nixos.wiki/wiki/Flakes#Using_flakes_project_from_a_legacy_Nix
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{
src = ./.;
}).shellNix
Copy link
Member Author

Choose a reason for hiding this comment

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

if you want to changes shell, please update in flake.nix at attribute devShell that run same function as before in shell.nix (mkShell).