diff --git a/builder.nix b/builder.nix index ec71bd6..a834e6e 100644 --- a/builder.nix +++ b/builder.nix @@ -1,22 +1,49 @@ -{ buildGoModule }: src: desc: buildGoModule { - pname = desc.name; - version = desc.version; - inherit src; - +{ name +, src +, provider-source-address +, version +, vendorSha256 ? null +, buildGoModule +, GOOS +, GOARCH +}: buildGoModule { + pname = "${name}-${GOOS}-${GOARCH}"; subPackages = ["."]; - vendorSha256 = desc.vendorSha256 or null; + + inherit + src + version + vendorSha256 + ; + + CGO_ENABLED = "0"; + ldflags = ["-extldflags=-static"]; + + postConfigure = '' + export GOOS=${GOOS} + export GOARCH=${GOARCH} + ''; # Terraform allow checking the provider versions, but this breaks # if the versions are not provided via file paths. - postBuild = "mv $NIX_BUILD_TOP/go/bin/${desc.name}{,_v${desc.version}}"; - postInstall = with desc; '' - dir=$out/libexec/terraform-providers/${provider-source-address}/${version}/''${GOOS}_''${GOARCH} + postBuild = '' + ( + dir=$GOPATH/bin/${GOOS}_${GOARCH} + if [[ -n "$(shopt -s nullglob; echo $dir/*)" ]] + then + mv $dir/* $dir/.. + fi + if [[ -d $dir ]] + then + rmdir $dir + fi + ) + mv $GOPATH/bin/${name} $GOPATH/bin/${name}_v${version} + ''; + postInstall = '' + dir=$out/libexec/terraform-providers/${provider-source-address}/${version}/${GOOS}_${GOARCH} mkdir -p "$dir" mv $out/bin/* "$dir/terraform-provider-$(basename ${provider-source-address})_${version}" rmdir $out/bin ''; - passthru = desc; - - CGO_ENABLED = "0"; - ldflags = ["-extldflags=-static"]; } diff --git a/default.nix b/default.nix index a32a571..6b6ffb8 100644 --- a/default.nix +++ b/default.nix @@ -1,25 +1,71 @@ { pkgs ? import {} , namespace ? "registry.terraform.io/corpix" +, repo ? "terraform-provider-${name}" , name ? "nixos" , version ? "0.0.1" }: let + inherit (builtins) + toString + baseNameOf + filterSource + trace + ; inherit (pkgs) + symlinkJoin buildGoModule ; + inherit (pkgs.lib) + concatStringsSep + hasPrefix + hasSuffix + ; inherit (pkgs.nix-gitignore) gitignoreSourcePure ; ## - mkProvider = import ./builder.nix { - inherit buildGoModule; - }; + providerSourceFilter = name: type: + let bname = baseNameOf name; + in + ((type == "regular") + && ((hasSuffix ".go" name) + || (hasSuffix ".s" name) + || (hasSuffix "/go.sum" name) + || (hasSuffix "/go.mod" name) + || (hasSuffix "/provider/nix_conf_wrapper.nix" name) + || (hasSuffix "/vendor/modules.txt" name))) + || ((type == "directory") + && !(hasPrefix "." bname)); + sources = let src = filterSource providerSourceFilter ./.; + in trace "sources: ${src}" src; + + ## + + builder = import ./builder.nix; + build = platform: builder + { + name = repo; + version = version; + src = sources; + + provider-source-address = "${namespace}/${name}"; + + inherit (platform) + GOOS + GOARCH + ; + inherit buildGoModule; + }; -in mkProvider - (gitignoreSourcePure [./.gitignore] ./.) - { - name = "terraform-provider-${name}"; - version = version; - provider-source-address = "${namespace}/${name}"; - } + artifacts = map build + [ + { GOOS = "linux"; GOARCH = "amd64"; } + { GOOS = "linux"; GOARCH = "arm64"; } + { GOOS = "darwin"; GOARCH = "amd64"; } + { GOOS = "darwin"; GOARCH = "arm64"; } + ]; +in symlinkJoin { + name = repo; + paths = artifacts; +} diff --git a/makefile b/makefile index 6012138..b618a96 100644 --- a/makefile +++ b/makefile @@ -8,11 +8,10 @@ result = $(root)/result/libexec/terraform-providers namespace = registry.terraform.io/corpix name = nixos version ?= $(shell git rev-list --tags --max-count=1 | xargs git describe --tags) -target = linux_amd64 gpg_key ?= 190E440CECF0D6C28E22C8F7755E11DE93BDB108 -provider_root = $(result)/$(namespace)/$(name)/$(version)/$(target) -provider_binary = $(provider_root)/terraform-provider-$(name)_$(version) +provider_root = $(result)/$(namespace)/$(name)/$(version) +provider_binary = terraform-provider-$(name)_$(version) .PHONY: build build: @@ -30,8 +29,16 @@ release: build rm -rf release || true mkdir -p release cd release - cp $(provider_binary) terraform-provider-$(name)_v$(version) - zip terraform-provider-$(name)_$(version)_$(target).zip * + for platform in $$(ls $(provider_root)); \ + do \ + cp -f \ + $(provider_root)/$$platform/$(provider_binary) \ + terraform-provider-$(name)_v$(version); \ + zip \ + terraform-provider-$(name)_$(version)_$$platform.zip \ + terraform-provider-$(name)_v$(version); \ + done + echo '{ "version": 1, "metadata": { "protocol_versions": ["5.0"] } }' \ | jq \ > terraform-provider-$(name)_$(version)_manifest.json