Am 22.02.2018 um 16:30 schrieb Ricardo Wurmus:
> (This wouldn’t help us much for wrapper scripts, though.)

Attached please fins an approach for solving this issue.

The basic idea is make the application/script use a python within a
virtual environment. I nee to rethink some details and check whether
this will work out.

See the comments in the code for how it is intended to work.

Hartmut Goebel

| Hartmut Goebel          |               |
| | compilers which you thought are impossible |

;;; Copyright © 2018 Hartmut Goebel <>

(use-modules (guix)
             (gnu packages python)
             (guix licenses))

;; TODO decide which path to use
(define VENV-DIR "/share/guix-venv/") ; trailing slash!

;; python-uniseg is a reasonable small package without any dependencies and
;; including a script
  (inherit python-uniseg)
  (name "my-python-uniseg")
  (version (package-version python-uniseg))
   `(#:tests? #f  ; copied from base package
     (modify-phases %standard-phases
       (replace 'wrap
         (lambda* (#:key inputs outputs #:allow-other-keys)
           ;; This sets up a small virtual environment for the package
           ;; and wraps the scripts to 
           ;; ../share/guix-venv/my-site-display-0.1
           ;; +- pyvenv.cfg
           ;; +- bin/python
           ;; +- lib/pythonX.Y/site-packages/NAME.pth
  (let* ((out  (assoc-ref outputs "out"))
         (venv (string-append out ,VENV-DIR ,name "-" ,version))
         (python (assoc-ref inputs "python"))
         (site-dir (string-append "/lib/python"
                                  "3.5" ;; FIXME (get-python-version python)
         (PYTHONPATH ;; FIXME: Why is ther only one entry?
          (cons (string-append out site-dir)
                  (or (getenv "PYTHONPATH") "")))))
    (mkdir-p (string-append venv "/bin"))
    (mkdir-p (string-append venv site-dir))
    ;; The existance of a pyvenv.cfg file marks this as being a virtual
    ;; environment
    (call-with-output-file (string-append venv "/pyvenv.cfg")
      (lambda (p)
        (format p "#include-system-site-packages = false")))
    ;; Link to all required packages using a .pth file
    (call-with-output-file (string-append venv site-dir "/" ,name ".pth")
      (lambda (p)
        (for-each (lambda (pkg)
                    (format p "~a~%" pkg))
    ;; Create the python "executable" within the virtual environment
     (lambda (name)
       (symlink (string-append python "/bin/" name)
                (string-append venv  "/bin/" name)))
     `("python" ,"python3"))
       ;; Re-wrap the scripts to use the python within the virtual env
       (add-after 'patch-shebangs 'patch-python-venv-shebang
         (lambda* (#:key outputs inputs #:allow-other-keys)
           (let* ((out (assoc-ref outputs "out"))
                  (venv (string-append out ,VENV-DIR ,name "-" ,version))
                  (python (assoc-ref inputs "python")))
         (define (venv-program file)
             (substitute* file
               (((string-append "#!" python "/bin/python([0-9](\\.[0-9])?)?"))
                (string-append "#!" venv "/bin/python"))))
         ;; FIXME: Use logic from python-build-system to find scripts
         (venv-program (string-append out "/bin/uniseg-dbpath"))
  ;; Some requirement to check if all required pathes are included into the
  ;; .pth-file.
   `(("req" ,python-simplejson)))

Reply via email to