Skip to content

Commit 6df7409

Browse files
committed
chore: create a nix package for generating GitHub Actions matrix
Add pytest tests for the package Add nix-eval-jobs in path for the package
1 parent e0643dc commit 6df7409

File tree

7 files changed

+417
-35
lines changed

7 files changed

+417
-35
lines changed

.github/workflows/nix-eval.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ jobs:
2929
name: Generate Nix Matrix
3030
run: |
3131
set -Eeu
32-
echo matrix="$(nix shell github:nix-community/nix-eval-jobs/v2.31.0 --command scripts/github-matrix.py checks legacyPackages)" >> "$GITHUB_OUTPUT"
32+
echo matrix="$(nix run .\#github-matrix checks legacyPackages)" >> "$GITHUB_OUTPUT"

flake.lock

Lines changed: 106 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
git-hooks.url = "github:cachix/git-hooks.nix";
2020
git-hooks.inputs.nixpkgs.follows = "nixpkgs";
2121
nixpkgs-go124.url = "github:Nixos/nixpkgs/d2ac4dfa61fba987a84a0a81555da57ae0b9a2b0";
22+
nix-eval-jobs.url = "github:jfroche/nix-eval-jobs/show-required-system-features";
2223
};
2324

2425
outputs =

nix/packages/default.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
cleanup-ami = pkgs.callPackage ./cleanup-ami.nix { };
3535
dbmate-tool = pkgs.callPackage ./dbmate-tool.nix { inherit (self.supabase) defaults; };
3636
docs = pkgs.callPackage ./docs.nix { };
37+
github-matrix = pkgs.callPackage ./github-matrix {
38+
nix-eval-jobs = inputs'.nix-eval-jobs.packages.default;
39+
};
3740
supabase-groonga = pkgs.callPackage ./groonga { };
3841
http-mock-server = pkgs.callPackage ./http-mock-server.nix { };
3942
local-infra-bootstrap = pkgs.callPackage ./local-infra-bootstrap.nix { };
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
lib,
3+
nix-eval-jobs,
4+
python3Packages,
5+
}:
6+
let
7+
pname = "github-matrix";
8+
in
9+
10+
python3Packages.buildPythonApplication {
11+
inherit pname;
12+
version = "0.1.0";
13+
pyproject = false;
14+
15+
src = ./.;
16+
17+
makeWrapperArgs = [ "--suffix PATH : ${lib.makeBinPath [ nix-eval-jobs ]}" ];
18+
19+
nativeCheckInputs = with python3Packages; [
20+
pytestCheckHook
21+
pytest-mypy
22+
];
23+
24+
installPhase = ''
25+
install -Dm755 github_matrix.py "$out/bin/${pname}"
26+
'';
27+
}

scripts/github-matrix.py renamed to nix/packages/github-matrix/github_matrix.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
TypedDict,
2020
)
2121

22+
System = Literal["x86_64-linux", "aarch64-linux", "aarch64-darwin"]
23+
RunnerType = Literal["ephemeral", "self-hosted"]
24+
2225

2326
class NixEvalJobsOutput(TypedDict):
2427
"""Raw output from nix-eval-jobs command."""
@@ -27,9 +30,8 @@ class NixEvalJobsOutput(TypedDict):
2730
attrPath: List[str]
2831
cacheStatus: Literal["notBuilt", "cached", "local"]
2932
drvPath: str
30-
isCached: bool
3133
name: str
32-
system: str
34+
system: System
3335
neededBuilds: NotRequired[List[Any]]
3436
neededSubstitutes: NotRequired[List[Any]]
3537
outputs: NotRequired[Dict[str, str]]
@@ -48,21 +50,29 @@ class GitHubActionPackage(TypedDict):
4850

4951
attr: str
5052
name: str
51-
system: str
53+
system: System
5254
runs_on: RunsOnConfig
5355
postgresql_version: NotRequired[str]
5456

5557

