diff --git a/flake-module.nix b/flake-module.nix index b10eafdd43..7d8b11d99f 100644 --- a/flake-module.nix +++ b/flake-module.nix @@ -43,9 +43,27 @@ devenvFlake: { flake-parts-lib, lib, inputs, ... }: { lib.concatMapAttrs (shellName: devenv: lib.concatMapAttrs - (containerName: container: - { "${shellPrefix shellName}container-${containerName}" = container.derivation; } - ) + (containerName: container: { + "${shellPrefix shellName}container-${containerName}-spec" = container.derivation; + }) + devenv.containers + ) + config.devenv.shells; + + config.apps = + lib.concatMapAttrs + (shellName: devenv: + lib.concatMapAttrs + (containerName: container: { + "${shellPrefix shellName}container-${containerName}" = { + type = "app"; + program = "${container.run}"; + }; + "${shellPrefix shellName}container-${containerName}-copy-to" = { + type = "app"; + program = "${container.copyTo}"; + }; + }) devenv.containers ) config.devenv.shells; diff --git a/src/modules/containers.nix b/src/modules/containers.nix index 904cc5fba4..373f53670c 100644 --- a/src/modules/containers.nix +++ b/src/modules/containers.nix @@ -158,6 +158,48 @@ let docker run -it ${config.name}:${config.version} "$@" ''; }; + + copyTo = lib.mkOption { + type = types.package; + internal = true; + default = pkgs.writeScript "devenv-container-copy-to" '' + ${config.copyScript} ${config.derivation} "''${1:-"containers-storage:localhost/"}" "''${@:2}" + ''; + }; + + run = lib.mkOption { + type = types.package; + internal = true; + default = pkgs.writeScript "devenv-container-run" '' + #!${pkgs.bash}/bin/bash + set -eEuo pipefail + + ${config.copyScript} ${config.derivation} "containers-storage:localhost/" + + container_args=() + runtime_args=() + + for arg in "$@" ; do + if [ "$arg" == "--" ] ; then + runtime_args=("''${container_args[@]}") + container_args=() + else + container_args+=("$arg") + fi + done + + args=( run -it "''${runtime_args[@]}" '${config.name}:${config.version}' "''${container_args[@]}" ) + + if command -v docker >/dev/null ; then + docker "''${args[@]}" + elif command -v podman >/dev/null ; then + podman "''${args[@]}" + else + echo "Found neither docker nor podman." >&2 + exit 1 + fi + ''; + }; }; }); in @@ -178,16 +220,19 @@ in }; }; - config = { - container.isBuilding = envContainerName != ""; - containers.${envContainerName}.isBuilding = true; - - containers.shell = { - startupCommand = "bash"; - }; + config = lib.mkMerge [ + { + containers.shell = { + startupCommand = "bash"; + }; - containers.processes = { - startupCommand = config.procfileScript; - }; - }; + containers.processes = { + startupCommand = config.procfileScript; + }; + } + (lib.mkIf (envContainerName != "") { + container.isBuilding = true; + containers.${envContainerName}.isBuilding = true; + }) + ]; }