From 06a6116c44717561d011d1d448961249d104289f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 12 Feb 2007 16:00:55 +0000 Subject: [PATCH] * Allow manual network configuration, i.e., specificying the IP address, gateway, and nameservers in the system configuration. svn path=/nixos/trunk/; revision=7898 --- system/options.nix | 23 ++++++++++++++++-- system/upstart.nix | 16 ++++++++----- upstart-jobs/network-interfaces.nix | 36 ++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/system/options.nix b/system/options.nix index 1a2c80f3403..56bb1fb6763 100644 --- a/system/options.nix +++ b/system/options.nix @@ -201,10 +201,9 @@ name = ["networking" "interfaces"]; default = []; example = [ - { interface = "eth0"; + { name = "eth0"; ipAddress = "131.211.84.78"; netmask = "255.255.255.128"; - gateway = "131.211.84.1"; } ]; description = " @@ -215,6 +214,26 @@ } + { + name = ["networking" "defaultGateway"]; + default = ""; + example = "131.211.84.1"; + description = " + The default gateway. It can be left empty if it is auto-detected through DHCP. + "; + } + + + { + name = ["networking" "nameservers"]; + default = []; + example = ["130.161.158.4" "130.161.33.17"]; + description = " + The list of nameservers. It can be left empty if it is auto-detected through DHCP. + "; + } + + { name = ["fileSystems"]; default = []; diff --git a/system/upstart.nix b/system/upstart.nix index 836a279346f..4e81e10f930 100644 --- a/system/upstart.nix +++ b/system/upstart.nix @@ -59,14 +59,11 @@ import ../upstart-jobs/gather.nix { # Network interfaces. (import ../upstart-jobs/network-interfaces.nix { inherit (pkgs) nettools kernel module_init_tools; + nameservers = config.get ["networking" "nameservers"]; + defaultGateway = config.get ["networking" "defaultGateway"]; + interfaces = config.get ["networking" "interfaces"]; }) - # DHCP client. - (import ../upstart-jobs/dhclient.nix { - inherit (pkgs) nettools; - dhcp = pkgs.dhcpWrapper; - }) - # Nix daemon - required for multi-user Nix. (import ../upstart-jobs/nix-daemon.nix { inherit nix; @@ -93,6 +90,13 @@ import ../upstart-jobs/gather.nix { ] + # DHCP client. + ++ optional ["networking" "useDHCP"] + (import ../upstart-jobs/dhclient.nix { + inherit (pkgs) nettools; + dhcp = pkgs.dhcpWrapper; + }) + # SSH daemon. ++ optional ["services" "sshd" "enable"] (import ../upstart-jobs/sshd.nix { diff --git a/upstart-jobs/network-interfaces.nix b/upstart-jobs/network-interfaces.nix index d85c7e02001..350bd6f6c90 100644 --- a/upstart-jobs/network-interfaces.nix +++ b/upstart-jobs/network-interfaces.nix @@ -1,5 +1,15 @@ # !!! Don't like it that I have to pass the kernel here. -{nettools, kernel, module_init_tools}: +{ nettools, kernel, module_init_tools +, nameservers, defaultGateway, interfaces +}: + +let + + # !!! use XML + names = map (i: i.name) interfaces; + ipAddresses = map (i: i.ipAddress) interfaces; + +in { name = "network-interfaces"; @@ -18,6 +28,30 @@ start script ${nettools}/sbin/ifconfig $i up || true done + # Configure the manually specified interfaces. + names=(${toString names}) + ipAddresses=(${toString ipAddresses}) + + for ((n = 0; n < \${#names[*]}; n++)); do + name=\${names[$n]} + ipAddress=\${ipAddresses[$n]} + echo \"Configuring interface $i...\" + ${nettools}/sbin/ifconfig \"$name\" up \"$ipAddress\" || true + done + + # Set the nameservers. + if test -n \"${toString nameservers}\"; then + rm -f /etc/resolv.conf + for i in ${toString nameservers}; do + echo \"nameserver $i\" >> /etc/resolv.conf + done + fi + + # Set the default gateway. + if test -n \"${defaultGateway}\"; then + ${nettools}/sbin/route add default gw \"${defaultGateway}\" || true + fi + end script # Hack: Upstart doesn't yet support what we want: a service that