From f2780fdc6219deb248f01ed3b2d5068f1cfa328d Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <eelco.dolstra@logicblox.com>
Date: Tue, 18 Sep 2007 15:06:24 +0000
Subject: [PATCH] * Add a top-level default.nix for NixOS which has attributes
 useful   for building parts of the system.  E.g.

  $ nix-build /etc/nixos/nixos -A upstartJobs.xserver

  to build the Upstart job for the X server, or

  $ nix-build /etc/nixos/nixos -A kernel

  to build the NixOS kernel.

* /etc/profile.sh: if ~/.nix-defexpr doesn't exist yet, create it as a
  directory and add links to root's channels, /etc/nixos/nixos and
  /etc/nixos/install-source.nix (as "nixpkgs_sys").

* boot.useKernel -> boot.kernel.

svn path=/nixos/trunk/; revision=9334
---
 default.nix                | 40 ++++++++++++++++++++++++++++++++++++++
 etc/profile.sh             | 10 ++++++++--
 installer/nixos-rebuild.sh | 13 +++----------
 system/options.nix         | 16 +++++++--------
 system/system.nix          | 15 ++++++--------
 upstart-jobs/make-job.nix  |  4 ++--
 6 files changed, 67 insertions(+), 31 deletions(-)
 create mode 100644 default.nix

diff --git a/default.nix b/default.nix
new file mode 100644
index 00000000000..90aa4d33ad9
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,40 @@
+let
+
+  configFileName =
+    let env = builtins.getEnv "NIXOS_CONFIG"; in
+    if env == "" then /etc/nixos/configuration.nix else env;
+
+  system = import system/system.nix {configuration = import configFileName;};
+
+in
+
+{ inherit (system)
+    activateConfiguration
+    bootStage1
+    bootStage2
+    etc
+    extraUtils
+    grubMenuBuilder
+    initialRamdisk
+    kernel
+    nix
+    nixosCheckout
+    nixosInstall
+    nixosRebuild
+    system
+    systemPath
+    config
+    ;
+
+  manifests = system.config.get ["installer" "manifests"]; # exported here because nixos-rebuild uses it
+
+  upstartJobsCombined = system.upstartJobs;
+
+  # Make it easier to build individual Upstart jobs (e.g., "nix-build
+  # /etc/nixos/nixos -A upstartJobs.xserver").  
+  upstartJobs = { recurseForDerivations = true; } //
+    builtins.listToAttrs (map (job:
+      { attr = if job ? jobName then job.jobName else job.name; value = job; }
+    ) system.upstartJobs.jobs);
+
+}
diff --git a/etc/profile.sh b/etc/profile.sh
index 81ce4693bee..ad9629c1bd6 100644
--- a/etc/profile.sh
+++ b/etc/profile.sh
@@ -62,9 +62,15 @@ fi
 
 
 # Set up a default Nix expression from which to install stuff.
-if ! test -L $HOME/.nix-defexpr; then
+if test ! -e $HOME/.nix-defexpr -o -L $HOME/.nix-defexpr; then
     echo "creating $HOME/.nix-defexpr" >&2
-    ln -s /etc/nixos/install-source.nix $HOME/.nix-defexpr
+    rm -f $HOME/.nix-defexpr
+    mkdir $HOME/.nix-defexpr
+    ln -s /etc/nixos/install-source.nix $HOME/.nix-defexpr/nixpkgs_sys
+    ln -s /etc/nixos/nixos $HOME/.nix-defexpr/nixos
+    if test "$USER" != root; then
+        ln -s /nix/var/nix/gcroots/per-user/root/channels $HOME/.nix-defexpr/channels_root
+    fi
 fi
 
 
diff --git a/installer/nixos-rebuild.sh b/installer/nixos-rebuild.sh
index 190e21d371c..94ef51ee468 100644
--- a/installer/nixos-rebuild.sh
+++ b/installer/nixos-rebuild.sh
@@ -31,10 +31,7 @@ if test -z "$NIXOS_CONFIG"; then NIXOS_CONFIG=/etc/nixos/configuration.nix; fi
 # Pull the manifests defined in the configuration (the "manifests"
 # attribute).  Wonderfully hacky.
 if test -z "$NIXOS_NO_PULL"; then
