Skip to content

Commit

Permalink
adding cross-compilation for darwin and linux to address #1
Browse files Browse the repository at this point in the history
  • Loading branch information
corpix committed Jun 13, 2022
1 parent 8dd875d commit c187b97
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 28 deletions.
53 changes: 40 additions & 13 deletions builder.nix
Original file line number Diff line number Diff line change
@@ -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"];
}
66 changes: 56 additions & 10 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
{ pkgs ? import <nixpkgs> {}
, 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;
}
17 changes: 12 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit c187b97

Please sign in to comment.