services: networking: Set interfaces up; delete default route only when needed.
[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 (provision '(networking))
37 (name-servers '())
38 (inetutils inetutils)
39 (net-tools net-tools))
40 "Return a service that starts @var{interface} with address @var{ip}. If
41 @var{gateway} is true, it must be a string specifying the default network
42 gateway."
43
44 ;; TODO: Eventually we should do this using Guile's networking procedures,
45 ;; like 'configure-qemu-networking' does, but the patch that does this is
46 ;; not yet in stock Guile.
47 (with-monad %store-monad
48 (return
49 (service
50 (documentation
51 (string-append "Set up networking on the '" interface
52 "' interface using a static IP address."))
53 (provision provision)
54 (start #~(lambda _
55 ;; Return #t if successfully started.
56 (and (zero? (system* (string-append #$inetutils
57 "/bin/ifconfig")
58 "-i" #$interface "-A" #$ip
59 "-i" #$interface "--up"))
60 #$(if gateway
61 #~(zero? (system* (string-append #$net-tools
62 "/sbin/route")
63 "add" "-net" "default"
64 "gw" #$gateway))
65 #t)
66 #$(if (pair? name-servers)
67 #~(call-with-output-file "/etc/resolv.conf"
68 (lambda (port)
69 (display
70 "# Generated by 'static-networking-service'.\n"
71 port)
72 (for-each (lambda (server)
73 (format port "nameserver ~a~%"
74 server))
75 '#$name-servers)))
76 #t))))
77 (stop #~(lambda _
78 ;; Return #f is successfully stopped.
79 (not (and (system* (string-append #$inetutils "/bin/ifconfig")
80 #$interface "down")
81 #$(if gateway
82 #~(system* (string-append #$net-tools
83 "/sbin/route")
84 "del" "-net" "default")
85 #t)))))
86 (respawn? #f)))))
87
88 ;;; networking.scm ends here