Playwright

From NixOS Wiki
Revision as of 10:42, 27 August 2023 by imported>Voidless (Created page with "== Installing browsers for playwright under NixOS == Normally at first run, playwright will tell you to run <code>playwright install</code>. The purpose of this is to install...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Installing browsers for playwright under NixOS

Normally at first run, playwright will tell you to run playwright install. The purpose of this is to install browsers for you that it can than use for testing. The installation itself will technically work. Unfortunately the installed browsers will not be suitable to be used inside NixOS. This is due to the fact that dependencies will not be at places where the browsers expect them to be. To mitigate this problem, nixpkgs has a package called playwright-driver.browsers. Before you start your script make sure to set

export PLAYWRIGHT_BROWSERS_PATH=/path/to/drivers

Python example

The following flake may be used with nix develop to develop your playwright script:

{
  description = "Python shell flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";

    mach-nix.url = "github:davhau/mach-nix";
  };

  outputs = { self, nixpkgs, mach-nix, flake-utils, ... }:
    let
      pythonVersion = "python39";
    in
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        mach = mach-nix.lib.${system};

        pythonEnv = mach.mkPython {
          python = pythonVersion;
          requirements = ''
            playwright
          '';
        };
      in
      {
        devShells.default = pkgs.mkShellNoCC {
          packages = [ pythonEnv ];

          shellHook = ''
            export PYTHONPATH="${pythonEnv}/bin/python"
            export PLAYWRIGHT_BROWSERS_PATH="${pkgs.playwright-driver.browsers}";
          '';
        };
      }
    );
}