-    manifests=$(nix-instantiate --eval-only --xml --strict \
-        $NIXOS/system/system.nix \
-        --arg configuration "import $NIXOS_CONFIG" \
-        -A manifests \
+    manifests=$(nix-instantiate --eval-only --xml --strict $NIXOS -A manifests \
         | grep '<string'  | sed 's^.*"\(.*\)".*^\1^g')
     
     for i in $manifests; do
@@ -47,14 +44,10 @@ fi
 # or "boot"), or just build it and create a symlink "result" in the
 # current directory (for "build" and "test").
 if test "$action" = "switch" -o "$action" = "boot"; then
-    nix-env -p /nix/var/nix/profiles/system -f $NIXOS/system/system.nix \
-        --arg configuration "import $NIXOS_CONFIG" \
-        --set -A system
+    nix-env -p /nix/var/nix/profiles/system -f $NIXOS --set -A system
     pathToConfig=/nix/var/nix/profiles/system
 elif test "$action" = "test" -o "$action" = "build"; then
-    nix-build $NIXOS/system/system.nix \
-        --arg configuration "import $NIXOS_CONFIG" \
-        -A system -K -k
+    nix-build $NIXOS -A system -K -k
     pathToConfig=./result
 else
     showSyntax
diff --git a/system/options.nix b/system/options.nix
index 8815a1a2209..abe1d248259 100644
--- a/system/options.nix
+++ b/system/options.nix
@@ -56,14 +56,14 @@
   }
 
   {
-	name = ["boot" "useKernel"];
-	default = pkgs :(pkgs.kernel);
-	description = "
-	Function that takes package collection and returns kernel 
-	package. Do not collect old generations after changing it
-	until you get to boot successfully. In principle, you can 
-	specify a kernel that will build, but not boot.
-	";
+    name = ["boot" "kernel"];
+    default = pkgs: pkgs.kernel;
+    description = "
+      Function that takes package collection and returns kernel 
+      package. Do not collect old generations after changing it
+      until you get to boot successfully. In principle, you can 
+      specify a kernel that will build, but not boot.
+    ";
   }
 
   {
diff --git a/system/system.nix b/system/system.nix
index 082af065d02..974071c7bf4 100644
--- a/system/system.nix
+++ b/system/system.nix
@@ -31,7 +31,7 @@ rec {
 
   nix = pkgs.nixUnstable; # we need the exportReferencesGraph feature
 
-  useKernel = (config.get ["boot" "useKernel"]) pkgs;
+  kernel = (config.get ["boot" "kernel"]) pkgs;
 
   rootModules = 
     (config.get ["boot" "initrd" "extraKernelModules"]) ++
@@ -41,8 +41,7 @@ rec {
   # Determine the set of modules that we need to mount the root FS.
   modulesClosure = import ../helpers/modules-closure.nix {
     inherit (pkgs) stdenv module_init_tools;
-    kernel = useKernel;
-    inherit rootModules;
+    inherit kernel rootModules;
   };
 
 
@@ -147,7 +146,7 @@ rec {
     src = ./modprobe;
     isExecutable = true;
     inherit (pkgs) module_init_tools;
-    kernel = useKernel;
+    inherit kernel;
   };
 
 
@@ -253,8 +252,7 @@ rec {
     src = ./activate-configuration.sh;
     isExecutable = true;
 
-    inherit etc wrapperDir systemPath modprobe defaultShell;
-    kernel = useKernel;
+    inherit etc wrapperDir systemPath modprobe defaultShell kernel;
     readOnlyRoot = config.get ["boot" "readOnlyRoot"];
     hostName = config.get ["networking" "hostName"];
     setuidPrograms =
@@ -281,8 +279,7 @@ rec {
   bootStage2 = import ../boot/boot-stage-2.nix {
     inherit (pkgs) substituteAll writeText coreutils 
       utillinux udev upstart;
-    kernel = useKernel;
-    inherit activateConfiguration;
+    inherit kernel activateConfiguration;
     readOnlyRoot = config.get ["boot" "readOnlyRoot"];
     upstartPath = [
       pkgs.coreutils
@@ -326,7 +323,7 @@ rec {
     inherit grubMenuBuilder;
     inherit etc;
     inherit systemPath;
-    kernel = useKernel + "/vmlinuz";
+    kernel = kernel + "/vmlinuz";
     initrd = initialRamdisk + "/initrd";
     # Most of these are needed by grub-install.
     path = [
diff --git a/upstart-jobs/make-job.nix b/upstart-jobs/make-job.nix
index faffc0cdcff..e6c92bb4d3a 100644
--- a/upstart-jobs/make-job.nix
+++ b/upstart-jobs/make-job.nix
@@ -5,8 +5,8 @@
     job.jobDrv
   else
     (
-      runCommand job.name {inherit (job) job;}
-        "ensureDir $out/etc/event.d; echo \"$job\" > $out/etc/event.d/$name"
+      runCommand ("upstart-" + job.name) {inherit (job) job; jobName = job.name;}
+        "ensureDir $out/etc/event.d; echo \"$job\" > $out/etc/event.d/$jobName"
     )
 )