Unverified Commit f00c0c85 authored by Gaétan Lepage's avatar Gaétan Lepage Committed by GitHub
Browse files

python3Packages.ruff: reuse already built ruff binary (#412112)

parents 08bdd25c cf311adc
Loading
Loading
Loading
Loading
+29 −17
Original line number Diff line number Diff line
{
  buildPythonPackage,
  hatchling,
  lib,
  ruff,
  rustPlatform,
  installShellFiles,
}:

buildPythonPackage {
@@ -10,26 +10,38 @@ buildPythonPackage {
    pname
    version
    src
    cargoDeps
    postInstall
    meta
    ;
  pyproject = true;

  build-system = [ hatchling ];

  # Do not rely on path lookup at runtime to find the ruff binary
  postPatch = ''
  postPatch =
    # Do not rely on path lookup at runtime to find the ruff binary.
    # Use the propagated binary instead.
    ''
      substituteInPlace python/ruff/__main__.py \
        --replace-fail \
          'ruff_exe = "ruff" + sysconfig.get_config_var("EXE")' \
        'return "${placeholder "out"}/bin/ruff"'
  '';
          'return "${lib.getExe ruff}"'
    ''
    # Sidestep the maturin build system in favour of reusing the binary already built by nixpkgs,
    # to avoid rebuilding the ruff binary for every active python package set.
    + ''
      substituteInPlace pyproject.toml \
        --replace-fail 'requires = ["maturin>=1.0,<2.0"]' 'requires = ["hatchling"]' \
        --replace-fail 'build-backend = "maturin"' 'build-backend = "hatchling.build"'

  pyproject = true;
      cat >> pyproject.toml <<EOF
      [tool.hatch.build]
      packages = ['python/ruff']

  nativeBuildInputs = [
    installShellFiles
    rustPlatform.cargoSetupHook
    rustPlatform.maturinBuildHook
  ];
      EOF
    '';

  postInstall = ''
    mkdir -p $out/bin && ln -s ${lib.getExe ruff} $out/bin/ruff
  '';

  pythonImportsCheck = [ "ruff" ];
}