1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu services base)
22 #:use-module (guix store)
23 #:use-module (gnu services)
24 #:use-module (gnu services dmd)
25 #:use-module (gnu services networking)
26 #:use-module (gnu system pam)
27 #:use-module (gnu system shadow) ; 'user-account', etc.
28 #:use-module (gnu system file-systems) ; 'file-system', etc.
29 #:use-module (gnu packages admin)
30 #:use-module ((gnu packages linux)
31 #:select (eudev kbd e2fsprogs lvm2 fuse alsa-utils crda gpm))
32 #:use-module ((gnu packages base)
33 #:select (canonical-package glibc))
34 #:use-module (gnu packages package-management)
35 #:use-module (gnu packages lsh)
36 #:use-module (gnu packages lsof)
37 #:use-module ((gnu build file-systems)
38 #:select (mount-flags->bit-mask))
39 #:use-module (guix gexp)
40 #:use-module (guix records)
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-26)
43 #:use-module (ice-9 match)
44 #:use-module (ice-9 format)
45 #:export (root-file-system-service
48 device-mapping-service
50 user-processes-service
52 console-keymap-service
57 mingetty-configuration
58 mingetty-configuration?
63 %nscd-default-configuration
79 guix-publish-configuration
80 guix-publish-configuration?
82 guix-publish-service-type
90 ;;; Base system services---i.e., services that 99% of the users will want to
100 (define %root-file-system-dmd-service
102 (documentation "Take care of the root file system.")
103 (provision '(root-file-system))
106 ;; Return #f if successfully stopped.
109 (call-with-blocked-asyncs
111 (let ((null (%make-void-port "w")))
113 (display "closing log\n")
114 ;; XXX: Ideally we'd use 'stop-logging', but that one
115 ;; doesn't actually close the port as of dmd 0.1.
116 (close-port (@@ (dmd comm) log-output-port))
117 (set! (@@ (dmd comm) log-output-port) null)
119 ;; Redirect the default output ports..
120 (set-current-output-port null)
121 (set-current-error-port null)
123 ;; Close /dev/console.
124 (for-each close-fdes '(0 1 2))
126 ;; At this point, there are no open files left, so the
127 ;; root file system can be re-mounted read-only.
129 (logior MS_REMOUNT MS_RDONLY)
135 (define root-file-system-service-type
136 (dmd-service-type 'root-file-system
137 (const %root-file-system-dmd-service)))
139 (define (root-file-system-service)
140 "Return a service whose sole purpose is to re-mount read-only the root file
141 system upon shutdown (aka. cleanly \"umounting\" root.)
143 This service must be the root of the service dependency graph so that its
144 'stop' action is invoked when dmd is the only process left."
145 (service root-file-system-service-type #f))
147 (define (file-system->dmd-service-name file-system)
148 "Return the symbol that denotes the service mounting and unmounting
150 (symbol-append 'file-system-
151 (string->symbol (file-system-mount-point file-system))))
153 (define (mapped-device->dmd-service-name md)
154 "Return the symbol that denotes the dmd service of MD, a <mapped-device>."
155 (symbol-append 'device-mapping-
156 (string->symbol (mapped-device-target md))))
158 (define dependency->dmd-service-name
160 ((? mapped-device? md)
161 (mapped-device->dmd-service-name md))
163 (file-system->dmd-service-name fs))))
165 (define file-system-service-type
166 ;; TODO(?): Make this an extensible service that takes <file-system> objects
167 ;; and returns a list of <dmd-service>.
170 (lambda (file-system)
171 (let ((target (file-system-mount-point file-system))
172 (device (file-system-device file-system))
173 (type (file-system-type file-system))
174 (title (file-system-title file-system))
175 (check? (file-system-check? file-system))
176 (create? (file-system-create-mount-point? file-system))
177 (dependencies (file-system-dependencies file-system)))
179 (provision (list (file-system->dmd-service-name file-system)))
180 (requirement `(root-file-system
181 ,@(map dependency->dmd-service-name dependencies)))
182 (documentation "Check, mount, and unmount the given file system.")
183 (start #~(lambda args
184 ;; FIXME: Use or factorize with 'mount-file-system'.
185 (let ((device (canonicalize-device-spec #$device '#$title))
186 (flags #$(mount-flags->bit-mask
187 (file-system-flags file-system))))
193 ;; Make sure fsck.ext2 & co. can be found.
197 "/run/current-system/profile/sbin:"
199 (check-file-system device #$type))
202 (mount device #$target #$type flags
203 #$(file-system-options file-system))
205 ;; For read-only bind mounts, an extra remount is needed,
206 ;; as per <http://lwn.net/Articles/281157/>, which still
207 ;; applies to Linux 4.0.
208 (when (and (= MS_BIND (logand flags MS_BIND))
209 (= MS_RDONLY (logand flags MS_RDONLY)))
210 (mount device #$target #$type
211 (logior MS_BIND MS_REMOUNT MS_RDONLY))))
214 ;; Normally there are no processes left at this point, so
215 ;; TARGET can be safely unmounted.
217 ;; Make sure PID 1 doesn't keep TARGET busy.
223 (define* (file-system-service file-system)
224 "Return a service that mounts @var{file-system}, a @code{<file-system>}
226 (service file-system-service-type file-system))
228 (define user-unmount-service-type
231 (lambda (known-mount-points)
233 (documentation "Unmount manually-mounted file systems.")
234 (provision '(user-file-systems))
237 (define (known? mount-point)
239 (cons* "/proc" "/sys" '#$known-mount-points)))
241 ;; Make sure we don't keep the user's mount points busy.
244 (for-each (lambda (mount-point)
245 (format #t "unmounting '~a'...~%" mount-point)
248 (umount mount-point))
250 (let ((errno (system-error-errno args)))
251 (format #t "failed to unmount '~a': ~a~%"
252 mount-point (strerror errno))))))
253 (filter (negate known?) (mount-points)))
256 (define (user-unmount-service known-mount-points)
257 "Return a service whose sole purpose is to unmount file systems not listed
258 in KNOWN-MOUNT-POINTS when it is stopped."
259 (service user-unmount-service-type known-mount-points))
261 (define %do-not-kill-file
262 ;; Name of the file listing PIDs of processes that must survive when halting
263 ;; the system. Typical example is user-space file systems.
264 "/etc/dmd/do-not-kill")
266 (define user-processes-service-type
270 ((requirements grace-delay)
272 (documentation "When stopped, terminate all user processes.")
273 (provision '(user-processes))
274 (requirement (cons* 'root-file-system 'user-file-systems
275 (map file-system->dmd-service-name
279 (define (kill-except omit signal)
280 ;; Kill all the processes with SIGNAL except those listed
281 ;; in OMIT and the current process.
282 (let ((omit (cons (getpid) omit)))
283 (for-each (lambda (pid)
284 (unless (memv pid omit)
290 ;; List of PIDs that must not be killed.
291 (if (file-exists? #$%do-not-kill-file)
293 (call-with-input-file #$%do-not-kill-file
294 (compose string-tokenize
295 (@ (ice-9 rdelim) read-string))))
299 (car (gettimeofday)))
302 ;; Really sleep N seconds.
303 ;; Work around <http://bugs.gnu.org/19581>.
305 (let loop ((elapsed 0))
307 (sleep (- n elapsed))
308 (loop (- (now) start)))))
310 (define lset= (@ (srfi srfi-1) lset=))
312 (display "sending all processes the TERM signal\n")
314 (if (null? omitted-pids)
316 ;; Easy: terminate all of them.
318 (sleep* #$grace-delay)
321 ;; Kill them all except OMITTED-PIDS. XXX: We would
322 ;; like to (kill -1 SIGSTOP) to get a fixed list of
323 ;; processes, like 'killall5' does, but that seems
325 (kill-except omitted-pids SIGTERM)
326 (sleep* #$grace-delay)
327 (kill-except omitted-pids SIGKILL)
328 (delete-file #$%do-not-kill-file)))
331 (let ((pids (processes)))
332 (unless (lset= = pids (cons 1 omitted-pids))
333 (format #t "waiting for process termination\
334 (processes left: ~s)~%"
339 (display "all processes have been terminated\n")
343 (define* (user-processes-service file-systems #:key (grace-delay 4))
344 "Return the service that is responsible for terminating all the processes so
345 that the root file system can be re-mounted read-only, just before
346 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
347 has been sent are terminated with SIGKILL.
349 The returned service will depend on 'root-file-system' and on all the dmd
350 services corresponding to FILE-SYSTEMS.
352 All the services that spawn processes must depend on this one so that they are
353 stopped before 'kill' is called."
354 (service user-processes-service-type
355 (list file-systems grace-delay)))
362 (define host-name-service-type
367 (documentation "Initialize the machine's host name.")
368 (provision '(host-name))
370 (sethostname #$name)))
373 (define (host-name-service name)
374 "Return a service that sets the host name to @var{name}."
375 (service host-name-service-type name))
377 (define (unicode-start tty)
378 "Return a gexp to start Unicode support on @var{tty}."
380 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
381 ;; 'tty' command, that command returns TTY.
383 (let ((pid (primitive-fork)))
387 (dup2 (open-fdes #$tty O_RDONLY) 0)
389 (dup2 (open-fdes #$tty O_WRONLY) 1)
390 (execl (string-append #$kbd "/bin/unicode_start")
393 (zero? (cdr (waitpid pid))))))))
395 (define console-keymap-service-type
400 (documentation (string-append "Load console keymap (loadkeys)."))
401 (provision '(console-keymap))
403 (zero? (system* (string-append #$kbd "/bin/loadkeys")
407 (define (console-keymap-service file)
408 "Return a service to load console keymap from @var{file}."
409 (service console-keymap-service-type file))
411 (define console-font-service-type
416 (let ((device (string-append "/dev/" tty)))
418 (documentation "Load a Unicode console font.")
419 (provision (list (symbol-append 'console-font-
420 (string->symbol tty))))
422 ;; Start after mingetty has been started on TTY, otherwise the settings
424 (requirement (list (symbol-append 'term-
425 (string->symbol tty))))
428 (and #$(unicode-start device)
430 (system* (string-append #$kbd "/bin/setfont")
431 "-C" #$device #$font)))))
435 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
436 "Return a service that sets up Unicode support in @var{tty} and loads
437 @var{font} for that tty (fonts are per virtual console in Linux.)"
438 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
439 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
440 ;; codepoints notably found in the UTF-8 manual.
441 (service console-font-service-type (list tty font)))
443 (define-record-type* <mingetty-configuration>
444 mingetty-configuration make-mingetty-configuration
445 mingetty-configuration?
446 (mingetty mingetty-configuration-mingetty ;<package>
448 (tty mingetty-configuration-tty) ;string
449 (motd mingetty-configuration-motd ;file-like
450 (default (plain-file "motd" "Welcome.\n")))
451 (auto-login mingetty-auto-login ;string | #f
453 (login-program mingetty-login-program ;gexp
455 (login-pause? mingetty-login-pause? ;Boolean
458 ;; Allow empty passwords by default so that first-time users can log in when
459 ;; the 'root' account has just been created.
460 (allow-empty-passwords? mingetty-configuration-allow-empty-passwords?
461 (default #t))) ;Boolean
463 (define (mingetty-pam-service conf)
464 "Return the list of PAM service needed for CONF."
465 ;; Let 'login' be known to PAM. All the mingetty services will have that
466 ;; PAM service, but that's fine because they're all identical and duplicates
468 (list (unix-pam-service "login"
469 #:allow-empty-passwords?
470 (mingetty-configuration-allow-empty-passwords? conf)
472 (mingetty-configuration-motd conf))))
474 (define mingetty-dmd-service
476 (($ <mingetty-configuration> mingetty tty motd auto-login login-program
477 login-pause? allow-empty-passwords?)
480 (documentation "Run mingetty on an tty.")
481 (provision (list (symbol-append 'term- (string->symbol tty))))
483 ;; Since the login prompt shows the host name, wait for the 'host-name'
484 ;; service to be done. Also wait for udev essentially so that the tty
485 ;; text is not lost in the middle of kernel messages (XXX).
486 (requirement '(user-processes host-name udev))
488 (start #~(make-forkexec-constructor
489 (list (string-append #$mingetty "/sbin/mingetty")
492 #~("--autologin" #$auto-login)
495 #~("--loginprog" #$login-program)
500 (stop #~(make-kill-destructor)))))))
502 (define mingetty-service-type
503 (service-type (name 'mingetty)
504 (extensions (list (service-extension dmd-root-service-type
505 mingetty-dmd-service)
506 (service-extension pam-root-service-type
507 mingetty-pam-service)))))
509 (define* (mingetty-service config)
510 "Return a service to run mingetty according to @var{config}, which specifies
511 the tty to run, among other things."
512 (service mingetty-service-type config))
514 (define-record-type* <nscd-configuration> nscd-configuration
515 make-nscd-configuration
517 (log-file nscd-configuration-log-file ;string
518 (default "/var/log/nscd.log"))
519 (debug-level nscd-debug-level ;integer
521 ;; TODO: See nscd.conf in glibc for other options to add.
522 (caches nscd-configuration-caches ;list of <nscd-cache>
523 (default %nscd-default-caches))
524 (name-services nscd-configuration-name-services ;list of <packages>
526 (glibc nscd-configuration-glibc ;<package>
527 (default (canonical-package glibc))))
529 (define-record-type* <nscd-cache> nscd-cache make-nscd-cache
531 (database nscd-cache-database) ;symbol
532 (positive-time-to-live nscd-cache-positive-time-to-live) ;integer
533 (negative-time-to-live nscd-cache-negative-time-to-live
534 (default 20)) ;integer
535 (suggested-size nscd-cache-suggested-size ;integer ("default module
538 (check-files? nscd-cache-check-files? ;Boolean
540 (persistent? nscd-cache-persistent? ;Boolean
542 (shared? nscd-cache-shared? ;Boolean
544 (max-database-size nscd-cache-max-database-size ;integer
545 (default (* 32 (expt 2 20))))
546 (auto-propagate? nscd-cache-auto-propagate? ;Boolean
549 (define %nscd-default-caches
550 ;; Caches that we want to enable by default. Note that when providing an
551 ;; empty nscd.conf, all caches are disabled.
552 (list (nscd-cache (database 'hosts)
554 ;; Aggressively cache the host name cache to improve
555 ;; privacy and resilience.
556 (positive-time-to-live (* 3600 12))
557 (negative-time-to-live 20)
560 (nscd-cache (database 'services)
562 ;; Services are unlikely to change, so we can be even more
564 (positive-time-to-live (* 3600 24))
565 (negative-time-to-live 3600)
566 (check-files? #t) ;check /etc/services changes
569 (define %nscd-default-configuration
570 ;; Default nscd configuration.
571 (nscd-configuration))
573 (define (nscd.conf-file config)
574 "Return the @file{nscd.conf} configuration file for @var{config}, an
575 @code{<nscd-configuration>} object."
576 (define cache->config
578 (($ <nscd-cache> (= symbol->string database)
579 positive-ttl negative-ttl size check-files?
580 persistent? shared? max-size propagate?)
581 (string-append "\nenable-cache\t" database "\tyes\n"
583 "positive-time-to-live\t" database "\t"
584 (number->string positive-ttl) "\n"
585 "negative-time-to-live\t" database "\t"
586 (number->string negative-ttl) "\n"
587 "suggested-size\t" database "\t"
588 (number->string size) "\n"
589 "check-files\t" database "\t"
590 (if check-files? "yes\n" "no\n")
591 "persistent\t" database "\t"
592 (if persistent? "yes\n" "no\n")
593 "shared\t" database "\t"
594 (if shared? "yes\n" "no\n")
595 "max-db-size\t" database "\t"
596 (number->string max-size) "\n"
597 "auto-propagate\t" database "\t"
598 (if propagate? "yes\n" "no\n")))))
601 (($ <nscd-configuration> log-file debug-level caches)
602 (plain-file "nscd.conf"
604 # Configuration of libc's name service cache daemon (nscd).\n\n"
606 (string-append "logfile\t" log-file)
610 (string-append "debug-level\t"
611 (number->string debug-level))
615 (map cache->config caches)))))))
617 (define (nscd-dmd-service config)
618 "Return a dmd service for CONFIG, an <nscd-configuration> object."
619 (let ((nscd.conf (nscd.conf-file config))
620 (name-services (nscd-configuration-name-services config)))
622 (documentation "Run libc's name service cache daemon (nscd).")
624 (requirement '(user-processes))
625 (start #~(make-forkexec-constructor
626 (list (string-append #$(nscd-configuration-glibc config)
628 "-f" #$nscd.conf "--foreground")
630 #:environment-variables
631 (list (string-append "LD_LIBRARY_PATH="
634 (string-append dir "/lib"))
635 (list #$@name-services))
637 (stop #~(make-kill-destructor))
641 (define nscd-activation
642 ;; Actions to take before starting nscd.
644 (use-modules (guix build utils))
645 (mkdir-p "/var/run/nscd")
646 (mkdir-p "/var/db/nscd"))) ;for the persistent cache
648 (define nscd-service-type
649 (service-type (name 'nscd)
651 (list (service-extension activation-service-type
652 (const nscd-activation))
653 (service-extension dmd-root-service-type
656 ;; This can be extended by providing additional name services
658 (compose concatenate)
659 (extend (lambda (config name-services)
662 (name-services (append
663 (nscd-configuration-name-services config)
666 (define* (nscd-service #:optional (config %nscd-default-configuration))
667 "Return a service that runs libc's name service cache daemon (nscd) with the
668 given @var{config}---an @code{<nscd-configuration>} object. @xref{Name
669 Service Switch}, for an example."
670 (service nscd-service-type config))
672 (define syslog-service-type
675 (lambda (config-file)
677 (documentation "Run the syslog daemon (syslogd).")
678 (provision '(syslogd))
679 (requirement '(user-processes))
680 (start #~(make-forkexec-constructor
681 (list (string-append #$inetutils "/libexec/syslogd")
682 "--no-detach" "--rcfile" #$config-file)))
683 (stop #~(make-kill-destructor))))))
685 ;; Snippet adapted from the GNU inetutils manual.
686 (define %default-syslog.conf
687 (plain-file "syslog.conf" "
688 # Log all error messages, authentication messages of
689 # level notice or higher and anything of level err or
690 # higher to the console.
691 # Don't log private authentication messages!
692 *.alert;auth.notice;authpriv.none /dev/console
694 # Log anything (except mail) of level info or higher.
695 # Don't log private authentication messages!
696 *.info;mail.none;authpriv.none /var/log/messages
698 # Same, in a different place.
699 *.info;mail.none;authpriv.none /dev/tty12
701 # The authpriv file has restricted access.
702 authpriv.* /var/log/secure
704 # Log all the mail messages in one place.
705 mail.* /var/log/maillog
708 (define* (syslog-service #:key (config-file %default-syslog.conf))
709 "Return a service that runs @code{syslogd}.
710 If configuration file name @var{config-file} is not specified, use some
711 reasonable default settings."
712 (service syslog-service-type config-file))
719 (define* (guix-build-accounts count #:key
723 "Return a list of COUNT user accounts for Guix build users, with UIDs
724 starting at FIRST-UID, and under GID."
725 (unfold (cut > <> count)
728 (name (format #f "guixbuilder~2,'0d" n))
730 (uid (+ first-uid n -1))
733 ;; guix-daemon expects GROUP to be listed as a
734 ;; supplementary group too:
735 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
736 (supplementary-groups (list group "kvm"))
738 (comment (format #f "Guix Build User ~2d" n))
739 (home-directory "/var/empty")
740 (shell #~(string-append #$shadow "/sbin/nologin"))))
744 (define (hydra-key-authorization guix)
745 "Return a gexp with code to register the hydra.gnu.org public key with
747 #~(unless (file-exists? "/etc/guix/acl")
748 (let ((pid (primitive-fork)))
751 (let* ((key (string-append #$guix
752 "/share/guix/hydra.gnu.org.pub"))
753 (port (open-file key "r0b")))
754 (format #t "registering public key '~a'...~%" key)
755 (close-port (current-input-port))
757 (execl (string-append #$guix "/bin/guix")
758 "guix" "archive" "--authorize")
761 (let ((status (cdr (waitpid pid))))
762 (unless (zero? status)
763 (format (current-error-port) "warning: \
764 failed to register hydra.gnu.org public key: ~a~%" status))))))))
766 (define-record-type* <guix-configuration>
767 guix-configuration make-guix-configuration
769 (guix guix-configuration-guix ;<package>
771 (build-group guix-configuration-build-group ;string
772 (default "guixbuild"))
773 (build-accounts guix-configuration-build-accounts ;integer
775 (authorize-key? guix-configuration-authorize-key? ;Boolean
777 (use-substitutes? guix-configuration-use-substitutes? ;Boolean
779 (substitute-urls guix-configuration-substitute-urls ;list of strings
780 (default %default-substitute-urls))
781 (extra-options guix-configuration-extra-options ;list of strings
783 (lsof guix-configuration-lsof ;<package>
785 (lsh guix-configuration-lsh ;<package>
788 (define %default-guix-configuration
789 (guix-configuration))
791 (define (guix-dmd-service config)
792 "Return a <dmd-service> for the Guix daemon service with CONFIG."
794 (($ <guix-configuration> guix build-group build-accounts authorize-key?
795 use-substitutes? substitute-urls extra-options
798 (documentation "Run the Guix daemon.")
799 (provision '(guix-daemon))
800 (requirement '(user-processes))
802 #~(make-forkexec-constructor
803 (list (string-append #$guix "/bin/guix-daemon")
804 "--build-users-group" #$build-group
805 #$@(if use-substitutes?
807 '("--no-substitutes"))
808 "--substitute-urls" #$(string-join substitute-urls)
811 ;; Add 'lsof' (for the GC) and 'lsh' (for offloading) to the
813 #:environment-variables
814 (list (string-append "PATH=" #$lsof "/bin:" #$lsh "/bin"))))
815 (stop #~(make-kill-destructor)))))))
817 (define (guix-accounts config)
818 "Return the user accounts and user groups for CONFIG."
820 (($ <guix-configuration> _ build-group build-accounts)
825 ;; Use a fixed GID so that we can create the store with the right
828 (guix-build-accounts build-accounts
829 #:group build-group)))))
831 (define (guix-activation config)
832 "Return the activation gexp for CONFIG."
834 (($ <guix-configuration> guix build-group build-accounts authorize-key?)
835 ;; Assume that the store has BUILD-GROUP as its group. We could
836 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
837 ;; chown leads to an entire copy of the tree, which is a bad idea.
839 ;; Optionally authorize hydra.gnu.org's key.
841 (hydra-key-authorization guix)))))
843 (define guix-service-type
847 (list (service-extension dmd-root-service-type guix-dmd-service)
848 (service-extension account-service-type guix-accounts)
849 (service-extension activation-service-type guix-activation)))))
851 (define* (guix-service #:optional (config %default-guix-configuration))
852 "Return a service that runs the Guix build daemon according to
854 (service guix-service-type config))
857 (define-record-type* <guix-publish-configuration>
858 guix-publish-configuration make-guix-publish-configuration
859 guix-publish-configuration?
860 (guix guix-publish-configuration-guix ;package
862 (port guix-publish-configuration-port ;number
864 (host guix-publish-configuration-host ;string
865 (default "localhost")))
867 (define guix-publish-dmd-service
869 (($ <guix-publish-configuration> guix port host)
871 (provision '(guix-publish))
872 (requirement '(guix-daemon))
873 (start #~(make-forkexec-constructor
874 (list (string-append #$guix "/bin/guix")
875 "publish" "-u" "guix-publish"
876 "-p" #$(number->string port)
877 (string-append "--listen=" #$host))))
878 (stop #~(make-kill-destructor)))))))
880 (define %guix-publish-accounts
881 (list (user-group (name "guix-publish") (system? #t))
883 (name "guix-publish")
884 (group "guix-publish")
886 (comment "guix publish user")
887 (home-directory "/var/empty")
888 (shell #~(string-append #$shadow "/sbin/nologin")))))
890 (define guix-publish-service-type
891 (service-type (name 'guix-publish)
893 (list (service-extension dmd-root-service-type
894 guix-publish-dmd-service)
895 (service-extension account-service-type
896 (const %guix-publish-accounts))))))
898 (define* (guix-publish-service #:key (guix guix) (port 80) (host "localhost"))
899 "Return a service that runs @command{guix publish} listening on @var{host}
900 and @var{port} (@pxref{Invoking guix publish}).
902 This assumes that @file{/etc/guix} already contains a signing key pair as
903 created by @command{guix archive --generate-key} (@pxref{Invoking guix
904 archive}). If that is not the case, the service will fail to start."
905 (service guix-publish-service-type
906 (guix-publish-configuration (guix guix) (port port) (host host))))
913 (define-record-type* <udev-configuration>
914 udev-configuration make-udev-configuration
916 (udev udev-configuration-udev ;<package>
918 (rules udev-configuration-rules ;list of <package>
921 (define (udev-rules-union packages)
922 "Return the union of the @code{lib/udev/rules.d} directories found in each
923 item of @var{packages}."
926 (use-modules (guix build union)
931 (define %standard-locations
932 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
934 (define (rules-sub-directory directory)
935 ;; Return the sub-directory of DIRECTORY containing udev rules, or
936 ;; #f if none was found.
937 (find directory-exists?
938 (map (cut string-append directory <>) %standard-locations)))
940 (mkdir-p (string-append #$output "/lib/udev"))
941 (union-build (string-append #$output "/lib/udev/rules.d")
942 (filter-map rules-sub-directory '#$packages))))
944 (computed-file "udev-rules" build
945 #:modules '((guix build union)
946 (guix build utils))))
948 (define* (kvm-udev-rule)
949 "Return a directory with a udev rule that changes the group of
950 @file{/dev/kvm} to \"kvm\" and makes it #o660."
951 ;; Apparently QEMU-KVM used to ship this rule, but now we have to add it by
953 (computed-file "kvm-udev-rules"
955 (use-modules (guix build utils))
958 (string-append #$output "/lib/udev/rules.d"))
961 (call-with-output-file
962 (string-append rules.d "/90-kvm.rules")
964 ;; Build users are part of the "kvm" group, so we
965 ;; can fearlessly make /dev/kvm 660 (see
966 ;; <http://bugs.gnu.org/18994>, for background.)
968 KERNEL==\"kvm\", GROUP=\"kvm\", MODE=\"0660\"\n" port))))
969 #:modules '((guix build utils))))
971 (define udev-dmd-service
972 ;; Return a <dmd-service> for UDEV with RULES.
974 (($ <udev-configuration> udev rules)
975 (let* ((rules (udev-rules-union (cons* udev (kvm-udev-rule) rules)))
976 (udev.conf (computed-file "udev.conf"
977 #~(call-with-output-file #$output
980 "udev_rules=\"~a/lib/udev/rules.d\"\n"
986 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device nodes can
988 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
989 (requirement '(root-file-system))
991 (documentation "Populate the /dev directory, dynamically.")
994 (@ (srfi srfi-1) find))
997 ;; Choose the right 'udevd'.
999 (map (lambda (suffix)
1000 (string-append #$udev suffix))
1001 '("/libexec/udev/udevd" ;udev
1002 "/sbin/udevd")))) ;eudev
1004 (define (wait-for-udevd)
1005 ;; Wait until someone's listening on udevd's control
1007 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
1009 (catch 'system-error
1011 (connect sock PF_UNIX "/run/udev/control")
1014 (format #t "waiting for udevd...~%")
1018 ;; Allow udev to find the modules.
1019 (setenv "LINUX_MODULE_DIRECTORY"
1020 "/run/booted-system/kernel/lib/modules")
1022 ;; The first one is for udev, the second one for eudev.
1023 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
1024 (setenv "EUDEV_RULES_DIRECTORY"
1025 (string-append #$rules "/lib/udev/rules.d"))
1027 (let ((pid (primitive-fork)))
1030 (exec-command (list udevd)))
1032 ;; Wait until udevd is up and running. This
1033 ;; appears to be needed so that the events
1034 ;; triggered below are actually handled.
1037 ;; Trigger device node creation.
1038 (system* (string-append #$udev "/bin/udevadm")
1039 "trigger" "--action=add")
1041 ;; Wait for things to settle down.
1042 (system* (string-append #$udev "/bin/udevadm")
1045 (stop #~(make-kill-destructor))
1047 ;; When halting the system, 'udev' is actually killed by
1048 ;; 'user-processes', i.e., before its own 'stop' method was called.
1049 ;; Thus, make sure it is not respawned.
1052 (define udev-service-type
1053 (service-type (name 'udev)
1055 (list (service-extension dmd-root-service-type
1058 (compose concatenate) ;concatenate the list of rules
1059 (extend (lambda (config rules)
1061 (($ <udev-configuration> udev initial-rules)
1064 (rules (append initial-rules rules)))))))))
1066 (define* (udev-service #:key (udev eudev) (rules '()))
1067 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
1068 extra rules from the packages listed in @var{rules}."
1069 (service udev-service-type
1070 (udev-configuration (udev udev) (rules rules))))
1072 (define device-mapping-service-type
1076 ((target open close)
1078 (provision (list (symbol-append 'device-mapping- (string->symbol target))))
1079 (requirement '(udev))
1080 (documentation "Map a device node using Linux's device mapper.")
1081 (start #~(lambda () #$open))
1082 (stop #~(lambda _ (not #$close)))
1085 (define (device-mapping-service target open close)
1086 "Return a service that maps device @var{target}, a string such as
1087 @code{\"home\"} (meaning @code{/dev/mapper/home}). Evaluate @var{open}, a
1088 gexp, to open it, and evaluate @var{close} to close it."
1089 (service device-mapping-service-type
1090 (list target open close)))
1092 (define swap-service-type
1097 (if (string-prefix? "/dev/mapper/" device)
1098 (list (symbol-append 'device-mapping-
1099 (string->symbol (basename device))))
1103 (provision (list (symbol-append 'swap- (string->symbol device))))
1104 (requirement `(udev ,@requirement))
1105 (documentation "Enable the given swap device.")
1107 (restart-on-EINTR (swapon #$device))
1110 (restart-on-EINTR (swapoff #$device))
1114 (define (swap-service device)
1115 "Return a service that uses @var{device} as a swap device."
1116 (service swap-service-type device))
1119 (define-record-type* <gpm-configuration>
1120 gpm-configuration make-gpm-configuration gpm-configuration?
1121 (gpm gpm-configuration-gpm) ;package
1122 (options gpm-configuration-options)) ;list of strings
1124 (define gpm-dmd-service
1126 (($ <gpm-configuration> dmd options)
1128 (requirement '(udev))
1131 ;; 'gpm' runs in the background and sets a PID file.
1132 ;; Note that it requires running as "root".
1133 (false-if-exception (delete-file "/var/run/gpm.pid"))
1134 (fork+exec-command (list (string-append #$gpm "/sbin/gpm")
1137 ;; Wait for the PID file to appear; declare failure if
1138 ;; it doesn't show up.
1140 (or (file-exists? "/var/run/gpm.pid")
1148 ;; Return #f if successfully stopped.
1149 (not (zero? (system* (string-append #$gpm "/sbin/gpm")
1152 (define gpm-service-type
1153 (service-type (name 'gpm)
1155 (list (service-extension dmd-root-service-type
1156 gpm-dmd-service)))))
1158 (define* (gpm-service #:key (gpm gpm)
1159 (options '("-m" "/dev/input/mice" "-t" "ps2")))
1160 "Run @var{gpm}, the general-purpose mouse daemon, with the given
1161 command-line @var{options}. GPM allows users to use the mouse in the console,
1162 notably to select, copy, and paste text. The default value of @var{options}
1163 uses the @code{ps2} protocol, which works for both USB and PS/2 mice.
1165 This service is not part of @var{%base-services}."
1166 ;; To test in QEMU, use "-usbdevice mouse" and then, in the monitor, use
1167 ;; "info mice" and "mouse_set X" to use the right mouse.
1168 (service gpm-service-type
1169 (gpm-configuration (gpm gpm) (options options))))
1172 (define %base-services
1173 ;; Convenience variable holding the basic services.
1174 (let ((motd (plain-file "motd" "
1175 This is the GNU operating system, welcome!\n\n")))
1176 (list (console-font-service "tty1")
1177 (console-font-service "tty2")
1178 (console-font-service "tty3")
1179 (console-font-service "tty4")
1180 (console-font-service "tty5")
1181 (console-font-service "tty6")
1183 (mingetty-service (mingetty-configuration
1184 (tty "tty1") (motd motd)))
1185 (mingetty-service (mingetty-configuration
1186 (tty "tty2") (motd motd)))
1187 (mingetty-service (mingetty-configuration
1188 (tty "tty3") (motd motd)))
1189 (mingetty-service (mingetty-configuration
1190 (tty "tty4") (motd motd)))
1191 (mingetty-service (mingetty-configuration
1192 (tty "tty5") (motd motd)))
1193 (mingetty-service (mingetty-configuration
1194 (tty "tty6") (motd motd)))
1196 (static-networking-service "lo" "127.0.0.1"
1197 #:provision '(loopback))
1202 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
1203 ;; used, so enable them by default. The FUSE and ALSA rules are
1204 ;; less critical, but handy.
1205 (udev-service #:rules (list lvm2 fuse alsa-utils crda)))))
1207 ;;; base.scm ends here