You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.5 KiB

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018, 2019 Ludovic Courtès <>
;;; This file is part of GNU Guix.
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; GNU General Public License for more details.
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <>.
(define-module (guix scripts repl)
#:use-module (guix ui)
#:use-module (guix scripts)
#:use-module (guix repl)
#:use-module (guix utils)
#:use-module (guix packages)
#:use-module (gnu packages)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-37)
#:use-module (ice-9 match)
#:use-module (rnrs bytevectors)
#:autoload (system repl repl) (start-repl)
#:autoload (system repl server)
(make-tcp-server-socket make-unix-domain-server-socket)
#:export (guix-repl))
;;; Commentary:
;;; This command provides a Guile REPL
(define %default-options
`((type . guile)))
(define %options
(list (option '(#\h "help") #f #f
(lambda args
(exit 0)))
(option '(#\V "version") #f #f
(lambda args
(show-version-and-exit "guix repl")))
(option '(#\t "type") #t #f
(lambda (opt name arg result)
(alist-cons 'type (string->symbol arg) result)))
(option '("listen") #t #f
(lambda (opt name arg result)
(alist-cons 'listen arg result)))))
(define (show-help)
(display (G_ "Usage: guix repl [OPTIONS...]
Start a Guile REPL in the Guix execution environment.\n"))
(display (G_ "
-t, --type=TYPE start a REPL of the given TYPE"))
(display (G_ "
-h, --help display this help and exit"))
(display (G_ "
-V, --version display version information and exit"))
(define user-module
;; Module where we execute user code.
(let ((module (resolve-module '(guix-user) #f #f #:ensure #t)))
(beautify-user-module! module)
(define (call-with-connection spec thunk)
"Dynamically-bind the current input and output ports according to SPEC and
call THUNK."
(if (not spec)
;; Note: the "PROTO:" prefix in SPEC is here so that we can eventually
;; parse things like "fd:123" in a non-ambiguous way.
(match (string-index spec #\:)
(leave (G_ "~A: invalid listen specification~%") spec))
(let ((protocol (string-take spec index))
(address (string-drop spec (+ index 1))))
(define socket
(match protocol
(make-tcp-server-socket #:port (string->number address)))
(make-unix-domain-server-socket #:path address))
(leave (G_ "~A: unsupported protocol family~%")
(listen socket 10)
(let loop ()
(match (accept socket)
((connection . address)
(if (= AF_UNIX (sockaddr:fam address))
(info (G_ "accepted connection~%"))
(info (G_ "accepted connection from ~a~%")
(inet-ntop (sockaddr:fam address)
(sockaddr:addr address))))
(const #t)
(lambda ()
(parameterize ((current-input-port connection)
(current-output-port connection))
(lambda ()
(false-if-exception (close-port connection))
(info (G_ "connection closed~%"))))))
(define (guix-repl . args)
(define opts
;; Return the list of package names.
(args-fold* args %options
(lambda (opt name arg result)
(leave (G_ "~A: unrecognized option~%") name))
(lambda (arg result)
(leave (G_ "~A: extraneous argument~%") arg))
(let ((type (assoc-ref opts 'type)))
(call-with-connection (assoc-ref opts 'listen)
(lambda ()
(case type
(lambda ()
(set-current-module user-module)
(and=> (getenv "HOME")
(lambda (home)
(let ((guile (string-append home "/.guile")))
(when (file-exists? guile)
(load guile)))))
;; Do not exit repl on SIGINT.
((@@ (ice-9 top-repl) call-with-sigint)
(lambda ()
(leave (G_ "~a: unknown type of REPL~%") type))))))))
;; Local Variables:
;; eval: (put 'call-with-connection 'scheme-indent-function 1)
;; End: