8 writeDirectReferencesToFile,
10 writeStringReferencesToFile,
22 defaultSingularity = singularity;
24 lib.makeExtensible (final: {
25 # TODO(@ShamrockLee): Remove after Nixpkgs 24.11 branch-off.
28 "`singularity-tools.shellScript` is deprecated. Use `writeScript`, `writeShellScripts` or `writers.writeBash` instead."
38 # TODO(@ShamrockLee): Remove after Nixpkgs 24.11 branch-off.
41 "`singularity-tools.mkLayer` is deprecated, as it is no longer used to implement `singularity-tools.buildImages`."
46 # May be "apptainer" instead of "singularity"
47 projectName ? (singularity.projectName or "singularity"),
49 runCommand "${projectName}-layer-${name}" { inherit contents; } ''
51 for f in $contents ; do
64 runScript ? "#!${stdenv.shell}\nexec /bin/sh",
65 singularity ? defaultSingularity,
68 projectName = singularity.projectName or "singularity";
69 runAsRootFile = buildPackages.writers.writeBash "run-as-root.sh" ''
73 runScriptFile = writeScript "run-script.sh" ''
79 if builtins ? getContext then
80 lib.splitString "\n" (writeStringReferencesToFile runScriptFile.text).text
82 [ (writeDirectReferencesToFile runScriptFile) ];
83 result = vmTools.runInLinuxVM (
84 runCommand "${projectName}-image-${name}.sif"
92 layerClosure = writeClosure ([ bashInteractive ] ++ runScriptReferences ++ contents);
93 preVM = vmTools.createEmptyImage {
95 fullName = "${projectName}-run-disk";
96 # Leaving "$out" for the Singularity/Container image
97 destination = "disk-image";
104 mkfs -t ext3 -b 4096 /dev/${vmTools.hd}
105 mount /dev/${vmTools.hd} workspace
106 mkdir -p workspace/img
111 ${lib.optionalString (runAsRoot != null) ''
112 mkdir -p ./${builtins.storeDir}
113 mount --rbind "${builtins.storeDir}" ./${builtins.storeDir}
114 unshare -imnpuf --mount-proc chroot ./ ${runAsRootFile}
115 umount -R ./${builtins.storeDir}
118 # Build /bin and copy across closure
119 mkdir -p bin ./${builtins.storeDir}
120 # Loop over the line-separated paths in $layerClosure
121 while IFS= read -r f; do
123 done < "$layerClosure"
125 # TODO(@ShamrockLee):
126 # Once vmTools.runInLinuxVMm works with `__structuredAttrs = true` (#334705),
127 # set __structuredAttrs = true and pass contents as an attribute
128 # so that we could loop with `for c in ''${contents[@]}`
129 # instead of expanding all the paths in contents into the Bash string.
130 for c in ${lib.escapeShellArgs contents} ; do
131 for f in "$c"/bin/* ; do
132 if [ ! -e "bin/$(basename "$f")" ] ; then
139 if [ ! -e bin/sh ]; then
140 ln -s ${lib.getExe bashInteractive} bin/sh
142 mkdir -p .singularity.d
145 cp "${runScriptFile}" .singularity.d/runscript
147 # Fill out .singularity.d
148 mkdir -p .singularity.d/env
149 touch .singularity.d/env/94-appsbase.sh
152 mkdir -p /var/lib/${projectName}/mnt/session
153 echo "root:x:0:0:System administrator:/root:/bin/sh" > /etc/passwd
154 echo > /etc/resolv.conf
155 TMPDIR="$(pwd -P)" ${projectName} build "$out" ./img