Skip to content

Commit 40594f9

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

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

skylib/kustomize/kustomize.bzl

Lines changed: 10 additions & 3 deletions
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+
"copy_kustomize_bin_from_nix_store",
19+
"is_running_on_nixos",
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,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
[
16+
"sh",
17+
"-c",
18+
command,
19+
],
20+
environment = {
21+
"NIX_PATH": "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos",
22+
},
23+
)
24+
kustomize_downloaded = result.return_code == 0
25+
if not kustomize_downloaded:
26+
fail("Failed to run '%s'" % command)
27+
28+
path_to_kustomize = result.stdout.strip()
29+
return path_to_kustomize
30+
31+
def is_running_on_nixos(repo_ctx):
32+
"""Check if Bazel is executed on NixOS.
33+
34+
Args:
35+
repo_ctx: context of the repository rule,
36+
containing helper functions and information about attributes
37+
38+
Returns:
39+
boolean: indicating if Bazel is executed on NixOS.
40+
"""
41+
result = repo_ctx.execute([
42+
"sh",
43+
"-c",
44+
"test -f /etc/os-release && cat /etc/os-release | head -n1",
45+
])
46+
os_release_file_read_success = result.return_code == 0
47+
if not os_release_file_read_success:
48+
return False
49+
50+
os_release_first_line = result.stdout
51+
host_is_nixos = os_release_first_line.strip() == "NAME=NixOS"
52+
return host_is_nixos
53+
54+
def copy_kustomize_bin_from_nix_store(repo_ctx, path):
55+
"""Copy kustomize binary from nix_store to given path.
56+
57+
Downloads kustomize binary via nix package manger,
58+
then copies the binary to Bazel cache. This operation
59+
does not impact host configuration in any way.
60+
61+
Args:
62+
repo_ctx: context of the repository rule,
63+
containing helper functions and information about attributes
64+
path: path under which the kustomize should be copied
65+
66+
Returns:
67+
None
68+
"""
69+
repo_ctx.file("%s/kustomize" % path)
70+
result = repo_ctx.execute([
71+
"sh",
72+
"-c",
73+
"cp -f %s %s/kustomize" % (_get_path_to_kustomize_on_nixos(repo_ctx), path),
74+
])
75+
if result.return_code != 0:
76+
fail("Failed to copy kustomize bin from nix_store")

0 commit comments

Comments
 (0)