-
Notifications
You must be signed in to change notification settings - Fork 20
/
default.nix
116 lines (104 loc) · 3.06 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{ system ? builtins.currentSystem
, nixpkgs ? import (import ./flake.lock.nix).nixpkgs {
inherit system;
configuration = { };
overlays = [ ];
}
}:
let
# shadow the above system on purpose as it should only be used to construct
# nixpkgs.
system = null;
inherit (nixpkgs) lib stdenv unzip;
fetchArchURL = system: archSrc:
let
src = archSrc.${system} or (throw "system ${system} not supported");
in
nixpkgs.fetchurl src;
mkTerraformProvider =
{ owner
, repo
, version
, archSrc
, # TODO: pass this down
registry ? "registry.terraform.io"
}:
let
inherit (nixpkgs.go) GOARCH GOOS;
provider-source-address = "${registry}/${owner}/${repo}";
in
stdenv.mkDerivation {
pname = "terraform-provider-${repo}";
version = version;
src = fetchArchURL nixpkgs.stdenv.hostPlatform.system archSrc;
unpackPhase = "unzip -o $src";
nativeBuildInputs = [ unzip ];
buildPhase = ":";
# The upstream terraform wrapper assumes the provider filename here.
installPhase = ''
dir=$out/libexec/terraform-providers/${provider-source-address}/${version}/${GOOS}_${GOARCH}
mkdir -p "$dir"
mv terraform-* "$dir/"
'';
passthru = {
inherit provider-source-address;
};
};
providers = lib.mapAttrs
(name: type:
if type == "directory" then
lib.mapAttrs
(name': type':
if type == "directory" then
let data = lib.importJSON (./providers + "/${name}/${name'}/default.json"); in
mkTerraformProvider data
else
null
)
(builtins.readDir (./providers + "/${name}"))
else
null
)
(builtins.readDir ./providers);
# DEPRECATED
wrapTerraform = terraform: fn:
let
# It would be nice to be able to use a buildEnv here, but Terraform
# only allows a symlink at the root of the plugin dir. So instead we
# create a bunch of trampoline files.
plugins = nixpkgs.runCommand "terraform-plugins"
{
nativeBuildInputs = [ nixpkgs.findutils ];
}
''
for plugin_dir in ${toString (fn providers)}; do
plugin=$(find $plugin_dir -type f)
plugin_rel=''${plugin#"$plugin_dir/"}
plugin_out=$out/$plugin_rel
mkdir -p "$(dirname "$plugin_out")"
cat <<EOS > "$plugin_out"
#!${nixpkgs.bash}/bin/sh
exec "$plugin" "\$@"
EOS
chmod +x "$plugin_out"
done
'';
wrapper = nixpkgs.writeShellScriptBin "terraform" ''
export NIX_TERRAFORM_PLUGIN_DIR=${plugins}/libexec/terraform-providers
exec ${terraform}/bin/terraform "$@"
'';
in
builtins.trace "wrapTerraform is deprecated. Please use nixpkgs.terraform.withPlugins in nixos-22.05 or later."
wrapper;
tests = lib.recurseIntoAttrs (import ./test/file {
inherit system nixpkgs;
});
in
{
inherit
mkTerraformProvider
providers
tests
wrapTerraform
;
}