about summary refs log tree commit diff
path: root/pkgs/tools/admin/google-cloud-sdk/withExtraComponents.nix
blob: 63070a85dd8fe5b51c811b119219c4319c90fbea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{ lib, google-cloud-sdk, symlinkJoin, 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_));

  installCheck =
    let
      compNames = builtins.map lib.getName comps_;
    in
    ''
      $out/bin/gcloud components list --only-local-state --format 'value(id)' > component_list.txt
      for comp in ${builtins.toString compNames}; do
        snapshot_file="$out/google-cloud-sdk/.install/$comp.snapshot.json"

        if ! [ -f "$snapshot_file"  ]; then
          echo "Failed to install component '$comp'"
          exit 1
        fi

        if grep --quiet '"is_hidden":true' "$snapshot_file"; then
          continue
        fi

        if ! grep --quiet "^$comp$" component_list.txt; then
          echo "Failed to install component '$comp'"
          exit 1
        fi
      done
    '';
in
# The `gcloud` entrypoint script has some custom logic to determine the "real" cloud sdk
# root. In order to not trip up this logic and still have the symlink joined root we copy
# over this file. Since this file also has a Python wrapper, we need to copy that as well.
symlinkJoin {
  name = "google-cloud-sdk-${google-cloud-sdk.version}";
  inherit (google-cloud-sdk) meta;

  paths = [
    google-cloud-sdk
  ] ++ comps;

  postBuild = ''
    sed -i ';' $out/google-cloud-sdk/bin/.gcloud-wrapped
    sed -i -e "s#${google-cloud-sdk}#$out#" "$out/google-cloud-sdk/bin/gcloud"
    ${installCheck}
  '';
}