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