2020-04-29 14:12:59 +01:00
|
|
|
{ pkgs ? import <nixpkgs> { }
|
2019-12-11 13:31:22 +01:00
|
|
|
, lib ? pkgs.lib
|
|
|
|
, poetry ? null
|
2021-01-26 14:29:24 +03:00
|
|
|
, poetryLib ? import ./lib.nix { inherit lib pkgs; stdenv = pkgs.stdenv; }
|
2019-06-17 16:27:16 +01:00
|
|
|
}:
|
|
|
|
let
|
2021-03-04 14:13:55 +02:00
|
|
|
# Poetry2nix version
|
2022-10-28 13:58:39 +13:00
|
|
|
version = "1.36.0";
|
2021-03-04 14:13:55 +02:00
|
|
|
|
2022-09-29 20:39:37 +03:00
|
|
|
inherit (poetryLib) isCompatible readTOML normalizePackageName normalizePackageSet;
|
2019-06-24 16:23:02 +01:00
|
|
|
|
2020-01-01 17:00:03 +00:00
|
|
|
# Map SPDX identifiers to license names
|
2020-01-01 18:01:11 +00:00
|
|
|
spdxLicenses = lib.listToAttrs (lib.filter (pair: pair.name != null) (builtins.map (v: { name = if lib.hasAttr "spdxId" v then v.spdxId else null; value = v; }) (lib.attrValues lib.licenses)));
|
2020-01-01 17:00:03 +00:00
|
|
|
# Get license by id falling back to input string
|
2020-01-12 22:18:49 +00:00
|
|
|
getLicenseBySpdxId = spdxId: spdxLicenses.${spdxId} or spdxId;
|
2020-01-01 17:00:03 +00:00
|
|
|
|
2020-07-14 14:34:03 +02:00
|
|
|
# Experimental withPlugins functionality
|
|
|
|
toPluginAble = (import ./plugins.nix { inherit pkgs lib; }).toPluginAble;
|
2020-10-01 20:37:45 +02:00
|
|
|
|
2022-09-29 20:39:37 +03:00
|
|
|
# List of known build systems that are passed through from nixpkgs unmodified
|
|
|
|
knownBuildSystems = builtins.fromJSON (builtins.readFile ./known-build-systems.json);
|
|
|
|
nixpkgsBuildSystems = lib.subtractLists [ "poetry" "poetry-core" ] knownBuildSystems;
|
|
|
|
|
2020-10-01 20:37:45 +02:00
|
|
|
mkInputAttrs =
|
|
|
|
{ py
|
|
|
|
, pyProject
|
|
|
|
, attrs
|
2020-10-01 21:46:16 +02:00
|
|
|
, includeBuildSystem ? true
|
2022-09-02 14:58:48 +12:00
|
|
|
, groups ? [ ]
|
2022-10-09 07:11:17 -04:00
|
|
|
, checkGroups ? [ "dev" ]
|
2020-10-01 20:37:45 +02:00
|
|
|
}:
|
|
|
|
let
|
|
|
|
getInputs = attr: attrs.${attr} or [ ];
|
|
|
|
|
|
|
|
# Get dependencies and filter out depending on interpreter version
|
2022-09-02 14:58:48 +12:00
|
|
|
getDeps = depSet:
|
2020-10-01 20:37:45 +02:00
|
|
|
let
|
|
|
|
compat = isCompatible (poetryLib.getPythonVersion py);
|
2022-09-02 14:58:48 +12:00
|
|
|
depAttrs = builtins.map (d: lib.toLower d) (builtins.attrNames depSet);
|
2020-10-01 20:37:45 +02:00
|
|
|
in
|
|
|
|
(
|
|
|
|
builtins.map
|
|
|
|
(
|
|
|
|
dep:
|
|
|
|
let
|
2022-09-29 20:39:37 +03:00
|
|
|
pkg = py.pkgs."${normalizePackageName dep}";
|
2022-09-02 14:58:48 +12:00
|
|
|
constraints = depSet.${dep}.python or "";
|
2020-10-01 20:37:45 +02:00
|
|
|
isCompat = compat constraints;
|
|
|
|
in
|
|
|
|
if isCompat then pkg else null
|
|
|
|
)
|
|
|
|
depAttrs
|
|
|
|
);
|
|
|
|
|
|
|
|
buildSystemPkgs = poetryLib.getBuildSystemPkgs {
|
|
|
|
inherit pyProject;
|
|
|
|
pythonPackages = py.pkgs;
|
|
|
|
};
|
|
|
|
|
|
|
|
mkInput = attr: extraInputs: getInputs attr ++ extraInputs;
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2020-10-01 21:46:16 +02:00
|
|
|
buildInputs = mkInput "buildInputs" (if includeBuildSystem then buildSystemPkgs else [ ]);
|
2022-09-02 14:58:48 +12:00
|
|
|
propagatedBuildInputs = mkInput "propagatedBuildInputs" (
|
|
|
|
(getDeps pyProject.tool.poetry."dependencies" or { })
|
|
|
|
++ (
|
|
|
|
# >=poetry-1.2.0 dependency groups
|
|
|
|
if pyProject.tool.poetry.group or { } != { }
|
|
|
|
then lib.flatten (map (g: getDeps pyProject.tool.poetry.group.${g}.dependencies) groups)
|
|
|
|
else [ ]
|
|
|
|
)
|
|
|
|
);
|
2020-10-01 20:37:45 +02:00
|
|
|
nativeBuildInputs = mkInput "nativeBuildInputs" [ ];
|
2022-09-02 14:58:48 +12:00
|
|
|
checkInputs = mkInput "checkInputs" (
|
|
|
|
getDeps (pyProject.tool.poetry."dev-dependencies" or { }) # <poetry-1.2.0
|
2022-10-09 07:11:17 -04:00
|
|
|
# >=poetry-1.2.0 dependency groups
|
|
|
|
++ lib.flatten (map (g: getDeps (pyProject.tool.poetry.group.${g}.dependencies or { })) checkGroups)
|
2022-09-02 14:58:48 +12:00
|
|
|
);
|
2020-10-01 20:37:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-21 19:26:09 +02:00
|
|
|
in
|
|
|
|
lib.makeScope pkgs.newScope (self: {
|
|
|
|
|
2021-03-04 14:13:55 +02:00
|
|
|
inherit version;
|
2020-07-14 14:34:03 +02:00
|
|
|
|
2021-01-14 18:39:12 -05:00
|
|
|
/* Returns a package of editable sources whose changes will be available without needing to restart the
|
2021-12-25 16:00:20 -08:00
|
|
|
nix-shell.
|
|
|
|
In editablePackageSources you can pass a mapping from package name to source directory to have
|
|
|
|
those packages available in the resulting environment, whose source changes are immediately available.
|
2021-02-02 14:02:45 +01:00
|
|
|
|
2021-01-14 18:39:12 -05:00
|
|
|
*/
|
|
|
|
mkPoetryEditablePackage =
|
|
|
|
{ projectDir ? null
|
|
|
|
, pyproject ? projectDir + "/pyproject.toml"
|
|
|
|
, python ? pkgs.python3
|
|
|
|
, pyProject ? readTOML pyproject
|
|
|
|
# Example: { my-app = ./src; }
|
|
|
|
, editablePackageSources
|
|
|
|
}:
|
|
|
|
assert editablePackageSources != { };
|
|
|
|
import ./editable.nix {
|
|
|
|
inherit pyProject python pkgs lib poetryLib editablePackageSources;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Returns a package containing scripts defined in tool.poetry.scripts.
|
|
|
|
*/
|
|
|
|
mkPoetryScriptsPackage =
|
|
|
|
{ projectDir ? null
|
|
|
|
, pyproject ? projectDir + "/pyproject.toml"
|
|
|
|
, python ? pkgs.python3
|
|
|
|
, pyProject ? readTOML pyproject
|
|
|
|
, scripts ? pyProject.tool.poetry.scripts
|
|
|
|
}:
|
|
|
|
assert scripts != { };
|
|
|
|
import ./shell-scripts.nix {
|
|
|
|
inherit lib python scripts;
|
|
|
|
};
|
|
|
|
|
2020-02-24 12:23:09 +00:00
|
|
|
/*
|
2021-12-25 16:00:20 -08:00
|
|
|
Returns an attrset { python, poetryPackages, pyProject, poetryLock } for the given pyproject/lockfile.
|
2020-02-24 12:23:09 +00:00
|
|
|
*/
|
|
|
|
mkPoetryPackages =
|
2020-02-25 14:40:36 +00:00
|
|
|
{ projectDir ? null
|
|
|
|
, pyproject ? projectDir + "/pyproject.toml"
|
|
|
|
, poetrylock ? projectDir + "/poetry.lock"
|
2020-07-21 20:45:03 +02:00
|
|
|
, overrides ? self.defaultPoetryOverrides
|
2019-12-11 13:31:22 +01:00
|
|
|
, python ? pkgs.python3
|
2020-02-25 14:40:36 +00:00
|
|
|
, pwd ? projectDir
|
2020-04-21 16:49:01 +02:00
|
|
|
, preferWheels ? false
|
2021-01-14 18:39:12 -05:00
|
|
|
# Example: { my-app = ./src; }
|
|
|
|
, editablePackageSources ? { }
|
2022-01-05 23:27:06 +13:00
|
|
|
, pyProject ? readTOML pyproject
|
2022-09-02 14:58:48 +12:00
|
|
|
, groups ? [ ]
|
2022-10-09 07:11:17 -04:00
|
|
|
, checkGroups ? [ "dev" ]
|
2022-09-29 20:39:37 +03:00
|
|
|
}:
|
2020-04-29 14:12:59 +01:00
|
|
|
let
|
2022-01-19 03:56:47 +12:00
|
|
|
/* The default list of poetry2nix override overlays */
|
|
|
|
mkEvalPep508 = import ./pep508.nix {
|
|
|
|
inherit lib poetryLib;
|
|
|
|
inherit (python) stdenv;
|
|
|
|
};
|
|
|
|
getFunctorFn = fn: if builtins.typeOf fn == "set" then fn.__functor else fn;
|
|
|
|
|
2020-04-29 14:12:59 +01:00
|
|
|
poetryPkg = poetry.override { inherit python; };
|
2021-01-14 18:39:12 -05:00
|
|
|
|
|
|
|
scripts = pyProject.tool.poetry.scripts or { };
|
|
|
|
hasScripts = scripts != { };
|
|
|
|
scriptsPackage = self.mkPoetryScriptsPackage {
|
|
|
|
inherit python scripts;
|
|
|
|
};
|
|
|
|
|
2022-01-06 04:57:41 +13:00
|
|
|
editablePackageSources' = lib.filterAttrs (name: path: path != null) editablePackageSources;
|
|
|
|
hasEditable = editablePackageSources' != { };
|
2021-01-14 18:39:12 -05:00
|
|
|
editablePackage = self.mkPoetryEditablePackage {
|
2022-01-06 04:57:41 +13:00
|
|
|
inherit pyProject python;
|
|
|
|
editablePackageSources = editablePackageSources';
|
2021-01-14 18:39:12 -05:00
|
|
|
};
|
|
|
|
|
2020-04-29 14:12:59 +01:00
|
|
|
poetryLock = readTOML poetrylock;
|
2022-10-11 11:58:27 +13:00
|
|
|
|
|
|
|
# Lock file version 1.1 files
|
2020-04-29 14:12:59 +01:00
|
|
|
lockFiles =
|
|
|
|
let
|
2020-04-15 10:22:26 +01:00
|
|
|
lockfiles = lib.getAttrFromPath [ "metadata" "files" ] poetryLock;
|
2020-04-29 14:12:59 +01:00
|
|
|
in
|
2022-09-29 20:39:37 +03:00
|
|
|
lib.listToAttrs (lib.mapAttrsToList (n: v: { name = normalizePackageName n; value = v; }) lockfiles);
|
2022-10-11 11:58:27 +13:00
|
|
|
|
2020-04-29 14:12:59 +01:00
|
|
|
evalPep508 = mkEvalPep508 python;
|
|
|
|
|
|
|
|
# Filter packages by their PEP508 markers & pyproject interpreter version
|
|
|
|
partitions =
|
|
|
|
let
|
2020-10-01 19:08:04 +02:00
|
|
|
supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true && isCompatible (poetryLib.getPythonVersion python) pkgMeta.python-versions;
|
2019-12-11 13:31:22 +01:00
|
|
|
in
|
2020-04-29 14:12:59 +01:00
|
|
|
lib.partition supportsPythonVersion poetryLock.package;
|
2020-06-08 20:33:07 +02:00
|
|
|
compatible = partitions.right;
|
2020-04-29 14:12:59 +01:00
|
|
|
incompatible = partitions.wrong;
|
|
|
|
|
2021-03-12 09:59:33 +01:00
|
|
|
# Create an overridden version of pythonPackages
|
2020-04-29 14:12:59 +01:00
|
|
|
#
|
|
|
|
# We need to avoid mixing multiple versions of pythonPackages in the same
|
|
|
|
# closure as python can only ever have one version of a dependency
|
|
|
|
baseOverlay = self: super:
|
|
|
|
let
|
2020-05-19 21:06:02 +01:00
|
|
|
lockPkgs = builtins.listToAttrs (
|
|
|
|
builtins.map
|
|
|
|
(
|
2022-09-29 20:39:37 +03:00
|
|
|
pkgMeta:
|
2022-10-07 09:37:55 +03:00
|
|
|
let normalizedName = normalizePackageName pkgMeta.name; in
|
|
|
|
{
|
|
|
|
name = normalizedName;
|
2020-05-19 21:06:02 +01:00
|
|
|
value = self.mkPoetryDep (
|
|
|
|
pkgMeta // {
|
|
|
|
inherit pwd preferWheels;
|
|
|
|
source = pkgMeta.source or null;
|
2022-10-11 11:58:27 +13:00
|
|
|
# Default to files from lock file version 2.0 and fall back to 1.1
|
|
|
|
files = pkgMeta.files or lockFiles.${normalizedName};
|
2020-05-19 21:06:02 +01:00
|
|
|
pythonPackages = self;
|
2022-09-02 04:17:07 +12:00
|
|
|
|
2022-10-07 09:37:55 +03:00
|
|
|
sourceSpec = (
|
|
|
|
(normalizePackageSet pyProject.tool.poetry.dependencies or { }).${normalizedName}
|
|
|
|
or (normalizePackageSet pyProject.tool.poetry.dev-dependencies or { }).${normalizedName}
|
2022-10-20 08:55:38 +00:00
|
|
|
or (normalizePackageSet pyProject.tool.poetry.group.dev.dependencies or { }).${normalizedName} # Poetry 1.2.0+
|
2022-10-07 09:37:55 +03:00
|
|
|
or { }
|
|
|
|
);
|
2020-05-19 21:06:02 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
2020-10-01 18:17:54 +02:00
|
|
|
(lib.reverseList compatible)
|
2020-05-19 21:06:02 +01:00
|
|
|
);
|
2022-10-07 09:37:55 +03:00
|
|
|
buildSystems = builtins.listToAttrs (builtins.map (x: { name = x; value = super.${x}; }) nixpkgsBuildSystems);
|
2020-04-29 14:12:59 +01:00
|
|
|
in
|
2022-10-07 09:37:55 +03:00
|
|
|
lockPkgs // buildSystems // {
|
2022-01-05 07:43:37 +00:00
|
|
|
# Create a dummy null package for the current project in case any dependencies depend on the root project (issue #307)
|
|
|
|
${pyProject.tool.poetry.name} = null;
|
|
|
|
};
|
2020-05-19 21:06:02 +01:00
|
|
|
overlays = builtins.map
|
|
|
|
getFunctorFn
|
2020-04-29 14:12:59 +01:00
|
|
|
(
|
2020-03-14 23:13:51 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
self: super:
|
2020-04-29 14:12:59 +01:00
|
|
|
{
|
|
|
|
mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
|
2022-01-19 03:56:47 +12:00
|
|
|
inherit lib python poetryLib evalPep508;
|
2020-04-29 14:12:59 +01:00
|
|
|
};
|
2020-10-01 17:17:28 +02:00
|
|
|
|
2022-01-19 02:20:37 +12:00
|
|
|
# # Use poetry-core from the poetry build (pep517/518 build-system)
|
|
|
|
poetry-core = poetryPkg.passthru.python.pkgs.poetry-core;
|
|
|
|
poetry = poetryPkg;
|
2020-10-01 17:17:28 +02:00
|
|
|
|
2020-07-14 14:34:03 +02:00
|
|
|
__toPluginAble = toPluginAble self;
|
2021-06-03 18:43:02 +02:00
|
|
|
} // lib.optionalAttrs (! super ? setuptools-scm) {
|
|
|
|
# The canonical name is setuptools-scm
|
|
|
|
setuptools-scm = super.setuptools_scm;
|
2020-04-29 14:12:59 +01:00
|
|
|
}
|
2020-03-14 23:13:51 +00:00
|
|
|
)
|
2022-01-17 22:05:58 +12:00
|
|
|
|
|
|
|
# Fix infinite recursion in a lot of packages because of checkInputs
|
|
|
|
(self: super: lib.mapAttrs
|
|
|
|
(name: value: (
|
|
|
|
if lib.isDerivation value && lib.hasAttr "overridePythonAttrs" value
|
|
|
|
then value.overridePythonAttrs (_: { doCheck = false; })
|
|
|
|
else value
|
|
|
|
))
|
|
|
|
super)
|
|
|
|
|
2020-03-14 23:13:51 +00:00
|
|
|
# Null out any filtered packages, we don't want python.pkgs from nixpkgs
|
2022-09-29 20:39:37 +03:00
|
|
|
(self: super: builtins.listToAttrs (builtins.map (x: { name = normalizePackageName x.name; value = null; }) incompatible))
|
2020-03-14 23:13:51 +00:00
|
|
|
# Create poetry2nix layer
|
|
|
|
baseOverlay
|
2022-01-17 22:05:58 +12:00
|
|
|
|
2020-03-14 23:13:51 +00:00
|
|
|
] ++ # User provided overrides
|
2020-07-21 20:45:03 +02:00
|
|
|
(if builtins.typeOf overrides == "list" then overrides else [ overrides ])
|
2020-03-14 23:13:51 +00:00
|
|
|
);
|
2020-04-29 14:12:59 +01:00
|
|
|
packageOverrides = lib.foldr lib.composeExtensions (self: super: { }) overlays;
|
|
|
|
py = python.override { inherit packageOverrides; self = py; };
|
2020-10-01 20:37:45 +02:00
|
|
|
|
2022-10-09 07:11:17 -04:00
|
|
|
inputAttrs = mkInputAttrs { inherit py pyProject groups checkGroups; attrs = { }; includeBuildSystem = false; };
|
2020-10-01 20:37:45 +02:00
|
|
|
|
2021-03-12 14:49:21 +01:00
|
|
|
requiredPythonModules = python.pkgs.requiredPythonModules;
|
2021-03-12 13:04:58 +01:00
|
|
|
/* Include all the nested dependencies which are required for each package.
|
2021-12-25 16:00:20 -08:00
|
|
|
This guarantees that using the "poetryPackages" attribute will return
|
|
|
|
complete list of dependencies for the poetry project to be portable.
|
2021-03-12 13:04:58 +01:00
|
|
|
*/
|
2021-03-12 09:59:33 +01:00
|
|
|
storePackages = requiredPythonModules (builtins.foldl' (acc: v: acc ++ v) [ ] (lib.attrValues inputAttrs));
|
2020-04-29 14:12:59 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
python = py;
|
2021-01-14 18:39:12 -05:00
|
|
|
poetryPackages = storePackages
|
|
|
|
++ lib.optional hasScripts scriptsPackage
|
|
|
|
++ lib.optional hasEditable editablePackage;
|
2020-04-29 14:12:59 +01:00
|
|
|
poetryLock = poetryLock;
|
|
|
|
inherit pyProject;
|
|
|
|
};
|
2019-12-17 14:01:45 +01:00
|
|
|
|
2020-01-01 17:49:12 +00:00
|
|
|
/* Returns a package with a python interpreter and all packages specified in the poetry.lock lock file.
|
2021-12-25 16:00:20 -08:00
|
|
|
In editablePackageSources you can pass a mapping from package name to source directory to have
|
|
|
|
those packages available in the resulting environment, whose source changes are immediately available.
|
2020-01-01 17:49:12 +00:00
|
|
|
|
2021-12-25 16:00:20 -08:00
|
|
|
Example:
|
|
|
|
poetry2nix.mkPoetryEnv { poetrylock = ./poetry.lock; python = python3; }
|
2020-01-01 17:49:12 +00:00
|
|
|
*/
|
2019-12-17 14:01:45 +01:00
|
|
|
mkPoetryEnv =
|
2020-02-25 14:40:36 +00:00
|
|
|
{ projectDir ? null
|
|
|
|
, pyproject ? projectDir + "/pyproject.toml"
|
|
|
|
, poetrylock ? projectDir + "/poetry.lock"
|
2020-07-21 20:45:03 +02:00
|
|
|
, overrides ? self.defaultPoetryOverrides
|
2020-02-25 14:40:36 +00:00
|
|
|
, pwd ? projectDir
|
2019-12-17 14:01:45 +01:00
|
|
|
, python ? pkgs.python3
|
2020-04-21 16:49:01 +02:00
|
|
|
, preferWheels ? false
|
2020-05-08 16:29:14 +02:00
|
|
|
, editablePackageSources ? { }
|
2021-12-27 16:48:38 -08:00
|
|
|
, extraPackages ? ps: [ ]
|
2022-09-02 14:58:48 +12:00
|
|
|
, groups ? [ "dev" ]
|
2019-12-17 20:58:41 +01:00
|
|
|
}:
|
2020-04-29 14:12:59 +01:00
|
|
|
let
|
2022-09-29 20:39:37 +03:00
|
|
|
inherit (lib) hasAttr;
|
2022-01-05 23:27:06 +13:00
|
|
|
|
|
|
|
pyProject = readTOML pyproject;
|
|
|
|
|
|
|
|
# Automatically add dependencies with develop = true as editable packages, but only if path dependencies
|
|
|
|
getEditableDeps = set: lib.mapAttrs
|
|
|
|
(name: value: projectDir + "/${value.path}")
|
|
|
|
(lib.filterAttrs (name: dep: dep.develop or false && hasAttr "path" dep) set);
|
|
|
|
|
2022-04-07 12:05:58 +01:00
|
|
|
excludedEditablePackageNames = builtins.filter
|
|
|
|
(pkg: editablePackageSources."${pkg}" == null)
|
|
|
|
(builtins.attrNames editablePackageSources);
|
|
|
|
|
|
|
|
allEditablePackageSources = (
|
2022-01-05 23:27:06 +13:00
|
|
|
(getEditableDeps (pyProject.tool.poetry."dependencies" or { }))
|
|
|
|
// (getEditableDeps (pyProject.tool.poetry."dev-dependencies" or { }))
|
2022-09-02 14:58:48 +12:00
|
|
|
// (
|
|
|
|
# Poetry>=1.2.0
|
|
|
|
if pyProject.tool.poetry.group or { } != { } then
|
|
|
|
builtins.foldl' (acc: g: acc // getEditableDeps pyProject.tool.poetry.group.${g}.dependencies) { } groups
|
|
|
|
else { }
|
|
|
|
)
|
2022-01-05 23:27:06 +13:00
|
|
|
// editablePackageSources
|
|
|
|
);
|
|
|
|
|
2022-04-07 12:05:58 +01:00
|
|
|
editablePackageSources' = builtins.removeAttrs
|
|
|
|
allEditablePackageSources
|
|
|
|
excludedEditablePackageNames;
|
|
|
|
|
2021-01-14 18:39:12 -05:00
|
|
|
poetryPython = self.mkPoetryPackages {
|
2022-09-02 14:58:48 +12:00
|
|
|
inherit pyproject poetrylock overrides python pwd preferWheels pyProject groups;
|
2022-01-05 23:27:06 +13:00
|
|
|
editablePackageSources = editablePackageSources';
|
2020-08-17 02:22:47 +02:00
|
|
|
};
|
|
|
|
|
2021-01-14 18:39:12 -05:00
|
|
|
inherit (poetryPython) poetryPackages;
|
2022-01-05 08:06:47 +00:00
|
|
|
|
|
|
|
# Don't add editable sources to the environment since they will sometimes fail to build and are not useful in the development env
|
2022-01-05 23:27:06 +13:00
|
|
|
editableAttrs = lib.attrNames editablePackageSources';
|
2022-01-05 08:06:47 +00:00
|
|
|
envPkgs = builtins.filter (drv: ! lib.elem (drv.pname or drv.name or "") editableAttrs) poetryPackages;
|
2020-05-24 00:59:50 +02:00
|
|
|
|
2020-04-29 14:12:59 +01:00
|
|
|
in
|
2022-01-05 08:06:47 +00:00
|
|
|
poetryPython.python.withPackages (ps: envPkgs ++ (extraPackages ps));
|
2019-12-16 11:23:09 +01:00
|
|
|
|
2020-04-30 00:55:42 +02:00
|
|
|
/* Creates a Python application from pyproject.toml and poetry.lock
|
|
|
|
|
2021-12-25 16:00:20 -08:00
|
|
|
The result also contains a .dependencyEnv attribute which is a python
|
|
|
|
environment of all dependencies and this apps modules. This is useful if
|
|
|
|
you rely on dependencies to invoke your modules for deployment: e.g. this
|
|
|
|
allows `gunicorn my-module:app`.
|
2020-04-30 00:55:42 +02:00
|
|
|
*/
|
2019-12-16 11:23:09 +01:00
|
|
|
mkPoetryApplication =
|
2020-02-26 16:35:42 +00:00
|
|
|
{ projectDir ? null
|
2022-01-01 22:38:02 -08:00
|
|
|
, src ? (
|
|
|
|
# Assume that a project which is the result of a derivation is already adequately filtered
|
|
|
|
if lib.isDerivation projectDir then projectDir else self.cleanPythonSources { src = projectDir; }
|
|
|
|
)
|
2020-02-25 14:40:36 +00:00
|
|
|
, pyproject ? projectDir + "/pyproject.toml"
|
|
|
|
, poetrylock ? projectDir + "/poetry.lock"
|
2020-07-21 20:45:03 +02:00
|
|
|
, overrides ? self.defaultPoetryOverrides
|
2020-04-29 14:12:59 +01:00
|
|
|
, meta ? { }
|
2019-12-16 11:23:09 +01:00
|
|
|
, python ? pkgs.python3
|
2020-02-25 14:40:36 +00:00
|
|
|
, pwd ? projectDir
|
2020-04-21 16:49:01 +02:00
|
|
|
, preferWheels ? false
|
2022-09-02 14:58:48 +12:00
|
|
|
, groups ? [ ]
|
2022-10-09 07:11:17 -04:00
|
|
|
, checkGroups ? [ "dev" ]
|
2019-12-16 11:23:09 +01:00
|
|
|
, ...
|
2020-03-14 23:13:51 +00:00
|
|
|
}@attrs:
|
2020-04-29 14:12:59 +01:00
|
|
|
let
|
2020-07-21 19:26:09 +02:00
|
|
|
poetryPython = self.mkPoetryPackages {
|
2022-10-09 07:11:17 -04:00
|
|
|
inherit pyproject poetrylock overrides python pwd preferWheels groups checkGroups;
|
2020-04-29 14:12:59 +01:00
|
|
|
};
|
|
|
|
py = poetryPython.python;
|
|
|
|
|
2022-10-28 00:40:18 +03:00
|
|
|
hooks = py.pkgs.callPackage ./hooks { };
|
|
|
|
|
2020-04-29 14:12:59 +01:00
|
|
|
inherit (poetryPython) pyProject;
|
|
|
|
specialAttrs = [
|
|
|
|
"overrides"
|
|
|
|
"poetrylock"
|
|
|
|
"projectDir"
|
|
|
|
"pwd"
|
|
|
|
"pyproject"
|
|
|
|
"preferWheels"
|
|
|
|
];
|
|
|
|
passedAttrs = builtins.removeAttrs attrs specialAttrs;
|
|
|
|
|
2022-10-09 07:11:17 -04:00
|
|
|
inputAttrs = mkInputAttrs { inherit py pyProject attrs groups checkGroups; };
|
2020-10-01 20:37:45 +02:00
|
|
|
|
2020-05-19 21:06:02 +01:00
|
|
|
app = py.pkgs.buildPythonPackage (
|
2020-10-01 20:37:45 +02:00
|
|
|
passedAttrs // inputAttrs // {
|
2021-11-07 09:50:59 -05:00
|
|
|
nativeBuildInputs = inputAttrs.nativeBuildInputs ++ [
|
2022-10-28 00:40:18 +03:00
|
|
|
hooks.removePathDependenciesHook
|
|
|
|
hooks.removeGitDependenciesHook
|
2021-11-07 09:50:59 -05:00
|
|
|
];
|
2020-10-01 20:37:45 +02:00
|
|
|
} // {
|
2022-09-29 20:39:37 +03:00
|
|
|
pname = normalizePackageName pyProject.tool.poetry.name;
|
2020-05-19 21:06:02 +01:00
|
|
|
version = pyProject.tool.poetry.version;
|
2020-04-30 00:55:42 +02:00
|
|
|
|
2020-05-19 21:06:02 +01:00
|
|
|
inherit src;
|
2020-04-30 00:55:42 +02:00
|
|
|
|
2020-05-19 21:06:02 +01:00
|
|
|
format = "pyproject";
|
|
|
|
# Like buildPythonApplication, but without the toPythonModule part
|
|
|
|
# Meaning this ends up looking like an application but it also
|
|
|
|
# provides python modules
|
|
|
|
namePrefix = "";
|
2020-04-30 00:55:42 +02:00
|
|
|
|
2020-05-19 21:06:02 +01:00
|
|
|
passthru = {
|
|
|
|
python = py;
|
|
|
|
dependencyEnv = (
|
|
|
|
lib.makeOverridable ({ app, ... }@attrs:
|
|
|
|
let
|
|
|
|
args = builtins.removeAttrs attrs [ "app" ] // {
|
|
|
|
extraLibs = [ app ];
|
|
|
|
};
|
|
|
|
in
|
2020-05-19 21:11:52 +01:00
|
|
|
py.buildEnv.override args)
|
|
|
|
) { inherit app; };
|
2020-05-19 21:06:02 +01:00
|
|
|
};
|
2020-04-30 00:55:42 +02:00
|
|
|
|
2021-09-29 21:21:02 -05:00
|
|
|
# Extract position from explicitly passed attrs so meta.position won't point to poetry2nix internals
|
|
|
|
pos = builtins.unsafeGetAttrPos (lib.elemAt (lib.attrNames attrs) 0) attrs;
|
|
|
|
|
2020-10-01 19:08:57 +02:00
|
|
|
meta = lib.optionalAttrs (lib.hasAttr "description" pyProject.tool.poetry)
|
|
|
|
{
|
|
|
|
inherit (pyProject.tool.poetry) description;
|
|
|
|
} // lib.optionalAttrs (lib.hasAttr "homepage" pyProject.tool.poetry) {
|
2020-05-20 15:26:22 +01:00
|
|
|
inherit (pyProject.tool.poetry) homepage;
|
|
|
|
} // {
|
2020-05-19 21:06:02 +01:00
|
|
|
inherit (py.meta) platforms;
|
|
|
|
license = getLicenseBySpdxId (pyProject.tool.poetry.license or "unknown");
|
2020-05-20 15:27:17 +01:00
|
|
|
} // meta;
|
2020-04-29 14:12:59 +01:00
|
|
|
|
2020-05-19 21:06:02 +01:00
|
|
|
}
|
|
|
|
);
|
2020-04-30 00:55:42 +02:00
|
|
|
in
|
|
|
|
app;
|
2019-12-28 18:49:11 +00:00
|
|
|
|
2020-01-01 17:49:12 +00:00
|
|
|
/* Poetry2nix CLI used to supplement SHA-256 hashes for git dependencies */
|
2020-07-21 19:26:09 +02:00
|
|
|
cli = import ./cli.nix {
|
|
|
|
inherit pkgs lib;
|
|
|
|
inherit (self) version;
|
|
|
|
};
|
|
|
|
|
|
|
|
# inherit mkPoetryEnv mkPoetryApplication mkPoetryPackages;
|
2020-01-02 21:56:08 +00:00
|
|
|
|
2020-02-26 18:18:56 +00:00
|
|
|
inherit (poetryLib) cleanPythonSources;
|
|
|
|
|
2020-07-21 20:45:03 +02:00
|
|
|
|
|
|
|
/*
|
2021-12-25 16:00:20 -08:00
|
|
|
Create a new default set of overrides with the same structure as the built-in ones
|
2020-07-21 20:45:03 +02:00
|
|
|
*/
|
|
|
|
mkDefaultPoetryOverrides = defaults: {
|
|
|
|
__functor = defaults;
|
|
|
|
|
|
|
|
extend = overlay:
|
|
|
|
let
|
|
|
|
composed = lib.foldr lib.composeExtensions overlay [ defaults ];
|
|
|
|
in
|
|
|
|
self.mkDefaultPoetryOverrides composed;
|
|
|
|
|
2020-07-21 20:50:07 +02:00
|
|
|
overrideOverlay = fn:
|
2020-07-21 20:45:03 +02:00
|
|
|
let
|
2020-07-21 20:50:07 +02:00
|
|
|
overlay = self: super:
|
|
|
|
let
|
|
|
|
defaultSet = defaults self super;
|
|
|
|
customSet = fn self super;
|
|
|
|
in
|
|
|
|
defaultSet // customSet;
|
2020-07-21 20:45:03 +02:00
|
|
|
in
|
2020-07-21 20:50:07 +02:00
|
|
|
self.mkDefaultPoetryOverrides overlay;
|
2020-07-21 20:45:03 +02:00
|
|
|
};
|
|
|
|
|
2020-01-02 21:56:08 +00:00
|
|
|
/*
|
2021-12-25 16:00:20 -08:00
|
|
|
The default list of poetry2nix override overlays
|
2020-01-02 21:56:08 +00:00
|
|
|
|
2021-12-25 16:00:20 -08:00
|
|
|
Can be overriden by calling defaultPoetryOverrides.overrideOverlay which takes an overlay function
|
2020-01-02 21:56:08 +00:00
|
|
|
*/
|
2022-09-29 20:39:37 +03:00
|
|
|
defaultPoetryOverrides = self.mkDefaultPoetryOverrides (import ./overrides { inherit pkgs lib poetryLib; });
|
2020-02-25 14:24:48 +01:00
|
|
|
|
|
|
|
/*
|
2021-12-25 16:00:20 -08:00
|
|
|
Convenience functions for specifying overlays with or without the poerty2nix default overrides
|
2020-02-25 14:24:48 +01:00
|
|
|
*/
|
|
|
|
overrides = {
|
|
|
|
/*
|
2021-12-25 16:00:20 -08:00
|
|
|
Returns the specified overlay in a list
|
2020-02-25 14:24:48 +01:00
|
|
|
*/
|
|
|
|
withoutDefaults = overlay: [
|
|
|
|
overlay
|
|
|
|
];
|
|
|
|
|
|
|
|
/*
|
2021-12-25 16:00:20 -08:00
|
|
|
Returns the specified overlay and returns a list
|
|
|
|
combining it with poetry2nix default overrides
|
2020-02-25 14:24:48 +01:00
|
|
|
*/
|
|
|
|
withDefaults = overlay: [
|
2020-07-21 19:29:37 +02:00
|
|
|
self.defaultPoetryOverrides
|
2020-02-25 14:24:48 +01:00
|
|
|
overlay
|
|
|
|
];
|
|
|
|
};
|
2020-07-21 19:26:09 +02:00
|
|
|
})
|