diff --git a/lib/builders/build-docker-image.nix b/lib/builders/build-docker-image.nix new file mode 100644 index 00000000..fa454bc8 --- /dev/null +++ b/lib/builders/build-docker-image.nix @@ -0,0 +1,33 @@ +{ dockerTools, foodogsquaredLib }: + +{ name, contents ? [ ], pathsToLink ? [ ], enableTypicalSetup ? true, ... }@attrs: + +dockerTools.buildImage (attrs // { + name = "fds-${name}"; + + copyToRoot = foodogsquaredLib.buildFDSEnv { + inherit pathsToLink; + name = "fds-${name}-root"; + paths = + contents + ++ lib.optionals enableTypicalSetup (with dockerTools; [ + usrBinEnv + binSh + caCertificates + fakeNss + ]); + }; + + runAsRoot = '' + ${lib.optionalString enableTypicalSetup '' + mkdir -p /data + ''} + ${attrs.runAsRoot} + ''; + + config = attrs.config // lib.optionalAttrs enableTypicalSetup { + Cmd = [ "/bin/bash" ]; + WorkingDir = "/data"; + Volumes."/data" = { }; + }; +}) diff --git a/lib/builders/default.nix b/lib/builders/default.nix index c7a9a54e..f0aadb91 100644 --- a/lib/builders/default.nix +++ b/lib/builders/default.nix @@ -224,4 +224,50 @@ ``` */ buildDconfDb = pkgs.callPackage ./build-dconf-db.nix { }; + + /** + A wrapper for building Docker images. + + # Arguments + + A sole attribute set with the following attributes: + + name + : Name of the container. + + contents + : The contents of the FDS environment to be built with. + + pathsToLink + : A list of directories to be shared with all of the derivations listed + from `contents`. + + enableTypicalSetup + : Enable typical configuration. + + The rest of the attributes are considered as part of the + `dockerTools.buildImage` argument. + + # Type + + ``` + buildDockerImage :: Attr -> Derivation + ``` + + # Example + + ```nix + buildDockerImage { + name = "typical-webdev"; + contents = with pkgs; [ + hello + ruby + npm + pnpm + ]; + enableTypicalSetup = true; + } + ``` + */ + buildDockerImage = pkgs.callPackage ./build-docker-image.nix { foodogsquaredLib = self; }; } diff --git a/lib/default.nix b/lib/default.nix index 47d01499..e3804469 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -39,7 +39,7 @@ in pkgs.lib.makeExtensible (self: inherit (self.builders) makeXDGMimeAssociationList makeXDGPortalConfiguration makeXDGDesktopEntry - buildHugoSite buildFDSEnv buildDconfDb; + buildHugoSite buildFDSEnv buildDconfDb buildDockerImage; inherit (self.trivial) countAttrs filterAttrs'; inherit (self.data) importYAML renderTeraTemplate renderMustacheTemplate; inherit (self.fetchers) fetchInternetArchive fetchUgeeDriver;