nixpkgs/pkgs/build-support/singularity-tools/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.2 KiB
Nix
Raw Normal View History

{
lib,
# Build helpers
stdenv,
runCommand,
vmTools,
writeClosure,
writeScript,
# Native build inputs
e2fsprogs,
gawk,
util-linux,
# Build inputs
bash,
runtimeShell,
singularity,
storeDir ? builtins.storeDir,
2023-01-28 17:02:47 +00:00
}:
2016-11-14 01:26:59 +00:00
rec {
shellScript =
name: text:
2016-11-14 01:26:59 +00:00
writeScript name ''
#!${runtimeShell}
2016-11-14 01:26:59 +00:00
set -e
${text}
'';
2023-01-28 17:02:47 +00:00
mkLayer =
{
name,
contents ? [ ],
# May be "apptainer" instead of "singularity"
projectName ? (singularity.projectName or "singularity"),
2023-01-28 17:02:47 +00:00
}:
runCommand "${projectName}-layer-${name}" { inherit contents; } ''
2016-11-14 01:26:59 +00:00
mkdir $out
for f in $contents ; do
cp -ra $f $out/
done
'';
2023-01-28 17:02:47 +00:00
buildImage =
let
defaultSingularity = singularity;
in
{
name,
contents ? [ ],
diskSize ? 1024,
memSize ? 1024,
runAsRoot ? null,
runScript ? "#!${stdenv.shell}\nexec /bin/sh",
singularity ? defaultSingularity,
2023-01-28 17:02:47 +00:00
}:
let
projectName = singularity.projectName or "singularity";
2023-01-28 17:02:47 +00:00
runAsRootFile = shellScript "run-as-root.sh" runAsRoot;
runScriptFile = shellScript "run-script.sh" runScript;
result = vmTools.runInLinuxVM (
runCommand "${projectName}-image-${name}.img"
2023-01-28 17:02:47 +00:00
{
buildInputs = [
singularity
e2fsprogs
util-linux
gawk
];
layerClosure = writeClosure contents;
2016-11-14 01:26:59 +00:00
preVM = vmTools.createEmptyImage {
size = diskSize;
fullName = "${projectName}-run-disk";
2016-11-14 01:26:59 +00:00
};
inherit memSize;
2016-11-14 01:26:59 +00:00
}
''
rm -rf $out
mkdir disk
mkfs -t ext3 -b 4096 /dev/${vmTools.hd}
mount /dev/${vmTools.hd} disk
2020-01-30 23:44:01 +00:00
mkdir -p disk/img
cd disk/img
2017-11-07 03:47:20 +00:00
mkdir proc sys dev
2016-11-14 01:26:59 +00:00
# Run root script
2021-01-24 00:40:18 +00:00
${lib.optionalString (runAsRoot != null) ''
2016-11-14 01:26:59 +00:00
mkdir -p ./${storeDir}
mount --rbind ${storeDir} ./${storeDir}
unshare -imnpuf --mount-proc chroot ./ ${runAsRootFile}
umount -R ./${storeDir}
''}
# Build /bin and copy across closure
mkdir -p bin ./${builtins.storeDir}
2016-11-14 01:26:59 +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
2016-11-14 01:26:59 +00:00
if [ ! -e bin/$(basename $f) ] ; then
ln -s $f bin/
fi
done
done
2020-01-30 23:44:01 +00:00
# Create runScript and link shell
if [ ! -e bin/sh ]; then
ln -s ${runtimeShell} bin/sh
fi
mkdir -p .${projectName}.d
ln -s ${runScriptFile} .${projectName}.d/runscript
2016-11-14 01:26:59 +00:00
# Fill out .${projectName}.d
mkdir -p .${projectName}.d/env
touch .${projectName}.d/env/94-appsbase.sh
2016-11-14 01:26:59 +00:00
2018-11-01 00:21:02 +00:00
cd ..
mkdir -p /var/lib/${projectName}/mnt/session
2018-11-01 00:21:02 +00:00
echo "root:x:0:0:System administrator:/root:/bin/sh" > /etc/passwd
2020-07-15 12:05:37 +00:00
echo > /etc/resolv.conf
TMPDIR=$(pwd -P) ${projectName} build $out ./img
''
);
2016-11-14 01:26:59 +00:00
2023-01-28 17:02:47 +00:00
in
result;
2016-11-14 01:26:59 +00:00
}