Skip to content

Commit

Permalink
playwright: download browsers for Mac OS
Browse files Browse the repository at this point in the history
  • Loading branch information
kalekseev committed Nov 24, 2024
1 parent 87ed7eb commit c161dd4
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 142 deletions.
44 changes: 34 additions & 10 deletions pkgs/development/python-modules/playwright/update.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps unzip
#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps unzip nix-prefetch
set -euo pipefail

root="$(dirname "$(readlink -f "$0")")"
Expand Down Expand Up @@ -30,18 +30,38 @@ replace_sha() {
}

prefetch_browser() {
nix store prefetch-file --json --hash-type sha256 --unpack "$1" | jq -r .hash
# nix-prefetch is used to obtain sha with `stripRoot = false`
# doesn't work on macOS https://github.com/msteen/nix-prefetch/issues/53
nix-prefetch -q "{ stdenv, fetchzip }: stdenv.mkDerivation rec { name=\"browser\"; src = fetchzip { url = \"$1\"; stripRoot = $2; }; }"
}

update_browser() {
name="$1"
suffix="$2"
arm64_suffix="${3:-$2-arm64}"
platform="$2"
stripRoot="false"
if [ "$platform" = "darwin" ]; then
if [ "$name" = "webkit" ]; then
suffix="mac-14"
else
suffix="mac"
fi
else
if [ "$name" = "ffmpeg" ]; then
suffix="linux"
elif [ "$name" = "firefox" ]; then
stripRoot="true"
suffix="ubuntu-22.04"
else
suffix="ubuntu-22.04"
fi
fi
aarch64_suffix="$suffix-arm64"

revision="$(jq -r ".browsers.$name.revision" "$playwright_dir/browsers.json")"
replace_sha "$playwright_dir/$name.nix" "x86_64-linux" \
"$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$suffix.zip")"
replace_sha "$playwright_dir/$name.nix" "aarch64-linux" \
"$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$arm64_suffix.zip")"
replace_sha "$playwright_dir/$name.nix" "x86_64-$platform" \
"$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$suffix.zip" $stripRoot)"
replace_sha "$playwright_dir/$name.nix" "aarch64-$platform" \
"$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$aarch64_suffix.zip" $stripRoot)"
}

curl -fsSl \
Expand All @@ -58,10 +78,14 @@ curl -fsSl \

# We currently use Chromium from nixpkgs, so we don't need to download it here
# Likewise, darwin can be ignored here atm as we are using an impure install anyway.
update_browser "firefox" "ubuntu-22.04"
update_browser "webkit" "ubuntu-22.04"
update_browser "firefox" "linux"
update_browser "webkit" "linux"
update_browser "ffmpeg" "linux"

update_browser "chromium" "darwin"
update_browser "firefox" "darwin"
update_browser "webkit" "darwin"
update_browser "ffmpeg" "darwin"

# Update package-lock.json files for all npm deps that are built in playwright

Expand Down
57 changes: 41 additions & 16 deletions pkgs/development/web/playwright/chromium.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,47 @@
makeWrapper,
fontconfig_file,
chromium,
fetchzip,
revision,
suffix,
system,
throwSystem,
...
}:
runCommand "playwright-chromium"
{
nativeBuildInputs = [
makeWrapper
];
}
''
mkdir -p $out/chrome-linux
let
chromium-linux =
runCommand "playwright-chromium"
{
nativeBuildInputs = [
makeWrapper
];
}
''
mkdir -p $out/chrome-linux
# See here for the Chrome options:
# https://github.com/NixOS/nixpkgs/issues/136207#issuecomment-908637738
# We add --disable-gpu to be able to run in gpu-less environments such
# as headless nixos test vms.
makeWrapper ${chromium}/bin/chromium $out/chrome-linux/chrome \
--set-default SSL_CERT_FILE /etc/ssl/certs/ca-bundle.crt \
--set-default FONTCONFIG_FILE ${fontconfig_file}
''
# See here for the Chrome options:
# https://github.com/NixOS/nixpkgs/issues/136207#issuecomment-908637738
# We add --disable-gpu to be able to run in gpu-less environments such
# as headless nixos test vms.
makeWrapper ${chromium}/bin/chromium $out/chrome-linux/chrome \
--set-default SSL_CERT_FILE /etc/ssl/certs/ca-bundle.crt \
--set-default FONTCONFIG_FILE ${fontconfig_file}
'';
chromium-darwin = fetchzip {
url = "https://playwright.azureedge.net/builds/chromium/${revision}/chromium-${suffix}.zip";
stripRoot = false;
hash =
{
x86_64-darwin = "sha256-G9DPo/pXFuaZICT47TSE+bvncS//ydylJVpnfQYp68U=";
aarch64-darwin = "sha256-2JedruJ/y7Zidn6oRfUxi0MleaH5Cp6XQ+F7ImSCTjA=";
}
.${system} or throwSystem;
};
in
{
x86_64-linux = chromium-linux;
aarch64-linux = chromium-linux;
x86_64-darwin = chromium-darwin;
aarch64-darwin = chromium-darwin;
}
.${system} or throwSystem
37 changes: 6 additions & 31 deletions pkgs/development/web/playwright/driver.nix
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,11 @@ let

