summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJairo Llopis <yajo.sk8@gmail.com>2023-05-02 12:08:34 +0100
committerMartin Weinelt <hexa@darmstadt.ccc.de>2023-12-20 20:11:17 +0100
commit4c1f249333bce383f24ee41ab8aa12da970872cd (patch)
treed4008babc23a0924d6b0a0fe6aee2d22f43608f2
parent6108e5bd7a7239c48f099a0ca936170c2de29a9e (diff)
setuptools-scm: add setup hook for version and sources inclusion
Packages using this build-time dependency expect to be able to know their version, which is autogenerated by extracting metadata from the SCM. For example, from Git tags. Also they assume that all SCM-tracked files are included in sdist and bdist, as documented here: - https://setuptools-scm.readthedocs.io/en/latest/usage/#file-finders-hook-makes-most-of-manifestin-unnecessary - https://setuptools.pypa.io/en/latest/userguide/datafiles.html None of these behaviors were happening while on nix builds, where SCM data is stripped off from sources by default. These build hooks should put nix builds closer to normal ones with Python's `build` app. A similar fix was used in https://github.com/nix-community/poetry2nix/pull/1077 (for the version problem only) and has given good results so far. @moduon MT-1075
-rw-r--r--pkgs/development/python-modules/setuptools-scm/default.nix2
-rw-r--r--pkgs/development/python-modules/setuptools-scm/setup-hook.sh32
2 files changed, 34 insertions, 0 deletions
diff --git a/pkgs/development/python-modules/setuptools-scm/default.nix b/pkgs/development/python-modules/setuptools-scm/default.nix
index 21fe5c77fbb7..01a1f0ff0d4b 100644
--- a/pkgs/development/python-modules/setuptools-scm/default.nix
+++ b/pkgs/development/python-modules/setuptools-scm/default.nix
@@ -57,6 +57,8 @@ buildPythonPackage rec {
pytest = callPackage ./tests.nix { };
};
+ setupHook = ./setup-hook.sh;
+
meta = with lib; {
changelog = "https://github.com/pypa/setuptools_scm/blob/${version}/CHANGELOG.md";
homepage = "https://github.com/pypa/setuptools_scm/";
diff --git a/pkgs/development/python-modules/setuptools-scm/setup-hook.sh b/pkgs/development/python-modules/setuptools-scm/setup-hook.sh
new file mode 100644
index 000000000000..f8c30f91ca07
--- /dev/null
+++ b/pkgs/development/python-modules/setuptools-scm/setup-hook.sh
@@ -0,0 +1,32 @@
+# Let built package know its version.
+# Usually, when a package uses setuptools-scm as a build-time dependency, it
+# expects to get the package version from SCM data. However, while doing a nix
+# build, the source tree doesn't contain SCM data, so we should almost always
+# get the version from the derivation attribute.
+version-pretend-hook() {
+ if [ -z "$dontPretendSetuptoolsSCMVersion" -a -z "$SETUPTOOLS_SCM_PRETEND_VERSION" ]; then
+ echo Setting SETUPTOOLS_SCM_PRETEND_VERSION to $version
+ export SETUPTOOLS_SCM_PRETEND_VERSION="$version"
+ fi
+}
+
+# Include all tracked files.
+# When a package uses setuptools-scm as a build-time dependency, it usually
+# expects it to include all scm-tracked files in the built package, by default.
+# This is the official setuptools-scm behavior, documented in
+# https://setuptools-scm.readthedocs.io/en/latest/usage/#file-finders-hook-makes-most-of-manifestin-unnecessary
+# and https://setuptools.pypa.io/en/latest/userguide/datafiles.html.
+# However, while doing a nix build, the source tree doesn't contain SCM data,
+# so it would include only `.py` files by default.
+# We generate a MANIFEST.in automatically that includes all tracked files to
+# emulate this behavior of setuptools-scm.
+include-tracked-files-hook() {
+ if [ -z "$dontIncludeSetuptoolsSCMTrackedFiles" ]; then
+ echo Including all tracked files automatically
+ old_manifest="$(if [ -f MANIFEST.in ]; then cat MANIFEST.in; fi)"
+ echo 'global-include **' > MANIFEST.in
+ echo "$old_manifest" >> MANIFEST.in
+ fi
+}
+
+preBuildHooks+=(version-pretend-hook include-tracked-files-hook)