···11-{ lib, google-cloud-sdk, runCommand, components }:
11+{ lib, google-cloud-sdk, symlinkJoin, components }:
2233comps_:
44···1919 defaultComponents = with components; [ alpha beta ];
20202121 comps = [ google-cloud-sdk ] ++ filterPreInstalled (findDepsRecursive (defaultComponents ++ comps_));
2222-in
2323-# Components are installed by copying the `google-cloud-sdk` package, along
2424-# with each component, over to a new location, and then patching that location
2525-# with `sed` to ensure the proper paths are used.
2626-# For some reason, this does not work properly with a `symlinkJoin`: the
2727-# `gcloud` binary doesn't seem able to find the installed components.
2828-runCommand "google-cloud-sdk-${google-cloud-sdk.version}"
2929-{
3030- inherit (google-cloud-sdk) meta;
3131- inherit comps;
3232- passAsFile = [ "comps" ];
33223434- doInstallCheck = true;
3535- disallowedRequisites = [ google-cloud-sdk ];
3636- installCheckPhase =
2323+ installCheck =
3724 let
3838- compNames = builtins.map (drv: drv.name) comps_;
2525+ compNames = builtins.map lib.getName comps_;
3926 in
4027 ''
4141- $out/bin/gcloud components list > component_list.txt
2828+ $out/bin/gcloud components list --only-local-state --format 'value(id)' > component_list.txt
4229 for comp in ${builtins.toString compNames}; do
4343- if [ ! grep ... component_list.txt | grep "Not Installed" ]; then
3030+ snapshot_file="$out/google-cloud-sdk/.install/$comp.snapshot.json"
3131+3232+ if ! [ -f "$snapshot_file" ]; then
3333+ echo "Failed to install component '$comp'"
3434+ exit 1
3535+ fi
3636+3737+ if grep --quiet '"is_hidden":true' "$snapshot_file"; then
3838+ continue
3939+ fi
4040+4141+ if ! grep --quiet "^$comp$" component_list.txt; then
4442 echo "Failed to install component '$comp'"
4543 exit 1
4644 fi
4745 done
4846 '';
4949-}
5050- ''
5151- mkdir -p $out
4747+in
4848+# The `gcloud` entrypoint script has some custom logic to determine the "real" cloud sdk
4949+# root. In order to not trip up this logic and still have the symlink joined root we copy
5050+# over this file. Since this file also has a Python wrapper, we need to copy that as well.
5151+symlinkJoin {
5252+ name = "google-cloud-sdk-${google-cloud-sdk.version}";
5353+ inherit (google-cloud-sdk) meta;
52545353- # Install each component
5454- for comp in $(cat $compsPath); do
5555- echo "installing component $comp"
5656- cp -dRf $comp/. $out
5757- find $out -type d -exec chmod 744 {} +
5858- done
5555+ paths = [
5656+ google-cloud-sdk
5757+ ] ++ comps;
59586060- # Replace references to the original google-cloud-sdk with this one
6161- find $out/google-cloud-sdk -type f -exec sed -i -e "s#${google-cloud-sdk}#$out#" {} \;
6262- ''
5959+ postBuild = ''
6060+ sed -i ';' $out/google-cloud-sdk/bin/.gcloud-wrapped
6161+ sed -i -e "s#${google-cloud-sdk}#$out#" "$out/google-cloud-sdk/bin/gcloud"
6262+ ${installCheck}
6363+ '';
6464+}