From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 6GsSCCXRCmDPEgAA0tVLHw (envelope-from ) for ; Fri, 22 Jan 2021 13:20:37 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id uNvvAyXRCmD6XgAA1q6Kng (envelope-from ) for ; Fri, 22 Jan 2021 13:20:37 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 433F9940599 for ; Fri, 22 Jan 2021 13:20:36 +0000 (UTC) Received: from localhost ([::1]:56750 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l2wMd-000766-2G for larch@yhetil.org; Fri, 22 Jan 2021 08:20:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l2wM6-00075m-Ko for guix-patches@gnu.org; Fri, 22 Jan 2021 08:20:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:48644) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l2wM6-0000Ts-4v for guix-patches@gnu.org; Fri, 22 Jan 2021 08:20:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l2wM5-0002SX-Vz for guix-patches@gnu.org; Fri, 22 Jan 2021 08:20:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#46028] [PATCH core-updates v2] gnu: python: Replace PYTHONPATH by GUIX_PYTHONPATH_X_Y. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 22 Jan 2021 13:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 46028 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 46028@debbugs.gnu.org Received: via spool by 46028-submit@debbugs.gnu.org id=B46028.16113215639388 (code B ref 46028); Fri, 22 Jan 2021 13:20:01 +0000 Received: (at 46028) by debbugs.gnu.org; 22 Jan 2021 13:19:23 +0000 Received: from localhost ([127.0.0.1]:60189 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l2wLS-0002RM-Ge for submit@debbugs.gnu.org; Fri, 22 Jan 2021 08:19:22 -0500 Received: from mail-qv1-f43.google.com ([209.85.219.43]:36724) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l2wLP-0002R6-K9 for 46028@debbugs.gnu.org; Fri, 22 Jan 2021 08:19:21 -0500 Received: by mail-qv1-f43.google.com with SMTP id j18so2593647qvu.3 for <46028@debbugs.gnu.org>; Fri, 22 Jan 2021 05:19:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+7HUvB3DghWtR8jpiJZY+mvU+2vvErPx90cn1tQlCxQ=; b=IdvHrPGRzcUyaJZpTGrbnVdPED6v/OTTNM9v+ASeS+bmp4ggjbVw0dtLqGWHy6Ki7v 0KUjCDg6Iw0T4wG4xHrF14PaT25yiRNxzo6NomwJaPIhPYWkCSvl01sHvrOgfpaOZIG2 UACPDkAE0iSoclWX/YgsomxesVnDh/bEBNYUnChlfCb4alnL3b8cHeg6mXv+B5Vrk81h fIxMeIUWDkwbzQbsQwCUr+/Xmz91vIIK6kvD2b7r4mYPRg+DpBdaQfpOa9JlxMMhky9j zA4uQWcs6S/km/GtPMpd67jdgS6k80RObsKDT04/Q8KOSOTqJM++7/kNF0r4d7jQ0Ps6 dtrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+7HUvB3DghWtR8jpiJZY+mvU+2vvErPx90cn1tQlCxQ=; b=cYHe7MJojWEOBr3TcVcCve5msUriSqXJWaeT70970+LwjCRHV6PE5zc9cCD8UEyTNO KHE5QcQ4z+QxyuZaIMFxZfQijkE22JVgpSygg9CiWiQ9xkvnyvdhAD/VTL15i0JPxIw3 cLj2JPGnpArq+QEBSdE5N1SQ6wioDh/S8R+OwEwnDeJ9fULIx2kaXZzV9VrdbKKrafn9 mo+gFdADpvETQRqEMomLs6Hwi0/cd0JprhHhPweDJbLZG0uDJQuNu2VRXvQZ+vEpyIgE RCuyqlq3O22rBksBqRDvH83FvfKrYtc8jLN0QNS7O3+H4pGM8GpgmMOCDhKlx0Ku7SjN Gp8Q== X-Gm-Message-State: AOAM5323Rar6WyrFsvK/eHvTG5nyT+KoDkAiiu8FtsSvwndsMk+sPObW 6wJkf13lBJ4dlCEVCI3LomsMStU4Bw8= X-Google-Smtp-Source: ABdhPJyMkMhvT0M6ir48TPFiNJCKd78gU/sBWNTEeUI+3jeOvBn414EdeeN1PYjVPqE8tUwyYL/+LQ== X-Received: by 2002:ad4:452f:: with SMTP id l15mr4335148qvu.49.1611321553675; Fri, 22 Jan 2021 05:19:13 -0800 (PST) Received: from localhost.localdomain (dsl-152-143.b2b2c.ca. [66.158.152.143]) by smtp.gmail.com with ESMTPSA id u65sm6440485qkb.58.2021.01.22.05.19.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 05:19:12 -0800 (PST) From: Maxim Cournoyer Date: Fri, 22 Jan 2021 08:18:58 -0500 Message-Id: <20210122131858.11206-1-maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210122045104.30229-2-maxim.cournoyer@gmail.com> References: <20210122045104.30229-2-maxim.cournoyer@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: 2.75 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20161025 header.b=IdvHrPGR; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 433F9940599 X-Spam-Score: 2.75 X-Migadu-Scanner: scn1.migadu.com X-TUID: +wrKdRfFhBp8 Using PYTHONPATH as a mean to discover the Python packages had the following issues: 1. It is not versioned, so different versions of Python would clash if installed in a shared profile. 2. It would interfere with the host Python site on foreign distributions, sometimes preventing a a user to login their GDM session (!). 3. It would take precedence over user installed Python packages installed through pip. 4. It would leak into Python virtualenvs, which are supposed to create isolated Python environments. This changes fixes the above issues by making use of a sitecustomize.py module. The newly introduced GUIX_PYTHONPATH_X_Y environment variable, where X is the major and Y the minor version numbers, is read from the environment and spliced in 'sys.path' just before Python's own site location, which provides the expected behavior. * gnu/packages/python.scm (customize-site) (guix-pythonpath-search-path): New procedures. * gnu/packages/python.scm (python-2.7)[phases]{customize-site}: New phase. [native-search-paths]: Adjust. * gnu/packages/python.scm (python-3.9)[native-search-paths]: Likewise. [phases]{customize-site}: Override the inherited phase, so that it uses the correct version. * gnu/packages/commencement.scm (python-boot0) [phases]{customize-site}: Likewise. [native-search-paths]: Override the inherited one. --- gnu/packages/commencement.scm | 4 ++- gnu/packages/python.scm | 57 +++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/gnu/packages/commencement.scm b/gnu/packages/commencement.scm index 0713bcb843..6d7593e223 100644 --- a/gnu/packages/commencement.scm +++ b/gnu/packages/commencement.scm @@ -3176,6 +3176,7 @@ memoized as a function of '%current-system'." ;; built, since it requires Linux headers. (("'linux', ") "")))) (delete 'set-TZDIR) + (replace 'customize-site ,(customize-site version)) ,@(if (hurd-system?) `((add-before 'build 'fix-regen (lambda* (#:key inputs #:allow-other-keys) @@ -3184,7 +3185,8 @@ memoized as a function of '%current-system'." (("/usr/include/") (string-append libc "/include/"))))))) '()))) - ((#:tests? _ #f) #f)))))) + ((#:tests? _ #f) #f)))) + (native-search-paths (list (guix-pythonpath-search-path version))))) (define/system-dependent ld-wrapper-boot0 ;; The first 'ld' wrapper, defined with 'define/system-dependent' because diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm index d3df1a4dab..c3453d63a9 100644 --- a/gnu/packages/python.scm +++ b/gnu/packages/python.scm @@ -102,7 +102,44 @@ #:use-module (guix build-system gnu) #:use-module (guix build-system trivial) #:use-module (srfi srfi-1) - #:use-module (srfi srfi-26)) + #:use-module (srfi srfi-26) + #:export (customize-site + guix-pythonpath-search-path)) + +(define* (customize-site version) + "Generate a sitecustomize.py phase, using VERSION." + `(lambda* (#:key outputs #:allow-other-keys) + (let* ((out (assoc-ref outputs "out")) + (site-packages (string-append + out "/lib/python" + ,(version-major+minor version) + "/site-packages")) + (pythonpath (format #f "GUIX_PYTHONPATH_~a_~a" + ,(version-major version) + ,(version-minor version))) + (sitecustomize.py (string-append site-packages + "/sitecustomize.py"))) + (with-output-to-file sitecustomize.py + (lambda _ + (format #t "\ +import os +import sys + +# Insert the GUIX_PYTHONPATH_X_Y values just before Python's own site. +sys_path_absolute = [os.path.realpath(p) for p in sys.path] +index = sys_path_absolute.index(~s) +sys.path = sys.path[:index] + os.environ[~s].split(':') + sys.path[index:] +" site-packages pythonpath)))))) + +(define (guix-pythonpath-search-path version) + "Generate a GUIX_PYTHONPATH_X_Y search path specification, using VERSION." + (search-path-specification + (variable (format #f "GUIX_PYTHONPATH_~a_~a" + (version-major version) + (version-minor version))) + (files (list (string-append "lib/python" + (version-major+minor version) + "/site-packages"))))) (define-public python-2.7 (package @@ -313,7 +350,9 @@ "/site-packages"))) (install-file tkinter.so target) (delete-file tkinter.so))))) - #t)))))) + #t))) + (add-after 'install 'customize-site + ,(customize-site version))))) (inputs `(("bzip2" ,bzip2) ("expat" ,expat) @@ -332,10 +371,7 @@ `(("python2" ,this-package) ("which" ,which)) '()))) - (native-search-paths - (list (search-path-specification - (variable "PYTHONPATH") - (files '("lib/python2.7/site-packages"))))) + (native-search-paths (list (guix-pythonpath-search-path version))) (home-page "https://www.python.org") (synopsis "High-level, dynamically-typed programming language") (description @@ -463,7 +499,8 @@ data types.") "-x" "lib2to3/.*" ,out)))) (list "none" "-O" "-OO")) - #t))))))) + #t))) + (replace 'customize-site ,(customize-site version)))))) (native-inputs `(("tzdata" ,tzdata-for-tests) ,@(if (%current-target-system) @@ -471,11 +508,7 @@ data types.") '()) ,@(package-native-inputs python-2))) (native-search-paths - (list (search-path-specification - (variable "PYTHONPATH") - (files (list (string-append "lib/python" - (version-major+minor version) - "/site-packages")))) + (list (guix-pythonpath-search-path version) ;; Used to locate tzdata by the zoneinfo module introduced in ;; Python 3.9. (search-path-specification -- 2.29.2