1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016, 2018 Efraim Flashner <efraim@flashner.co.il>
5 ;;; Copyright © 2016 John Darrington <jmd@gnu.org>
6 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
7 ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
8 ;;; Copyright © 2017, 2018 Marius Bakke <mbakke@fastmail.com>
9 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
10 ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
11 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
12 ;;; Copyright © 2019 Florian Pelz <pelzflorian@pelzflorian.de>
14 ;;; This file is part of GNU Guix.
16 ;;; GNU Guix is free software; you can redistribute it and/or modify it
17 ;;; under the terms of the GNU General Public License as published by
18 ;;; the Free Software Foundation; either version 3 of the License, or (at
19 ;;; your option) any later version.
21 ;;; GNU Guix is distributed in the hope that it will be useful, but
22 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;;; GNU General Public License for more details.
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
29 (define-module (gnu services networking)
30 #:use-module (gnu services)
31 #:use-module (gnu services base)
32 #:use-module (gnu services shepherd)
33 #:use-module (gnu services dbus)
34 #:use-module (gnu system shadow)
35 #:use-module (gnu system pam)
36 #:use-module (gnu packages admin)
37 #:use-module (gnu packages base)
38 #:use-module (gnu packages bash)
39 #:use-module (gnu packages connman)
40 #:use-module (gnu packages freedesktop)
41 #:use-module (gnu packages linux)
42 #:use-module (gnu packages tor)
43 #:use-module (gnu packages usb-modeswitch)
44 #:use-module (gnu packages messaging)
45 #:use-module (gnu packages networking)
46 #:use-module (gnu packages ntp)
47 #:use-module (gnu packages wicd)
48 #:use-module (gnu packages gnome)
49 #:use-module (guix gexp)
50 #:use-module (guix records)
51 #:use-module (guix modules)
52 #:use-module (guix deprecation)
53 #:use-module (srfi srfi-1)
54 #:use-module (srfi srfi-9)
55 #:use-module (srfi srfi-26)
56 #:use-module (ice-9 match)
57 #:re-export (static-networking-service
58 static-networking-service-type)
59 #:export (%facebook-host-aliases
61 dhcp-client-service-type
66 dhcpd-configuration-package
67 dhcpd-configuration-config-file
68 dhcpd-configuration-version
69 dhcpd-configuration-run-directory
70 dhcpd-configuration-lease-file
71 dhcpd-configuration-pid-file
72 dhcpd-configuration-interfaces
81 openntpd-configuration
82 openntpd-configuration?
98 network-manager-configuration
99 network-manager-configuration?
100 network-manager-configuration-dns
101 network-manager-service-type
103 connman-configuration
104 connman-configuration?
107 modem-manager-configuration
108 modem-manager-configuration?
109 modem-manager-service-type
111 usb-modeswitch-configuration
112 usb-modeswitch-configuration?
113 usb-modeswitch-configuration-usb-modeswitch
114 usb-modeswitch-configuration-usb-modeswitch-data
115 usb-modeswitch-service-type
117 <wpa-supplicant-configuration>
118 wpa-supplicant-configuration
119 wpa-supplicant-configuration?
120 wpa-supplicant-configuration-wpa-supplicant
121 wpa-supplicant-configuration-pid-file
122 wpa-supplicant-configuration-dbus?
123 wpa-supplicant-configuration-interface
124 wpa-supplicant-configuration-config-file
125 wpa-supplicant-configuration-extra-options
126 wpa-supplicant-service-type
128 openvswitch-service-type
129 openvswitch-configuration
131 iptables-configuration
132 iptables-configuration?
133 iptables-configuration-iptables
134 iptables-configuration-ipv4-rules
135 iptables-configuration-ipv6-rules
136 iptables-service-type))
140 ;;; Networking services.
144 (define %facebook-host-aliases
145 ;; This is the list of known Facebook hosts to be added to /etc/hosts if you
148 # Block Facebook IPv4.
149 127.0.0.1 www.facebook.com
150 127.0.0.1 facebook.com
151 127.0.0.1 login.facebook.com
152 127.0.0.1 www.login.facebook.com
154 127.0.0.1 www.fbcdn.net
156 127.0.0.1 www.fbcdn.com
157 127.0.0.1 static.ak.fbcdn.net
158 127.0.0.1 static.ak.connect.facebook.com
159 127.0.0.1 connect.facebook.net
160 127.0.0.1 www.connect.facebook.net
161 127.0.0.1 apps.facebook.com
163 # Block Facebook IPv6.
164 fe80::1%lo0 facebook.com
165 fe80::1%lo0 login.facebook.com
166 fe80::1%lo0 www.login.facebook.com
167 fe80::1%lo0 fbcdn.net
168 fe80::1%lo0 www.fbcdn.net
169 fe80::1%lo0 fbcdn.com
170 fe80::1%lo0 www.fbcdn.com
171 fe80::1%lo0 static.ak.fbcdn.net
172 fe80::1%lo0 static.ak.connect.facebook.com
173 fe80::1%lo0 connect.facebook.net
174 fe80::1%lo0 www.connect.facebook.net
175 fe80::1%lo0 apps.facebook.com\n")
177 (define dhcp-client-service-type
178 (shepherd-service-type
182 (file-append dhcp "/sbin/dhclient"))
185 "/var/run/dhclient.pid")
188 (documentation "Set up networking via DHCP.")
189 (requirement '(user-processes udev))
191 ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when
192 ;; networking is unavailable, but also means that the interface is not up
193 ;; yet when 'start' completes. To wait for the interface to be ready, one
194 ;; should instead monitor udev events.
195 (provision '(networking))
198 ;; When invoked without any arguments, 'dhclient' discovers all
199 ;; non-loopback interfaces *that are up*. However, the relevant
200 ;; interfaces are typically down at this point. Thus we perform
201 ;; our own interface discovery here.
204 (and (arp-network-interface? interface)
205 (not (loopback-network-interface? interface)))))
207 (filter valid? (all-network-interface-names)))
209 ;; XXX: Make sure the interfaces are up so that 'dhclient' can
210 ;; actually send/receive over them.
211 (for-each set-network-interface-up ifaces)
213 (false-if-exception (delete-file #$pid-file))
214 (let ((pid (fork+exec-command
215 (cons* #$dhclient "-nw"
216 "-pf" #$pid-file ifaces))))
217 (and (zero? (cdr (waitpid pid)))
218 (read-pid-file #$pid-file)))))
219 (stop #~(make-kill-destructor))))
222 (define-deprecated (dhcp-client-service #:key (dhcp isc-dhcp))
223 dhcp-client-service-type
224 "Return a service that runs @var{dhcp}, a Dynamic Host Configuration
225 Protocol (DHCP) client, on all the non-loopback network interfaces."
226 (service dhcp-client-service-type dhcp))
228 (define-record-type* <dhcpd-configuration>
229 dhcpd-configuration make-dhcpd-configuration
231 (package dhcpd-configuration-package ;<package>
233 (config-file dhcpd-configuration-config-file ;file-like
235 (version dhcpd-configuration-version ;"4", "6", or "4o6"
237 (run-directory dhcpd-configuration-run-directory
238 (default "/run/dhcpd"))
239 (lease-file dhcpd-configuration-lease-file
240 (default "/var/db/dhcpd.leases"))
241 (pid-file dhcpd-configuration-pid-file
242 (default "/run/dhcpd/dhcpd.pid"))
243 ;; list of strings, e.g. (list "enp0s25")
244 (interfaces dhcpd-configuration-interfaces
247 (define dhcpd-shepherd-service
249 (($ <dhcpd-configuration> package config-file version run-directory
250 lease-file pid-file interfaces)
252 (error "Must supply a config-file"))
253 (list (shepherd-service
254 ;; Allow users to easily run multiple versions simultaneously.
255 (provision (list (string->symbol
256 (string-append "dhcpv" version "-daemon"))))
257 (documentation (string-append "Run the DHCPv" version " daemon"))
258 (requirement '(networking))
259 (start #~(make-forkexec-constructor
260 '(#$(file-append package "/sbin/dhcpd")
261 #$(string-append "-" version)
266 #:pid-file #$pid-file))
267 (stop #~(make-kill-destructor)))))))
269 (define dhcpd-activation
271 (($ <dhcpd-configuration> package config-file version run-directory
272 lease-file pid-file interfaces)
273 (with-imported-modules '((guix build utils))
275 (unless (file-exists? #$run-directory)
276 (mkdir #$run-directory))
277 ;; According to the DHCP manual (man dhcpd.leases), the lease
278 ;; database must be present for dhcpd to start successfully.
279 (unless (file-exists? #$lease-file)
280 (with-output-to-file #$lease-file
281 (lambda _ (display ""))))
282 ;; Validate the config.
284 #$(file-append package "/sbin/dhcpd") "-t" "-cf"
287 (define dhcpd-service-type
291 (list (service-extension shepherd-root-service-type dhcpd-shepherd-service)
292 (service-extension activation-service-type dhcpd-activation)))))
295 ;; Default set of NTP servers. These URLs are managed by the NTP Pool project.
296 ;; Within Guix, Leo Famulari <leo@famulari.name> is the administrative contact
297 ;; for this NTP pool "zone".
298 '("0.guix.pool.ntp.org"
299 "1.guix.pool.ntp.org"
300 "2.guix.pool.ntp.org"
301 "3.guix.pool.ntp.org"))
309 (define-record-type* <ntp-configuration>
310 ntp-configuration make-ntp-configuration
312 (ntp ntp-configuration-ntp
314 (servers ntp-configuration-servers
315 (default %ntp-servers))
316 (allow-large-adjustment? ntp-allow-large-adjustment?
319 (define ntp-shepherd-service
321 (($ <ntp-configuration> ntp servers allow-large-adjustment?)
323 ;; TODO: Add authentication support.
325 (string-append "driftfile /var/run/ntpd/ntp.drift\n"
326 (string-join (map (cut string-append "server " <>)
330 # Disable status queries as a workaround for CVE-2013-5211:
331 # <http://support.ntp.org/bin/view/Main/SecurityNotice#DRDoS_Amplification_Attack_using>.
332 restrict default kod nomodify notrap nopeer noquery
333 restrict -6 default kod nomodify notrap nopeer noquery
335 # Yet, allow use of the local 'ntpq'.
340 (plain-file "ntpd.conf" config))
342 (list (shepherd-service
344 (documentation "Run the Network Time Protocol (NTP) daemon.")
345 (requirement '(user-processes networking))
346 (start #~(make-forkexec-constructor
347 (list (string-append #$ntp "/bin/ntpd") "-n"
348 "-c" #$ntpd.conf "-u" "ntpd"
349 #$@(if allow-large-adjustment?
352 (stop #~(make-kill-destructor))))))))
354 (define %ntp-accounts
359 (comment "NTP daemon user")
360 (home-directory "/var/empty")
361 (shell (file-append shadow "/sbin/nologin")))))
364 (define (ntp-service-activation config)
365 "Return the activation gexp for CONFIG."
366 (with-imported-modules '((guix build utils))
368 (use-modules (guix build utils))
372 (let ((directory "/var/run/ntpd"))
374 (chown directory (passwd:uid %user) (passwd:gid %user))))))
376 (define ntp-service-type
377 (service-type (name 'ntp)
379 (list (service-extension shepherd-root-service-type
380 ntp-shepherd-service)
381 (service-extension account-service-type
382 (const %ntp-accounts))
383 (service-extension activation-service-type
384 ntp-service-activation)))
386 "Run the @command{ntpd}, the Network Time Protocol (NTP)
387 daemon of the @uref{http://www.ntp.org, Network Time Foundation}. The daemon
388 will keep the system clock synchronized with that of the given servers.")
389 (default-value (ntp-configuration))))
391 (define-deprecated (ntp-service #:key (ntp ntp)
392 (servers %ntp-servers)
393 allow-large-adjustment?)
395 "Return a service that runs the daemon from @var{ntp}, the
396 @uref{http://www.ntp.org, Network Time Protocol package}. The daemon will
397 keep the system clock synchronized with that of @var{servers}.
398 @var{allow-large-adjustment?} determines whether @command{ntpd} is allowed to
399 make an initial adjustment of more than 1,000 seconds."
400 (service ntp-service-type
401 (ntp-configuration (ntp ntp)
403 (allow-large-adjustment?
404 allow-large-adjustment?))))
411 (define-record-type* <openntpd-configuration>
412 openntpd-configuration make-openntpd-configuration
413 openntpd-configuration?
414 (openntpd openntpd-configuration-openntpd
416 (listen-on openntpd-listen-on
417 (default '("127.0.0.1"
419 (query-from openntpd-query-from
421 (sensor openntpd-sensor
423 (server openntpd-server
424 (default %ntp-servers))
425 (servers openntpd-servers
427 (constraint-from openntpd-constraint-from
429 (constraints-from openntpd-constraints-from
431 (allow-large-adjustment? openntpd-allow-large-adjustment?
432 (default #f))) ; upstream default
434 (define (openntpd-shepherd-service config)
435 (match-record config <openntpd-configuration>
436 (openntpd listen-on query-from sensor server servers constraint-from
437 constraints-from allow-large-adjustment?)
442 (lambda (field value)
444 (map (cut string-append field <> "\n")
446 '("listen on " "query from " "sensor " "server " "servers "
448 (list listen-on query-from sensor server servers constraint-from))
449 ;; The 'constraints from' field needs to be enclosed in double quotes.
451 (map (cut string-append "constraints from \"" <> "\"\n")
455 (plain-file "ntpd.conf" config))
457 (list (shepherd-service
459 (documentation "Run the Network Time Protocol (NTP) daemon.")
460 (requirement '(user-processes networking))
461 (start #~(make-forkexec-constructor
462 (list (string-append #$openntpd "/sbin/ntpd")
464 "-d" ;; don't daemonize
465 #$@(if allow-large-adjustment?
468 ;; When ntpd is daemonized it repeatedly tries to respawn
469 ;; while running, leading shepherd to disable it. To
470 ;; prevent spamming stderr, redirect output to logfile.
471 #:log-file "/var/log/ntpd"))
472 (stop #~(make-kill-destructor)))))))
474 (define (openntpd-service-activation config)
475 "Return the activation gexp for CONFIG."
476 (with-imported-modules '((guix build utils))
478 (use-modules (guix build utils))
482 (unless (file-exists? "/var/db/ntpd.drift")
483 (with-output-to-file "/var/db/ntpd.drift"
485 (format #t "0.0")))))))
487 (define openntpd-service-type
488 (service-type (name 'openntpd)
490 (list (service-extension shepherd-root-service-type
491 openntpd-shepherd-service)
492 (service-extension account-service-type
493 (const %ntp-accounts))
494 (service-extension profile-service-type
495 (compose list openntpd-configuration-openntpd))
496 (service-extension activation-service-type
497 openntpd-service-activation)))
498 (default-value (openntpd-configuration))
500 "Run the @command{ntpd}, the Network Time Protocol (NTP)
501 daemon, as implemented by @uref{http://www.openntpd.org, OpenNTPD}. The
502 daemon will keep the system clock synchronized with that of the given servers.")))
509 (define-record-type* <inetd-configuration> inetd-configuration
510 make-inetd-configuration
512 (program inetd-configuration-program ;file-like
513 (default (file-append inetutils "/libexec/inetd")))
514 (entries inetd-configuration-entries ;list of <inetd-entry>
517 (define-record-type* <inetd-entry> inetd-entry make-inetd-entry
519 (node inetd-entry-node ;string or #f
521 (name inetd-entry-name) ;string, from /etc/services
523 (socket-type inetd-entry-socket-type) ;stream | dgram | raw |
525 (protocol inetd-entry-protocol) ;string, from /etc/protocols
527 (wait? inetd-entry-wait? ;Boolean
529 (user inetd-entry-user) ;string
531 (program inetd-entry-program ;string or file-like object
532 (default "internal"))
533 (arguments inetd-entry-arguments ;list of strings or file-like objects
536 (define (inetd-config-file entries)
537 (apply mixed-text-file "inetd.conf"
540 (let* ((node (inetd-entry-node entry))
541 (name (inetd-entry-name entry))
543 (if node (string-append node ":" name) name))
545 (match (inetd-entry-socket-type entry)
546 ((or 'stream 'dgram 'raw 'rdm 'seqpacket)
547 (symbol->string (inetd-entry-socket-type entry)))))
548 (protocol (inetd-entry-protocol entry))
549 (wait (if (inetd-entry-wait? entry) "wait" "nowait"))
550 (user (inetd-entry-user entry))
551 (program (inetd-entry-program entry))
552 (args (inetd-entry-arguments entry)))
555 (list #$@(list socket type protocol wait user program) #$@args)
559 (define inetd-shepherd-service
561 (($ <inetd-configuration> program ()) '()) ; empty list of entries -> do nothing
562 (($ <inetd-configuration> program entries)
565 (documentation "Run inetd.")
567 (requirement '(user-processes networking syslogd))
568 (start #~(make-forkexec-constructor
569 (list #$program #$(inetd-config-file entries))
570 #:pid-file "/var/run/inetd.pid"))
571 (stop #~(make-kill-destructor)))))))
573 (define-public inetd-service-type
577 (list (service-extension shepherd-root-service-type
578 inetd-shepherd-service)))
580 ;; The service can be extended with additional lists of entries.
581 (compose concatenate)
582 (extend (lambda (config entries)
585 (entries (append (inetd-configuration-entries config)
588 "Start @command{inetd}, the @dfn{Internet superserver}. It is responsible
589 for listening on Internet sockets and spawning the corresponding services on
597 (define-record-type* <tor-configuration>
598 tor-configuration make-tor-configuration
600 (tor tor-configuration-tor
602 (config-file tor-configuration-config-file
603 (default (plain-file "empty" "")))
604 (hidden-services tor-configuration-hidden-services
606 (socks-socket-type tor-configuration-socks-socket-type ; 'tcp or 'unix
609 (define %tor-accounts
610 ;; User account and groups for Tor.
611 (list (user-group (name "tor") (system? #t))
616 (comment "Tor daemon user")
617 (home-directory "/var/empty")
618 (shell (file-append shadow "/sbin/nologin")))))
620 (define-record-type <hidden-service>
621 (hidden-service name mapping)
623 (name hidden-service-name) ;string
624 (mapping hidden-service-mapping)) ;list of port/address tuples
626 (define (tor-configuration->torrc config)
627 "Return a 'torrc' file for CONFIG."
629 (($ <tor-configuration> tor config-file services socks-socket-type)
632 (with-imported-modules '((guix build utils))
634 (use-modules (guix build utils)
637 (call-with-output-file #$output
640 ### These lines were generated from your system configuration:
642 DataDirectory /var/lib/tor
643 PidFile /var/run/tor/tor.pid
644 Log notice syslog\n" port)
645 (when (eq? 'unix '#$socks-socket-type)
647 SocksPort unix:/var/run/tor/socks-sock
648 UnixSocksGroupWritable 1\n" port))
650 (for-each (match-lambda
651 ((service (ports hosts) ...)
653 HiddenServiceDir /var/lib/tor/hidden-services/~a~%"
655 (for-each (lambda (tcp-port host)
657 HiddenServicePort ~a ~a~%"
660 '#$(map (match-lambda
661 (($ <hidden-service> name mapping)
662 (cons name mapping)))
666 ### End of automatically generated lines.\n\n" port)
668 ;; Append the user's config file.
669 (call-with-input-file #$config-file
671 (dump-port input port)))
674 (define (tor-shepherd-service config)
675 "Return a <shepherd-service> running Tor."
677 (($ <tor-configuration> tor)
678 (let ((torrc (tor-configuration->torrc config)))
679 (with-imported-modules (source-module-closure
680 '((gnu build shepherd)
681 (gnu system file-systems)))
682 (list (shepherd-service
685 ;; Tor needs at least one network interface to be up, hence the
686 ;; dependency on 'loopback'.
687 (requirement '(user-processes loopback syslogd))
689 (modules '((gnu build shepherd)
690 (gnu system file-systems)))
692 (start #~(make-forkexec-constructor/container
693 (list #$(file-append tor "/bin/tor") "-f" #$torrc)
695 #:mappings (list (file-system-mapping
696 (source "/var/lib/tor")
700 (source "/dev/log") ;for syslog
703 (source "/var/run/tor")
706 #:pid-file "/var/run/tor/tor.pid"))
707 (stop #~(make-kill-destructor))
708 (documentation "Run the Tor anonymous network overlay."))))))))
710 (define (tor-activation config)
711 "Set up directories for Tor and its hidden services, if any."
713 (use-modules (guix build utils))
718 (define (initialize service)
719 (let ((directory (string-append "/var/lib/tor/hidden-services/"
722 (chown directory (passwd:uid %user) (passwd:gid %user))
724 ;; The daemon bails out if we give wider permissions.
725 (chmod directory #o700)))
727 ;; Allow Tor to write its PID file.
728 (mkdir-p "/var/run/tor")
729 (chown "/var/run/tor" (passwd:uid %user) (passwd:gid %user))
730 ;; Set the group permissions to rw so that if the system administrator
731 ;; has specified UnixSocksGroupWritable=1 in their torrc file, members
732 ;; of the "tor" group will be able to use the SOCKS socket.
733 (chmod "/var/run/tor" #o750)
735 ;; Allow Tor to access the hidden services' directories.
736 (mkdir-p "/var/lib/tor")
737 (chown "/var/lib/tor" (passwd:uid %user) (passwd:gid %user))
738 (chmod "/var/lib/tor" #o700)
740 ;; Make sure /var/lib is accessible to the 'tor' user.
741 (chmod "/var/lib" #o755)
744 '#$(map hidden-service-name
745 (tor-configuration-hidden-services config)))))
747 (define tor-service-type
748 (service-type (name 'tor)
750 (list (service-extension shepherd-root-service-type
751 tor-shepherd-service)
752 (service-extension account-service-type
753 (const %tor-accounts))
754 (service-extension activation-service-type
757 ;; This can be extended with hidden services.
758 (compose concatenate)
759 (extend (lambda (config services)
763 (append (tor-configuration-hidden-services config)
765 (default-value (tor-configuration))
767 "Run the @uref{https://torproject.org, Tor} anonymous
768 networking daemon.")))
770 (define-deprecated (tor-service #:optional
771 (config-file (plain-file "empty" ""))
774 "Return a service to run the @uref{https://torproject.org, Tor} anonymous
777 The daemon runs as the @code{tor} unprivileged user. It is passed
778 @var{config-file}, a file-like object, with an additional @code{User tor} line
779 and lines for hidden services added via @code{tor-hidden-service}. Run
780 @command{man tor} for information about the configuration file."
781 (service tor-service-type
782 (tor-configuration (tor tor)
783 (config-file config-file))))
785 (define tor-hidden-service-type
786 ;; A type that extends Tor with hidden services.
787 (service-type (name 'tor-hidden-service)
789 (list (service-extension tor-service-type list)))
791 "Define a new Tor @dfn{hidden service}.")))
793 (define (tor-hidden-service name mapping)
794 "Define a new Tor @dfn{hidden service} called @var{name} and implementing
795 @var{mapping}. @var{mapping} is a list of port/host tuples, such as:
798 '((22 \"127.0.0.1:22\")
799 (80 \"127.0.0.1:8080\"))
802 In this example, port 22 of the hidden service is mapped to local port 22, and
803 port 80 is mapped to local port 8080.
805 This creates a @file{/var/lib/tor/hidden-services/@var{name}} directory, where
806 the @file{hostname} file contains the @code{.onion} host name for the hidden
809 See @uref{https://www.torproject.org/docs/tor-hidden-service.html.en, the Tor
810 project's documentation} for more information."
811 (service tor-hidden-service-type
812 (hidden-service name mapping)))
819 (define %wicd-activation
820 ;; Activation gexp for Wicd.
822 (use-modules (guix build utils))
824 (mkdir-p "/etc/wicd")
825 (let ((file-name "/etc/wicd/dhclient.conf.template.default"))
826 (unless (file-exists? file-name)
827 (copy-file (string-append #$wicd file-name)
830 ;; Wicd invokes 'wpa_supplicant', which needs this directory for its
831 ;; named socket files.
832 (mkdir-p "/var/run/wpa_supplicant")
833 (chmod "/var/run/wpa_supplicant" #o750)))
835 (define (wicd-shepherd-service wicd)
836 "Return a shepherd service for WICD."
837 (list (shepherd-service
838 (documentation "Run the Wicd network manager.")
839 (provision '(networking))
840 (requirement '(user-processes dbus-system loopback))
841 (start #~(make-forkexec-constructor
842 (list (string-append #$wicd "/sbin/wicd")
844 (stop #~(make-kill-destructor)))))
846 (define wicd-service-type
847 (service-type (name 'wicd)
849 (list (service-extension shepherd-root-service-type
850 wicd-shepherd-service)
851 (service-extension dbus-root-service-type
853 (service-extension activation-service-type
854 (const %wicd-activation))
856 ;; Add Wicd to the global profile.
857 (service-extension profile-service-type list)))
859 "Run @url{https://launchpad.net/wicd,Wicd}, a network
860 management daemon that aims to simplify wired and wireless networking.")))
862 (define* (wicd-service #:key (wicd wicd))
863 "Return a service that runs @url{https://launchpad.net/wicd,Wicd}, a network
864 management daemon that aims to simplify wired and wireless networking.
866 This service adds the @var{wicd} package to the global profile, providing
867 several commands to interact with the daemon and configure networking:
868 @command{wicd-client}, a graphical user interface, and the @command{wicd-cli}
869 and @command{wicd-curses} user interfaces."
870 (service wicd-service-type wicd))
877 (define-record-type* <modem-manager-configuration>
878 modem-manager-configuration make-modem-manager-configuration
879 modem-manager-configuration?
880 (modem-manager modem-manager-configuration-modem-manager
881 (default modem-manager)))
888 (define-record-type* <network-manager-configuration>
889 network-manager-configuration make-network-manager-configuration
890 network-manager-configuration?
891 (network-manager network-manager-configuration-network-manager
892 (default network-manager))
893 (dns network-manager-configuration-dns
895 (vpn-plugins network-manager-vpn-plugins ;list of <package>
898 (define network-manager-activation
899 ;; Activation gexp for NetworkManager
901 (($ <network-manager-configuration> network-manager dns vpn-plugins)
903 (use-modules (guix build utils))
904 (mkdir-p "/etc/NetworkManager/system-connections")
905 #$@(if (equal? dns "dnsmasq")
906 ;; create directory to store dnsmasq lease file
907 '((mkdir-p "/var/lib/misc"))
910 (define (vpn-plugin-directory plugins)
911 "Return a directory containing PLUGINS, the NM VPN plugins."
912 (directory-union "network-manager-vpn-plugins" plugins))
914 (define network-manager-environment
916 (($ <network-manager-configuration> network-manager dns vpn-plugins)
917 ;; Define this variable in the global environment such that
918 ;; "nmcli connection import type openvpn file foo.ovpn" works.
919 `(("NM_VPN_PLUGIN_DIR"
920 . ,(file-append (vpn-plugin-directory vpn-plugins)
921 "/lib/NetworkManager/VPN"))))))
923 (define network-manager-shepherd-service
925 (($ <network-manager-configuration> network-manager dns vpn-plugins)
926 (let ((conf (plain-file "NetworkManager.conf"
927 (string-append "[main]\ndns=" dns "\n")))
928 (vpn (vpn-plugin-directory vpn-plugins)))
929 (list (shepherd-service
930 (documentation "Run the NetworkManager.")
931 (provision '(networking))
932 (requirement '(user-processes dbus-system wpa-supplicant loopback))
933 (start #~(make-forkexec-constructor
934 (list (string-append #$network-manager
935 "/sbin/NetworkManager")
936 (string-append "--config=" #$conf)
938 #:environment-variables
939 (list (string-append "NM_VPN_PLUGIN_DIR=" #$vpn
940 "/lib/NetworkManager/VPN"))))
941 (stop #~(make-kill-destructor))))))))
943 (define network-manager-service-type
947 (($ <network-manager-configuration> network-manager)
948 (list network-manager)))))
951 (name 'network-manager)
953 (list (service-extension shepherd-root-service-type
954 network-manager-shepherd-service)
955 (service-extension dbus-root-service-type config->package)
956 (service-extension polkit-service-type config->package)
957 (service-extension activation-service-type
958 network-manager-activation)
959 (service-extension session-environment-service-type
960 network-manager-environment)
961 ;; Add network-manager to the system profile.
962 (service-extension profile-service-type config->package)))
963 (default-value (network-manager-configuration))
965 "Run @uref{https://wiki.gnome.org/Projects/NetworkManager,
966 NetworkManager}, a network management daemon that aims to simplify wired and
967 wireless networking."))))
974 (define-record-type* <connman-configuration>
975 connman-configuration make-connman-configuration
976 connman-configuration?
977 (connman connman-configuration-connman
979 (disable-vpn? connman-configuration-disable-vpn?
982 (define (connman-activation config)
983 (let ((disable-vpn? (connman-configuration-disable-vpn? config)))
984 (with-imported-modules '((guix build utils))
986 (use-modules (guix build utils))
987 (mkdir-p "/var/lib/connman/")
988 (unless #$disable-vpn?
989 (mkdir-p "/var/lib/connman-vpn/"))))))
991 (define (connman-shepherd-service config)
992 "Return a shepherd service for Connman"
994 (connman-configuration? config)
995 (let ((connman (connman-configuration-connman config))
996 (disable-vpn? (connman-configuration-disable-vpn? config)))
997 (list (shepherd-service
998 (documentation "Run Connman")
999 (provision '(networking))
1001 '(user-processes dbus-system loopback wpa-supplicant))
1002 (start #~(make-forkexec-constructor
1003 (list (string-append #$connman
1006 #$@(if disable-vpn? '("--noplugin=vpn") '()))
1008 ;; As connman(8) notes, when passing '-n', connman
1009 ;; "directs log output to the controlling terminal in
1010 ;; addition to syslog." Redirect stdout and stderr
1011 ;; to avoid spamming the console (XXX: for some reason
1012 ;; redirecting to /dev/null doesn't work.)
1013 #:log-file "/var/log/connman.log"))
1014 (stop #~(make-kill-destructor)))))))
1016 (define connman-service-type
1017 (let ((connman-package (compose list connman-configuration-connman)))
1018 (service-type (name 'connman)
1020 (list (service-extension shepherd-root-service-type
1021 connman-shepherd-service)
1022 (service-extension polkit-service-type
1024 (service-extension dbus-root-service-type
1026 (service-extension activation-service-type
1028 ;; Add connman to the system profile.
1029 (service-extension profile-service-type
1031 (default-value (connman-configuration))
1033 "Run @url{https://01.org/connman,Connman},
1034 a network connection manager."))))
1041 (define modem-manager-service-type
1042 (let ((config->package
1044 (($ <modem-manager-configuration> modem-manager)
1045 (list modem-manager)))))
1046 (service-type (name 'modem-manager)
1048 (list (service-extension dbus-root-service-type
1050 (service-extension udev-service-type
1052 (service-extension polkit-service-type
1054 (default-value (modem-manager-configuration))
1056 "Run @uref{https://wiki.gnome.org/Projects/ModemManager,
1057 ModemManager}, a modem management daemon that aims to simplify dialup
1065 (define-record-type* <usb-modeswitch-configuration>
1066 usb-modeswitch-configuration make-usb-modeswitch-configuration
1067 usb-modeswitch-configuration?
1068 (usb-modeswitch usb-modeswitch-configuration-usb-modeswitch
1069 (default usb-modeswitch))
1070 (usb-modeswitch-data usb-modeswitch-configuration-usb-modeswitch-data
1071 (default usb-modeswitch-data))
1072 (config-file usb-modeswitch-configuration-config-file
1073 (default #~(string-append #$usb-modeswitch:dispatcher
1074 "/etc/usb_modeswitch.conf"))))
1076 (define (usb-modeswitch-sh usb-modeswitch config-file)
1077 "Build a copy of usb_modeswitch.sh located in package USB-MODESWITCH,
1078 modified to pass the CONFIG-FILE in its calls to usb_modeswitch_dispatcher,
1079 and wrap it to actually find the dispatcher in USB-MODESWITCH. The script
1080 will be run by USB_ModeSwitch’s udev rules file when a modeswitchable USB
1081 device is detected."
1084 (with-imported-modules '((guix build utils))
1086 (use-modules (guix build utils))
1089 #~(string-append " --config-file=" #$config-file)
1092 (install-file (string-append #$usb-modeswitch:dispatcher
1093 "/lib/udev/usb_modeswitch")
1096 ;; insert CFG-PARAM into usb_modeswitch_dispatcher command-lines
1097 (substitute* (string-append #$output "/usb_modeswitch")
1098 (("(exec usb_modeswitch_dispatcher .*)( 2>>)" _ left right)
1099 (string-append left cfg-param right))
1100 (("(exec usb_modeswitch_dispatcher .*)( &)" _ left right)
1101 (string-append left cfg-param right)))
1103 ;; wrap-program needs bash in PATH:
1104 (putenv (string-append "PATH=" #$bash "/bin"))
1105 (wrap-program (string-append #$output "/usb_modeswitch")
1106 `("PATH" ":" = (,(string-append #$coreutils "/bin")
1108 #$usb-modeswitch:dispatcher
1111 (define (usb-modeswitch-configuration->udev-rules config)
1112 "Build a rules file for extending udev-service-type from the rules in the
1113 usb-modeswitch package specified in CONFIG. The rules file will invoke
1114 usb_modeswitch.sh from the usb-modeswitch package, modified to pass the right
1117 (($ <usb-modeswitch-configuration> usb-modeswitch data config-file)
1119 "usb_modeswitch.rules"
1120 (with-imported-modules '((guix build utils))
1122 (use-modules (guix build utils))
1123 (let ((in (string-append #$data "/udev/40-usb_modeswitch.rules"))
1124 (out (string-append #$output "/lib/udev/rules.d"))
1125 (script #$(usb-modeswitch-sh usb-modeswitch config-file)))
1128 (install-file in out)
1129 (substitute* "40-usb_modeswitch.rules"
1130 (("PROGRAM=\"usb_modeswitch")
1131 (string-append "PROGRAM=\"" script "/usb_modeswitch"))
1132 (("RUN\\+=\"usb_modeswitch")
1133 (string-append "RUN+=\"" script "/usb_modeswitch"))))))))))
1135 (define usb-modeswitch-service-type
1137 (name 'usb-modeswitch)
1143 (let ((rules (usb-modeswitch-configuration->udev-rules config)))
1145 (default-value (usb-modeswitch-configuration))
1146 (description "Run @uref{http://www.draisberghof.de/usb_modeswitch/,
1147 USB_ModeSwitch}, a mode switching tool for controlling USB devices with
1148 multiple @dfn{modes}. When plugged in for the first time many USB
1149 devices (primarily high-speed WAN modems) act like a flash storage containing
1150 installers for Windows drivers. USB_ModeSwitch replays the sequence the
1151 Windows drivers would send to switch their mode from storage to modem (or
1152 whatever the thing is supposed to do).")))
1159 (define-record-type* <wpa-supplicant-configuration>
1160 wpa-supplicant-configuration make-wpa-supplicant-configuration
1161 wpa-supplicant-configuration?
1162 (wpa-supplicant wpa-supplicant-configuration-wpa-supplicant ;<package>
1163 (default wpa-supplicant))
1164 (pid-file wpa-supplicant-configuration-pid-file ;string
1165 (default "/var/run/wpa_supplicant.pid"))
1166 (dbus? wpa-supplicant-configuration-dbus? ;Boolean
1168 (interface wpa-supplicant-configuration-interface ;#f | string
1170 (config-file wpa-supplicant-configuration-config-file ;#f | <file-like>
1172 (extra-options wpa-supplicant-configuration-extra-options ;list of strings
1175 (define wpa-supplicant-shepherd-service
1177 (($ <wpa-supplicant-configuration> wpa-supplicant pid-file dbus? interface
1178 config-file extra-options)
1179 (list (shepherd-service
1180 (documentation "Run the WPA supplicant daemon")
1181 (provision '(wpa-supplicant))
1182 (requirement '(user-processes dbus-system loopback syslogd))
1183 (start #~(make-forkexec-constructor
1184 (list (string-append #$wpa-supplicant
1185 "/sbin/wpa_supplicant")
1186 (string-append "-P" #$pid-file)
1187 "-B" ;run in background
1188 "-s" ;log to syslogd
1193 #~((string-append "-i" #$interface))
1196 #~((string-append "-c" #$config-file))
1199 #:pid-file #$pid-file))
1200 (stop #~(make-kill-destructor)))))))
1202 (define wpa-supplicant-service-type
1203 (let ((config->package
1205 (($ <wpa-supplicant-configuration> wpa-supplicant)
1206 (list wpa-supplicant)))))
1207 (service-type (name 'wpa-supplicant)
1209 (list (service-extension shepherd-root-service-type
1210 wpa-supplicant-shepherd-service)
1211 (service-extension dbus-root-service-type config->package)
1212 (service-extension profile-service-type config->package)))
1213 (description "Run the WPA Supplicant daemon, a service that
1214 implements authentication, key negotiation and more for wireless networks.")
1215 (default-value (wpa-supplicant-configuration)))))
1222 (define-record-type* <openvswitch-configuration>
1223 openvswitch-configuration make-openvswitch-configuration
1224 openvswitch-configuration?
1225 (package openvswitch-configuration-package
1226 (default openvswitch)))
1228 (define openvswitch-activation
1230 (($ <openvswitch-configuration> package)
1231 (let ((ovsdb-tool (file-append package "/bin/ovsdb-tool")))
1232 (with-imported-modules '((guix build utils))
1234 (use-modules (guix build utils))
1235 (mkdir-p "/var/run/openvswitch")
1236 (mkdir-p "/var/lib/openvswitch")
1237 (let ((conf.db "/var/lib/openvswitch/conf.db"))
1238 (unless (file-exists? conf.db)
1239 (system* #$ovsdb-tool "create" conf.db)))))))))
1241 (define openvswitch-shepherd-service
1243 (($ <openvswitch-configuration> package)
1244 (let ((ovsdb-server (file-append package "/sbin/ovsdb-server"))
1245 (ovs-vswitchd (file-append package "/sbin/ovs-vswitchd")))
1248 (provision '(ovsdb))
1249 (documentation "Run the Open vSwitch database server.")
1250 (start #~(make-forkexec-constructor
1251 (list #$ovsdb-server "--pidfile"
1252 "--remote=punix:/var/run/openvswitch/db.sock")
1253 #:pid-file "/var/run/openvswitch/ovsdb-server.pid"))
1254 (stop #~(make-kill-destructor)))
1256 (provision '(vswitchd))
1257 (requirement '(ovsdb))
1258 (documentation "Run the Open vSwitch daemon.")
1259 (start #~(make-forkexec-constructor
1260 (list #$ovs-vswitchd "--pidfile")
1261 #:pid-file "/var/run/openvswitch/ovs-vswitchd.pid"))
1262 (stop #~(make-kill-destructor))))))))
1264 (define openvswitch-service-type
1268 (list (service-extension activation-service-type
1269 openvswitch-activation)
1270 (service-extension profile-service-type
1271 (compose list openvswitch-configuration-package))
1272 (service-extension shepherd-root-service-type
1273 openvswitch-shepherd-service)))
1275 "Run @uref{http://www.openvswitch.org, Open vSwitch}, a multilayer virtual
1276 switch designed to enable massive network automation through programmatic
1278 (default-value (openvswitch-configuration))))
1284 (define %iptables-accept-all-rules
1285 (plain-file "iptables-accept-all.rules"
1293 (define-record-type* <iptables-configuration>
1294 iptables-configuration make-iptables-configuration iptables-configuration?
1295 (iptables iptables-configuration-iptables
1297 (ipv4-rules iptables-configuration-ipv4-rules
1298 (default %iptables-accept-all-rules))
1299 (ipv6-rules iptables-configuration-ipv6-rules
1300 (default %iptables-accept-all-rules)))
1302 (define iptables-shepherd-service
1304 (($ <iptables-configuration> iptables ipv4-rules ipv6-rules)
1305 (let ((iptables-restore (file-append iptables "/sbin/iptables-restore"))
1306 (ip6tables-restore (file-append iptables "/sbin/ip6tables-restore")))
1308 (documentation "Packet filtering framework")
1309 (provision '(iptables))
1311 (invoke #$iptables-restore #$ipv4-rules)
1312 (invoke #$ip6tables-restore #$ipv6-rules)))
1314 (invoke #$iptables-restore #$%iptables-accept-all-rules)
1315 (invoke #$ip6tables-restore #$%iptables-accept-all-rules))))))))
1317 (define iptables-service-type
1321 "Run @command{iptables-restore}, setting up the specified rules.")
1323 (list (service-extension shepherd-root-service-type
1324 (compose list iptables-shepherd-service))))))
1326 ;;; networking.scm ends here