python3Minimal: make it truly minimal

Make python3 minimal truly minimal by disabling some more dependencies.

This makes it easy to bootstrap python.

After this, the only remaining runtime deps for python3Minimal are:
- bash (for launching subprocesses with shell=True)
- libc
- libgcc
- libffi

All remaining deps seem to be fundamental and cannot be further reduced, except bash which could potentially be removed and replaced with `$SHELL` at runtime.

Done:
- add some more withXXX switches to the cpython package
- use new switches in python3Minimal to disable some deps
- set some other deps to null in python3Minimal
- Set `allowedReferences` to guarantee that the closure remains minimal in the future.
This commit is contained in:
DavHau
2025-05-13 01:02:39 +07:00
parent b62e21c3c5
commit b634edb1a4
2 changed files with 43 additions and 3 deletions
@@ -14,10 +14,12 @@
# runtime dependencies
bzip2,
withExpat ? true,
expat,
libffi,
libuuid,
libxcrypt,
withMpdecimal ? true,
mpdecimal,
ncurses,
openssl,
@@ -92,6 +94,9 @@
# tests
testers,
# allow pythonMinimal to prevent accidental dependencies it doesn't want
allowedReferenceNames ? [ ],
}@inputs:
# Note: this package is used for bootstrapping fetchurl, and thus
@@ -216,17 +221,21 @@ let
buildInputs = lib.filter (p: p != null) (
[
bzip2
expat
libffi
libuuid
libxcrypt
mpdecimal
ncurses
openssl
sqlite
xz
zlib
]
++ optionals withMpdecimal [
mpdecimal
]
++ optionals withExpat [
expat
]
++ optionals bluezSupport [
bluez
]
@@ -420,7 +429,7 @@ stdenv.mkDerivation (finalAttrs: {
env = {
CPPFLAGS = concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs);
LDFLAGS = concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs);
LIBS = "${optionalString (!stdenv.hostPlatform.isDarwin) "-lcrypt"}";
LIBS = "${optionalString (!stdenv.hostPlatform.isDarwin && libxcrypt != null) "-lcrypt"}";
NIX_LDFLAGS = lib.optionalString (stdenv.cc.isGNU && !stdenv.hostPlatform.isStatic) (
{
"glibc" = "-lgcc_s";
@@ -436,7 +445,11 @@ stdenv.mkDerivation (finalAttrs: {
configureFlags =
[
"--without-ensurepip"
]
++ optionals withExpat [
"--with-system-expat"
]
++ optionals withMpdecimal [
"--with-system-libmpdec"
]
++ optionals (openssl != null) [
@@ -743,6 +756,19 @@ stdenv.mkDerivation (finalAttrs: {
buildPackages.bashNonInteractive
];
# Optionally set allowedReferences to guarantee minimal dependencies
# Allows python3Minimal to stay minimal and not have deps added by accident
# Doesn't do anything if allowedReferenceNames is empty (was not set)
${if allowedReferenceNames != [ ] then "allowedReferences" else null} =
# map allowed names to their derivations
(map (name: inputs.${name}) allowedReferenceNames) ++ [
# any version of python depends on libc and libgcc
stdenv.cc.cc.lib
stdenv.cc.libc
# allows python referring to its own store path
"out"
];
separateDebugInfo = true;
passthru = passthru // {