about summary refs log tree commit diff
path: root/pkgs/tools/admin/google-cloud-sdk/components.nix
blob: d1010fcec62a31f47ef285406d6e723f53a15f6c (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{ stdenv
, lib
, google-cloud-sdk
, system
, snapshotPath
, autoPatchelfHook
, python3
, ...
}:

let
  # Mapping from GCS component architecture names to Nix archictecture names
  arches = {
    x86 = "i686";
    x86_64 = "x86_64";
    arm = "aarch64";
  };

  # Mapping from GCS component operating systems to Nix operating systems
  oses = {
    LINUX = "linux";
    MACOSX = "darwin";
    WINDOWS = "windows";
    CYGWIN = "cygwin";
  };

  # Convert an archicecture + OS to a Nix platform
  toNixPlatform = arch: os:
    let
      arch' = arches.${arch} or (throw "unsupported architecture '${arch}'");
      os' = oses.${os} or (throw "unsupported OS '${os}'");
    in
    "${arch'}-${os'}";

  # All architectures that are supported by GCS
  allArches = builtins.attrNames arches;

  # A description of all available google-cloud-sdk components.
  # It's a JSON file with a list of components, along with some metadata
  snapshot = builtins.fromJSON (builtins.readFile snapshotPath);

  # Generate a snapshot file for a single component.  It has the same format as
  # `snapshot`, but only contains a single component.  These files are
  # installed with google-cloud-sdk to let it know which components are
  # available.
  snapshotFromComponent =
    { component
    , revision
    , schema_version
    , version
    }:
    builtins.toJSON {
      components = [ component ];
      inherit revision schema_version version;
    };

  # Generate a set of components from a JSON file describing these components
  componentsFromSnapshot =
    { components
    , revision
    , schema_version
    , version
    , ...
    }:
    lib.fix (
      self:
      builtins.listToAttrs (
        builtins.map
          (component: {
            name = component.id;
            value = componentFromSnapshot self { inherit component revision schema_version version; };
          })
          components
      )
    );

  # Generate a single component from its snapshot, along with a set of
  # available dependencies to choose from.
  componentFromSnapshot =
    # Component derivations that can be used as dependencies
    components:
    # This component's snapshot
    { component
    , revision
    , schema_version
    , version
    } @ attrs:
    let
      baseUrl = builtins.dirOf schema_version.url;
      # Architectures supported by this component.  Defaults to all available
      # architectures.
      architectures = builtins.filter
        (arch: builtins.elem arch (builtins.attrNames arches))
        (lib.attrByPath [ "platform" "architectures" ] allArches component);
      # Operating systems supported by this component
      operating_systems = builtins.filter
        (os: builtins.elem os (builtins.attrNames oses))
        component.platform.operating_systems;
    in
    mkComponent
      {
        pname = component.id;
        version = component.version.version_string;
        src =
          lib.optionalString (lib.hasAttrByPath [ "data" "source" ] component) "${baseUrl}/${component.data.source}";
        sha256 = lib.attrByPath [ "data" "checksum" ] "" component;
        dependencies = builtins.map (dep: builtins.getAttr dep components) component.dependencies;
        platforms =
          if component.platform == { }
          then lib.platforms.all
          else
            builtins.concatMap
              (arch: builtins.map (os: toNixPlatform arch os) operating_systems)
              architectures;
        snapshot = snapshotFromComponent attrs;
      };

  # Filter out dependencies not supported by current system
  filterForSystem = builtins.filter (drv: builtins.elem system drv.meta.platforms);

  # Make a google-cloud-sdk component
  mkComponent =
    { pname
    , version
      # Source tarball, if any
    , src ? ""
      # Checksum for the source tarball, if there is a source
    , sha256 ? ""
      # Other components this one depends on
    , dependencies ? [ ]
      # Short text describing the component
    , description ? ""
      # Platforms supported
    , platforms ? lib.platforms.all
      # The snapshot corresponding to this component
    , snapshot
    }: stdenv.mkDerivation {
      inherit pname version snapshot;
      src =
        lib.optionalString (src != "")
          (builtins.fetchurl
            {
              url = src;
              inherit sha256;
            }) ;
      dontUnpack = true;
      installPhase = ''
        mkdir -p $out/google-cloud-sdk/.install

        # If there is a source, unpack it
        if [ ! -z "$src" ]; then
          tar -xf $src -C $out/google-cloud-sdk/

          # If the source has binaries, link them to `$out/bin`
          if [ -d "$out/google-cloud-sdk/bin" ]; then
            mkdir $out/bin
            find $out/google-cloud-sdk/bin/ -type f -exec ln -s {} $out/bin/ \;
          fi
        fi

        # Write the snapshot file to the `.install` folder
        cp $snapshotPath $out/google-cloud-sdk/.install/${pname}.snapshot.json
      '';
      nativeBuildInputs = [
        autoPatchelfHook
        python3
        stdenv.cc.cc
      ];
      passthru = {
        dependencies = filterForSystem dependencies;
      };
      passAsFile = [ "snapshot" ];
      meta = {
        inherit description platforms;
      };
    };
in
componentsFromSnapshot snapshot