Basic flake: run existing (python, bash) script

Just another small example to remind myself how (and that I need) to use pkgs.symlinkJoin and PATH manipulation with wrapProgram in order to use nix’s chromedriver instead of my system one.

# flake.nix
{
  inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-22.05;

  outputs = { self, nixpkgs }:
    let
      system = "aarch64-darwin";
      pkgs = nixpkgs.legacyPackages.${system};
      py3pkgs = pkgs.python3.pkgs;
      venvDir = "./.venv";

      pydeps = [ py3pkgs.python py3pkgs.selenium ];

      shell = pkgs.mkShell
        {
          name = "python-env";
          buildInputs = pydeps;
        };

      pyscript = pkgs.writers.writePython3Bin "tester"
        {
          libraries = pydeps;
        } "${builtins.readFile ./tester.py}";

      runner = pkgs.symlinkJoin
        {
          name = "run tester";
          paths = [ pyscript pkgs.chromedriver ];
          buildInputs = [ pkgs.makeBinaryWrapper ];
          postBuild = "wrapProgram $out/bin/tester --prefix PATH : $out/bin";
        };
    in
    {
      apps.${system}.default = {
        type = "app";
        program = "${runner}/bin/tester";
      };
      devShells.${system}.default = shell;
    };
}
# tester.py
import subprocess

import selenium

print(selenium.__version__, selenium.__file__, sep="\n")

subprocess.run(
    "which chromedriver; chromedriver --version;", shell=True
)
$ nix run
3.141.0
/nix/store/1hgvcb1fnw9gyz9sd7xplsf5d97cijrb-python3-3.9.13-env/lib/python3.9/site-packages/selenium/__init__.py
/nix/store/pc1qixi6pkyijrxmb9cqlc5di7zaqfnn-run-tester/bin/chromedriver
ChromeDriver 103.0.5060.24 (e47b049c438cd0a74dc95a011fceb27db18cb080-refs/branch-heads/5060@{#232})
2 Likes