passthru = {
browsersJSON = (lib.importJSON ./browsers.json).browsers;
browsers =
{
x86_64-linux = browsers-linux { };
aarch64-linux = browsers-linux { };
x86_64-darwin = browsers-mac;
aarch64-darwin = browsers-mac;
}
.${system} or throwSystem;
browsers-chromium = browsers-linux { };
browsers = browsers { };
browsers-chromium = browsers {
withFirefox = false;
withWebkit = false;
};
};
});

Expand Down Expand Up @@ -196,28 +192,7 @@ let
};
});

browsers-mac = stdenv.mkDerivation {
pname = "playwright-browsers";
inherit (playwright) version;

dontUnpack = true;

nativeBuildInputs = [ cacert ];

installPhase = ''
runHook preInstall
export PLAYWRIGHT_BROWSERS_PATH=$out
${playwright-core}/cli.js install
rm -r $out/.links
runHook postInstall
'';

meta.platforms = lib.platforms.darwin;
};

browsers-linux = lib.makeOverridable (
browsers = lib.makeOverridable (
{
withChromium ? true,
withFirefox ? true,
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/web/playwright/ffmpeg.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ fetchzip {
{
x86_64-linux = "sha256-FEm62UvMv0h6Sav93WmbPLw3CW1L1xg4nD26ca5ol38=";
aarch64-linux = "sha256-jtQ+NS++VHRiKoIV++PIxEnyVnYtVwUyNlSILKSH4A4=";
x86_64-darwin = "sha256-ED6noxSDeEUt2DkIQ4gNe/kL+zHVeb2AD5klBk93F88=";
aarch64-darwin = "sha256-3Adnvb7zvMXKFOhb8uuj5kx0wEIFicmckYx9WLlNNf0=";
}
.${system} or throwSystem;
}
61 changes: 39 additions & 22 deletions pkgs/development/web/playwright/firefox.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,48 @@
throwSystem,
}:
let
suffix' =
if lib.hasPrefix "linux" suffix then "ubuntu-22.04" + (lib.removePrefix "linux" suffix) else suffix;
in
stdenv.mkDerivation {
name = "playwright-firefox";
src = fetchzip {
url = "https://playwright.azureedge.net/builds/firefox/${revision}/firefox-${suffix'}.zip";
firefox-linux = stdenv.mkDerivation {
name = "playwright-firefox";
src = fetchzip {
url = "https://playwright.azureedge.net/builds/firefox/${revision}/firefox-${
"ubuntu-22.04" + (lib.removePrefix "linux" suffix)
}.zip";
hash =
{
x86_64-linux = "sha256-Hd9LlSRLW51gDoFyszqvg46Q/sMizLRsVKAN9atbwsw=";
aarch64-linux = "sha256-SEXH3gLOfNjOcnNWQjQ5gaaow47veVs0BoTYSgXw+24=";
}
.${system} or throwSystem;
};

inherit (firefox-bin.unwrapped)
nativeBuildInputs
buildInputs
runtimeDependencies
appendRunpaths
patchelfFlags
;

buildPhase = ''
mkdir -p $out/firefox
cp -R . $out/firefox
'';
};
firefox-darwin = fetchzip {
url = "https://playwright.azureedge.net/builds/firefox/${revision}/firefox-${suffix}.zip";
stripRoot = false;
hash =
{
x86_64-linux = "sha256-Hd9LlSRLW51gDoFyszqvg46Q/sMizLRsVKAN9atbwsw=";
aarch64-linux = "sha256-SEXH3gLOfNjOcnNWQjQ5gaaow47veVs0BoTYSgXw+24=";
x86_64-darwin = "sha256-tclyoXGK0oc40ppqFVHJEGOtiFpc/D8wB37tBHhYhjg=";
aarch64-darwin = "sha256-EEY3cxZaZwNbWwkWy8m9hSDgEV3hotcDPD1ehiQWFig=";
}
.${system} or throwSystem;
};

inherit (firefox-bin.unwrapped)
nativeBuildInputs
buildInputs
runtimeDependencies
appendRunpaths
patchelfFlags
;

buildPhase = ''
mkdir -p $out/firefox
cp -R . $out/firefox
'';
in
{
x86_64-linux = firefox-linux;
aarch64-linux = firefox-linux;
x86_64-darwin = firefox-darwin;
aarch64-darwin = firefox-darwin;
}
.${system} or throwSystem
Loading

0 comments on commit c161dd4

Please sign in to comment.