2020-04-24 23:36:52 +00:00
|
|
|
{ runCommand
|
2021-02-05 17:12:51 +00:00
|
|
|
, lib
|
2020-04-24 23:36:52 +00:00
|
|
|
, stdenv
|
|
|
|
, storeDir ? builtins.storeDir
|
|
|
|
, writeScript
|
|
|
|
, singularity
|
|
|
|
, writeReferencesToFile
|
|
|
|
, bash
|
|
|
|
, vmTools
|
|
|
|
, gawk
|
2020-11-24 20:58:05 +00:00
|
|
|
, util-linux
|
2020-04-24 23:36:52 +00:00
|
|
|
, runtimeShell
|
2023-02-09 11:40:11 +00:00
|
|
|
, e2fsprogs
|
|
|
|
}:
|
2020-04-24 23:36:52 +00:00
|
|
|
rec {
|
|
|
|
shellScript = name: text:
|
|
|
|
writeScript name ''
|
|
|
|
#!${runtimeShell}
|
|
|
|
set -e
|
|
|
|
${text}
|
|
|
|
'';
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
mkLayer =
|
|
|
|
{ name
|
|
|
|
, contents ? [ ]
|
|
|
|
# May be "apptainer" instead of "singularity"
|
|
|
|
, projectName ? (singularity.projectName or "singularity")
|
|
|
|
}:
|
|
|
|
runCommand "${projectName}-layer-${name}"
|
|
|
|
{
|
|
|
|
inherit contents;
|
|
|
|
} ''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir $out
|
|
|
|
for f in $contents ; do
|
|
|
|
cp -ra $f $out/
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
buildImage =
|
|
|
|
let
|
|
|
|
defaultSingularity = singularity;
|
|
|
|
in
|
|
|
|
{ name
|
|
|
|
, contents ? [ ]
|
|
|
|
, diskSize ? 1024
|
|
|
|
, runScript ? "#!${stdenv.shell}\nexec /bin/sh"
|
|
|
|
, runAsRoot ? null
|
|
|
|
, memSize ? 512
|
|
|
|
, singularity ? defaultSingularity
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
projectName = singularity.projectName or "singularity";
|
|
|
|
layer = mkLayer {
|
|
|
|
inherit name;
|
|
|
|
contents = contents ++ [ bash runScriptFile ];
|
|
|
|
inherit projectName;
|
|
|
|
};
|
|
|
|
runAsRootFile = shellScript "run-as-root.sh" runAsRoot;
|
|
|
|
runScriptFile = shellScript "run-script.sh" runScript;
|
|
|
|
result = vmTools.runInLinuxVM (
|
|
|
|
runCommand "${projectName}-image-${name}.img"
|
|
|
|
{
|
2020-11-24 20:58:05 +00:00
|
|
|
buildInputs = [ singularity e2fsprogs util-linux gawk ];
|
2020-04-24 23:36:52 +00:00
|
|
|
layerClosure = writeReferencesToFile layer;
|
|
|
|
preVM = vmTools.createEmptyImage {
|
|
|
|
size = diskSize;
|
2023-02-09 11:40:11 +00:00
|
|
|
fullName = "${projectName}-run-disk";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2022-03-05 16:20:37 +00:00
|
|
|
inherit memSize;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
''
|
|
|
|
rm -rf $out
|
|
|
|
mkdir disk
|
|
|
|
mkfs -t ext3 -b 4096 /dev/${vmTools.hd}
|
|
|
|
mount /dev/${vmTools.hd} disk
|
|
|
|
mkdir -p disk/img
|
|
|
|
cd disk/img
|
|
|
|
mkdir proc sys dev
|
|
|
|
|
|
|
|
# Run root script
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (runAsRoot != null) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p ./${storeDir}
|
|
|
|
mount --rbind ${storeDir} ./${storeDir}
|
|
|
|
unshare -imnpuf --mount-proc chroot ./ ${runAsRootFile}
|
|
|
|
umount -R ./${storeDir}
|
|
|
|
''}
|
|
|
|
|
|
|
|
# Build /bin and copy across closure
|
2021-12-06 16:07:01 +00:00
|
|
|
mkdir -p bin ./${builtins.storeDir}
|
2020-04-24 23:36:52 +00:00
|
|
|
for f in $(cat $layerClosure) ; do
|
|
|
|
cp -ar $f ./$f
|
|
|
|
done
|
|
|
|
|
|
|
|
for c in ${toString contents} ; do
|
|
|
|
for f in $c/bin/* ; do
|
|
|
|
if [ ! -e bin/$(basename $f) ] ; then
|
|
|
|
ln -s $f bin/
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
# Create runScript and link shell
|
2020-09-25 04:45:31 +00:00
|
|
|
if [ ! -e bin/sh ]; then
|
|
|
|
ln -s ${runtimeShell} bin/sh
|
|
|
|
fi
|
2023-02-09 11:40:11 +00:00
|
|
|
mkdir -p .${projectName}.d
|
|
|
|
ln -s ${runScriptFile} .${projectName}.d/runscript
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
# Fill out .${projectName}.d
|
|
|
|
mkdir -p .${projectName}.d/env
|
|
|
|
touch .${projectName}.d/env/94-appsbase.sh
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cd ..
|
2023-02-09 11:40:11 +00:00
|
|
|
mkdir -p /var/lib/${projectName}/mnt/{container,final,overlay,session,source}
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "root:x:0:0:System administrator:/root:/bin/sh" > /etc/passwd
|
2020-08-20 17:08:02 +00:00
|
|
|
echo > /etc/resolv.conf
|
2023-02-09 11:40:11 +00:00
|
|
|
TMPDIR=$(pwd -P) ${projectName} build $out ./img
|
2020-04-24 23:36:52 +00:00
|
|
|
'');
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
in
|
|
|
|
result;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|