nixpkgs/pkgs/tools/admin/google-cloud-sdk/withExtraComponents.nix
Xavier Lambein 95a77857ef google-cloud-sdk: add support for components
This commit adds a set of Google Cloud SDK components in
`pkgs.google-cloud-sdk.components` which can be installed along with
the SDK through `pkgs.google-cloud-sdk.withExtraComponents [ ... ]`.
2022-08-21 23:53:00 +02:00

62 lines
2.1 KiB
Nix

{ lib, google-cloud-sdk, callPackage, runCommand, components }:
comps_:
let
# Remove components which are already installed by default
filterPreInstalled =
let
preInstalledComponents = with components; [ bq bq-nix core core-nix gcloud-deps gcloud gsutil gsutil-nix ];
in
builtins.filter (drv: !(builtins.elem drv preInstalledComponents));
# Recursively build a list of components with their dependencies
# TODO this could be made faster, it checks the dependencies too many times
findDepsRecursive = lib.converge
(drvs: lib.unique (drvs ++ (builtins.concatMap (drv: drv.dependencies) drvs)));
# Components to install by default
defaultComponents = with components; [ alpha beta ];
comps = [ google-cloud-sdk ] ++ filterPreInstalled (findDepsRecursive (defaultComponents ++ comps_));
in
# Components are installed by copying the `google-cloud-sdk` package, along
# with each component, over to a new location, and then patching that location
# with `sed` to ensure the proper paths are used.
# For some reason, this does not work properly with a `symlinkJoin`: the
# `gcloud` binary doesn't seem able to find the installed components.
runCommand "google-cloud-sdk-${google-cloud-sdk.version}"
{
inherit (google-cloud-sdk) meta;
inherit comps;
passAsFile = [ "comps" ];
doInstallCheck = true;
installCheckPhase =
let
compNames = builtins.map (drv: drv.name) comps_;
in
''
$out/bin/gcloud components list > component_list.txt
for comp in ${builtins.toString compNames}; do
if [ ! grep ... component_list.txt | grep "Not Installed" ]; then
echo "Failed to install component '$comp'"
exit 1
fi
done
'';
}
''
mkdir -p $out
# Install each component
for comp in $(cat $compsPath); do
echo "installing component $comp"
cp -dRf $comp/. $out
find $out -type d -exec chmod 744 {} +
done
# Replace references to the original google-cloud-sdk with this one
find $out/google-cloud-sdk/bin/ -type f -exec sed -i -e "s#${google-cloud-sdk}#$out#" {} \;
''