Lightpanda: the headless browser designed for AI and automation
have zig deps be generated too
Muki Kiboigo1d8a54621574
diff --git a/.github/workflows/nix-deps.yml b/.github/workflows/nix-deps.ymlindex 91df1e570..ef31c01f4 100644--- a/.github/workflows/nix-deps.yml+++ b/.github/workflows/nix-deps.yml@@ -2,8 +2,8 @@ name: nix-deps# Checks the flake, then regenerates nix/v8-deps.nix (hashes for the# prebuilt V8 archives pinned in .github/actions/install/action.yml) and-# opens a pull request with the result, every night or on demand from the-# Actions tab.+# nix/zig-deps.nix (the build.zig.zon fetch hash), opening a pull request+# with each result, every night or on demand from the Actions tab.## Mirrors orderfile.yml's regen-and-PR pattern. See orderfile.yml for the# token/permission rationale.@@ -15,6 +15,10 @@ on:# Allows you to run this workflow manually from the Actions tabworkflow_dispatch:+ # TODO: remove before merging to main — only here so pushes to this+ # branch exercise the workflow while it's being tested.+ pull_request:+permissions:contents: write@@ -77,3 +81,51 @@ jobs:gh pr create --base main --head "$BRANCH" \--title "nix: regenerate v8-deps.nix" \--body "Picks up the prebuilt V8 archive hashes for the versions pinned in .github/actions/install/action.yml."++ zig-deps:+ name: regenerate zig deps++ needs: flake-check+ runs-on: ubuntu-latest+ timeout-minutes: 15++ steps:+ - name: Generate token for browser+ id: app-token+ uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2.2.2+ with:+ app-id: ${{ secrets.DISTRIBUTION_APP_ID }}+ private-key: ${{ secrets.DISTRIBUTION_APP_PRIVATE_KEY }}+ owner: lightpanda-io+ repositories: browser++ - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3++ - uses: DeterminateSystems/nix-installer-action@dbbcd8c3ec10ff26bded0022d5b9c00c1c3b687a # v23++ - name: regenerate nix/zig-deps.nix+ run: nix run .#generate-zig-deps++ - name: open a pull request+ env:+ GH_TOKEN: ${{ steps.app-token.outputs.token }}+ BRANCH: zig-deps-regen+ run: |+ if git diff --quiet -- nix/zig-deps.nix; then+ echo "zig deps unchanged"+ exit 0+ fi+ git config user.name "github-actions[bot]"+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"+ git add nix/zig-deps.nix+ git commit -m "nix: regenerate zig-deps.nix"+ # One fixed branch, force-pushed: a re-run refreshes the open PR+ # instead of stacking a second one.+ git push --force origin "HEAD:refs/heads/$BRANCH"+ if [ -n "$(gh pr list --head "$BRANCH" --base main --state open --json number --jq '.[].number')" ]; then+ echo "refreshed the open pull request"+ exit 0+ fi+ gh pr create --base main --head "$BRANCH" \+ --title "nix: regenerate zig-deps.nix" \+ --body "Picks up the fixed-output hash for build.zig.zon's current dependency set."diff --git a/flake.nix b/flake.nixindex d8d88e289..c9a44ead5 100644--- a/flake.nix+++ b/flake.nix@@ -49,97 +49,110 @@"aarch64-darwin"](- system:- let- overlays = [- (final: prev: {- zig = zig-flake.packages.${prev.system}."zig_0_17_0";- zls = zls.packages.${prev.system}.zls;- })- ];-- pkgs = import nixpkgs {- inherit system overlays;- };-- minimalRustToolchain = fenix.packages.${system}.stable.minimalToolchain;- rustToolchain = fenix.packages.${system}.stable.toolchain;- craneLib = (crane.mkLib pkgs).overrideToolchain minimalRustToolchain;-- # We need crtbeginS.o for building.- crtFiles = pkgs.runCommand "crt-files" { } ''- mkdir -p $out/lib- cp -r ${pkgs.gcc.cc}/lib/gcc $out/lib/gcc- '';-- buildDeps =- pkgs: with pkgs; [- gcc- gcc.cc.lib- crtFiles- glibc.dev+ system:+ let+ overlays = [+ (final: prev: {+ zig = zig-flake.packages.${prev.system}."zig_0_17_0";+ zls = zls.packages.${prev.system}.zls;+ })];- v8BuildDeps =- pkgs: with pkgs; [- python3- pkg-config- expat.dev- glib.dev- zlib- ];+ pkgs = import nixpkgs {+ inherit system overlays;+ };++ minimalRustToolchain = fenix.packages.${system}.stable.minimalToolchain;+ rustToolchain = fenix.packages.${system}.stable.toolchain;+ craneLib = (crane.mkLib pkgs).overrideToolchain minimalRustToolchain;++ # We need crtbeginS.o for building.+ crtFiles = pkgs.runCommand "crt-files" { } ''+ mkdir -p $out/lib+ cp -r ${pkgs.gcc.cc}/lib/gcc $out/lib/gcc+ '';++ buildDeps =+ pkgs: with pkgs; [+ gcc+ gcc.cc.lib+ crtFiles+ glibc.dev+ ];++ v8BuildDeps =+ pkgs: with pkgs; [+ python3+ pkg-config+ expat.dev+ glib.dev+ zlib+ ];++ # This build pipeline (V8 mainly) is very unhappy without an FHS-compliant env.+ fhs = pkgs.buildFHSEnv {+ name = "fhs-shell";+ multiArch = true;+ targetPkgs =+ pkgs:+ (buildDeps pkgs)+ ++ (v8BuildDeps pkgs)+ ++ (with pkgs; [+ zig+ zls+ rustToolchain+ ]);+ };- # This build pipeline (V8 mainly) is very unhappy without an FHS-compliant env.- fhs = pkgs.buildFHSEnv {- name = "fhs-shell";- multiArch = true;- targetPkgs =- pkgs:- (buildDeps pkgs)- ++ (v8BuildDeps pkgs)- ++ (with pkgs; [- zig- zls- rustToolchain- ]);- };-- lightpanda = pkgs.callPackage ./nix/package.nix {- inherit craneLib;- buildInputs = (buildDeps pkgs) ++ (with pkgs; [ zig ]);- src = ./.;- inherit version;- };-- in- {- devShells.default = fhs.env;-- packages = {- inherit lightpanda;- default = lightpanda;- };-- apps = {- generate-v8-deps = {- type = "app";- program = "${pkgs.writeShellScript "generate-v8-deps" ''- export PATH="${- pkgs.lib.makeBinPath [- pkgs.curl- pkgs.gnused- pkgs.coreutils- pkgs.gawk- pkgs.nix- ]- }:$PATH"- export SSL_CERT_FILE="${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"- exec bash ${./nix/update-v8-deps.sh} "$PWD"- ''}";+ lightpanda = pkgs.callPackage ./nix/package.nix {+ inherit craneLib;+ buildInputs = (buildDeps pkgs) ++ (with pkgs; [ zig ]);+ src = ./.;+ inherit version;};- };+ in+ {+ devShells.default = fhs.env;++ packages = {+ inherit lightpanda;+ default = lightpanda;+ };++ apps = {+ generate-v8-deps = {+ type = "app";+ program = "${pkgs.writeShellScript "generate-v8-deps" ''+ export PATH="${+ pkgs.lib.makeBinPath [+ pkgs.curl+ pkgs.gnused+ pkgs.coreutils+ pkgs.gawk+ pkgs.nix+ ]+ }:$PATH"+ export SSL_CERT_FILE="${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"+ exec bash ${./nix/update-v8-deps.sh} "$PWD"+ ''}";+ };++ generate-zig-deps = {+ type = "app";+ program = "${pkgs.writeShellScript "generate-zig-deps" ''+ export PATH="${+ pkgs.lib.makeBinPath [+ pkgs.coreutils+ pkgs.nix+ pkgs.zig+ ]+ }:$PATH"+ exec bash ${./nix/update-zig-deps-hash.sh} "$PWD"+ ''}";+ };+ };- }- );+ }+ );}diff --git a/nix/package.nix b/nix/package.nixindex 8a5a7cf70..063618ba7 100644--- a/nix/package.nix+++ b/nix/package.nix@@ -29,11 +29,15 @@ let};rustFfiLib = "${rustLib}/lib/liblightpanda_ffi.a";+ # Generated by nix/update-zig-deps-hash.sh — rerun it when build.zig.zon+ # changes.+ zigDepsHash = (import ./zig-deps.nix).hash;+zigDeps =pkgs.runCommand "lightpanda-zig-deps"{nativeBuildInputs = [ zig ];- outputHash = "sha256-O25mwh9d+nU95cL2T6CMqj735f4rIbNoHumhg9CiGeQ=";+ outputHash = zigDepsHash;outputHashMode = "recursive";}''@@ -139,6 +143,11 @@ mkZigDerivation {runHook postInstall'';+ # Lets `nix build .#lightpanda.zigDeps` target just the fixed-output+ # derivation that update-zig-deps-hash.sh needs to rebuild, instead of+ # building the whole package.+ passthru = { inherit zigDeps; };+meta = {description = "Headless browser designed for AI and automation";homepage = "https://lightpanda.io";diff --git a/nix/update-zig-deps-hash.sh b/nix/update-zig-deps-hash.shnew file mode 100755index 000000000..b94771e93--- /dev/null+++ b/nix/update-zig-deps-hash.sh@@ -0,0 +1,46 @@+#!/usr/bin/env bash+set -euo pipefail++repo="${1:-"$(dirname "${BASH_SOURCE[0]}")/.."}"+cd "$repo"+repo="$PWD"++OUT=nix/zig-deps.nix++workdir=$(mktemp -d)+trap 'rm -rf "$workdir"' EXIT++# `--fetch=all` walks build.zig.zon's dependency tree regardless of what+# build.zig references, so only the manifest is needed to reproduce the+# fetch — paired with an empty build.zig so `zig build` has a valid root+# module to run against.+cd "$workdir"+cp "$repo/build.zig.zon" build.zig.zon+printf 'const std = @import("std");\npub fn build(b: *std.Build) void {\n _ = b;\n}\n' > build.zig++export HOME="$workdir/home"+mkdir -p "$HOME"+export ZIG_GLOBAL_CACHE_DIR="$workdir/cache"+export ZIG_LOCAL_CACHE_DIR="$ZIG_GLOBAL_CACHE_DIR"++echo "fetching build.zig.zon deps..." >&2+zig build --fetch=all++# nix/package.nix's zigDeps derivation copies $ZIG_GLOBAL_CACHE_DIR/p to+# $out with outputHashMode = "recursive" — hash that same directory the+# same way (NAR serialisation) to predict the fixed-output hash exactly,+# without needing to build anything.+hash=$(nix hash path "$ZIG_GLOBAL_CACHE_DIR/p")++{+ echo "# Generated by nix/update-zig-deps-hash.sh — do not edit by hand."+ echo "#"+ echo "# The fixed-output hash of \`zig build --fetch=all\`'s cache (every"+ echo "# build.zig.zon dependency). Rerun the generator whenever build.zig.zon"+ echo "# changes."+ echo "{"+ echo " hash = \"${hash}\";"+ echo "}"+} > "$repo/$OUT"++echo "wrote ${hash} to ${OUT}"diff --git a/nix/zig-deps.nix b/nix/zig-deps.nixnew file mode 100644index 000000000..fc158b150--- /dev/null+++ b/nix/zig-deps.nix@@ -0,0 +1,8 @@+# Generated by nix/update-zig-deps-hash.sh — do not edit by hand.+#+# The fixed-output hash of `zig build --fetch=all`'s cache (every+# build.zig.zon dependency). Rerun the generator whenever build.zig.zon+# changes.+{+ hash = "sha256-O25mwh9d+nU95cL2T6CMqj735f4rIbNoHumhg9CiGeQ=";+}