use terranix flake parts module directly

This commit is contained in:
William 2025-10-29 11:14:52 -03:00
parent 90cdc7b8a5
commit b75f9752d1
2 changed files with 12 additions and 80 deletions

View file

@ -1,22 +1,17 @@
{ inputs, ... }: { 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 = imports = [
{ system, ... }: inputs.terranix.flakeModule
{ ];
packages = mkTerranixDerivation { inherit system configs; };
perSystem = {
terranix.terranixConfigurations = {
# Example:
# myconfig = {
# modules = [ ./terraform/myconfig.nix ];
# extraArgs = { }; # optional
# };
}; };
};
} }

View file

@ -7,7 +7,6 @@ let
nixpkgs-stable nixpkgs-stable
home-manager home-manager
agenix agenix
terranix
; ;
in in
@ -191,66 +190,4 @@ in
}; };
in in
lib.mapAttrs (_: lib.recursiveUpdate commonVHostConfig) domains; 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;
} }