Author: eelco
Date: Mon Sep 13 17:26:42 2010
New Revision: 23766
URL: https://svn.nixos.org/websvn/nix/?rev=23766&sc=1

Log:
* Speed up the initialisation of /etc in the activation script a lot
  by doing it in Perl instead of in Bash.  Forking a zillion times is
  slow.

Added:
   nixos/trunk/modules/system/etc/setup-etc.pl
Modified:
   nixos/trunk/modules/system/etc/etc.nix

Modified: nixos/trunk/modules/system/etc/etc.nix
==============================================================================
--- nixos/trunk/modules/system/etc/etc.nix      Mon Sep 13 16:06:07 2010        
(r23765)
+++ nixos/trunk/modules/system/etc/etc.nix      Mon Sep 13 17:26:42 2010        
(r23766)
@@ -37,7 +37,7 @@
       chmod ${mode} "$target"
     '';
 
-  makeEtc = pkgs.stdenv.mkDerivation {
+  etc = pkgs.stdenv.mkDerivation {
     name = "etc";
 
     builder = ./make-etc.sh;
@@ -53,39 +53,13 @@
 {
   require = [option];
 
-  system.build.etc = makeEtc;
+  system.build.etc = etc;
 
   system.activationScripts.etc = stringAfter [ "systemConfig" "stdio" ]
     ''
       # Set up the statically computed bits of /etc.
       echo "setting up /etc..."
-      staticEtc=/etc/static
-      rm -f $staticEtc
-      ln -s ${makeEtc}/etc $staticEtc
-      for i in $(cd $staticEtc && find * -type l); do
-          mkdir -p /etc/$(dirname $i)
-          rm -f /etc/$i
-          if test -e "$staticEtc/$i.mode"; then
-              # Create a regular file in /etc.
-              cp $staticEtc/$i /etc/$i
-              chown 0.0 /etc/$i
-              chmod "$(cat "$staticEtc/$i.mode")" /etc/$i
-          else
-              # Create a symlink in /etc.
-              ln -s $staticEtc/$i /etc/$i
-          fi
-      done
-
-      # Remove dangling symlinks that point to /etc/static.  These are
-      # configuration files that existed in a previous configuration but not
-      # in the current one.  For efficiency, don't look under /etc/nixos
-      # (where all the NixOS sources live).
-      for i in $(find /etc/ \( -path /etc/nixos -prune \) -o -type l); do
-          target=$(readlink "$i")
-          if test "''${target:0:''${#staticEtc}}" = "$staticEtc" -a ! -e "$i"; 
then
-              rm -f "$i"
-          fi
-      done
+      ${pkgs.perl}/bin/perl ${./setup-etc.pl} ${etc}/etc
     '';
 
 }

Added: nixos/trunk/modules/system/etc/setup-etc.pl
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ nixos/trunk/modules/system/etc/setup-etc.pl Mon Sep 13 17:26:42 2010        
(r23766)
@@ -0,0 +1,65 @@
+use strict;
+use File::Find;
+use File::Copy;
+use File::Path;
+use File::Basename;
+
+my $etc = $ARGV[0] or die;
+my $static = "/etc/static";
+
+sub atomicSymlink {
+    my ($source, $target) = @_;
+    my $tmp = "$target.tmp";
+    unlink $tmp;
+    symlink $source, $tmp or return 1;
+    rename $tmp, $target or return 1;
+    return 1;
+}
+
+
+# Atomically update /etc/static to point at the etc files of the
+# current configuration.
+atomicSymlink $etc, $static or die;
+
+
+# For every file in the etc tree, create a corresponding symlink in
+# /etc to /etc/static.  The indirection through /etc/static is to make
+# switching to a new configuration somewhat more atomic.
+sub link {
+    my $fn = substr $File::Find::name, length($etc) + 1 or next;
+    my $target = "/etc/$fn";
+    File::Path::make_path(dirname $target);
+    if (-e "$_.mode") {
+        open MODE, "<$_.mode";
+        my $mode = <MODE>; chomp $mode;
+        close MODE;
+        copy "$static/$fn", "$target.tmp" or warn;
+        chmod oct($mode), "$target.tmp" or warn;
+        rename "$target.tmp", $target or warn;
+    } elsif (-l "$_") {
+        atomicSymlink "$static/$fn", $target or warn;
+    }
+}
+
+find(\&link, $etc);
+
+
+# Remove dangling symlinks that point to /etc/static.  These are
+# configuration files that existed in a previous configuration but not
+# in the current one.  For efficiency, don't look under /etc/nixos
+# (where all the NixOS sources live).
+sub cleanup {
+    if ($File::Find::name eq "/etc/nixos") {
+        $File::Find::prune = 1;
+        return;
+    }
+    if (-l $_) {
+        my $target = readlink $_;
+        if (substr($target, 0, length $static) eq $static) {
+            my $x = "/etc/static/" . substr($File::Find::name, length "/etc/");
+            unlink "$_" unless -e "$x";
+        }
+    }
+}
+
+find(\&cleanup, "/etc");
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to