2023-04-05 00:35:49 +02:00
|
|
|
# The ideal is that a Liminix system can boot with only the files in
|
2023-04-15 18:15:44 +02:00
|
|
|
# /nix/store. This package generates a small program that is run at early
|
2023-04-05 00:35:49 +02:00
|
|
|
# boot (from the initramfs) to populate directories such as /etc,
|
|
|
|
# /bin, /home according to whatever the configuration says
|
|
|
|
# they should contain
|
|
|
|
|
|
|
|
{
|
|
|
|
writeText
|
|
|
|
, lib
|
2023-05-17 16:03:45 +02:00
|
|
|
, s6-init-bin
|
2023-05-20 00:53:29 +02:00
|
|
|
, closureInfo
|
2023-04-15 18:15:44 +02:00
|
|
|
, stdenv
|
2023-04-05 00:35:49 +02:00
|
|
|
}:
|
|
|
|
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
|
2023-04-15 00:19:56 +02:00
|
|
|
, uid ? 0
|
|
|
|
, gid ? 0
|
2023-04-05 00:35:49 +02:00
|
|
|
}:
|
|
|
|
let
|
|
|
|
pathname = "${prefix}/${filename}";
|
2023-04-15 18:15:44 +02:00
|
|
|
qpathname = builtins.toJSON pathname;
|
|
|
|
mode' = if mode != null
|
|
|
|
then mode
|
|
|
|
else
|
|
|
|
(if type == "d" then "0755" else "0644");
|
2023-04-05 00:35:49 +02:00
|
|
|
cmds = {
|
2023-04-15 18:15:44 +02:00
|
|
|
"f" = "PRINTFILE(${qpathname}, ${mode'}, ${builtins.toJSON (escaped file)});";
|
|
|
|
"d" = "MKDIR(${qpathname}, ${mode'});\n" +
|
2023-04-05 00:35:49 +02:00
|
|
|
(builtins.concatStringsSep "\n"
|
|
|
|
(visit pathname contents));
|
2023-04-15 18:15:44 +02:00
|
|
|
"c" = "MKNOD_C(${qpathname}, ${mode'}, ${major}, ${minor});";
|
|
|
|
"b" = "MKNOD_B(${qpathname}, ${mode'}, ${major}, ${minor});";
|
|
|
|
"s" = "LN_S(${builtins.toJSON target}, ${qpathname});";
|
|
|
|
"l" = "LN(${builtins.toJSON target}, ${qpathname})";
|
|
|
|
"i" = "MKNOD_P(${qpathname}, ${mode'});";
|
2023-04-05 00:35:49 +02:00
|
|
|
};
|
|
|
|
cmd = cmds.${type};
|
2023-04-15 23:53:28 +02:00
|
|
|
chown = if uid>0 || gid>0
|
|
|
|
then "\nCHOWN(${qpathname},${toString uid},${toString gid});\n"
|
|
|
|
else "";
|
|
|
|
in "${cmd} ${chown}";
|
2023-04-05 00:35:49 +02:00
|
|
|
in mapAttrsToList (makeFile prefix) attrset;
|
2023-04-15 18:15:44 +02:00
|
|
|
activateScript = attrset: writeText "makedevs.c" ''
|
|
|
|
#include "defs.h"
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
chdir(argv[1]);
|
|
|
|
${(builtins.concatStringsSep "\n" (visit "." attrset))}
|
|
|
|
}
|
2023-04-05 00:35:49 +02:00
|
|
|
'';
|
|
|
|
in attrset:
|
2023-05-20 00:53:29 +02:00
|
|
|
let makedevs = activateScript attrset;
|
|
|
|
in stdenv.mkDerivation {
|
2023-04-15 18:15:44 +02:00
|
|
|
name="make-stuff";
|
|
|
|
src = ./.;
|
|
|
|
|
|
|
|
CFLAGS = "-Os";
|
2023-10-16 20:55:17 +02:00
|
|
|
LDFLAGS = "-static -Xlinker -static";
|
2023-04-15 18:15:44 +02:00
|
|
|
|
|
|
|
postConfigure = ''
|
2023-05-20 00:53:29 +02:00
|
|
|
cp ${makedevs} makedevs.c
|
2023-04-15 18:15:44 +02:00
|
|
|
'';
|
|
|
|
makeFlags = ["makedevs"];
|
|
|
|
installPhase = ''
|
2023-05-20 00:53:29 +02:00
|
|
|
closure=${closureInfo { rootPaths = [ makedevs ]; }}
|
|
|
|
mkdir -p $out/bin $out/etc
|
|
|
|
cp $closure/store-paths $out/etc/nix-store-paths
|
2023-04-15 23:53:28 +02:00
|
|
|
$STRIP --remove-section=.note --remove-section=.comment --strip-all makedevs -o $out/bin/activate
|
2023-05-17 16:03:45 +02:00
|
|
|
ln -s ${s6-init-bin}/bin/init $out/bin/init
|
2023-12-24 00:53:47 +01:00
|
|
|
cat > $out/bin/install <<EOF
|
2024-01-07 17:44:36 +01:00
|
|
|
#!/bin/sh -e
|
|
|
|
prefix=\''${1-/}
|
|
|
|
src=\''${prefix}$out
|
2024-01-08 19:58:07 +01:00
|
|
|
dest=\$prefix
|
|
|
|
${# if we are running on a normal mounted system then
|
|
|
|
# the actual device root is mounted on /persist
|
|
|
|
# and /nix is bind mounted from /persist/nix
|
|
|
|
# (see the code in preinit). So we need to check for this
|
|
|
|
# case otherwise we will install into a ramfs/rootfs
|
|
|
|
""
|
|
|
|
}
|
|
|
|
if test -d $dest/persist; then dest=$dest/persist; fi
|
|
|
|
cp -v -fP \$src/bin/* \$src/etc/* \$dest
|
|
|
|
${if attrset ? boot then ''
|
2024-01-09 14:10:02 +01:00
|
|
|
(cd \$dest && rm ./boot && ln -sf ${lib.strings.removePrefix "/" attrset.boot.target} ./boot)
|
2024-01-08 19:58:07 +01:00
|
|
|
'' else ""}
|
2023-12-24 00:53:47 +01:00
|
|
|
EOF
|
|
|
|
chmod +x $out/bin/install
|
2023-04-15 18:15:44 +02:00
|
|
|
'';
|
|
|
|
}
|