Skip to content

Commit

Permalink
flake-parts: apps outputs to run and upload container images
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarewk committed Mar 20, 2023
1 parent 71f5c3c commit b7b6dea
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
24 changes: 21 additions & 3 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 56 additions & 11 deletions src/modules/containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
})
];
}

0 comments on commit b7b6dea

Please sign in to comment.