Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkPythonMetaPackage: init meta package function (and psycopg2-binary) #337621

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/languages-frameworks/python.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,22 @@ modifications.

Do pay attention to passing in the right Python version!

#### `mkPythonMetaPackage` function {#mkpythonmetapackage-function}

This will create a meta package containing [metadata files](https://packaging.python.org/en/latest/specifications/recording-installed-packages/) to satisfy a dependency on a package, without it actually having been installed into the environment.
In nixpkgs this is used to package Python packages with split binary/source distributions such as [psycopg2](https://pypi.org/project/psycopg2/)/[psycopg2-binary](https://pypi.org/project/psycopg2-binary/).

```nix
mkPythonMetaPackage {
pname = "pscycopg2-binary";
inherit (psycopg2) optional-dependencies version;
dependencies = [ psycopg2 ];
meta = {
inherit (psycopg2.meta) description homepage;
};
}
```

#### `python.buildEnv` function {#python.buildenv-function}

Python environments can be created using the low-level `pkgs.buildEnv` function.
Expand Down
58 changes: 58 additions & 0 deletions pkgs/development/interpreters/python/meta-package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
buildPythonPackage,
lib,
hatchling,
}:
{
pname,
version,
dependencies ? [ ],
optional-dependencies ? { },
passthru ? { },
meta ? { },
adisbladis marked this conversation as resolved.
Show resolved Hide resolved
}:

# Create a "fake" meta package to satisfy a dependency on a package, but don't actually build it.
# This is useful for packages that have a split binary/source dichotomy like psycopg2/psycopg2-binary,
# where we want to use the former, but some projects declare a dependency on the latter.

buildPythonPackage {
inherit
pname
version
dependencies
optional-dependencies
meta
passthru
;

pyproject = true;

# Make a minimal pyproject.toml that can be built
unpackPhase = ''
cat > pyproject.toml << EOF
[project]
name = "${pname}"
version = "${version}"
dependencies = ${builtins.toJSON (map lib.getName dependencies)}

[project.optional-dependencies]
${lib.optionalString (optional-dependencies != { }) (
(lib.concatStringsSep "\n" (
lib.mapAttrsToList (
group: deps: group + " = " + builtins.toJSON (map lib.getName deps)
) optional-dependencies
))
)}

[tool.hatch.build.targets.wheel]
bypass-selection = true

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
EOF
'';

build-system = [ hatchling ];
}
3 changes: 3 additions & 0 deletions pkgs/development/interpreters/python/python-packages-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ let

removePythonPrefix = lib.removePrefix namePrefix;

mkPythonMetaPackage = callPackage ./meta-package.nix { };

# Convert derivation to a Python module.
toPythonModule = drv:
drv.overrideAttrs( oldAttrs: {
Expand Down Expand Up @@ -97,6 +99,7 @@ in {
inherit buildPythonPackage buildPythonApplication;
inherit hasPythonModule requiredPythonModules makePythonPath disabled disabledIf;
inherit toPythonModule toPythonApplication;
inherit mkPythonMetaPackage;

python = toPythonModule python;

Expand Down
13 changes: 13 additions & 0 deletions pkgs/development/python-modules/psycopg2-binary/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
mkPythonMetaPackage,
psycopg2,
}:
mkPythonMetaPackage {
pname = "pscycopg2-binary";
inherit (psycopg2) version;
dependencies = [ psycopg2 ];
optional-dependencies = psycopg2.optional-dependencies or { };
meta = {
inherit (psycopg2.meta) description homepage;
};
}
2 changes: 2 additions & 0 deletions pkgs/top-level/python-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10952,6 +10952,8 @@ self: super: with self; {

psycopg2cffi = callPackage ../development/python-modules/psycopg2cffi { };

psycopg2-binary = callPackage ../development/python-modules/psycopg2-binary { };

psygnal = callPackage ../development/python-modules/psygnal { };

ptable = callPackage ../development/python-modules/ptable { };
Expand Down