Skip to content

Commit 446532c

Browse files
Introduce NixOS support (download proper kustomize binary)
1 parent 94f6892 commit 446532c

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

skylib/kustomize/kustomize.bzl

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ load(
1313
"@io_bazel_rules_docker//skylib:path.bzl",
1414
_get_runfile_path = "runfile",
1515
)
16+
load(
17+
"//skylib/kustomize:kustomize_nixos.bzl",
18+
"is_running_on_nixos",
19+
"copy_kustomize_bin_from_nix_store"
20+
)
1621
load("//skylib:push.bzl", "K8sPushInfo")
1722
load("//skylib:stamp.bzl", "stamp")
1823

@@ -37,9 +42,11 @@ sh_binary(
3742
visibility = ["//visibility:public"],
3843
)
3944
""")
40-
41-
filename, sha256 = _binaries[platform]
42-
ctx.download_and_extract(filename, "bin/", sha256 = sha256)
45+
if is_running_on_nixos(ctx):
46+
copy_kustomize_bin_from_nix_store(ctx, path)
47+
else:
48+
filename, sha256 = _binaries[platform]
49+
ctx.download_and_extract(filename, "bin/", sha256 = sha256)
4350

4451
_download_binary = repository_rule(
4552
_download_binary_impl,

skylib/kustomize/kustomize_nixos.bzl

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2020 Adobe. All rights reserved.
2+
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License. You may obtain a copy
4+
# of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
6+
# Unless required by applicable law or agreed to in writing, software distributed under
7+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
# OF ANY KIND, either express or implied. See the License for the specific language
9+
# governing permissions and limitations under the License.
10+
11+
def _get_path_to_kustomize_on_nixos(repo_ctx):
12+
"""Ephemeral download of kustomize binary."""
13+
command = "nix-shell --pure -p bash busybox kustomize --run 'which kustomize'"
14+
result = repo_ctx.execute([
15+
"sh",
16+
"-c",
17+
command,
18+
],
19+
environment = {
20+
"NIX_PATH": "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
21+
})
22+
kustomize_downloaded = result.return_code == 0
23+
if not kustomize_downloaded:
24+
fail("Failed to run '%s'" % command)
25+
26+
path_to_kustomize = result.stdout.strip()
27+
return path_to_kustomize
28+
29+
def is_running_on_nixos(repo_ctx):
30+
"""Check if Bazel is executed on NixOS.
31+
32+
Args:
33+
repo_ctx: context of the repository rule,
34+
containing helper functions and information about attributes
35+
36+
Returns:
37+
boolean: indicating if Bazel is executed on NixOS.
38+
"""
39+
result = repo_ctx.execute([
40+
"sh",
41+
"-c",
42+
"test -f /etc/os-release && cat /etc/os-release | head -n1",
43+
])
44+
os_release_file_read_success = result.return_code == 0
45+
if not os_release_file_read_success:
46+
return False
47+
48+
os_release_first_line = result.stdout
49+
host_is_nixos = os_release_first_line.strip() == 'NAME=NixOS'
50+
return host_is_nixos
51+
52+
def copy_kustomize_bin_from_nix_store(repo_ctx, path):
53+
"""Copy kustomize binary from nix_store to given path.
54+
55+
Downloads kustomize binary via nix package manger,
56+
then copies the binary to Bazel cache. This operation
57+
does not impact host configuration in any way.
58+
59+
Args:
60+
repo_ctx: context of the repository rule,
61+
containing helper functions and information about attributes
62+
path: path under which the kustomize should be copied
63+
64+
Returns:
65+
None
66+
"""
67+
repo_ctx.file("%s/kustomize" % path)
68+
result = repo_ctx.execute([
69+
"sh",
70+
"-c",
71+
"cp -f %s %s/kustomize" % (_get_path_to_kustomize_on_nixos(repo_ctx), path),
72+
])
73+
if result.return_code != 0:
74+
fail("Failed to copy kustomize bin from nix_store")

0 commit comments

Comments
 (0)