Skip to content

Commit 9aa16ce

Browse files
committed
feat: implement composite action
1 parent 78e0c89 commit 9aa16ce

File tree

6 files changed

+173
-28
lines changed

6 files changed

+173
-28
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: 'Install Nix'
2+
description: 'Installs Nix on GitHub Actions for the supported platforms: Linux and macOS.'
3+
author: 'Domen Kožar'
4+
inputs:
5+
extra_nix_config:
6+
description: 'Gets appended to `/etc/nix/nix.conf` if passed.'
7+
github_access_token:
8+
description: 'Configure nix to pull from github using the given github token.'
9+
install_url:
10+
description: 'Installation URL that will contain a script to install Nix.'
11+
install_options:
12+
description: 'Additional installer flags passed to the installer script.'
13+
nix_path:
14+
description: 'Set NIX_PATH environment variable.'
15+
enable_kvm:
16+
description: 'Enable KVM for hardware-accelerated virtualization on Linux, if available.'
17+
required: false
18+
default: true
19+
branding:
20+
color: 'blue'
21+
icon: 'sun'
22+
runs:
23+
using: 'composite'
24+
steps:
25+
- run : ${GITHUB_ACTION_PATH}/install-nix.sh
26+
shell: bash
27+
env:
28+
INPUT_EXTRA_NIX_CONFIG: ${{ inputs.extra_nix_config }}
29+
INPUT_GITHUB_ACCESS_TOKEN: ${{ inputs.github_access_token }}
30+
INPUT_INSTALL_OPTIONS: ${{ inputs.install_options }}
31+
INPUT_INSTALL_URL: ${{ inputs.install_url }}
32+
INPUT_NIX_PATH: ${{ inputs.nix_path }}
33+
INPUT_ENABLE_KVM: ${{ inputs.enable_kvm }}
34+
GITHUB_TOKEN: ${{ github.token }}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if nix_path="$(type -p nix)" ; then
5+
echo "Aborting: Nix is already installed at ${nix_path}"
6+
exit
7+
fi
8+
9+
if [[ ($OSTYPE =~ linux) && ($INPUT_ENABLE_KVM == 'true') ]]; then
10+
enable_kvm() {
11+
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-install-nix-action-kvm.rules
12+
sudo udevadm control --reload-rules && sudo udevadm trigger --name-match=kvm
13+
}
14+
15+
echo '::group::Enabling KVM support'
16+
enable_kvm && echo 'Enabled KVM' || echo 'KVM is not available'
17+
echo '::endgroup::'
18+
fi
19+
20+
# GitHub command to put the following log messages into a group which is collapsed by default
21+
echo "::group::Installing Nix"
22+
23+
# Create a temporary workdir
24+
workdir=$(mktemp -d)
25+
trap 'rm -rf "$workdir"' EXIT
26+
27+
# Configure Nix
28+
add_config() {
29+
echo "$1" >> "$workdir/nix.conf"
30+
}
31+
add_config "show-trace = true"
32+
# Set jobs to number of cores
33+
add_config "max-jobs = auto"
34+
if [[ $OSTYPE =~ darwin ]]; then
35+
add_config "ssl-cert-file = /etc/ssl/cert.pem"
36+
fi
37+
# Allow binary caches for user
38+
add_config "trusted-users = root ${USER:-}"
39+
# Add a GitHub access token.
40+
# Token-less access is subject to lower rate limits.
41+
if [[ -n "${INPUT_GITHUB_ACCESS_TOKEN:-}" ]]; then
42+
echo "::debug::Using the provided github_access_token for github.com"
43+
add_config "access-tokens = github.com=$INPUT_GITHUB_ACCESS_TOKEN"
44+
# Use the default GitHub token if available.
45+
# Skip this step if running an Enterprise instance. The default token there does not work for github.com.
46+
elif [[ -n "${GITHUB_TOKEN:-}" && $GITHUB_SERVER_URL == "https://github.com" ]]; then
47+
echo "::debug::Using the default GITHUB_TOKEN for github.com"
48+
add_config "access-tokens = github.com=$GITHUB_TOKEN"
49+
else
50+
echo "::debug::Continuing without a GitHub access token"
51+
fi
52+
# Append extra nix configuration if provided
53+
if [[ -n "${INPUT_EXTRA_NIX_CONFIG:-}" ]]; then
54+
add_config "$INPUT_EXTRA_NIX_CONFIG"
55+
fi
56+
if [[ ! $INPUT_EXTRA_NIX_CONFIG =~ "experimental-features" ]]; then
57+
add_config "experimental-features = nix-command flakes"
58+
fi
59+
# Always allow substituting from the cache, even if the derivation has `allowSubstitutes = false`.
60+
# This is a CI optimisation to avoid having to download the inputs for already-cached derivations to rebuild trivial text files.
61+
if [[ ! $INPUT_EXTRA_NIX_CONFIG =~ "always-allow-substitutes" ]]; then
62+
add_config "always-allow-substitutes = true"
63+
fi
64+
65+
# Nix installer flags
66+
installer_options=(
67+
--no-channel-add
68+
--darwin-use-unencrypted-nix-store-volume
69+
--nix-extra-conf-file "$workdir/nix.conf"
70+
)
71+
72+
# only use the nix-daemon settings if on darwin (which get ignored) or systemd is supported
73+
if [[ (! $INPUT_INSTALL_OPTIONS =~ "--no-daemon") && ($OSTYPE =~ darwin || -e /run/systemd/system) ]]; then
74+
installer_options+=(
75+
--daemon
76+
--daemon-user-count "$(python3 -c 'import multiprocessing as mp; print(mp.cpu_count() * 2)')"
77+
)
78+
else
79+
# "fix" the following error when running nix*
80+
# error: the group 'nixbld' specified in 'build-users-group' does not exist
81+
add_config "build-users-group ="
82+
sudo mkdir -p /etc/nix
83+
sudo chmod 0755 /etc/nix
84+
sudo cp "$workdir/nix.conf" /etc/nix/nix.conf
85+
fi
86+
87+
if [[ -n "${INPUT_INSTALL_OPTIONS:-}" ]]; then
88+
IFS=' ' read -r -a extra_installer_options <<< "$INPUT_INSTALL_OPTIONS"
89+
installer_options=("${extra_installer_options[@]}" "${installer_options[@]}")
90+
fi
91+
92+
echo "installer options: ${installer_options[*]}"
93+
94+
# There is --retry-on-errors, but only newer curl versions support that
95+
curl_retries=5
96+
while ! curl -sS -o "$workdir/install" -v --fail -L "${INPUT_INSTALL_URL:-https://releases.nixos.org/nix/nix-2.25.2/install}"
97+
do
98+
sleep 1
99+
((curl_retries--))
100+
if [[ $curl_retries -le 0 ]]; then
101+
echo "curl retries failed" >&2
102+
exit 1
103+
fi
104+
done
105+
106+
sh "$workdir/install" "${installer_options[@]}"
107+
108+
# Set paths
109+
echo "/nix/var/nix/profiles/default/bin" >> "$GITHUB_PATH"
110+
# new path for nix 2.14
111+
echo "$HOME/.nix-profile/bin" >> "$GITHUB_PATH"
112+
113+
if [[ -n "${INPUT_NIX_PATH:-}" ]]; then
114+
echo "NIX_PATH=${INPUT_NIX_PATH}" >> "$GITHUB_ENV"
115+
fi
116+
117+
# Set temporary directory (if not already set) to fix https://github.com/cachix/install-nix-action/issues/197
118+
if [[ -z "${TMPDIR:-}" ]]; then
119+
echo "TMPDIR=${RUNNER_TEMP}" >> "$GITHUB_ENV"
120+
fi
121+
122+
# Close the log message group which was opened above
123+
echo "::endgroup::"

