liminix/pkgs/systemconfig/default.nix
Daniel Barlow 54a1ab3529 support jffs2, with initramfs
the jffs2 filesystem contains only /nix/store and a script which is
run in early init (initramfs) and is responsible for recreating
"traditional" directories (/bin /etc/**/* /var &c) based on the
configuration.

this is tested only in qemu so far and could use some cleanup
2023-04-04 23:35:49 +01:00

60 lines
1.9 KiB
Nix

# The ideal is that a Liminix system can boot with only the files in
# /nix/store. This package generates a script that is run at early
# boot (from the initramfs) to populate directories such as /etc,
# /bin, /home according to whatever the configuration says
# they should contain
{
writeText
, runCommand
, lib
}:
let
inherit (lib.attrsets) mapAttrsToList;
escaped = msg : builtins.replaceStrings
["\n" "=" "\"" "$" ]
["\\x0a" "\\x3d" "\\x22" "\\x24"]
msg;
visit = prefix: attrset:
let makeFile = prefix : filename: {
type ? "f"
, mode ? null
, target ? null
, contents ? null
, file ? null
, major ? null
, minor ? null
}:
let
pathname = "${prefix}/${filename}";
chmod =
let m = if mode != null then mode else
(if type == "d" then "0755" else "0644");
in (if type == "s"
then ""
else "\nchmod ${m} ${pathname}");
cmds = {
"f" = "printf \"${escaped file}\" > ${pathname}";
"d" = "mkdir ${pathname}\n" +
(builtins.concatStringsSep "\n"
(visit pathname contents));
"c" = "mknod ${pathname} c ${major} ${minor}";
"b" = "mknod ${pathname} b ${major} ${minor}";
"s" = "ln -s ${target} ${pathname}";
"l" = "ln ${target} ${pathname}";
"i" = "mknod ${pathname} p";
};
cmd = cmds.${type};
in "${cmd}${chmod}";
in mapAttrsToList (makeFile prefix) attrset;
activateScript = attrset: writeText "systemConfig" ''
#!/bin/sh
t=$1
${(builtins.concatStringsSep "\n" (visit "$t" attrset))}
'';
in attrset:
runCommand "make-stuff" {} ''
mkdir -p $out
ln -s ${activateScript attrset} $out/activate
''