56-
BUILD_RUNNER_MAP: Dict[str, RunsOnConfig] = {
57-
"aarch64-linux": {
58-
"labels": ["blacksmith-8vcpu-ubuntu-2404-arm"],
59-
},
60-
"aarch64-darwin": {
61-
"group": "self-hosted-runners-nix",
62-
"labels": ["aarch64-darwin"],
58+
BUILD_RUNNER_MAP: Dict[RunnerType, Dict[System, RunsOnConfig]] = {
59+
"ephemeral": {
60+
"aarch64-linux": {
61+
"labels": ["blacksmith-8vcpu-ubuntu-2404-arm"],
62+
},
63+
"x86_64-linux": {
64+
"labels": ["blacksmith-8vcpu-ubuntu-2404"],
65+
},
6366
},
64-
"x86_64-linux": {
65-
"labels": ["blacksmith-8vcpu-ubuntu-2404"],
67+
"self-hosted": {
68+
"aarch64-darwin": {
69+
"group": "self-hosted-runners-nix",
70+
"labels": ["aarch64-darwin"],
71+
},
72+
"aarch64-linux": {
73+
"group": "self-hosted-runners-nix",
74+
"labels": ["aarch64-linux"],
75+
},
6676
},
6777
}
6878

@@ -76,6 +86,7 @@ def build_nix_eval_command(max_workers: int, flake_outputs: List[str]) -> List[s
7686
"--check-cache-status",
7787
"--force-recurse",
7888
"--quiet",
89+
"--show-required-system-features",
7990
"--option",
8091
"eval-cache",
8192
"false",
@@ -171,19 +182,33 @@ def is_large_pkg(pkg: NixEvalJobsOutput) -> bool:
171182
)
172183

173184

174-
def get_runner_for_package(pkg: NixEvalJobsOutput) -> RunsOnConfig:
175-
"""Determine the appropriate GitHub Actions runner for a package."""
185+
def is_kvm_pkg(pkg: NixEvalJobsOutput) -> bool:
186+
"""Determine if a package requires KVM"""
187+
return "kvm" in pkg.get("requiredSystemFeatures", [])
188+
189+
190+
def get_runner_for_package(pkg: NixEvalJobsOutput) -> RunsOnConfig | None:
191+
"""Determine the appropriate GitHub Actions runner for a package.
192+
193+
Priority order:
194+
1. KVM packages → self-hosted runners
195+
2. Large packages on Linux → 32vcpu ephemeral runners
196+
3. Darwin packages → self-hosted runners
197+
4. Default → ephemeral runners
198+
"""
176199
system = pkg["system"]
177-
if is_large_pkg(pkg):
178-
# Use larger runners for large packages for x86_64-linux and aarch64-linux
179-
if system == "x86_64-linux":
180-
return {"labels": ["blacksmith-32vcpu-ubuntu-2404"]}
181-
elif system == "aarch64-linux":
182-
return {"labels": ["blacksmith-32vcpu-ubuntu-2404-arm"]}
183-
if system in BUILD_RUNNER_MAP:
184-
return BUILD_RUNNER_MAP[system]
185-
else:
186-
raise ValueError(f"No runner configuration for system: {system}")
200+
201+
if is_kvm_pkg(pkg):
202+
return BUILD_RUNNER_MAP["self-hosted"].get(system)
203+
204+
if is_large_pkg(pkg) and system in ("x86_64-linux", "aarch64-linux"):
205+
suffix = "-arm" if system == "aarch64-linux" else ""
206+
return {"labels": [f"blacksmith-32vcpu-ubuntu-2404{suffix}"]}
207+
208+
if system == "aarch64-darwin":
209+
return BUILD_RUNNER_MAP["self-hosted"]["aarch64-darwin"]
210+
211+
return BUILD_RUNNER_MAP["ephemeral"].get(system)
187212

188213

189214
def main() -> None:
@@ -204,11 +229,14 @@ def main() -> None:
204229

205230
def clean_package_for_output(pkg: NixEvalJobsOutput) -> GitHubActionPackage:
206231
"""Convert nix-eval-jobs output to GitHub Actions matrix package"""
232+
runner = get_runner_for_package(pkg)
233+
if runner is None:
234+
raise ValueError(f"No runner configuration for system: {pkg['system']}")
207235
returned_pkg: GitHubActionPackage = {
208236
"attr": pkg["attr"],
209237
"name": pkg["name"],
210238
"system": pkg["system"],
211-
"runs_on": get_runner_for_package(pkg),
239+
"runs_on": runner,
212240
}
213241
if is_extension_pkg(pkg):
214242
# Extract PostgreSQL version from attribute path

0 commit comments

Comments
 (0)