Skip to content

Commit ce8192c

Browse files
committed
refactor(scripts): Migrate scripts from nix-blockchain-development
1 parent dfa0538 commit ce8192c

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed

scripts/ci-all.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
nix="$(if tty -s; then echo nom; else echo nix; fi)"
6+
7+
set -x
8+
nix flake check
9+
$nix build -L --json --no-link --keep-going \
10+
.#devShells.{x86_64-linux,{x86_64,aarch64}-darwin}.default

scripts/ci-matrix.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
root_dir="$(git rev-parse --show-toplevel)"
6+
7+
# shellcheck source=./nix-eval-jobs.sh
8+
source "$root_dir/scripts/nix-eval-jobs.sh"
9+
10+
eval_packages_to_json() {
11+
flake_attr_pre="${1:-checks}"
12+
flake_attr_post="${2:-}"
13+
14+
cachix_url="https://${CACHIX_CACHE}.cachix.org"
15+
16+
nix_eval_for_all_systems "$flake_attr_pre" "$flake_attr_post" \
17+
| jq -sr '{
18+
"x86_64-linux": "ubuntu-latest",
19+
"x86_64-darwin": "macos-14",
20+
"aarch64-darwin": "macos-14"
21+
} as $system_to_gh_platform
22+
|
23+
map({
24+
package: .attr,
25+
attrPath: "'"${flake_attr_pre}".'\(.system).\(.attr)",
26+
allowedToFail: false,
27+
isCached,
28+
system,
29+
cache_url: .outputs.out
30+
| "'"$cachix_url"'/\(match("^\/nix\/store\/([^-]+)-").captures[0].string).narinfo",
31+
os: $system_to_gh_platform[.system]
32+
})
33+
| sort_by(.package | ascii_downcase)
34+
'
35+
}
36+
37+
save_gh_ci_matrix() {
38+
packages_to_build=$(echo "$packages" | jq -c '. | map(select(.isCached | not))')
39+
matrix='{"include":'"$packages_to_build"'}'
40+
res_path=''
41+
if [ "${IS_INITIAL:-true}" = "true" ]; then
42+
res_path='matrix-pre.json'
43+
else
44+
res_path='matrix-post.json'
45+
fi
46+
echo "$matrix" > "$res_path"
47+
echo "matrix=$matrix" >> "${GITHUB_OUTPUT:-${result_dir}/gh-output.env}"
48+
}
49+
50+
convert_nix_eval_to_table_summary_json() {
51+
is_initial="${IS_INITIAL:-true}"
52+
echo "$packages" \
53+
| jq '
54+
def getStatus(pkg; key):
55+
if (pkg | has(key))
56+
then if pkg[key].isCached
57+
then "[✅ cached](\(pkg[key].cache_url))"
58+
else if "'$is_initial'" == "true"
59+
then "⏳ building..."
60+
else "❌ build failed" end
61+
end else "🚫 not supported" end;
62+
63+
group_by(.package)
64+
| map(
65+
. | INDEX(.system) as $pkg
66+
| .[0].package as $name
67+
| {
68+
package: $name,
69+
"x86_64-linux": getStatus($pkg; "x86_64-linux"),
70+
"x86_64-darwin": getStatus($pkg; "x86_64-darwin"),
71+
"aarch64-darwin": getStatus($pkg; "aarch64-darwin"),
72+
}
73+
)
74+
| sort_by(.package)'
75+
}
76+
77+
printTableForCacheStatus() {
78+
packages="$(eval_packages_to_json "$@")"
79+
80+
save_gh_ci_matrix
81+
82+
{
83+
echo "Thanks for your Pull Request!"
84+
echo
85+
echo "Below you will find a summary of the cachix status of each package, for each supported platform."
86+
echo
87+
# shellcheck disable=SC2016
88+
echo '| package | `x86_64-linux` | `x86_64-darwin` | `aarch64-darwin` |'
89+
echo '| ------- | -------------- | --------------- | ---------------- |'
90+
convert_nix_eval_to_table_summary_json | jq -r '
91+
.[] | "| `\(.package)` | \(.["x86_64-linux"]) | \(.["x86_64-darwin"]) | \(.["aarch64-darwin"]) |"
92+
'
93+
echo
94+
} > comment.md
95+
}
96+
97+
printTableForCacheStatus "$@"

scripts/ci.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
error() {
6+
echo "Error: " "$@" >&2
7+
exit 1
8+
}
9+
10+
nix="$(if tty -s; then echo nom; else echo nix; fi)"
11+
12+
get_platform() {
13+
case "$(uname -s).$(uname -m)" in
14+
Linux.x86_64)
15+
system=x86_64-linux
16+
;;
17+
Linux.i?86)
18+
system=i686-linux
19+
;;
20+
Linux.aarch64)
21+
system=aarch64-linux
22+
;;
23+
Linux.armv6l_linux)
24+
system=armv6l-linux
25+
;;
26+
Linux.armv7l_linux)
27+
system=armv7l-linux
28+
;;
29+
Darwin.x86_64)
30+
system=x86_64-darwin
31+
;;
32+
Darwin.arm64|Darwin.aarch64)
33+
system=aarch64-darwin
34+
;;
35+
*) error "sorry, there is no binary distribution of Nix for your platform";;
36+
esac
37+
38+
echo "$system"
39+
}
40+
41+
system="$(get_platform)"
42+
43+
set -x
44+
nix flake check
45+
$nix build --json --print-build-logs ".#devShells.$system.default"

