From 04121437bea46a74c71eaca507007a4e9c9ac20a Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Mon, 14 Nov 2016 12:26:59 +1100 Subject: [PATCH] singularity: init 2.2 --- lib/maintainers.nix | 1 + .../virtualization/singularity/default.nix | 25 +++++ .../singularity-tools/default.nix | 100 ++++++++++++++++++ pkgs/top-level/all-packages.nix | 4 + 4 files changed, 130 insertions(+) create mode 100644 pkgs/applications/virtualization/singularity/default.nix create mode 100644 pkgs/build-support/singularity-tools/default.nix diff --git a/lib/maintainers.nix b/lib/maintainers.nix index b15f4542fb5..73e23170d82 100644 --- a/lib/maintainers.nix +++ b/lib/maintainers.nix @@ -198,6 +198,7 @@ jagajaga = "Arseniy Seroka "; javaguirre = "Javier Aguirre "; jb55 = "William Casarin "; + jbedo = "Justin Bedő "; jcumming = "Jack Cummings "; jefdaj = "Jeffrey David Johnson "; jfb = "James Felix Black "; diff --git a/pkgs/applications/virtualization/singularity/default.nix b/pkgs/applications/virtualization/singularity/default.nix new file mode 100644 index 00000000000..e318a0b64d7 --- /dev/null +++ b/pkgs/applications/virtualization/singularity/default.nix @@ -0,0 +1,25 @@ +{ stdenv +, fetchFromGitHub +, autoreconfHook }: + +stdenv.mkDerivation rec { + name = "singularity-${version}"; + version = "2.2"; + + src = fetchFromGitHub { + owner = "singularityware"; + repo = "singularity"; + rev = version; + sha256 = "19g43gfdy5s8y4252474cp39d6ypn5dd37wp0s21fgd13vqy26px"; + }; + + buildInputs = [ autoreconfHook ]; + + meta = with stdenv.lib; { + homepage = http://singularity.lbl.gov/; + description = "Designed around the notion of extreme mobility of compute and reproducible science, Singularity enables users to have full control of their operating system environment"; + license = "BSD license with 2 modifications"; + platforms = platforms.linux; + maintainers = [ maintainers.jbedo ]; + }; +} diff --git a/pkgs/build-support/singularity-tools/default.nix b/pkgs/build-support/singularity-tools/default.nix new file mode 100644 index 00000000000..3c27b9fc1ad --- /dev/null +++ b/pkgs/build-support/singularity-tools/default.nix @@ -0,0 +1,100 @@ +{ runCommand +, stdenv +, storeDir ? builtins.storeDir +, writeScript +, singularity +, writeReferencesToFile +, bash +, vmTools +, gawk +, utillinux +, e2fsprogs +, squashfsTools }: + +rec { + shellScript = name: text: + writeScript name '' + #!${stdenv.shell} + set -e + ${text} + ''; + + mkLayer = { + name, + contents ? [], + }: + runCommand "singularity-layer-${name}" { + inherit contents; + } '' + mkdir $out + for f in $contents ; do + cp -ra $f $out/ + done + ''; + + buildImage = { + name, + contents ? [], + diskSize ? 1024, + runScript ? "#!${stdenv.shell}\nexec /bin/sh", + runAsRoot ? null, + extraSpace ? 0 + }: + let layer = mkLayer { + inherit name; + contents = contents ++ [ bash runScriptFile ]; + }; + runAsRootFile = shellScript "run-as-root.sh" runAsRoot; + runScriptFile = shellScript "run-script.sh" runScript; + result = vmTools.runInLinuxVM ( + runCommand "singularity-image-${name}.img" { + buildInputs = [ singularity e2fsprogs utillinux gawk ]; + layerClosure = writeReferencesToFile layer; + preVM = vmTools.createEmptyImage { + size = diskSize; + fullName = "singularity-run-disk"; + }; + } + '' + rm -rf $out + mkdir disk + mkfs -t ext3 -b 4096 /dev/${vmTools.hd} + mount /dev/${vmTools.hd} disk + cd disk + + # Run root script + ${stdenv.lib.optionalString (runAsRoot != null) '' + 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 nix/store + for f in $(cat $layerClosure) ; do + cp -ar $f ./$f + for f in $f/bin/* ; do + if [ ! -e bin/$(basename $f) ] ; then + ln -s $f bin/ + fi + done + done + + # Create runScript + ln -s ${runScriptFile} singularity + + # Size calculation + cd .. + umount disk + size=$(resize2fs -P /dev/${vmTools.hd} | awk '{print $NF}') + mount /dev/${vmTools.hd} disk + cd disk + + export PATH=$PATH:${e2fsprogs}/bin/ + singularity create -s $((1 + size * 4 / 1024 + ${toString extraSpace})) $out + tar -c . | singularity import $out + ''); + + in result; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index b3a881fc9ad..63c725e2625 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -282,6 +282,8 @@ in pathsFromGraph = ../build-support/kernel/paths-from-graph.pl; + singularity-tools = callPackage ../build-support/singularity-tools { }; + srcOnly = args: callPackage ../build-support/src-only args; substituteAll = callPackage ../build-support/substitute/substitute-all.nix { }; @@ -13253,6 +13255,8 @@ in slack = callPackage ../applications/networking/instant-messengers/slack { }; + singularity = callPackage ../applications/virtualization/singularity { }; + spectrwm = callPackage ../applications/window-managers/spectrwm { }; wlc = callPackage ../development/libraries/wlc { };