diff --git a/flake.lock b/flake.lock index feb5089..f9d54f9 100644 --- a/flake.lock +++ b/flake.lock @@ -241,6 +241,27 @@ "type": "github" } }, + "flake-parts_3": { + "inputs": { + "nixpkgs-lib": [ + "terranix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1736143030, + "narHash": "sha256-+hu54pAoLDEZT9pjHlqL9DNzWz0NbUn8NEAHP7PQPzU=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "b905f6fc23a9051a6e1b741e1438dbfc0634c6de", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems_3" @@ -783,6 +804,7 @@ "nixpkgs-stable": "nixpkgs-stable_2", "noctalia": "noctalia", "stylix": "stylix", + "terranix": "terranix", "zen-browser": "zen-browser" } }, @@ -936,6 +958,43 @@ "type": "github" } }, + "systems_6": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "terranix": { + "inputs": { + "flake-parts": "flake-parts_3", + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems_6" + }, + "locked": { + "lastModified": 1757278723, + "narHash": "sha256-hTMi6oGU+6VRnW9SZZ+muFcbfMEf2ajjOp7Z2KM5MMY=", + "owner": "terranix", + "repo": "terranix", + "rev": "924573fa6587ac57b0d15037fbd2d3f0fcdf17fb", + "type": "github" + }, + "original": { + "owner": "terranix", + "repo": "terranix", + "type": "github" + } + }, "tinted-foot": { "flake": false, "locked": { diff --git a/flake.nix b/flake.nix index 72aa094..ccbec85 100644 --- a/flake.nix +++ b/flake.nix @@ -47,6 +47,11 @@ url = "github:nix-community/nix-index-database"; inputs.nixpkgs.follows = "nixpkgs"; }; + + terranix = { + url = "github:terranix/terranix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = @@ -65,6 +70,7 @@ ./overlays.nix ./packages.nix ./deploy.nix + ./terranixConfigurations.nix ]; }; } diff --git a/terranixConfigurations.nix b/terranixConfigurations.nix new file mode 100644 index 0000000..9dd1c8f --- /dev/null +++ b/terranixConfigurations.nix @@ -0,0 +1,22 @@ +{ inputs, ... }: + +let + lib = inputs.nixpkgs.lib; + utils = import ./utils.nix { inherit inputs lib; }; + inherit (utils) mkTerranixDerivation; + + configs = { + # Example: + # myconfig = { + # modules = [ ./terraform/myconfig.nix ]; + # }; + }; +in + +{ + perSystem = + { system, ... }: + { + packages = mkTerranixDerivation { inherit system configs; }; + }; +} diff --git a/utils.nix b/utils.nix index 38cf968..fb92954 100644 --- a/utils.nix +++ b/utils.nix @@ -7,6 +7,7 @@ let nixpkgs-stable home-manager agenix + terranix ; in @@ -190,4 +191,66 @@ in }; in lib.mapAttrs (_: lib.recursiveUpdate commonVHostConfig) domains; + + # Terranix configuration system + mkTerranixDerivation = + { + system, + configs, + }: + lib.mapAttrs ( + name: cfg: + let + pkgs = nixpkgs.legacyPackages.${system}; + modules = cfg.modules; + extraArgs = cfg.extraArgs or { }; + + terraformConfiguration = terranix.lib.terranixConfiguration { + inherit system modules; + extraArgs = { + inherit lib pkgs; + } // extraArgs; + }; + + tf-json = pkgs.writeShellScriptBin "default" '' + cat ${terraformConfiguration} | ${pkgs.jq}/bin/jq + ''; + + apply = pkgs.writeShellScriptBin "apply" '' + if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi + cp ${terraformConfiguration} config.tf.json \ + && ${pkgs.terraform}/bin/terraform init \ + && ${pkgs.terraform}/bin/terraform apply + ''; + + plan = pkgs.writeShellScriptBin "plan" '' + if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi + cp ${terraformConfiguration} config.tf.json \ + && ${pkgs.terraform}/bin/terraform init \ + && ${pkgs.terraform}/bin/terraform plan + ''; + + destroy = pkgs.writeShellScriptBin "destroy" '' + if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi + cp ${terraformConfiguration} config.tf.json \ + && ${pkgs.terraform}/bin/terraform init \ + && ${pkgs.terraform}/bin/terraform destroy + ''; + + create-state-bucket = pkgs.writeShellScriptBin "create-state-bucket" '' + BUCKET_NAME=''${1:-"terraform-state-bucket"} + ACCOUNT_ID=''${2:?"Error: Cloudflare account ID required as second argument"} + R2_ENDPOINT="https://$ACCOUNT_ID.r2.cloudflarestorage.com" + + echo "Creating R2 bucket $BUCKET_NAME..." + ${pkgs.awscli}/bin/aws s3api create-bucket \ + --bucket "$BUCKET_NAME" \ + --endpoint-url "$R2_ENDPOINT" + echo "Bucket created successfully at $R2_ENDPOINT" + ''; + in + tf-json // { + inherit apply plan destroy create-state-bucket; + } + ) configs; }