;;; genenetwork-machines --- Julia/Pluto notebook service ;;; Copyright © 2026 the GeneNetwork team ;;; ;;; This file is part of genenetwork-machines. See the GPL header in ;;; genenetwork/containers/julia-notebook-container.scm for the full ;;; license text. ;;; A PlutoSliderServer serving a directory of Pluto notebooks, shared ;;; by the julia-notebook system container ;;; (genenetwork/containers/julia-notebook-container.scm) and usable ;;; elsewhere (e.g. a VM). ;;; ;;; The package (pluto-notebook, in guix-bioinformatics) provides ;;; bin/runsliderserver; this service runs it as an unprivileged ;;; 'julia' user, with a writable HOME for the Julia depot and the ;;; nss-certs CA bundle so Julia's HTTP stack can reach the package ;;; registry. The notebook directory defaults to the one bundled in ;;; the package and can be pointed elsewhere with the ;;; notebooks-directory field (exposed as PLUTO_NOTEBOOKS_DIR, so a ;;; host directory can be mounted in). (define-module (genenetwork services julia-notebook) #:export (julia-notebook-configuration julia-notebook-service-type) #:use-module (gnu) #:use-module (gn packages julia) #:use-module (guix gexp) #:use-module (guix records) #:use-module (ice-9 match) #:use-module (gnu packages admin) #:use-module (gnu packages nss) #:use-module (gnu services base) #:use-module (gnu services shepherd)) ;; Writable state: the Julia depot (packages downloaded/compiled at run ;; time live under $HOME/.julia). In a container this directory is ;; recreated empty on restart unless the host depot is mounted over it ;; (see the container's header). (define %julia-notebook-datadir "/var/lib/julia-notebook") ;; Julia's libcurl wants a single CA bundle file, but nss-certs ships a ;; directory of PEMs and hash symlinks; concatenate the PEMs once, at ;; build time. (define %julia-notebook-ca-bundle (computed-file "julia-notebook-ca-certificates.crt" (with-imported-modules '((guix build utils)) #~(begin (use-modules (ice-9 rdelim) (guix build utils)) (define certs-dir #$(file-append nss-certs "/etc/ssl/certs")) (call-with-output-file #$output (lambda (out) (for-each (lambda (pem) (call-with-input-file pem (lambda (in) (let loop ((line (read-line in 'concat))) (unless (eof-object? line) (display line out) (newline out) (loop (read-line in 'concat))))))) (find-files certs-dir "\\.pem$")))))))) (define-record-type* julia-notebook-configuration make-julia-notebook-configuration julia-notebook-configuration? (package julia-notebook-configuration-package ; package (default pluto-notebook)) (port julia-notebook-configuration-port ; string (default "4343")) ;; #f serves the notebooks bundled in the package; a string is passed ;; through as PLUTO_NOTEBOOKS_DIR. (notebooks-directory julia-notebook-configuration-notebooks-directory ; #f or string (default #f)) ;; Julia depot(s) -- the shared environment: packages, artifacts, the ;; registry and precompile caches all live here, so one depot means ;; every notebook (and every restart) reuses them. Either a single ;; directory or a colon-separated stack, e.g. ;; "/var/lib/julia-notebook/.julia:/gnu/store/...-julia-depot"; a stack ;; lets a read-only Guix-built depot supply the packages with a ;; writable depot on top for the registry and precompilation. #f ;; leaves Julia's default ($HOME/.julia). (depot-path julia-notebook-configuration-depot-path ; #f or string (default #f))) (define (julia-notebook-accounts config) (list (user-group (name "julia") (system? #t)) (user-account (name "julia") (group "julia") (system? #t) (comment "Julia notebook server user") (home-directory %julia-notebook-datadir) (shell (file-append shadow "/sbin/nologin"))))) (define (julia-notebook-activation config) #~(begin (use-modules (guix build utils)) (let ((user (getpw "julia"))) (mkdir-p #$%julia-notebook-datadir) (chown #$%julia-notebook-datadir (passwd:uid user) (passwd:gid user))))) (define (julia-notebook-shepherd-services config) (match-record config (package port notebooks-directory depot-path) (list (shepherd-service (provision '(julia-notebook)) (documentation "Run the Pluto notebook server (PlutoSliderServer).") (requirement '(user-processes loopback networking)) (start #~(make-forkexec-constructor (list #$(file-append package "/bin/runsliderserver")) #:user "julia" #:group "julia" #:environment-variables (append (list (string-append "HOME=" #$%julia-notebook-datadir) (string-append "JULIA_DEPOT_PATH=" #$(if depot-path depot-path (string-append %julia-notebook-datadir "/.julia"))) (string-append "PLUTO_PORT=" #$port) ;; The modern stack installs its Pluto environment ;; with Pkg on first run (into the depot), so Pkg ;; must be online; the nss-certs bundle below lets ;; it reach the registry over HTTPS. A mounted/ ;; persistent depot makes later runs offline. ;; Julia's Downloads uses libcurl, which reads ;; CURL_CA_BUNDLE/SSL_CERT_FILE; point all of them ;; at the concatenated nss-certs bundle. (string-append "SSL_CERT_FILE=" #$%julia-notebook-ca-bundle) (string-append "CURL_CA_BUNDLE=" #$%julia-notebook-ca-bundle) (string-append "SSL_CERT_DIR=" #$(file-append nss-certs "/etc/ssl/certs"))) #$(if notebooks-directory #~(list (string-append "PLUTO_NOTEBOOKS_DIR=" #$notebooks-directory)) #~'())) #:log-file "/var/log/julia-notebook.log")) (stop #~(make-kill-destructor)))))) (define julia-notebook-service-type (service-type (name 'julia-notebook) (extensions (list (service-extension account-service-type julia-notebook-accounts) (service-extension activation-service-type julia-notebook-activation) (service-extension shepherd-root-service-type julia-notebook-shepherd-services))) (default-value (julia-notebook-configuration)) (description "Run a PlutoSliderServer serving a directory of Pluto notebooks.")))