NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.45k stars 13.65k forks source link

Can't override Python when used with `withPackages` or `buildEnv` #341482

Open fzakaria opened 1 week ago

fzakaria commented 1 week ago

Describe the bug

I am overriding the CFLAGS of a Python interpreter.

{
  pkgs ?
    import (builtins.fetchTarball {
      name = "nixos-24.05";
      url = https://github.com/NixOS/nixpkgs/archive/24.05.tar.gz;
    }) {
      overlays = [
        (self: super: {
          pythonBindNow = super.python3.overrideAttrs (_: {
            # name = "python-now";
            NIX_CFLAGS_COMPILE = "-Wl,-z,now";
            doCheck = false;
          });
        })
      ];
    },
}:
pkgs.pythonBindNow

When I do this, there is correctly the BIND_NOW attribute.

nix-build python-bindnow-simple.nix
/nix/store/zzcxs2fpc19f5k36rvjp8j2xgkcqkld1-python3-3.11.9

readelf -a ./result/bin/python3 | grep BIND
 0x000000000000001e (FLAGS)              BIND_NOW

When I combine this with either withPackages or buildEnv it seems to be not be picking up the custom python

{
  pkgs ?
    import (builtins.fetchTarball {
      name = "nixos-24.05";
      url = https://github.com/NixOS/nixpkgs/archive/24.05.tar.gz;
    }) {
      overlays = [
        (self: super: {
          pythonBindNow = super.python3.overrideAttrs (_: {
            # name = "python-now";
            NIX_CFLAGS_COMPILE = "-Wl,-z,now";
            doCheck = false;
          });
        })
      ];
    },
}:
(pkgs.pythonBindNow.override {
  packageOverrides = python-self: python-super: {
    pyperformance = python-super.buildPythonPackage rec {
      pname = "pyperformance";
      version = "1.11.0";
      format = "pyproject";
      src = pkgs.fetchPypi {
        inherit pname version;
        sha256 = "sha256-AKT07QiCLhmTmupfM3WDrStPs8Kls/+ReuTf7RWkqYQ=";
      };
      nativeBuildInputs = [python-super.setuptools python-super.pyperf];
    };
  };
})
.withPackages (pp: with pp; [pp.pyperformance])
nix-build python-bindnow.nix
/nix/store/9v38hrdwcgn7gi6clwl8l18ssp28bib7-python3-3.11.9-env
readelf -a ./result/bin/python3 | grep BIND
# nothing!

Expected behavior

I expect the same python to carry through to the withPackages

Notify maintainers

@hexa @natsukium


Add a :+1: reaction to issues you find important.

fzakaria commented 1 week ago

I think it has to do with these lines: https://github.com/NixOS/nixpkgs/blob/da4d8527adf8a9924de870429300a48078b3b7ed/pkgs/development/interpreters/python/default.nix#L59

I had to overlay specifically python311 and not python3; It seems these hardcode the self rather than passing the current self.

Artturin commented 6 days ago

You can .override self

fzakaria commented 6 days ago

@Artturin I tried:

          python311 = (super.python311.overrideAttrs (_: {
            # name = "python-now";
            NIX_CFLAGS_COMPILE = "-Wl,-z,now";
            doCheck = false;
          })).override { self = self.python311; };

That also fails. It's not clear what I need to override for self.

fzakaria commented 6 days ago

Here are some more overrides that fail:

((pkgs.python311.override { self = pkgs.python311; })
.withPackages (pp: with pp; [pkgs.pyperformance])).override { python = pkgs.python311;}