poetry2nix/lib.nix

107 lines
3.6 KiB
Nix
Raw Normal View History

{ lib, pyproject-nix, pkgs, ... }:
let
inherit (import ./vendor/pyproject.nix/lib/util.nix { inherit lib; }) splitComma;
2020-01-09 12:42:56 +00:00
fromTOML = builtins.fromTOML or
(
2020-05-19 21:06:02 +01:00
toml: builtins.fromJSON (
builtins.readFile (
pkgs.runCommand "from-toml"
{
inherit toml;
allowSubstitutes = false;
preferLocalBuild = true;
}
''
${pkgs.remarshal}/bin/remarshal \
-if toml \
-i <(echo "$toml") \
-of json \
-o $out
''
)
2020-05-19 21:06:02 +01:00
)
);
2020-01-11 23:33:40 +00:00
readTOML = path: fromTOML (builtins.readFile path);
#
# Returns the appropriate manylinux dependencies and string representation for the file specified
#
getManyLinuxDeps = f:
let
ml = pkgs.pythonManylinuxPackages;
in
if lib.strings.hasInfix "manylinux1" f then { pkg = [ ml.manylinux1 ]; str = "1"; }
else if lib.strings.hasInfix "manylinux2010" f then { pkg = [ ml.manylinux2010 ]; str = "2010"; }
else if lib.strings.hasInfix "manylinux2014" f then { pkg = [ ml.manylinux2014 ]; str = "2014"; }
2022-03-28 19:40:46 +02:00
else if lib.strings.hasInfix "manylinux_" f then { pkg = [ ml.manylinux2014 ]; str = "pep600"; }
else { pkg = [ ]; str = null; };
getBuildSystemPkgs =
{ pythonPackages
, pyProject
2020-03-14 23:13:51 +00:00
}:
let
missingBuildBackendError = "No build-system.build-backend section in pyproject.toml. "
+ "Add such a section as described in https://python-poetry.org/docs/pyproject/#poetry-and-pep-517";
requires = lib.attrByPath [ "build-system" "requires" ] (throw missingBuildBackendError) pyProject;
requiredPkgs = builtins.map (n: (pyproject-nix.lib.pep508.parseString n).name) requires;
2020-10-01 19:08:57 +02:00
in
builtins.map (drvAttr: pythonPackages.${drvAttr} or (throw "unsupported build system requirement ${drvAttr}")) requiredPkgs;
2020-02-29 21:19:09 +00:00
# Find gitignore files recursively in parent directory stopping with .git
2020-03-14 23:13:51 +00:00
findGitIgnores = path:
let
parent = builtins.dirOf path;
2020-03-14 23:13:51 +00:00
gitIgnore = path + "/.gitignore";
isGitRoot = builtins.pathExists (path + "/.git");
hasGitIgnore = builtins.pathExists gitIgnore;
gitIgnores = if hasGitIgnore then [ gitIgnore ] else [ ];
2020-03-14 23:13:51 +00:00
in
lib.optionals (builtins.pathExists path && builtins.toString path != "/" && ! isGitRoot) (findGitIgnores parent) ++ gitIgnores;
2020-02-29 21:19:09 +00:00
/*
2021-12-25 16:00:20 -08:00
Provides a source filtering mechanism that:
2021-12-25 16:00:20 -08:00
- Filters gitignore's
- Filters pycache/pyc files
- Uses cleanSourceFilter to filter out .git/.hg, .o/.so, editor backup files & nix result symlinks
*/
2020-03-14 23:13:51 +00:00
cleanPythonSources = { src }:
let
gitIgnores = findGitIgnores src;
pycacheFilter = name: type:
(type == "directory" && ! lib.strings.hasInfix "__pycache__" name)
|| (type == "regular" && ! lib.strings.hasSuffix ".pyc" name)
;
in
lib.cleanSourceWith {
filter = lib.cleanSourceFilter;
src = lib.cleanSourceWith {
filter = pkgs.nix-gitignore.gitignoreFilterPure pycacheFilter gitIgnores src;
inherit src;
};
};
checkPythonVersions = pyVersion: python-versions: (
lib.any
(python-versions': lib.all
(cond:
let
conds = pyproject-nix.lib.poetry.parseVersionCond cond;
in
lib.all (cond': pyproject-nix.lib.pep440.comparators.${cond'.op} pyVersion cond'.version) conds)
(splitComma python-versions'))
(builtins.filter lib.isString (builtins.split " *\\|\\| *" python-versions)));
in
{
inherit
getManyLinuxDeps
readTOML
getBuildSystemPkgs
cleanPythonSources
checkPythonVersions
2019-12-16 14:51:45 +01:00
;
}