.github/workflows/application-js-cloudflare-feature.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ jobs:
3636
feature-lint:
3737
name: "Feature / Lint"
3838
runs-on: ubuntu-latest
39-
container:
40-
image: ghcr.io/matrixai/github-runner
4139
permissions:
4240
packages: read
4341
contents: read
4442
steps:
4543
- uses: actions/checkout@v4
44+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
4645
- name: Run linting
4746
run: |
4847
nix develop .#ci --command bash -c $'
@@ -53,8 +52,6 @@ jobs:
5352
feature-build:
5453
name: "Feature / Build"
5554
runs-on: ubuntu-latest
56-
container:
57-
image: ghcr.io/matrixai/github-runner
5855
permissions:
5956
packages: read
6057
contents: read
@@ -63,6 +60,7 @@ jobs:
6360
- uses: actions/checkout@v4
6461
with:
6562
lfs: true
63+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
6664
- name: Run build
6765
run: |
6866
nix develop .#ci --command bash -c $'
@@ -79,15 +77,14 @@ jobs:
7977
name: "Feature / Deployment"
8078
runs-on: ubuntu-latest
8179
needs: feature-build
82-
container:
83-
image: ghcr.io/matrixai/github-runner
8480
concurrency:
8581
group: feature-deployment
8682
cancel-in-progress: false
8783
steps:
8884
- uses: actions/checkout@v4
8985
with:
9086
lfs: true
87+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
9188
- uses: actions/download-artifact@v4
9289
with:
9390
name: public
@@ -108,4 +105,4 @@ jobs:
108105
npm run deploy -- \
109106
--feature "$GITHUB_REF_NAME" \
110107
--env "$GITHUB_REF_NAME"
111-
'
108+
'

