From 682e3b71a0c53d9aad6af1f51bc577a67b913c0f Mon Sep 17 00:00:00 2001 From: WxNzEMof <143541718+WxNzEMof@users.noreply.github.com> Date: Thu, 26 Dec 2024 18:52:28 +0000 Subject: [PATCH 1/2] playwright-test: Add PLAYWRIGHT_BROWSERS_PATH to build environment This allows writing self-contained node scripts (using a nix-shell shebang), without requiring a separate shell.nix which sets PLAYWRIGHT_BROWSERS_PATH to pkgs.playwright-driver.browsers. --- pkgs/development/web/playwright/driver.nix | 19 +++++++++++++++++++ pkgs/development/web/playwright/test.js | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 pkgs/development/web/playwright/test.js diff --git a/pkgs/development/web/playwright/driver.nix b/pkgs/development/web/playwright/driver.nix index 1058a61d48fb..ad38908c036f 100644 --- a/pkgs/development/web/playwright/driver.nix +++ b/pkgs/development/web/playwright/driver.nix @@ -12,6 +12,7 @@ makeFontsConf, makeWrapper, runCommand, + writeText, cacert, }: let @@ -191,9 +192,27 @@ let runHook postInstall ''; + setupHook = writeText "setupHook.sh" '' + addBrowsersPath () { + if [[ ! -v PLAYWRIGHT_BROWSERS_PATH ]] ; then + export PLAYWRIGHT_BROWSERS_PATH="${playwright-core.passthru.browsers}" + fi + } + + addEnvHooks "$targetOffset" addBrowsersPath + ''; + meta = playwright.meta // { mainProgram = "playwright"; }; + + passthru.tests.env = runCommand "playwright-core-env-test" { + buildInputs = [ + nodejs + playwright-core + playwright-test + ]; + } "node ${./test.js}"; }); browsers-mac = stdenv.mkDerivation { diff --git a/pkgs/development/web/playwright/test.js b/pkgs/development/web/playwright/test.js new file mode 100644 index 000000000000..2390bfe513b1 --- /dev/null +++ b/pkgs/development/web/playwright/test.js @@ -0,0 +1,8 @@ +const playwright = require('playwright'); +const fs = require('fs'); +playwright.chromium.launch() + .then((browser) => { + console.log('OK'); + fs.writeFileSync(process.env.out, ''); + process.exit(0); + }); From a1952a6083d1f4d66c24dd813083e2530d2d7611 Mon Sep 17 00:00:00 2001 From: WxNzEMof <143541718+WxNzEMof@users.noreply.github.com> Date: Fri, 27 Dec 2024 08:58:28 +0000 Subject: [PATCH 2/2] python3Packages.playwright: Add PLAYWRIGHT_BROWSERS_PATH to build environment This allows writing self-contained Python scripts (using a nix-shell shebang), without requiring a separate shell.nix which sets PLAYWRIGHT_BROWSERS_PATH to pkgs.playwright-driver.browsers. --- .../python-modules/playwright/default.nix | 16 ++++++++++++++++ .../python-modules/playwright/test.py | 10 ++++++++++ 2 files changed, 26 insertions(+) create mode 100644 pkgs/development/python-modules/playwright/test.py diff --git a/pkgs/development/python-modules/playwright/default.nix b/pkgs/development/python-modules/playwright/default.nix index 08462f56a616..c137b8f66bce 100644 --- a/pkgs/development/python-modules/playwright/default.nix +++ b/pkgs/development/python-modules/playwright/default.nix @@ -13,6 +13,9 @@ setuptools-scm, playwright-driver, nixosTests, + writeText, + runCommand, + pythonPackages, nodejs, }: @@ -86,6 +89,16 @@ buildPythonPackage rec { pyee ]; + setupHook = writeText "setupHook.sh" '' + addBrowsersPath () { + if [[ ! -v PLAYWRIGHT_BROWSERS_PATH ]] ; then + export PLAYWRIGHT_BROWSERS_PATH="${playwright-driver.browsers}" + fi + } + + addEnvHooks "$targetOffset" addBrowsersPath + ''; + postInstall = '' ln -s ${driver} $out/${python.sitePackages}/playwright/driver ''; @@ -101,6 +114,9 @@ buildPythonPackage rec { { driver = playwright-driver; browsers = playwright-driver.browsers; + env = runCommand "playwright-env-test" { + buildInputs = [ pythonPackages.playwright ]; + } "python ${./test.py}"; } // lib.optionalAttrs stdenv.hostPlatform.isLinux { inherit (nixosTests) playwright-python; diff --git a/pkgs/development/python-modules/playwright/test.py b/pkgs/development/python-modules/playwright/test.py new file mode 100644 index 000000000000..3ef73acfdd41 --- /dev/null +++ b/pkgs/development/python-modules/playwright/test.py @@ -0,0 +1,10 @@ +import os +import sys + +from playwright.sync_api import sync_playwright + +with sync_playwright() as p: + browser = p.chromium.launch() + context = browser.new_context() +with open(os.environ["out"], "w") as f: + f.write("OK")