| 
 | 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 | +}  | 
0 commit comments