From 4963abf63e3d1a3f759459ec096c1faa7f40ef81 Mon Sep 17 00:00:00 2001 From: Marc Weber Date: Fri, 6 Mar 2009 12:27:38 +0000 Subject: [PATCH] Convert "nework-interfaces" svn path=/nixos/branches/fix-style/; revision=14403 --- system/options.nix | 1 + upstart-jobs/default.nix | 6 - upstart-jobs/network-interfaces.nix | 177 +++++++++++++++------------- 3 files changed, 94 insertions(+), 90 deletions(-) diff --git a/system/options.nix b/system/options.nix index 9a502d9502e..0a57304ab2b 100644 --- a/system/options.nix +++ b/system/options.nix @@ -423,6 +423,7 @@ in (import ../upstart-jobs/filesystems.nix) # Mount file systems. (import ../upstart-jobs/swap.nix) + (import ../upstart-jobs/network-interfaces.nix) # security diff --git a/upstart-jobs/default.nix b/upstart-jobs/default.nix index 2de4ba70760..18c4d7f9e86 100644 --- a/upstart-jobs/default.nix +++ b/upstart-jobs/default.nix @@ -71,12 +71,6 @@ let jobs = map makeJob ([ - # Network interfaces. - (import ../upstart-jobs/network-interfaces.nix { - inherit modprobe config; - inherit (pkgs) nettools wirelesstools bash writeText; - }) - # Name service cache daemon. (import ../upstart-jobs/nscd.nix { inherit (pkgs) glibc; diff --git a/upstart-jobs/network-interfaces.nix b/upstart-jobs/network-interfaces.nix index 61469fffbd6..40803639331 100644 --- a/upstart-jobs/network-interfaces.nix +++ b/upstart-jobs/network-interfaces.nix @@ -1,7 +1,11 @@ -{nettools, modprobe, wirelesstools, bash, writeText, config}: +{pkgs, config, ...}: + +###### implementation let + inherit (pkgs) nettools wirelesstools bash writeText; + cfg = config.networking; # !!! use XML @@ -10,91 +14,96 @@ let subnetMasks = map (i: if i ? subnetMask then i.subnetMask else "default") cfg.interfaces; essids = map (i: if i ? essid then i.essid else "default") cfg.interfaces; wepKeys = map (i: if i ? wepKey then i.wepKey else "nokey") cfg.interfaces; + modprobe = config.system.sbin.modprobe; in + { - name = "network-interfaces"; - - job = '' - start on udev - stop on shutdown - - start script - export PATH=${modprobe}/sbin:$PATH - modprobe af_packet || true - - for i in $(cd /sys/class/net && ls -d *); do - echo "Bringing up network device $i..." - ${nettools}/sbin/ifconfig $i up || true - done - - # Configure the manually specified interfaces. - names=(${toString names}) - ipAddresses=(${toString ipAddresses}) - subnetMasks=(${toString subnetMasks}) - essids=(${toString essids}) - wepKeys=(${toString wepKeys}) - - for ((n = 0; n < ''${#names[*]}; n++)); do - name=''${names[$n]} - ipAddress=''${ipAddresses[$n]} - subnetMask=''${subnetMasks[$n]} - essid=''${essids[$n]} - wepKey=''${wepKeys[$n]} - - # Set wireless networking stuff. - if test "$essid" != default; then - ${wirelesstools}/sbin/iwconfig "$name" essid "$essid" || true - fi - - if test "$wepKey" != nokey; then - ${wirelesstools}/sbin/iwconfig "$name" key "$(cat "$wepKey")" || true - fi - - # Set IP address / netmask. - if test "$ipAddress" != dhcp; then - echo "Configuring interface $name..." - extraFlags= - if test "$subnetMask" != default; then - extraFlags="$extraFlags netmask $subnetMask" - fi - ${nettools}/sbin/ifconfig "$name" "$ipAddress" $extraFlags || true - fi - - done - - # Set the nameservers. - if test -n "${toString cfg.nameservers}"; then - rm -f /etc/resolv.conf - if test -n "${cfg.domain}"; then - echo "domain ${cfg.domain}" >> /etc/resolv.conf - fi - for i in ${toString cfg.nameservers}; do - echo "nameserver $i" >> /etc/resolv.conf - done - fi - - # Set the default gateway. - if test -n "${cfg.defaultGateway}"; then - ${nettools}/sbin/route add default gw "${cfg.defaultGateway}" || true - fi - - # Run any user-specified commands. - ${bash}/bin/sh ${writeText "local-net-cmds" cfg.localCommands} || true - - end script - - # Hack: Upstart doesn't yet support what we want: a service that - # doesn't have a running process associated with it. - respawn sleep 100000 - - stop script - for i in $(cd /sys/class/net && ls -d *); do - echo "Taking down network device $i..." - ${nettools}/sbin/ifconfig $i down || true - done - end script - ''; - + services = { + extraJobs = [{ + name = "network-interfaces"; + + job = '' + start on udev + stop on shutdown + + start script + export PATH=${modprobe}/sbin:$PATH + modprobe af_packet || true + + for i in $(cd /sys/class/net && ls -d *); do + echo "Bringing up network device $i..." + ${nettools}/sbin/ifconfig $i up || true + done + + # Configure the manually specified interfaces. + names=(${toString names}) + ipAddresses=(${toString ipAddresses}) + subnetMasks=(${toString subnetMasks}) + essids=(${toString essids}) + wepKeys=(${toString wepKeys}) + + for ((n = 0; n < ''${#names[*]}; n++)); do + name=''${names[$n]} + ipAddress=''${ipAddresses[$n]} + subnetMask=''${subnetMasks[$n]} + essid=''${essids[$n]} + wepKey=''${wepKeys[$n]} + + # Set wireless networking stuff. + if test "$essid" != default; then + ${wirelesstools}/sbin/iwconfig "$name" essid "$essid" || true + fi + + if test "$wepKey" != nokey; then + ${wirelesstools}/sbin/iwconfig "$name" key "$(cat "$wepKey")" || true + fi + + # Set IP address / netmask. + if test "$ipAddress" != dhcp; then + echo "Configuring interface $name..." + extraFlags= + if test "$subnetMask" != default; then + extraFlags="$extraFlags netmask $subnetMask" + fi + ${nettools}/sbin/ifconfig "$name" "$ipAddress" $extraFlags || true + fi + + done + + # Set the nameservers. + if test -n "${toString cfg.nameservers}"; then + rm -f /etc/resolv.conf + if test -n "${cfg.domain}"; then + echo "domain ${cfg.domain}" >> /etc/resolv.conf + fi + for i in ${toString cfg.nameservers}; do + echo "nameserver $i" >> /etc/resolv.conf + done + fi + + # Set the default gateway. + if test -n "${cfg.defaultGateway}"; then + ${nettools}/sbin/route add default gw "${cfg.defaultGateway}" || true + fi + + # Run any user-specified commands. + ${bash}/bin/sh ${writeText "local-net-cmds" cfg.localCommands} || true + + end script + + # Hack: Upstart doesn't yet support what we want: a service that + # doesn't have a running process associated with it. + respawn sleep 100000 + + stop script + for i in $(cd /sys/class/net && ls -d *); do + echo "Taking down network device $i..." + ${nettools}/sbin/ifconfig $i down || true + done + end script + ''; + }]; + }; }