Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Last active August 21, 2021 21:35
Show Gist options
  • Save Xophmeister/40d3533502ed347628d6b137eeaf7f36 to your computer and use it in GitHub Desktop.
Save Xophmeister/40d3533502ed347628d6b137eeaf7f36 to your computer and use it in GitHub Desktop.
Proof-of-concept Docker image building of multiple packages, with Nix
{ pkgs ? import <nixpkgs> {}, name, packages ? {}}:
with pkgs;
let
# We need bash and coreutils to display the container "run" message;
# dockerTools.buildImage expects a list of derivations, not a set
required = { inherit bash coreutils; };
contents = builtins.attrValues (required // packages);
prettyPrint = import ./prettyPrint.nix {};
in dockerTools.buildImage {
inherit name contents;
config = {
Cmd = [ "${bash}/bin/bash" "-c" ''
( ${coreutils}/bin/cat | ${coreutils}/bin/fmt -s ) >&2 <<EOF
This is the "${name}" container, containing the following software:
${prettyPrint packages}
Execute these tools explicitly from within the container, rather than running the container itself.
EOF
'' ];
};
}
let
pkgs = import <nixpkgs> {};
buildContainer = import ./buildContainer.nix;
in buildContainer {
name = "bionix-poc";
packages = with pkgs; { inherit samtools bwa; };
}
# Pretty printer for package/derivation sets
{ bullet ? "*", recSep ? "\n", emptyText ? "<No packages installed>" }:
packageSet:
with builtins;
let
# Pretty printer for derivations
ppDrv = drv:
let parsed = parseDrvName drv.name;
in "${bullet} ${parsed.name} v${parsed.version}";
# Extract derivations from set
packages = attrValues packageSet;
isEmpty = length packages == 0;
in if isEmpty
then emptyText
else concatStringsSep recSep (map ppDrv packages)
@Xophmeister
Copy link
Author

This is a proof-of-concept allowing the building of Docker containers with Nix containing multiple, independent packages. The packages we install are from NixPkgs, for simplicity, but they can easily be our own derivations... Why have I never used Nix until now!!

@Xophmeister
Copy link
Author

Rather than using ociTools, we can create Singularity images with:

singularity build my-image.sif docker-archive:$(nix-build)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment