From bce0bb82a075e59e42090e0810670f065a2f8c91 Mon Sep 17 00:00:00 2001 From: DavHau Date: Tue, 17 Jun 2025 21:10:47 +0700 Subject: [PATCH] python3Minimal: refactor - add withMinimal flag simplifies the interface of building cpython minimally --- .../interpreters/python/cpython/default.nix | 69 +++++++++++-------- .../interpreters/python/default.nix | 32 +-------- 2 files changed, 43 insertions(+), 58 deletions(-) diff --git a/pkgs/development/interpreters/python/cpython/default.nix b/pkgs/development/interpreters/python/cpython/default.nix index ec6d55011bad..e3874a4b8d42 100644 --- a/pkgs/development/interpreters/python/cpython/default.nix +++ b/pkgs/development/interpreters/python/cpython/default.nix @@ -12,17 +12,23 @@ pkg-config, python-setup-hook, + # high level switches + withMinimalDeps ? false, + # runtime dependencies bzip2, - withExpat ? true, + withExpat ? !withMinimalDeps, expat, libffi, libuuid, + withLibxcrypt ? !withMinimalDeps, libxcrypt, - withMpdecimal ? true, + withMpdecimal ? !withMinimalDeps, mpdecimal, ncurses, + withOpenssl ? !withMinimalDeps, openssl, + withSqlite ? !withMinimalDeps, sqlite, xz, zlib, @@ -35,12 +41,12 @@ # optional dependencies bluezSupport ? false, bluez, - mimetypesSupport ? true, + mimetypesSupport ? !withMinimalDeps, mailcap, tzdata, - withGdbm ? !stdenv.hostPlatform.isWindows, + withGdbm ? !withMinimalDeps && !stdenv.hostPlatform.isWindows, gdbm, - withReadline ? !stdenv.hostPlatform.isWindows, + withReadline ? !withMinimalDeps && !stdenv.hostPlatform.isWindows, readline, x11Support ? false, tcl, @@ -63,13 +69,13 @@ sourceVersion, hash, passthruFun, - stripConfig ? false, - stripIdlelib ? false, - stripTests ? false, - stripTkinter ? false, - rebuildBytecode ? true, + stripConfig ? withMinimalDeps, + stripIdlelib ? withMinimalDeps, + stripTests ? withMinimalDeps, + stripTkinter ? withMinimalDeps, + rebuildBytecode ? !withMinimalDeps, stripBytecode ? true, - includeSiteCustomize ? true, + includeSiteCustomize ? !withMinimalDeps, static ? stdenv.hostPlatform.isStatic, enableFramework ? false, noldconfigPatch ? ./. + "/${sourceVersion.major}.${sourceVersion.minor}/no-ldconfig.patch", @@ -85,7 +91,8 @@ # enabling LTO on 32bit arch causes downstream packages to fail when linking enableLTO ? - stdenv.hostPlatform.isDarwin || (stdenv.hostPlatform.is64bit && stdenv.hostPlatform.isLinux), + !withMinimalDeps + && (stdenv.hostPlatform.isDarwin || (stdenv.hostPlatform.is64bit && stdenv.hostPlatform.isLinux)), # enable asserts to ensure the build remains reproducible reproducibleBuild ? false, @@ -97,7 +104,9 @@ testers, # allow pythonMinimal to prevent accidental dependencies it doesn't want - allowedReferenceNames ? [ ], + # Having this as an option is useful to allow overriding, eg. adding things to + # python3Minimal + allowedReferenceNames ? if withMinimalDeps then [ "bashNonInteractive" ] else [ ], }@inputs: @@ -140,12 +149,12 @@ let ; # mixes libc and libxcrypt headers and libs and causes segfaults on importing crypt - libxcrypt = if stdenv.hostPlatform.isFreeBSD then null else inputs.libxcrypt; + libxcrypt = if stdenv.hostPlatform.isFreeBSD && withMinimalDeps then null else inputs.libxcrypt; buildPackages = pkgsBuildHost; inherit (passthru) pythonOnBuildForHost; - tzdataSupport = tzdata != null && passthru.pythonAtLeast "3.9"; + tzdataSupport = !withMinimalDeps && tzdata != null && passthru.pythonAtLeast "3.9"; passthru = let @@ -223,19 +232,22 @@ let ]; buildInputs = lib.filter (p: p != null) ( - [ + optionals (!withMinimalDeps) [ bzip2 libffi libuuid - libxcrypt ncurses - openssl - sqlite xz zlib ] - ++ optionals (passthru.pythonAtLeast "3.14") [ - zstd + ++ optionals withLibxcrypt [ + libxcrypt + ] + ++ optionals withOpenssl [ + openssl + ] + ++ optionals withSqlite [ + sqlite ] ++ optionals withMpdecimal [ mpdecimal @@ -243,6 +255,9 @@ let ++ optionals withExpat [ expat ] + ++ optionals (passthru.pythonAtLeast "3.14") [ + zstd + ] ++ optionals bluezSupport [ bluez ] @@ -435,7 +450,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 && libxcrypt != null) "-lcrypt"}"; + LIBS = "${optionalString (!stdenv.hostPlatform.isDarwin && withLibxcrypt) "-lcrypt"}"; NIX_LDFLAGS = lib.optionalString (stdenv.cc.isGNU && !stdenv.hostPlatform.isStatic) ( { "glibc" = "-lgcc_s"; @@ -457,7 +472,7 @@ stdenv.mkDerivation (finalAttrs: { ++ optionals withMpdecimal [ "--with-system-libmpdec" ] - ++ optionals (openssl != null) [ + ++ optionals withOpenssl [ "--with-openssl=${openssl.dev}" ] ++ optionals tzdataSupport [ @@ -484,10 +499,10 @@ stdenv.mkDerivation (finalAttrs: { ++ optionals enableDebug [ "--with-pydebug" ] - ++ optionals (sqlite != null) [ + ++ optionals withSqlite [ "--enable-loadable-sqlite-extensions" ] - ++ optionals (libxcrypt != null) [ + ++ optionals withLibxcrypt [ "CFLAGS=-I${libxcrypt}/include" "LIBS=-L${libxcrypt}/lib" ] @@ -592,7 +607,7 @@ stdenv.mkDerivation (finalAttrs: { [ (placeholder "out") ] - ++ lib.optional (libxcrypt != null) libxcrypt + ++ lib.optional withLibxcrypt libxcrypt ++ lib.optional tzdataSupport tzdata ); in @@ -758,7 +773,7 @@ stdenv.mkDerivation (finalAttrs: { # Enforce that we don't have references to the OpenSSL -dev package, which we # explicitly specify in our configure flags above. disallowedReferences = - lib.optionals (openssl != null && !static && !enableFramework) [ + lib.optionals (withOpenssl && !static && !enableFramework) [ openssl.dev ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ diff --git a/pkgs/development/interpreters/python/default.nix b/pkgs/development/interpreters/python/default.nix index 322de044a111..9679b39a2890 100644 --- a/pkgs/development/interpreters/python/default.nix +++ b/pkgs/development/interpreters/python/default.nix @@ -105,37 +105,7 @@ inherit passthruFun; pythonAttr = "python3Minimal"; # strip down that python version as much as possible - openssl = null; - readline = null; - ncurses = null; - gdbm = null; - sqlite = null; - tzdata = null; - libuuid = null; - bzip2 = null; - libxcrypt = null; - xz = null; - zlib = null; - libffi = null; - stripConfig = true; - stripIdlelib = true; - stripTests = true; - stripTkinter = true; - rebuildBytecode = false; - stripBytecode = true; - includeSiteCustomize = false; - enableOptimizations = false; - enableLTO = false; - mimetypesSupport = false; - withExpat = false; - withMpdecimal = false; - /* - The actual 'allowedReferences' attribute is set inside the cpython derivation. - This is necessary in order to survive overrides of dependencies. - */ - allowedReferenceNames = [ - "bashNonInteractive" - ]; + withMinimalDeps = true; } // sources.python313 )).overrideAttrs