| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 19 | (define-module (gnu services jupyter)
|
| 20 | #:use-module (gnu packages guile)
|
| 21 | #:use-module (gnu packages jupyter)
|
| 22 | #:use-module (gnu services)
|
| 23 | #:use-module (gnu services configuration)
|
| 24 | #:use-module (gnu services shepherd)
|
| 25 | #:use-module (guix gexp)
|
| 26 | #:use-module (guix packages)
|
| 27 | #:use-module (guix records)
|
| 28 | #:use-module (ice-9 match)
|
| 29 | #:export (jupyter-service-type
|
| 30 | jupyter-shepherd-service
|
| 31 | jupyter-configuration))
|
| 32 |
|
| 33 | (define list-of-file-likes? (list-of file-like?))
|
| 34 |
|
| 35 | (define-configuration/no-serialization jupyter-configuration
|
| 36 | (python-jupyter-core
|
| 37 | (file-like python-jupyter-core)
|
| 38 | "Package providing the @command{jupyter} binary.")
|
| 39 | (extensions
|
| 40 | (list-of-file-likes (list))
|
| 41 | "List of packages providing server extensions to Jupyter.")
|
| 42 | (kernels
|
| 43 | (list-of-file-likes (list))
|
| 44 | "Kernel packages to add to the profile, e.g. for python @code{python-ipykernel}.")
|
| 45 | (disable-check-xsrf?
|
| 46 | (boolean #f)
|
| 47 | "Whether to disable the XSRF check.")
|
| 48 | (token
|
| 49 | (string "MY_TOKEN")
|
| 50 | "Authentication token for the Jupyter server."))
|
| 51 |
|
| 52 | (define (jupyter-profile-service config)
|
| 53 | (match-record config <jupyter-configuration>
|
| 54 | (python-jupyter-core extensions kernels)
|
| 55 | (append (list python-jupyter-core) extensions kernels)))
|
| 56 |
|
| 57 | (define (jupyter-server-runtime-config-getter-procedure python-jupyter-core
|
| 58 | field)
|
| 59 | (with-extensions (list guile-json-4)
|
| 60 | #~(begin
|
| 61 | (use-modules (json))
|
| 62 | (lambda (process . args)
|
| 63 | (let* ((pid (process-id process))
|
| 64 | (port
|
| 65 | ((@ (ice-9 popen) open-input-pipe)
|
| 66 | (string-append #$(file-append python-jupyter-core
|
| 67 | "/bin/jupyter")
|
| 68 | " --runtime-dir")))
|
| 69 | (dir (read port))
|
| 70 | (file (format #f "~a/jpserver-~a.json" dir pid)))
|
| 71 | ((@ (ice-9 popen) close-pipe) port)
|
| 72 | (call-with-input-file file
|
| 73 | (lambda (port)
|
| 74 | (let ((url (assoc-ref (json->scm port) #$field)))
|
| 75 | (format #t "~a~%" url)))))))))
|
| 76 |
|
| 77 | (define* (jupyter-shepherd-service config
|
| 78 | #:optional
|
| 79 | (log-file "/var/log/jupyter.log"))
|
| 80 | (match-record config <jupyter-configuration>
|
| 81 | (python-jupyter-core disable-check-xsrf? token)
|
| 82 | (list
|
| 83 | (shepherd-service
|
| 84 | (provision '(jupyter))
|
| 85 | (documentation "Run a Jupyter server.")
|
| 86 | (start
|
| 87 | #~(make-forkexec-constructor
|
| 88 | (list #$(file-append python-jupyter-core "/bin/jupyter")
|
| 89 | "server" "--no-browser"
|
| 90 | #$(string-append "--IdentityProvider.token=" token)
|
| 91 | #$@(if disable-check-xsrf?
|
| 92 | #~("--ServerApp.disable_check_xsrf=True")
|
| 93 | #~()))
|
| 94 | #:log-file #$log-file))
|
| 95 | (stop #~(make-kill-destructor))
|
| 96 | (respawn? #f)
|
| 97 | (actions
|
| 98 | (list
|
| 99 | (shepherd-action
|
| 100 | (name 'get-url)
|
| 101 | (documentation "Print the Jupyter server URL.")
|
| 102 | (procedure
|
| 103 | (jupyter-server-runtime-config-getter-procedure
|
| 104 | python-jupyter-core "url")))))))))
|
| 105 |
|
| 106 | (define jupyter-service-type
|
| 107 | (service-type
|
| 108 | (name 'jupyter)
|
| 109 | (extensions
|
| 110 | (list
|
| 111 | (service-extension profile-service-type
|
| 112 | jupyter-profile-service)
|
| 113 | (service-extension shepherd-root-service-type
|
| 114 | jupyter-shepherd-service)))
|
| 115 | (default-value (jupyter-configuration))
|
| 116 | (description
|
| 117 | "Run a Jupyter server. The runtime file is reliably at
|
| 118 | @file{$(jupyter --runtime-dir)/jpserver-@var{pid}.json}. Provide the
|
| 119 | @code{get-url} function to return a valid URL to fetch from.")))
|