1# Make sure that the "with-gce" flag is set when building `google-cloud-sdk`
2# for GCE hosts. This flag prevents "google-compute-engine" from being a
3# default dependency which is undesirable because this package is
4#
5# 1) available only on GNU/Linux (requires `systemd` in particular)
6# 2) intended only for GCE guests (and is useless elsewhere)
7# 3) used by `google-cloud-sdk` only on GCE guests
8#
9
10{ stdenv, lib, fetchurl, makeWrapper, python, openssl, jq, callPackage, with-gce ? false }:
11
12let
13 pythonEnv = python.withPackages (p: with p; [
14 cffi
15 cryptography
16 openssl
17 crcmod
18 numpy
19 ] ++ lib.optional (with-gce) google-compute-engine);
20
21 data = import ./data.nix { };
22 sources = system:
23 data.googleCloudSdkPkgs.${system} or (throw "Unsupported system: ${system}");
24
25 components = callPackage ./components.nix {
26 snapshotPath = ./components.json;
27 };
28
29 withExtraComponents = callPackage ./withExtraComponents.nix { inherit components; };
30
31in stdenv.mkDerivation rec {
32 pname = "google-cloud-sdk";
33 inherit (data) version;
34
35 src = fetchurl (sources stdenv.hostPlatform.system);
36
37 buildInputs = [ python ];
38
39 nativeBuildInputs = [ jq makeWrapper ];
40
41 patches = [
42 # For kubectl configs, don't store the absolute path of the `gcloud` binary as it can be garbage-collected
43 ./gcloud-path.patch
44 # Disable checking for updates for the package
45 ./gsutil-disable-updates.patch
46 ];
47
48 installPhase = ''
49 runHook preInstall
50
51 mkdir -p $out/google-cloud-sdk
52 cp -R * .install $out/google-cloud-sdk/
53
54 mkdir -p $out/google-cloud-sdk/lib/surface/{alpha,beta}
55 cp ${./alpha__init__.py} $out/google-cloud-sdk/lib/surface/alpha/__init__.py
56 cp ${./beta__init__.py} $out/google-cloud-sdk/lib/surface/beta/__init__.py
57
58 # create wrappers with correct env
59 for program in gcloud bq gsutil git-credential-gcloud.sh docker-credential-gcloud; do
60 programPath="$out/google-cloud-sdk/bin/$program"
61 binaryPath="$out/bin/$program"
62 wrapProgram "$programPath" \
63 --set CLOUDSDK_PYTHON "${pythonEnv}/bin/python" \
64 --prefix PYTHONPATH : "${pythonEnv}/${python.sitePackages}" \
65 --prefix PATH : "${openssl.bin}/bin"
66
67 mkdir -p $out/bin
68 ln -s $programPath $binaryPath
69 done
70
71 # disable component updater and update check
72 substituteInPlace $out/google-cloud-sdk/lib/googlecloudsdk/core/config.json \
73 --replace "\"disable_updater\": false" "\"disable_updater\": true"
74 echo "
75 [component_manager]
76 disable_update_check = true" >> $out/google-cloud-sdk/properties
77
78 # setup bash completion
79 mkdir -p $out/share/bash-completion/completions
80 mv $out/google-cloud-sdk/completion.bash.inc $out/share/bash-completion/completions/gcloud
81 ln -s $out/share/bash-completion/completions/gcloud $out/share/bash-completion/completions/gsutil
82
83 # setup zsh completion
84 mkdir -p $out/share/zsh/site-functions
85 mv $out/google-cloud-sdk/completion.zsh.inc $out/share/zsh/site-functions/_gcloud
86 ln -s $out/share/zsh/site-functions/_gcloud $out/share/zsh/site-functions/_gsutil
87 # zsh doesn't load completions from $FPATH without #compdef as the first line
88 sed -i '1 i #compdef gcloud' $out/share/zsh/site-functions/_gcloud
89
90 # This directory contains compiled mac binaries. We used crcmod from
91 # nixpkgs instead.
92 rm -r $out/google-cloud-sdk/platform/gsutil/third_party/crcmod \
93 $out/google-cloud-sdk/platform/gsutil/third_party/crcmod_osx
94
95 # remove tests and test data
96 find $out -name tests -type d -exec rm -rf '{}' +
97 rm $out/google-cloud-sdk/platform/gsutil/gslib/commands/test.py
98
99 # compact all the JSON
100 find $out -name \*.json | while read path; do
101 jq -c . $path > $path.min
102 mv $path.min $path
103 done
104
105 runHook postInstall
106 '';
107
108 doInstallCheck = true;
109 installCheckPhase = ''
110 $out/bin/gcloud version --format json | jq '."Google Cloud SDK"' | grep "${version}"
111 '';
112
113 passthru = {
114 inherit components withExtraComponents;
115 updateScript = ./update.sh;
116 };
117
118 meta = with lib; {
119 description = "Tools for the google cloud platform";
120 longDescription = "The Google Cloud SDK. This package has the programs: gcloud, gsutil, and bq";
121 sourceProvenance = with sourceTypes; [
122 fromSource
123 binaryNativeCode # anthoscli and possibly more
124 ];
125 # This package contains vendored dependencies. All have free licenses.
126 license = licenses.free;
127 homepage = "https://cloud.google.com/sdk/";
128 changelog = "https://cloud.google.com/sdk/docs/release-notes";
129 maintainers = with maintainers; [ iammrinal0 pradyuman stephenmw zimbatm ];
130 platforms = builtins.attrNames data.googleCloudSdkPkgs;
131 mainProgram = "gcloud";
132 };
133}