refactor(pip): move flattenDependencies and ignoredDependencies to separate module pip-hotfixes

This commit is contained in:
DavHau 2023-08-28 15:02:45 +02:00
parent b58cc53fac
commit 6646e20dfb
4 changed files with 80 additions and 45 deletions

View File

@ -0,0 +1,48 @@
{
config,
lib,
...
}: let
l = lib // builtins;
cfg = config.pip;
metadata = config.lock.content.fetchPipMetadata;
ignored = l.genAttrs cfg.ignoredDependencies (name: true);
filterTarget = target:
l.filterAttrs (name: target: ! ignored ? ${name}) target;
# filter out ignored dependencies
targets = l.flip l.mapAttrs metadata.targets (
targetName: target:
l.flip l.mapAttrs (filterTarget target) (
packageName: deps:
l.filter (dep: ! ignored ? ${dep}) deps
)
);
in {
imports = [
./interface.nix
];
pip.targets = targets;
mkDerivation.propagatedBuildInputs =
if cfg.flattenDependencies
then
if targets.default ? ${config.name}
then
throw ''
Top-level package ${config.name} is listed in the lockfile.
Set `pip.flattenDependencies` to false to use only the top-level dependencies.
''
else let
topLevelDepNames = l.attrNames (targets.default);
in
l.map (name: cfg.drvs.${name}.public.out) topLevelDepNames
else if ! targets.default ? ${config.name}
then
throw ''
Top-level package ${config.name} is not listed in the lockfile.
Set `pip.flattenDependencies` to true to use all dependencies for the top-level package.
''
else [];
}

View File

@ -0,0 +1,25 @@
{
config,
lib,
...
}: let
l = lib // builtins;
t = l.types;
in {
options.pip = {
flattenDependencies = l.mkOption {
type = t.bool;
description = ''
Use all dependencies as top-level dependencies
'';
default = false;
};
ignoredDependencies = l.mkOption {
type = t.listOf t.str;
description = ''
list of dependencies to ignore
'';
default = ["wheel"];
};
};
}

View File

@ -9,19 +9,8 @@
python = config.deps.python;
metadata = config.lock.content.fetchPipMetadata;
ignored = l.genAttrs cfg.ignoredDependencies (name: true);
filterTarget = target:
l.filterAttrs (name: target: ! ignored ? ${name}) target;
# filter out ignored dependencies
targets = l.flip l.mapAttrs metadata.targets (
targetName: target:
l.flip l.mapAttrs (filterTarget target) (
packageName: deps:
l.filter (dep: ! ignored ? ${dep}) deps
)
);
targets = cfg.targets;
writers = import ../../../pkgs/writers {
inherit lib;
@ -113,6 +102,7 @@ in {
imports = [
commonModule
./interface.nix
../pip-hotfixes
];
config = {
@ -138,26 +128,6 @@ in {
mkDerivation = {
dontStrip = l.mkDefault true;
propagatedBuildInputs =
if cfg.flattenDependencies
then
if targets.default ? ${config.name}
then
throw ''
Top-level package ${config.name} is listed in the lockfile.
Set `pip.flattenDependencies` to false to use only the top-level dependencies.
''
else let
topLevelDepNames = l.attrNames (targets.default);
in
l.map (name: cfg.drvs.${name}.public.out) topLevelDepNames
else if ! targets.default ? ${config.name}
then
throw ''
Top-level package ${config.name} is not listed in the lockfile.
Set `pip.flattenDependencies` to true to use all dependencies for the top-level package.
''
else [];
};
};
}

View File

@ -9,19 +9,11 @@
t = l.types;
in {
options.pip = {
flattenDependencies = l.mkOption {
type = t.bool;
description = ''
Use all dependencies as top-level dependencies
'';
default = false;
};
ignoredDependencies = l.mkOption {
type = t.listOf t.str;
description = ''
list of dependencies to ignore
'';
default = ["wheel"];
# internal options to pass data between pip-hotfixes and pip
targets = l.mkOption {
type = t.raw;
internal = true;
description = "the targets of the lock file to build";
};
pypiSnapshotDate = l.mkOption {
type = t.str;