From c6c6b51893f86e0beb0d66b5a6d5ac765340bdaf Mon Sep 17 00:00:00 2001
From: Daniel Barlow <dan@telent.net>
Date: Mon, 26 Sep 2022 15:59:21 +0100
Subject: [PATCH] make s6-rc-database a package

---
 make-image.nix                  | 39 +++++++++------------------------
 overlay.nix                     |  2 ++
 pkgs/s6-rc-database/default.nix | 31 ++++++++++++++++++++++++++
 tests/smoke/run.sh              |  3 +--
 4 files changed, 44 insertions(+), 31 deletions(-)
 create mode 100644 pkgs/s6-rc-database/default.nix

diff --git a/make-image.nix b/make-image.nix
index c033b2429..e2734a3b3 100644
--- a/make-image.nix
+++ b/make-image.nix
@@ -2,45 +2,24 @@ pkgs: config:
 let
   inherit (pkgs)
     callPackage
-    closureInfo
     lib
     runCommand
     s6-rc
+    s6-init-files
     stdenv
     stdenvNoCC
     writeScript
     writeText;
 
-  # we need to generate s6 db,  by generating closure of all
-  # config.services and calling s6-rc-compile on them
-  allServices = closureInfo {
-    rootPaths = builtins.attrValues config.services;
+  s6-rc-db = pkgs.s6-rc-database.override {
+    services = builtins.attrValues config.services;
   };
-  s6db = stdenvNoCC.mkDerivation  {
-    name = "s6-rc-db";
-    nativeBuildInputs = [pkgs.buildPackages.s6-rc];
-    builder = writeText "find-s6-services" ''
-    source $stdenv/setup
-    mkdir -p $out
-    srcs=""
-    shopt -s nullglob
-    for i in $(cat  ${allServices}/store-paths ); do
-      if test -d $i; then
-        for j in $i/* ; do
-          if test -f $j/type ; then
-            srcs="$srcs $i"
-          fi
-        done
-      fi
-    done
-    s6-rc-compile $out/compiled $srcs
-    '';
-  };
-  s6-pseudofiles = pkgs.s6-init-files;
+
   profile  = writeScript ".profile" ''
     PATH=${lib.makeBinPath (with pkgs; [ s6-init-bin busybox execline s6-linux-init s6-rc])}
     export PATH
   '';
+
   pseudofiles = writeText "pseudofiles" ''
      / d 0755 0 0
      /bin d 0755 0 0
@@ -63,12 +42,14 @@ let
      /bin/sh s 0755 0 0 ${pkgs.busybox}/bin/sh
      /bin/busybox s 0755 0 0 ${pkgs.busybox}/bin/busybox
      /etc/s6-rc d 0755 0 0
-     /etc/s6-rc/compiled s 0755 0 0 ${s6db}/compiled
+     /etc/s6-rc/compiled s 0755 0 0 ${s6-rc-db}/compiled
      /etc/passwd f 0644 0 0 echo  "root::0:0:root:/:/bin/sh"
      /.profile s 0644 0 0 ${profile}
   '';
   storefs = callPackage <nixpkgs/nixos/lib/make-squashfs.nix> {
-    storeContents = [ pseudofiles s6-pseudofiles s6db pkgs.s6-linux-init ] ++ config.packages ;
+    # add pseudofiles to store so that the packages they
+    # depend on are also added
+    storeContents = [ pseudofiles s6-init-files ] ++ config.packages ;
     # comp =  "xz -Xdict-size 100%"
   };
 in runCommand "frob-squashfs" {
@@ -77,6 +58,6 @@ in runCommand "frob-squashfs" {
     cp ${storefs} ./store.img
     chmod +w store.img
     mksquashfs - store.img -no-recovery -quiet -no-progress  -root-becomes store -p "/ d 0755 0 0"
-    mksquashfs - store.img -no-recovery -quiet -no-progress  -root-becomes nix  -pf ${pseudofiles} -pf ${s6-pseudofiles}
+    mksquashfs - store.img -no-recovery -quiet -no-progress  -root-becomes nix  -pf ${pseudofiles} -pf ${s6-init-files}
     cp store.img $out
   ''
diff --git a/overlay.nix b/overlay.nix
index 22c984466..49c9c4ab2 100644
--- a/overlay.nix
+++ b/overlay.nix
@@ -10,6 +10,8 @@ final: prev: {
 
   s6-init-bin =  final.callPackage ./pkgs/s6-init-bin {};
 
+  s6-rc-database = final.callPackage ./pkgs/s6-rc-database {};
+
   pppoe = prev.rpPPPoE.overrideAttrs (o: {
     # use newer rp-pppoe, it builds cleanly
     src = final.fetchFromGitHub {
diff --git a/pkgs/s6-rc-database/default.nix b/pkgs/s6-rc-database/default.nix
new file mode 100644
index 000000000..9983bbf57
--- /dev/null
+++ b/pkgs/s6-rc-database/default.nix
@@ -0,0 +1,31 @@
+# generate s6-rc database,  by generating closure of all
+# config.services and calling s6-rc-compile on them
+
+{
+  stdenvNoCC
+, buildPackages
+, closureInfo
+, writeText
+, services ? []
+}:
+let closure-info = closureInfo { rootPaths = services; };
+in stdenvNoCC.mkDerivation  {
+  name = "s6-rc-database";
+  nativeBuildInputs = [buildPackages.s6-rc];
+  builder = writeText "find-s6-services" ''
+    source $stdenv/setup
+    mkdir -p $out
+    srcs=""
+    shopt -s nullglob
+    for i in $(cat  ${closure-info}/store-paths ); do
+      if test -d $i; then
+        for j in $i/* ; do
+          if test -f $j/type ; then
+            srcs="$srcs $i"
+          fi
+        done
+      fi
+    done
+    s6-rc-compile $out/compiled $srcs
+    '';
+  }
diff --git a/tests/smoke/run.sh b/tests/smoke/run.sh
index 8f0bc4e42..4742a6ef5 100755
--- a/tests/smoke/run.sh
+++ b/tests/smoke/run.sh
@@ -10,7 +10,7 @@ trap cleanup EXIT
 trap 'echo "command $(eval echo $BASH_COMMAND) failed with exit code $?"; exit $?' ERR
 unsquashfs -q -d $dest_path -excludes smoke.img  /dev
 cd $dest_path;
-db=nix/store/*-s6-rc-db/compiled/
+db=nix/store/*-s6-rc-database/compiled/
 test -d $db
 chmod -R +w $db
 # check we have closure of config.services (lo.link.service exists only
@@ -21,5 +21,4 @@ echo OK
 EOF
      )
 
-
 nix-shell -p s6-rc -p squashfsTools --run "$TESTS" || exit 1