Skip to content

Commit 49d7eb2

Browse files
committed
feat: use flake and terranix
this changes not breaking our workflow: * nix-shell refer to shell.nix * nix develop refer to flake.nix#devShells * TODO: rewrite *.tf to .nix
1 parent 4c0ec2d commit 49d7eb2

File tree

5 files changed

+239
-14
lines changed

5 files changed

+239
-14
lines changed

.envrc.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# legacy nix
12
use_nix
23

4+
# 👇 uncomment when you want to use flake
5+
# use flake
6+
37
export TF_VAR_do_token=
48
export TF_VAR_linode_token=
59
export TF_VAR_namecheap_username=

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ terraform.tfstate.*
66
# Edit at https://www.toptal.com/developers/gitignore?templates=direnv
77

88
### direnv ###
9-
.direnv
109
.envrc
10+
.direnv
1111

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

flake.lock

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:nixos/nixpkgs";
4+
5+
# terranix modules
6+
terranix = {
7+
url = "github:terranix/terranix";
8+
inputs.nixpkgs.follows = "nixpkgs";
9+
};
10+
11+
# Other sources / nix utilities
12+
flake-compat = { url = "github:edolstra/flake-compat"; flake = false; };
13+
flake-utils.url = "github:numtide/flake-utils";
14+
};
15+
16+
outputs = { self, nixpkgs, flake-utils, terranix, flake-compat }:
17+
flake-utils.lib.eachDefaultSystem (system:
18+
let
19+
pkgs = nixpkgs.legacyPackages.${system};
20+
terraform = pkgs.terraform;
21+
terraformConfiguration = terranix.lib.terranixConfiguration {
22+
inherit system;
23+
modules = [
24+
# TODO rewrite *.tf to .nix
25+
# see https://terranix.org/documentation/terranix-vs-hcl/
26+
];
27+
};
28+
in
29+
{
30+
defaultPackage = terraformConfiguration;
31+
32+
# nix develop
33+
devShell = pkgs.mkShell {
34+
buildInputs = with pkgs;[
35+
terraform
36+
terranix.defaultPackage.${system}
37+
38+
tfsec
39+
terrascan
40+
41+
ripgrep
42+
bat
43+
];
44+
};
45+
46+
# nix run ".#apply"
47+
apps.apply = {
48+
type = "app";
49+
program = toString (pkgs.writers.writeBash "apply" ''
50+
if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi
51+
cp ${terraformConfiguration} config.tf.json \
52+
&& ${terraform}/bin/terraform init \
53+
&& ${terraform}/bin/terraform apply
54+
'');
55+
};
56+
57+
# nix run ".#destroy"
58+
apps.destroy = {
59+
type = "app";
60+
program = toString (pkgs.writers.writeBash "destroy" ''
61+
if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi
62+
cp ${terraformConfiguration} config.tf.json \
63+
&& ${terraform}/bin/terraform init \
64+
&& ${terraform}/bin/terraform destroy
65+
'');
66+
};
67+
68+
# nix run
69+
# every run will be generated config.tf.json
70+
defaultApp = self.apps.${system}.apply;
71+
});
72+
}

shell.nix

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
with import <nixpkgs> {};
2-
3-
pkgs.mkShell {
4-
name = "area13";
5-
6-
buildInputs = [
7-
terraform
8-
tfsec
9-
terrascan
10-
ripgrep
11-
bat
12-
];
13-
}
1+
# See https://nixos.wiki/wiki/Flakes#Using_flakes_project_from_a_legacy_Nix
2+
(import
3+
(
4+
let
5+
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
6+
in
7+
fetchTarball {
8+
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
9+
sha256 = lock.nodes.flake-compat.locked.narHash;
10+
}
11+
)
12+
{
13+
src = ./.;
14+
}).shellNix

0 commit comments

Comments
 (0)