From acf656125cdbdd7cfbafb8aba577524ff7e14ea8 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 10 Dec 2006 22:29:44 +0000 Subject: [PATCH] * Lots of refactoring. * Clear the PATH in most scripts. This helps to ensure purity. svn path=/nixos/trunk/; revision=7299 --- boot/boot-stage-1-init.sh | 4 +-- boot/boot-stage-1.nix | 6 ++-- boot/boot-stage-2-init.sh | 22 +++++--------- boot/boot-stage-2.nix | 13 +++++---- boot/make-initrd.sh | 2 +- configuration/activate-configuration.sh | 25 ++++++++++++++-- configuration/boot-environment.nix | 27 ++++++++++------- configuration/switch-to-configuration.sh | 37 ++++++++++++++++++++++++ configuration/system-configuration.nix | 9 ++++-- configuration/system-configuration.sh | 26 ++--------------- installer/grub-menu-builder.sh | 3 ++ installer/nixos-installer.nix | 7 ++--- upgrade.sh | 2 +- upstart-jobs/dhclient.nix | 4 +-- upstart-jobs/xserver.nix | 4 +-- 15 files changed, 117 insertions(+), 74 deletions(-) create mode 100644 configuration/switch-to-configuration.sh diff --git a/boot/boot-stage-1-init.sh b/boot/boot-stage-1-init.sh index d62bd1579750..e3b26127d464 100644 --- a/boot/boot-stage-1-init.sh +++ b/boot/boot-stage-1-init.sh @@ -1,9 +1,9 @@ -#! @shell@ +#! @staticShell@ fail() { # If starting stage 2 failed, start an interactive shell. echo "Stage 2 failed, starting emergency shell..." - exec @shell@ + exec @staticShell@ } diff --git a/boot/boot-stage-1.nix b/boot/boot-stage-1.nix index ef1ebfc72255..f8b84eb6fdb4 100644 --- a/boot/boot-stage-1.nix +++ b/boot/boot-stage-1.nix @@ -4,7 +4,7 @@ # the second boot stage. The closure of the result of this expression # is supposed to be put into an initial RAM disk (initrd). -{ genericSubstituter, shell, staticTools +{ substituteAll, staticShell, staticTools , module_init_tools, extraUtils, modules , # Whether to find root device automatically using its label. @@ -24,10 +24,10 @@ assert !autoDetectRootDevice -> rootDevice != ""; assert autoDetectRootDevice -> rootLabel != ""; -genericSubstituter { +substituteAll { src = ./boot-stage-1-init.sh; isExecutable = true; - inherit shell modules; + inherit staticShell modules; inherit autoDetectRootDevice rootDevice rootLabel; path = [ staticTools diff --git a/boot/boot-stage-2-init.sh b/boot/boot-stage-2-init.sh index 824e89a8a755..4a4a5f6f7e25 100644 --- a/boot/boot-stage-2-init.sh +++ b/boot/boot-stage-2-init.sh @@ -63,32 +63,24 @@ needWritableDir /tmp 01777 needWritableDir /var 0755 needWritableDir /nix/var 0755 -mkdir -m 0755 -p /nix/var/nix/db -mkdir -m 0755 -p /nix/var/nix/gcroots -mkdir -m 0755 -p /nix/var/nix/temproots -mkdir -m 0755 -p /var/log +# Miscellaneous boot time cleanup. +rm -rf /var/run -ln -sf /nix/var/nix/profiles /nix/var/nix/gcroots/ + +# Create the minimal device nodes needed before we run udev. +mknod -m 0666 /dev/null c 1 3 # Run the script that performs all configuration activation that does # not have to be done at boot time. -source @activateConfiguration@ +@activateConfiguration@ # Ensure that the module tools can find the kernel modules. export MODULE_DIR=@kernel@/lib/modules/ -# Miscellaneous cleanup. -rm -rf /var/run -mkdir -m 0755 -p /var/run - -echo -n > /var/run/utmp # must exist -chmod 664 /var/run/utmp - - # Start udev. udevd --daemon @@ -106,4 +98,6 @@ udevsettle # wait for udev to finish # Start Upstart's init. export UPSTART_CFG_DIR=/etc/event.d +export PATH=/empty +for i in @upstartPath@; do PATH=$PATH:$i/bin; done exec @upstart@/sbin/init -v diff --git a/boot/boot-stage-2.nix b/boot/boot-stage-2.nix index 051a71056422..f501c12017f1 100644 --- a/boot/boot-stage-2.nix +++ b/boot/boot-stage-2.nix @@ -1,18 +1,19 @@ -{ genericSubstituter, shell, coreutils, findutils -, gnugrep, utillinux, kernel, udev, upstart +{ substituteAll, coreutils +, utillinux, kernel, udev, upstart , activateConfiguration , # Whether the root device is root only. If so, we'll mount a # ramdisk on /etc, /var and so on. readOnlyRoot + +, # Path for Upstart jobs. Should be quite minimal. + upstartPath }: let startPath = [ coreutils - findutils - gnugrep utillinux udev upstart @@ -20,9 +21,9 @@ let in -genericSubstituter { +substituteAll { src = ./boot-stage-2-init.sh; isExecutable = true; - inherit shell kernel upstart readOnlyRoot activateConfiguration; + inherit kernel upstart readOnlyRoot activateConfiguration upstartPath; inherit startPath; } diff --git a/boot/make-initrd.sh b/boot/make-initrd.sh index 8d07300466b6..45ee036e4e81 100644 --- a/boot/make-initrd.sh +++ b/boot/make-initrd.sh @@ -35,7 +35,7 @@ for ((n = 0; n < ${#objects[*]}; n++)); do mkdir -p $(dirname root/$symlink) ln -s $object$suffix root/$symlink done - + # Put the closure in a gzipped cpio archive. ensureDir $out diff --git a/configuration/activate-configuration.sh b/configuration/activate-configuration.sh index 0aee051503a7..a6971c9f7fc0 100644 --- a/configuration/activate-configuration.sh +++ b/configuration/activate-configuration.sh @@ -1,8 +1,7 @@ #! @shell@ - -# !!! Hack - should be done with udev rules. -chmod 666 /dev/null +export PATH=/empty +for i in @path@; do PATH=$PATH:$i/bin; done # Set up the statically computed bits of /etc. @@ -27,6 +26,15 @@ for i in $(find /etc/ -type l); do done +# Various log directories. +mkdir -m 0755 -p /var/run + +echo -n > /var/run/utmp # must exist +chmod 664 /var/run/utmp + +mkdir -m 0755 -p /var/log + + # Enable a password-less root login. source @accounts@ @@ -93,6 +101,17 @@ fi EOF +# Nix initialisation. +mkdir -m 0755 -p /nix/var/nix/db +mkdir -m 0755 -p /nix/var/nix/gcroots +mkdir -m 0755 -p /nix/var/nix/temproots + +ln -sf /nix/var/nix/profiles /nix/var/nix/gcroots/ + +chown root.nixbld /nix/store +chmod 1775 /nix/store + + # Make a few setuid programs work. wrapperDir=@wrapperDir@ if test -d $wrapperDir; then rm -f $wrapperDir/*; fi diff --git a/configuration/boot-environment.nix b/configuration/boot-environment.nix index 721d21cb668f..f68901087747 100644 --- a/configuration/boot-environment.nix +++ b/configuration/boot-environment.nix @@ -64,13 +64,13 @@ rec { # The init script of boot stage 1 (loading kernel modules for # mounting the root FS). bootStage1 = import ../boot/boot-stage-1.nix { - inherit (pkgs) genericSubstituter; + inherit (pkgs) substituteAll; inherit (pkgsDiet) module_init_tools; inherit extraUtils; inherit autoDetectRootDevice rootDevice rootLabel; inherit stage2Init; modules = modulesClosure; - shell = stdenvLinuxStuff.bootstrapTools.bash; + staticShell = stdenvLinuxStuff.bootstrapTools.bash; staticTools = stdenvLinuxStuff.staticTools; }; @@ -99,9 +99,8 @@ rec { # The installer. nixosInstaller = import ../installer/nixos-installer.nix { - inherit (pkgs) stdenv genericSubstituter; + inherit (pkgs) stdenv substituteAll; inherit nix; - shell = pkgs.bash + "/bin/sh"; }; @@ -127,6 +126,7 @@ rec { # DHCP client. (import ../upstart-jobs/dhclient.nix { + inherit (pkgs) nettools; dhcp = pkgs.dhcpWrapper; }) @@ -142,7 +142,7 @@ rec { # X server. (import ../upstart-jobs/xserver.nix { - inherit (pkgs) genericSubstituter; + inherit (pkgs) substituteAll; inherit (pkgs.xorg) xorgserver xf86inputkeyboard xf86inputmouse xf86videovesa; }) @@ -290,12 +290,10 @@ rec { # The script that activates the configuration, i.e., it sets up # /etc, accounts, etc. It doesn't do anything that can only be done # at boot time (such as start `init'). - activateConfiguration = pkgs.genericSubstituter { + activateConfiguration = pkgs.substituteAll { src = ./activate-configuration.sh; isExecutable = true; - shell = pkgs.bash + "/bin/sh"; - inherit etc; inherit readOnlyRoot; inherit (pkgs) kernel; @@ -303,6 +301,8 @@ rec { wrapperDir = setuidWrapper.wrapperDir; accounts = ../helpers/accounts.sh; + path = [pkgs.coreutils pkgs.gnugrep pkgs.findutils]; + # We don't want to put all of `startPath' and `path' in $PATH, since # then we get an embarrassingly long $PATH. So use the user # environment builder to make a directory with symlinks to those @@ -319,11 +319,16 @@ rec { # The init script of boot stage 2, which is supposed to do # everything else to bring up the system. bootStage2 = import ../boot/boot-stage-2.nix { - inherit (pkgs) genericSubstituter coreutils findutils - gnugrep utillinux kernel udev upstart; - shell = pkgs.bash + "/bin/sh"; + inherit (pkgs) substituteAll coreutils + utillinux kernel udev upstart; inherit readOnlyRoot; inherit activateConfiguration; + upstartPath = [ + pkgs.coreutils + pkgs.findutils + pkgs.gnugrep + pkgs.gnused + ]; }; diff --git a/configuration/switch-to-configuration.sh b/configuration/switch-to-configuration.sh new file mode 100644 index 000000000000..8c03311a7c53 --- /dev/null +++ b/configuration/switch-to-configuration.sh @@ -0,0 +1,37 @@ +#! @shell@ + +set -e +export PATH=/empty +for i in @path@; do PATH=$PATH:$i/bin; done +action="$1" + +if test -z "$action"; then + cat <&2 + fi +fi + +if test "$action" = "switch" -o "$action" = "test"; then + echo "Activating the configuration..." + @out@/activate + kill -TERM 1 # make Upstart reload its events +fi + +sync diff --git a/configuration/system-configuration.nix b/configuration/system-configuration.nix index c470dbf1cff7..39b4f961f163 100644 --- a/configuration/system-configuration.nix +++ b/configuration/system-configuration.nix @@ -34,24 +34,29 @@ rec { inherit upstartJobs; - systemConfiguration = pkgs.stdenv.mkDerivation { + systemConfiguration = pkgs.stdenvNew.mkDerivation { name = "system-configuration"; builder = ./system-configuration.sh; + switchToConfiguration = ./switch-to-configuration.sh; inherit (pkgs) grub coreutils gnused gnugrep diffutils findutils; inherit grubDevice; inherit bootStage2; inherit activateConfiguration; inherit grubMenuBuilder; + inherit etc; kernel = pkgs.kernel + "/vmlinuz"; initrd = initialRamdisk + "/initrd"; inherit extraKernelParams; + # Most of these are needed by grub-install. + path = [pkgs.coreutils pkgs.gnused pkgs.gnugrep pkgs.findutils pkgs.diffutils]; }; - grubMenuBuilder = pkgs.genericSubstituter { + grubMenuBuilder = pkgs.substituteAll { src = ../installer/grub-menu-builder.sh; isExecutable = true; inherit (pkgs) bash; + path = [pkgs.coreutils pkgs.gnused pkgs.gnugrep]; }; diff --git a/configuration/system-configuration.sh b/configuration/system-configuration.sh index 72cbe430e5d8..e1f240e218fd 100644 --- a/configuration/system-configuration.sh +++ b/configuration/system-configuration.sh @@ -7,6 +7,8 @@ ln -s $grub $out/grub ln -s $bootStage2 $out/init ln -s $initrd $out/initrd ln -s $activateConfiguration $out/activate +ln -s $etc/etc $out/etc + echo "$extraKernelParams" > $out/kernel-params cat > $out/menu.lst << GRUBEND @@ -15,27 +17,5 @@ initrd $initrd GRUBEND ensureDir $out/bin - -cat > $out/bin/switch-to-configuration <" diff --git a/installer/nixos-installer.nix b/installer/nixos-installer.nix index 818b3e198876..783a48f40a99 100644 --- a/installer/nixos-installer.nix +++ b/installer/nixos-installer.nix @@ -1,11 +1,10 @@ -{ stdenv, genericSubstituter, shell, nix -}: +{stdenv, substituteAll, nix}: -genericSubstituter { +substituteAll { src = ./nixos-installer.sh; dir = "bin"; isExecutable = true; - inherit shell nix; + inherit nix; pathsFromGraph = ../helpers/paths-from-graph.sh; diff --git a/upgrade.sh b/upgrade.sh index 857b5dc0155f..961bc1959ef0 100755 --- a/upgrade.sh +++ b/upgrade.sh @@ -1,4 +1,4 @@ #! /bin/sh set -e nix-env -p /nix/var/nix/profiles/system -f configuration/system-configuration.nix -i -A systemConfiguration -/nix/var/nix/profiles/system/bin/switch-to-configuration +/nix/var/nix/profiles/system/bin/switch-to-configuration switch diff --git a/upstart-jobs/dhclient.nix b/upstart-jobs/dhclient.nix index 7ccc87615ad0..3b8161820d87 100644 --- a/upstart-jobs/dhclient.nix +++ b/upstart-jobs/dhclient.nix @@ -1,4 +1,4 @@ -{dhcp}: +{dhcp, nettools}: { name = "dhclient"; @@ -23,7 +23,7 @@ script # fi #done - for i in $(ifconfig | grep '^[^ ]' | sed 's/ .*//'); do + for i in $(${nettools}/sbin/ifconfig | grep '^[^ ]' | sed 's/ .*//'); do if test \"$i\" != \"lo\"; then interfaces=\"$interfaces $i\" fi diff --git a/upstart-jobs/xserver.nix b/upstart-jobs/xserver.nix index 4a5fb534819c..8979092cbc29 100644 --- a/upstart-jobs/xserver.nix +++ b/upstart-jobs/xserver.nix @@ -1,4 +1,4 @@ -{ genericSubstituter +{ substituteAll , xorgserver @@ -18,7 +18,7 @@ let - config = genericSubstituter { + config = substituteAll { name = "xserver.conf"; src = ./xserver.conf; };