Skip to content

Commit

Permalink
containers: move entrypoint to Entrypoint and startupCommand to Cmd a…
Browse files Browse the repository at this point in the history
…nd fix arguments escaping
  • Loading branch information
nazarewk committed Apr 4, 2023
1 parent 8a0f634 commit b640f70
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
16 changes: 7 additions & 9 deletions src/devenv-devShell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function nix {
}

function container {
declare -A args
# shellcheck disable=SC1090
source "$(command -v docopts).sh"
# shellcheck disable=SC2016
eval "$(docopts -A args -h '
Usage: container [options] <container-name> [--] [<run-args>...]
Expand Down Expand Up @@ -49,17 +50,14 @@ Options:
# shellcheck disable=SC2086
nix run "${app_prefix}-copy-to" -- --registry "${registry}" "${copy_args[@]}" ${args['--copy-args']}
fi
eval "$(docopt_get_eval_array args '<run-args>' run_args)"

if [[ "${args['--docker-run']}" != false ]]; then
# shellcheck disable=SC1090
source "$(command -v docopts.sh)"
# shellcheck disable=SC2046
nix run "${app_prefix}-docker-run" -- $(docopt_get_values args '<run-args>')
# shellcheck disable=SC2154
nix run "${app_prefix}-docker-run" -- "${run_args[@]}"
elif [[ "${args['--podman-run']}" != false ]]; then
# shellcheck disable=SC1090
source "$(command -v docopts.sh)"
# shellcheck disable=SC2046
nix run "${app_prefix}-podman-run" -- $(docopt_get_values args '<run-args>')
# shellcheck disable=SC2154
nix run "${app_prefix}-podman-run" -- "${run_args[@]}"
fi
}

Expand Down
71 changes: 46 additions & 25 deletions src/modules/containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,38 @@ let
source ${shell.envScript}
exec ${toString cfg.startupCommand}
exec "$@"
'';
mkDerivation = cfg: nix2container.nix2container.buildImage {
name = cfg.name;
tag = cfg.version;
copyToRoot = [
(pkgs.runCommand "create-paths" { } ''
mkdir -p $out/tmp
'')
(pkgs.buildEnv {
name = "root";
paths = [
pkgs.coreutils-full
pkgs.bash
] ++ lib.optionals (cfg.copyToRoot != null) [ cfg.copyToRoot ];
pathsToLink = "/";
})
];
config = {
Env = lib.mapAttrsToList (name: value: "${name}=${lib.escapeShellArg (toString value)}") containerEnv;
Cmd = cfg.entrypoint;
};
};
mkDerivation = cfg: nix2container.nix2container.buildImage (
lib.attrsets.recursiveUpdate
{
name = cfg.name;
tag = cfg.version;
copyToRoot = [
(pkgs.runCommand "create-paths" { } ''
mkdir -p $out/tmp
'')
(pkgs.buildEnv {
name = "root";
paths = [
pkgs.coreutils-full
pkgs.bash
] ++ lib.optionals (cfg.copyToRoot != null) [ cfg.copyToRoot ];
pathsToLink = "/";
})
];
config = {
Env = lib.mapAttrsToList (name: value: "${name}=${lib.escapeShellArg (toString value)}") containerEnv;
Entrypoint = cfg.entrypoint;
Cmd = cfg.startupCommand;
} // (cfg.rawBuildConfig.config or { });
}
cfg.rawBuildConfig
);

# <registry> <args>
mkCopyScript = cfg: pkgs.writeScript "copy-container" ''
source "$(command -v docopts).sh"
eval "$(docopts -A args -h '
Usage: copy-container <spec-path> [options] [<skopeo-args>...]
Expand Down Expand Up @@ -84,8 +90,7 @@ let
if [[ ''${args['<skopeo-args>,#']} == 0 ]]; then
argv=(${toString cfg.defaultCopyArgs})
else
source "$(command -v docopts.sh)"
argv=( $(docopt_get_values args '<skopeo-args>') )
eval "$(docopt_get_eval_array args '<skopeo-args>' argv)"
fi
echo
Expand All @@ -109,6 +114,16 @@ let
default = "latest";
};

rawBuildConfig = lib.mkOption {
type = types.attrsOf types.anything;
description = ''
Raw argument overrides to be passed down to nix2container.buildImage.
see https://github.com/nlewo/nix2container#nix2containerbuildimage
'';
default = { };
};

copyToRoot = lib.mkOption {
type = types.nullOr types.path;
description = "Add a path to the container. Defaults to the whole git repo.";
Expand All @@ -117,9 +132,15 @@ let
};

startupCommand = lib.mkOption {
type = types.nullOr (types.either types.str types.package);
type = types.nullOr (types.oneOf [ types.str types.package (types.listOf types.anything) ]);
description = "Command to run in the container.";
default = null;
apply = input:
let type = builtins.typeOf input; in
if type == "null" then [ ]
else if type == "string" then [ input ]
else if type == "list" then builtins.map builtins.toString input
else [ (builtins.toString input) ];
};

entrypoint = lib.mkOption {
Expand Down

0 comments on commit b640f70

Please sign in to comment.