scripts/nix-eval-jobs.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
root_dir="$(git rev-parse --show-toplevel)"
6+
result_dir="$root_dir/.result"
7+
gc_roots_dir="$result_dir/gc-roots"
8+
9+
# shellcheck source=./system-info.sh
10+
source "$root_dir/scripts/system-info.sh"
11+
12+
create_result_dirs() {
13+
mkdir -p "$result_dir" "$gc_roots_dir"
14+
}
15+
16+
nix_eval_jobs() {
17+
flake_attr="$1"
18+
19+
get_platform
20+
get_available_memory_mb
21+
get_nix_eval_worker_count
22+
23+
{
24+
(
25+
set -x
26+
nix-eval-jobs \
27+
--quiet \
28+
--option warn-dirty false \
29+
--check-cache-status \
30+
--gc-roots-dir "$gc_roots_dir" \
31+
--workers "$max_workers" \
32+
--max-memory-size "$max_memory_mb" \
33+
--flake "$root_dir#$flake_attr"
34+
) \
35+
| tee /dev/fd/3 \
36+
| stdbuf -i0 -o0 -e0 jq --color-output -c '{ attr, isCached, out: .outputs.out }' > /dev/stderr
37+
} 3>&1 2> >(
38+
grep -vP "(SQLite database|warning: unknown setting 'allowed-users'|warning: unknown setting 'trusted-users')" \
39+
>&2
40+
)
41+
}
42+
43+
nix_eval_for_all_systems() {
44+
flake_pre="$1"
45+
flake_post="${2:+.$2}"
46+
47+
systems=( {x86_64-linux,{x86_64,aarch64}-darwin} )
48+
49+
for system in "${systems[@]}"; do
50+
nix_eval_jobs "${flake_pre}.${system}${flake_post}"
51+
done
52+
}

scripts/system-info.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
get_platform() {
6+
case "$(uname -s).$(uname -m)" in
7+
Linux.x86_64)
8+
export system=x86_64-linux
9+
export is_linux=true
10+
export is_darwin=false
11+
;;
12+
Linux.i?86)
13+
export system=i686-linux
14+
export is_linux=true
15+
export is_darwin=false
16+
;;
17+
Linux.aarch64)
18+
export system=aarch64-linux
19+
export is_linux=true
20+
export is_darwin=false
21+
;;
22+
Linux.armv6l_linux)
23+
export system=armv6l-linux
24+
export is_linux=true
25+
export is_darwin=false
26+
;;
27+
Linux.armv7l_linux)
28+
export system=armv7l-linux
29+
export is_linux=true
30+
export is_darwin=false
31+
;;
32+
Darwin.x86_64)
33+
export system=x86_64-darwin
34+
export is_linux=false
35+
export is_darwin=true
36+
;;
37+
Darwin.arm64|Darwin.aarch64)
38+
system=aarch64-darwin
39+
export is_linux=false
40+
export is_darwin=true
41+
;;
42+
*) error "sorry, there is no binary distribution of Nix for your platform";;
43+
esac
44+
}
45+
46+
get_nix_eval_worker_count() {
47+
if [[ -z "${MAX_WORKERS:-}" ]]; then
48+
available_parallelism="$(nproc)"
49+
export max_workers="$((available_parallelism < 8 ? available_parallelism : 8))"
50+
else
51+
export max_workers="$MAX_WORKERS"
52+
fi
53+
}
54+
55+
get_available_memory_mb() {
56+
if [ "$is_darwin" = 'true' ]; then
57+
free_pages="$(vm_stat | grep 'Pages free:' | tr -s ' ' | cut -d ' ' -f 3 | tr -d '.')"
58+
inactive_pages="$(vm_stat | grep 'Pages inactive:' | tr -s ' ' | cut -d ' ' -f 3 | tr -d '.')"
59+
pages="$((free_pages + inactive_pages))"
60+
page_size="$(pagesize)"
61+
export max_memory_mb="${MAX_MEMORY:-$(((pages * page_size) / 1024 / 1024 ))}"
62+
else
63+
free="$(< /proc/meminfo grep MemFree | tr -s ' ' | cut -d ' ' -f 2)"
64+
cached="$(< /proc/meminfo grep Cached | grep -v SwapCached | tr -s ' ' | cut -d ' ' -f 2)"
65+
buffers="$(< /proc/meminfo grep Buffers | tr -s ' ' | cut -d ' ' -f 2)"
66+
shmem="$(< /proc/meminfo grep Shmem: | tr -s ' ' | cut -d ' ' -f 2)"
67+
export max_memory_mb="${MAX_MEMORY:-$(((free + cached + buffers + shmem) / 1024 ))}"
68+
fi
69+
}

0 commit comments

Comments
 (0)