.github/workflows/library-js-feature.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ jobs:
88
feature-lint:
99
name: "Feature / Lint"
1010
runs-on: ubuntu-latest
11-
container:
12-
image: ghcr.io/matrixai/github-runner
1311
permissions:
1412
packages: read
1513
contents: read
1614
steps:
1715
- uses: actions/checkout@v4
16+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
1817
- name: Run linting
1918
run: |
2019
nix develop .#ci --command bash -c $'
@@ -25,14 +24,13 @@ jobs:
2524
feature-build:
2625
name: "Feature / Build"
2726
runs-on: ubuntu-latest
28-
container:
29-
image: ghcr.io/matrixai/github-runner
3027
permissions:
3128
packages: read
3229
contents: read
3330
actions: write
3431
steps:
3532
- uses: actions/checkout@v4
33+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
3634
- name: Run build
3735
run: |
3836
nix develop .#ci --command bash -c $'
@@ -48,15 +46,14 @@ jobs:
4846
feature-test:
4947
name: "Feature / Test"
5048
runs-on: ubuntu-latest
51-
container:
52-
image: ghcr.io/matrixai/github-runner
5349
permissions:
5450
packages: read
5551
contents: read
5652
actions: write
5753
checks: write
5854
steps:
5955
- uses: actions/checkout@v4
56+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
6057
- name: Run tests
6158
run: |
6259
nix develop .#ci --command bash -c $'
@@ -83,14 +80,13 @@ jobs:
8380
feature-bench:
8481
name: "Feature / Bench"
8582
runs-on: ubuntu-latest
86-
container:
87-
image: ghcr.io/matrixai/github-runner
8883
permissions:
8984
packages: read
9085
contents: read
9186
actions: write
9287
steps:
9388
- uses: actions/checkout@v4
89+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
9490
- name: Run bench
9591
run: |
9692
nix develop .#ci --command bash -c $'

.github/workflows/library-js-staging.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ jobs:
1919
staging-lint:
2020
name: "Staging / Lint"
2121
runs-on: ubuntu-latest
22-
container:
23-
image: ghcr.io/matrixai/github-runner
2422
permissions:
2523
packages: read
2624
contents: read
2725
steps:
2826
- uses: actions/checkout@v4
27+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
2928
- name: Run linting
3029
run: |
3130
nix develop .#ci --command bash -c $'
@@ -62,14 +61,13 @@ jobs:
6261
staging-build:
6362
name: "Staging / Build"
6463
runs-on: ubuntu-latest
65-
container:
66-
image: ghcr.io/matrixai/github-runner
6764
permissions:
6865
packages: read
6966
contents: read
7067
actions: write
7168
steps:
7269
- uses: actions/checkout@v4
70+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
7371
- name: Run build
7472
run: |
7573
nix develop .#ci --command bash -c $'
@@ -88,8 +86,6 @@ jobs:
8886
needs:
8987
- staging-build
9088
runs-on: ${{ matrix.os }}
91-
container:
92-
image: ${{ matrix.platform == 'linux' && 'ghcr.io/matrixai/github-runner' || null }}
9389
permissions:
9490
packages: read
9591
contents: read
@@ -133,6 +129,8 @@ jobs:
133129
npm run bench --if-present
134130
steps:
135131
- uses: actions/checkout@v4
132+
- if: matrix.platform == 'linux'
133+
uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
136134
- uses: actions/download-artifact@v4
137135
with:
138136
name: dist
@@ -199,4 +197,4 @@ jobs:
199197
--repo "$GITHUB_REPOSITORY"
200198
git checkout master
201199
git merge --ff-only "$GITHUB_SHA"
202-
git push origin master
200+
git push origin master

.github/workflows/library-js-tag.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ jobs:
1111
tag-build:
1212
name: "Tag / Build"
1313
runs-on: ubuntu-latest
14-
container:
15-
image: ghcr.io/matrixai/github-runner
1614
permissions:
1715
packages: read
1816
contents: read
1917
actions: write
2018
steps:
2119
- uses: actions/checkout@v4
20+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
2221
- name: Run build
2322
run: |
2423
nix develop .#ci --command bash -c $'
@@ -34,8 +33,6 @@ jobs:
3433
tag-prerelease:
3534
name: "Tag / Pre-release"
3635
runs-on: ubuntu-latest
37-
container:
38-
image: ghcr.io/matrixai/github-runner
3936
concurrency:
4037
group: tag-prerelease
4138
cancel-in-progress: false
@@ -47,6 +44,7 @@ jobs:
4744
if: startsWith(github.ref, 'refs/tags/v') && contains(github.ref, '-')
4845
steps:
4946
- uses: actions/checkout@v4
47+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
5048
- uses: actions/download-artifact@v4
5149
with:
5250
name: dist
@@ -68,8 +66,6 @@ jobs:
6866
tag-release:
6967
name: "Tag / Release"
7068
runs-on: ubuntu-latest
71-
container:
72-
image: ghcr.io/matrixai/github-runner
7369
concurrency:
7470
group: release-distribution
7571
cancel-in-progress: false
@@ -81,6 +77,7 @@ jobs:
8177
if: startsWith(github.ref, 'refs/tags/v') && !contains(github.ref, '-')
8278
steps:
8379
- uses: actions/checkout@v4
80+
- uses: MatrixAI/.github/.github/actions/install-nix@feature-actions
8481
- uses: actions/download-artifact@v4
8582
with:
8683
name: dist

0 commit comments

Comments
 (0)