1{
2 lib,
3 google-cloud-sdk,
4 symlinkJoin,
5 components,
6}:
7
8comps_:
9
10let
11 # Remove components which are already installed by default
12 filterPreInstalled =
13 let
14 preInstalledComponents = with components; [
15 bq
16 bq-nix
17 core
18 core-nix
19 gcloud-deps
20 gcloud
21 gsutil
22 gsutil-nix
23 ];
24 in
25 builtins.filter (drv: !(builtins.elem drv preInstalledComponents));
26
27 # Recursively build a list of components with their dependencies
28 # TODO this could be made faster, it checks the dependencies too many times
29 findDepsRecursive = lib.converge (
30 drvs: lib.unique (drvs ++ (builtins.concatMap (drv: drv.dependencies) drvs))
31 );
32
33 # Components to install by default
34 defaultComponents = with components; [
35 alpha
36 beta
37 ];
38
39 comps = [
40 google-cloud-sdk
41 ]
42 ++ filterPreInstalled (findDepsRecursive (defaultComponents ++ comps_));
43
44 installCheck =
45 let
46 compNames = builtins.map lib.getName comps_;
47 in
48 ''
49 $out/bin/gcloud components list --only-local-state --format 'value(id)' > component_list.txt
50 for comp in ${builtins.toString compNames}; do
51 snapshot_file="$out/google-cloud-sdk/.install/$comp.snapshot.json"
52
53 if ! [ -f "$snapshot_file" ]; then
54 echo "Failed to install component '$comp'"
55 exit 1
56 fi
57
58 if grep --quiet '"is_hidden":true' "$snapshot_file"; then
59 continue
60 fi
61
62 if ! grep --quiet "^$comp$" component_list.txt; then
63 echo "Failed to install component '$comp'"
64 exit 1
65 fi
66 done
67 '';
68in
69# The `gcloud` entrypoint script has some custom logic to determine the "real" cloud sdk
70# root. In order to not trip up this logic and still have the symlink joined root we copy
71# over this file. Since this file also has a Python wrapper, we need to copy that as well.
72symlinkJoin {
73 name = "google-cloud-sdk-${google-cloud-sdk.version}";
74 inherit (google-cloud-sdk) meta;
75
76 paths = [
77 google-cloud-sdk
78 ]
79 ++ comps;
80
81 postBuild = ''
82 sed -i ';' $out/google-cloud-sdk/bin/.gcloud-wrapped
83 sed -i -e "s#${google-cloud-sdk}#$out#" "$out/google-cloud-sdk/bin/gcloud"
84 ${installCheck}
85 '';
86}