2021-06-15 08:40:39 +00:00
|
|
|
{ stdenv
|
|
|
|
, lib
|
|
|
|
, buildGoModule
|
|
|
|
, fetchFromGitHub
|
|
|
|
, makeWrapper
|
|
|
|
, coreutils
|
|
|
|
, runCommand
|
|
|
|
, runtimeShell
|
|
|
|
, writeText
|
|
|
|
, terraform-providers
|
|
|
|
, fetchpatch
|
|
|
|
}:
|
2016-06-05 09:16:58 +00:00
|
|
|
|
2017-03-17 09:05:16 +00:00
|
|
|
let
|
2020-12-03 13:03:03 +00:00
|
|
|
generic = { version, sha256, vendorSha256 ? null, ... }@attrs:
|
|
|
|
let attrs' = builtins.removeAttrs attrs [ "version" "sha256" "vendorSha256" ];
|
2021-06-15 08:40:39 +00:00
|
|
|
in
|
|
|
|
buildGoModule ({
|
2022-09-16 06:23:39 +00:00
|
|
|
pname = "terraform";
|
|
|
|
inherit version vendorSha256;
|
2017-03-31 11:19:22 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
2019-11-14 15:30:19 +00:00
|
|
|
owner = "hashicorp";
|
|
|
|
repo = "terraform";
|
|
|
|
rev = "v${version}";
|
2017-03-31 11:19:22 +00:00
|
|
|
inherit sha256;
|
|
|
|
};
|
|
|
|
|
2021-12-09 10:10:21 +00:00
|
|
|
ldflags = [ "-s" "-w" ];
|
|
|
|
|
2020-12-03 13:03:03 +00:00
|
|
|
postConfigure = ''
|
2020-04-19 16:07:39 +00:00
|
|
|
# speakeasy hardcodes /bin/stty https://github.com/bgentry/speakeasy/issues/22
|
|
|
|
substituteInPlace vendor/github.com/bgentry/speakeasy/speakeasy_unix.go \
|
|
|
|
--replace "/bin/stty" "${coreutils}/bin/stty"
|
|
|
|
'';
|
|
|
|
|
2017-03-31 11:19:22 +00:00
|
|
|
postInstall = ''
|
|
|
|
# remove all plugins, they are part of the main binary now
|
2020-04-28 01:50:57 +00:00
|
|
|
for i in $out/bin/*; do
|
2017-03-31 11:19:22 +00:00
|
|
|
if [[ $(basename $i) != terraform ]]; then
|
|
|
|
rm "$i"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
'';
|
2017-03-17 09:05:16 +00:00
|
|
|
|
2017-03-31 11:19:22 +00:00
|
|
|
preCheck = ''
|
2020-12-03 13:03:03 +00:00
|
|
|
export HOME=$TMPDIR
|
|
|
|
export TF_SKIP_REMOTE_TESTS=1
|
2017-03-31 11:19:22 +00:00
|
|
|
'';
|
|
|
|
|
2020-12-03 13:03:03 +00:00
|
|
|
subPackages = [ "." ];
|
|
|
|
|
2021-01-11 07:54:33 +00:00
|
|
|
meta = with lib; {
|
2019-11-14 15:30:19 +00:00
|
|
|
description =
|
|
|
|
"Tool for building, changing, and versioning infrastructure";
|
|
|
|
homepage = "https://www.terraform.io/";
|
2020-12-08 18:06:00 +00:00
|
|
|
changelog = "https://github.com/hashicorp/terraform/blob/v${version}/CHANGELOG.md";
|
2017-03-31 11:19:22 +00:00
|
|
|
license = licenses.mpl20;
|
2019-11-14 15:30:19 +00:00
|
|
|
maintainers = with maintainers; [
|
2020-10-08 20:55:59 +00:00
|
|
|
Chili-Man
|
|
|
|
babariviere
|
2019-11-14 15:30:19 +00:00
|
|
|
kalbasit
|
|
|
|
marsam
|
2022-01-24 11:53:05 +00:00
|
|
|
maxeaubrey
|
2020-10-08 20:55:59 +00:00
|
|
|
timstott
|
2022-05-19 11:25:14 +00:00
|
|
|
zimbatm
|
2021-12-31 05:10:45 +00:00
|
|
|
zowoq
|
2022-05-19 11:25:14 +00:00
|
|
|
techknowlogick
|
|
|
|
];
|
2017-03-31 11:19:22 +00:00
|
|
|
};
|
|
|
|
} // attrs');
|
2017-08-30 16:25:34 +00:00
|
|
|
|
|
|
|
pluggable = terraform:
|
|
|
|
let
|
2017-08-30 22:53:14 +00:00
|
|
|
withPlugins = plugins:
|
|
|
|
let
|
|
|
|
actualPlugins = plugins terraform.plugins;
|
2017-08-30 16:25:34 +00:00
|
|
|
|
2018-03-28 00:25:23 +00:00
|
|
|
# Wrap PATH of plugins propagatedBuildInputs, plugins may have runtime dependencies on external binaries
|
2019-11-14 15:30:19 +00:00
|
|
|
wrapperInputs = lib.unique (lib.flatten
|
|
|
|
(lib.catAttrs "propagatedBuildInputs"
|
|
|
|
(builtins.filter (x: x != null) actualPlugins)));
|
2018-03-28 00:25:23 +00:00
|
|
|
|
2017-08-30 22:53:14 +00:00
|
|
|
passthru = {
|
2019-11-14 15:30:19 +00:00
|
|
|
withPlugins = newplugins:
|
|
|
|
withPlugins (x: newplugins x ++ actualPlugins);
|
2022-04-09 09:04:53 +00:00
|
|
|
full = withPlugins (p: lib.filter lib.isDerivation (lib.attrValues p.actualProviders));
|
2017-08-30 16:25:34 +00:00
|
|
|
|
2022-02-08 16:41:58 +00:00
|
|
|
# Expose wrappers around the override* functions of the terraform
|
|
|
|
# derivation.
|
|
|
|
#
|
|
|
|
# Note that this does not behave as anyone would expect if plugins
|
|
|
|
# are specified. The overrides are not on the user-visible wrapper
|
|
|
|
# derivation but instead on the function application that eventually
|
|
|
|
# generates the wrapper. This means:
|
|
|
|
#
|
|
|
|
# 1. When using overrideAttrs, only `passthru` attributes will
|
|
|
|
# become visible on the wrapper derivation. Other overrides that
|
|
|
|
# modify the derivation *may* still have an effect, but it can be
|
|
|
|
# difficult to follow.
|
|
|
|
#
|
|
|
|
# 2. Other overrides may work if they modify the terraform
|
|
|
|
# derivation, or they may have no effect, depending on what
|
|
|
|
# exactly is being changed.
|
|
|
|
#
|
|
|
|
# 3. Specifying overrides on the wrapper is unsupported.
|
|
|
|
#
|
|
|
|
# See nixpkgs#158620 for details.
|
2019-11-14 15:30:19 +00:00
|
|
|
overrideDerivation = f:
|
|
|
|
(pluggable (terraform.overrideDerivation f)).withPlugins plugins;
|
|
|
|
overrideAttrs = f:
|
|
|
|
(pluggable (terraform.overrideAttrs f)).withPlugins plugins;
|
|
|
|
override = x:
|
|
|
|
(pluggable (terraform.override x)).withPlugins plugins;
|
2017-08-30 22:53:14 +00:00
|
|
|
};
|
|
|
|
# Don't bother wrapping unless we actually have plugins, since the wrapper will stop automatic downloading
|
|
|
|
# of plugins, which might be counterintuitive if someone just wants a vanilla Terraform.
|
2021-06-15 08:40:39 +00:00
|
|
|
in
|
|
|
|
if actualPlugins == [ ] then
|
2019-11-14 15:30:19 +00:00
|
|
|
terraform.overrideAttrs
|
2021-06-15 08:40:39 +00:00
|
|
|
(orig: { passthru = orig.passthru // passthru; })
|
2019-11-14 15:30:19 +00:00
|
|
|
else
|
2022-01-28 12:16:31 +00:00
|
|
|
lib.appendToName "with-plugins" (stdenv.mkDerivation {
|
2020-11-11 19:36:31 +00:00
|
|
|
inherit (terraform) name meta;
|
2021-02-07 09:17:39 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
2022-01-28 12:16:31 +00:00
|
|
|
|
2022-02-08 16:41:58 +00:00
|
|
|
# Expose the passthru set with the override functions
|
|
|
|
# defined above, as well as any passthru values already
|
|
|
|
# set on `terraform` at this point (relevant in case a
|
|
|
|
# user overrides attributes).
|
|
|
|
passthru = terraform.passthru // passthru;
|
|
|
|
|
2022-01-28 12:16:31 +00:00
|
|
|
buildCommand = ''
|
|
|
|
# Create wrappers for terraform plugins because Terraform only
|
|
|
|
# walks inside of a tree of files.
|
|
|
|
for providerDir in ${toString actualPlugins}
|
|
|
|
do
|
|
|
|
for file in $(find $providerDir/libexec/terraform-providers -type f)
|
|
|
|
do
|
|
|
|
relFile=''${file#$providerDir/}
|
|
|
|
mkdir -p $out/$(dirname $relFile)
|
|
|
|
cat <<WRAPPER > $out/$relFile
|
|
|
|
#!${runtimeShell}
|
|
|
|
exec "$file" "$@"
|
|
|
|
WRAPPER
|
|
|
|
chmod +x $out/$relFile
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
# Create a wrapper for terraform to point it to the plugins dir.
|
|
|
|
mkdir -p $out/bin/
|
2020-04-28 01:50:57 +00:00
|
|
|
makeWrapper "${terraform}/bin/terraform" "$out/bin/terraform" \
|
2022-01-24 11:53:05 +00:00
|
|
|
--set NIX_TERRAFORM_PLUGIN_DIR $out/libexec/terraform-providers \
|
2019-11-14 15:30:19 +00:00
|
|
|
--prefix PATH : "${lib.makeBinPath wrapperInputs}"
|
|
|
|
'';
|
|
|
|
});
|
2021-06-15 08:40:39 +00:00
|
|
|
in
|
|
|
|
withPlugins (_: [ ]);
|
2019-11-14 15:30:19 +00:00
|
|
|
|
|
|
|
plugins = removeAttrs terraform-providers [
|
|
|
|
"override"
|
|
|
|
"overrideDerivation"
|
|
|
|
"recurseForDerivations"
|
|
|
|
];
|
2021-06-15 08:40:39 +00:00
|
|
|
in
|
|
|
|
rec {
|
2021-06-15 08:44:17 +00:00
|
|
|
# Constructor for other terraform versions
|
|
|
|
mkTerraform = attrs: pluggable (generic attrs);
|
|
|
|
|
2021-12-13 16:16:44 +00:00
|
|
|
terraform_1 = mkTerraform {
|
2023-01-04 18:01:19 +00:00
|
|
|
version = "1.3.7";
|
|
|
|
sha256 = "sha256-z49DXJ9oYObJQWHPeuKvQ6jJtAheYuy0+QmvZ74ZbTQ";
|
2022-11-17 20:26:12 +00:00
|
|
|
vendorSha256 = "sha256-fviukVGBkbxFs2fJpEp/tFMymXex7NRQdcGIIA9W88k=";
|
2021-06-08 18:33:16 +00:00
|
|
|
patches = [ ./provider-path-0_15.patch ];
|
2022-06-07 05:17:26 +00:00
|
|
|
passthru = {
|
|
|
|
inherit plugins;
|
|
|
|
tests = { inherit terraform_plugins_test; };
|
|
|
|
};
|
2021-06-15 08:44:17 +00:00
|
|
|
};
|
2021-06-08 18:33:16 +00:00
|
|
|
|
2018-10-04 20:41:19 +00:00
|
|
|
# Tests that the plugins are being used. Terraform looks at the specific
|
|
|
|
# file pattern and if the plugin is not found it will try to download it
|
|
|
|
# from the Internet. With sandboxing enable this test will fail if that is
|
|
|
|
# the case.
|
2021-06-15 08:40:39 +00:00
|
|
|
terraform_plugins_test =
|
|
|
|
let
|
|
|
|
mainTf = writeText "main.tf" ''
|
|
|
|
resource "random_id" "test" {}
|
2018-10-04 20:41:19 +00:00
|
|
|
'';
|
2021-12-13 16:16:44 +00:00
|
|
|
terraform = terraform_1.withPlugins (p: [ p.random ]);
|
2021-06-15 08:40:39 +00:00
|
|
|
test =
|
|
|
|
runCommand "terraform-plugin-test" { buildInputs = [ terraform ]; } ''
|
|
|
|
set -e
|
|
|
|
# make it fail outside of sandbox
|
|
|
|
export HTTP_PROXY=http://127.0.0.1:0 HTTPS_PROXY=https://127.0.0.1:0
|
|
|
|
cp ${mainTf} main.tf
|
|
|
|
terraform init
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
test;
|
2018-10-04 20:41:19 +00:00
|
|
|
|
2016-06-05 09:16:58 +00:00
|
|
|
}
|