Skip to content

Commit bfd7372

Browse files
committed
Expose buildTerraform / Update update_versions script
1 parent a89bf8c commit bfd7372

File tree

6 files changed

+60
-37
lines changed

6 files changed

+60
-37
lines changed

.envrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
templates/*/flake.lock
2-
result
1+
.direnv
32
.env
3+
result
4+
templates/*/flake.lock

flake.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+25-20
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,7 @@
1414
(system:
1515
let
1616
pkgs = nixpkgs.legacyPackages.${system};
17-
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
1817
versions = builtins.fromJSON (builtins.readFile ./versions.json);
19-
buildTerraform = { version, hash, vendorHash }:
20-
# https://www.hashicorp.com/blog/hashicorp-adopts-business-source-license
21-
if builtins.compareVersions version "1.6.0" >= 0
22-
then
23-
# https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/applications/networking/cluster/terraform/default.nix
24-
pkgs-unstable.mkTerraform
25-
{
26-
inherit version hash vendorHash;
27-
patches = [ "${nixpkgs-unstable}/pkgs/applications/networking/cluster/terraform/provider-path-0_15.patch" ];
28-
}
29-
else
30-
# https://github.com/NixOS/nixpkgs/blob/nixos-23.05/pkgs/applications/networking/cluster/terraform/default.nix
31-
pkgs.mkTerraform {
32-
inherit version hash vendorHash;
33-
patches = [ "${nixpkgs}/pkgs/applications/networking/cluster/terraform/provider-path-0_15.patch" ];
34-
};
3518
in
3619
{
3720
# https://github.com/NixOS/nix/issues/7165
@@ -40,8 +23,8 @@
4023
(builtins.map
4124
(version: {
4225
name = version;
43-
value = buildTerraform {
44-
inherit version;
26+
value = self.lib.buildTerraform {
27+
inherit system version;
4528
inherit (versions.${version}) hash vendorHash;
4629
};
4730
})
@@ -56,6 +39,28 @@
5639
];
5740
};
5841
}) // {
59-
lib.packageFromVersion = { system, version }: self.packages.${system}.${version};
42+
lib = {
43+
buildTerraform = { system, version, hash, vendorHash }:
44+
let
45+
pkgs = nixpkgs.legacyPackages.${system};
46+
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
47+
in
48+
# https://www.hashicorp.com/blog/hashicorp-adopts-business-source-license
49+
if builtins.compareVersions version "1.6.0" >= 0
50+
then
51+
# https://github.com/NixOS/nixpkgs/blob/nixpkgs-unstable/pkgs/applications/networking/cluster/terraform/default.nix
52+
pkgs-unstable.mkTerraform
53+
{
54+
inherit version hash vendorHash;
55+
patches = [ "${nixpkgs-unstable}/pkgs/applications/networking/cluster/terraform/provider-path-0_15.patch" ];
56+
}
57+
else
58+
# https://github.com/NixOS/nixpkgs/blob/nixos-23.05/pkgs/applications/networking/cluster/terraform/default.nix
59+
pkgs.mkTerraform {
60+
inherit version hash vendorHash;
61+
patches = [ "${nixpkgs}/pkgs/applications/networking/cluster/terraform/provider-path-0_15.patch" ];
62+
};
63+
packageFromVersion = { system, version }: self.packages.${system}.${version};
64+
};
6065
};
6166
}

scripts/update_versions

+22-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,21 @@ def by_version(release):
2525
def to_version(versions, release):
2626
version = release.tag_name.removeprefix("v")
2727
print(f"Calculating hash for {version}")
28-
# TODO: Use `nix-prefetch` to calculate the hash
29-
hash_output = subprocess.check_output(["nix-prefetch-git", "--quiet", "https://github.com/hashicorp/terraform", release.title])
30-
vendor_hash_output = subprocess.check_output([
28+
calculated_hash = subprocess.check_output([
29+
"nix-prefetch",
30+
"fetchFromGitHub",
31+
"--silent",
32+
"--option",
33+
"extra-experimental-features",
34+
"--owner",
35+
"hashicorp",
36+
"--repo",
37+
"terraform",
38+
"--rev",
39+
release.title
40+
]).decode("utf-8")
41+
print(f"Calculating vendorHash for {version}")
42+
calculated_vendor_hash = subprocess.check_output([
3143
"nix-prefetch",
3244
"--silent",
3345
"--option",
@@ -37,11 +49,14 @@ def to_version(versions, release):
3749
"../vendor_hash.nix",
3850
"--argstr",
3951
"version",
40-
version
41-
])
52+
version,
53+
"--argstr",
54+
"hash",
55+
calculated_hash
56+
]).decode("utf-8")
4257
versions[version] = {
43-
"hash": json.loads(hash_output.decode("utf-8"))["hash"],
44-
"vendorHash": vendor_hash_output.decode("utf-8")
58+
"hash": calculated_hash,
59+
"vendorHash": calculated_vendor_hash
4560
}
4661
return versions
4762

vendor_hash.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
{ version }: { sha256 }:
2-
((builtins.getFlake (toString ./.)).lib.packageFromVersion {
3-
inherit version;
1+
{ version, hash }: { sha256 }:
2+
((builtins.getFlake (toString ./.)).lib.buildTerraform {
3+
inherit version hash;
44
system = builtins.currentSystem;
5+
vendorHash = "";
56
}).go-modules.overrideAttrs (_: { vendorSha256 = sha256; })

0 commit comments

Comments
 (0)