services: networking: Fix typo in static networking service.
[jackhill/guix/guix.git] / gnu / services / networking.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu services networking)
20 #:use-module (gnu services)
21 #:use-module (gnu packages admin)
22 #:use-module (gnu packages linux)
23 #:use-module (guix gexp)
24 #:use-module (guix monads)
25 #:export (static-networking-service))
26
27 ;;; Commentary:
28 ;;;
29 ;;; Networking services.
30 ;;;
31 ;;; Code:
32
33 (define* (static-networking-service interface ip
34 #:key
35 gateway
36 (name-servers '())
37 (inetutils inetutils)
38 (net-tools net-tools))
39 "Return a service that starts INTERFACE with address IP. If GATEWAY is
40 true, it must be a string specifying the default network gateway."
41
42 ;; TODO: Eventually we should do this using Guile's networking procedures,
43 ;; like 'configure-qemu-networking' does, but the patch that does this is
44 ;; not yet in stock Guile.
45 (with-monad %store-monad
46 (return
47 (service
48 (documentation
49 (string-append "Set up networking on the '" interface
50 "' interface using a static IP address."))
51 (provision '(networking))
52 (start #~(lambda _
53 ;; Return #t if successfully started.
54 (and (zero? (system* (string-append #$inetutils
55 "/bin/ifconfig")
56 #$interface #$ip "up"))
57 #$(if gateway
58 #~(zero? (system* (string-append #$net-tools
59 "/sbin/route")
60 "add" "-net" "default"
61 "gw" #$gateway))
62 #t)
63 #$(if (pair? name-servers)
64 #~(call-with-output-file "/etc/resolv.conf"
65 (lambda (port)
66 (display
67 "# Generated by 'static-networking-service'.\n"
68 port)
69 (for-each (lambda (server)
70 (format port "nameserver ~a~%"
71 server))
72 '#$name-servers)))
73 #t))))
74 (stop #~(lambda _
75 ;; Return #f is successfully stopped.
76 (not (and (system* (string-append #$inetutils "/bin/ifconfig")
77 #$interface "down")
78 (system* (string-append #$net-tools "/sbin/route")
79 "del" "-net" "default")))))
80 (respawn? #f)))))
81
82 ;;; networking.scm ends here