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; python = config.deps.python;
metadata = config.lock.content.fetchPipMetadata; 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 # filter out ignored dependencies
targets = l.flip l.mapAttrs metadata.targets ( targets = cfg.targets;
targetName: target:
l.flip l.mapAttrs (filterTarget target) (
packageName: deps:
l.filter (dep: ! ignored ? ${dep}) deps
)
);
writers = import ../../../pkgs/writers { writers = import ../../../pkgs/writers {
inherit lib; inherit lib;
@ -113,6 +102,7 @@ in {
imports = [ imports = [
commonModule commonModule
./interface.nix ./interface.nix
../pip-hotfixes
]; ];
config = { config = {
@ -138,26 +128,6 @@ in {
mkDerivation = { mkDerivation = {
dontStrip = l.mkDefault true; 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; t = l.types;
in { in {
options.pip = { options.pip = {
flattenDependencies = l.mkOption { # internal options to pass data between pip-hotfixes and pip
type = t.bool; targets = l.mkOption {
description = '' type = t.raw;
Use all dependencies as top-level dependencies internal = true;
''; description = "the targets of the lock file to build";
default = false;
};
ignoredDependencies = l.mkOption {
type = t.listOf t.str;
description = ''
list of dependencies to ignore
'';
default = ["wheel"];
}; };
pypiSnapshotDate = l.mkOption { pypiSnapshotDate = l.mkOption {
type = t.str; type = t.str;