Skip to content

Commit

Permalink
refactor: make mkCommand signature more ergonomic in block type devel…
Browse files Browse the repository at this point in the history
…opment
  • Loading branch information
blaggacao committed Jun 22, 2023
1 parent 80e5792 commit 5ffdd93
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 71 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 35 additions & 13 deletions lib/_mkCommand.nix
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
{nixpkgs}: currentSystem: name: description: command: args: let
{nixpkgs}: currentSystem: name: description: deps: command: args: let
inherit (nixpkgs.legacyPackages.${currentSystem}) pkgs;
inherit (pkgs) lib stdenv haskell shellcheck runtimeShell;
inherit (pkgs.haskell.lib.compose) justStaticExecutables;
in
args
// {
inherit name description;
command =
pkgs.writeShellScript "${name}" (''
set -e
command = pkgs.writeTextFile {
inherit name;
executable = true;
checkPhase = ''
runHook preCheck
${stdenv.shellDryRun} "$target"
# use shellcheck which does not include docs
# pandoc takes long to build and documentation isn't needed for in nixpkgs usage
${lib.getExe (justStaticExecutables shellcheck.unwrapped)} "$target"
runHook postCheck
'';
text =
''
#!${runtimeShell}
set -o errexit
set -o nounset
set -o pipefail
if test -z "$PRJ_ROOT"; then
echo "All Standard Block Type Actions require an environment that fulfills the PRJ Base Directiory Specification"
echo "see: https://github.com/numtide/prj-spec"
echo "Tip: To achieve that, you can enter a Standard direnv environment or run the action via the Standard CLI/TUI"
exit 1
fi
if test -z "$PRJ_ROOT"; then
echo "All Standard Block Type Actions require an environment that fulfills the PRJ Base Directiory Specification"
echo "see: https://github.com/numtide/prj-spec"
echo "Tip: To achieve that, you can enter a Standard direnv environment or run the action via the Standard CLI/TUI"
exit 1
fi
# Action Code follows ...
''
+ command);
# Action Code follows ...
''
+ lib.optionalString (deps != []) ''
# Be optionally reproducible due to potential overhead to load some
# quaasi-ubiquitous dependencies that are already generally available
export PATH="${lib.makeBinPath deps}:$PATH"
''
+ command;
};
}
2 changes: 1 addition & 1 deletion lib/actions/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ in
inherit proviso;
};
in
mkCommand currentSystem "build" "build it" ''
mkCommand currentSystem "build" "build it" [] ''
# ${target}
nix build ${contextFreeDrv target}
''
Expand Down
2 changes: 1 addition & 1 deletion lib/actions/run.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ in
currentSystem: target: let
programName = target.meta.mainProgram or (getName target);
in
mkCommand currentSystem "run" "run it" ''${target.program or "${target}/bin/${programName}"} "$@" '' {}
mkCommand currentSystem "run" "run it" [] ''${target.program or "${target}/bin/${programName}"} "$@" '' {}
14 changes: 7 additions & 7 deletions lib/blockTypes/arion.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ in
target,
}: let
pkgs = nixpkgs.legacyPackages.${currentSystem};
cmd = "${pkgs.arion}/bin/arion --prebuilt-file ${target.config.out.dockerComposeYaml}";
cmd = "arion --prebuilt-file ${target.config.out.dockerComposeYaml}";
in [
(mkCommand currentSystem "up" "arion up" ''${cmd} up "$@" '' {})
(mkCommand currentSystem "ps" "exec this arion task to ps" ''${cmd} ps "$@" '' {})
(mkCommand currentSystem "stop" "arion stop" ''${cmd} stop "$@" '' {})
(mkCommand currentSystem "rm" "arion rm" ''${cmd} rm "$@" '' {})
(mkCommand currentSystem "config" "check the docker-compose yaml file" ''${cmd} config "$@" '' {})
(mkCommand currentSystem "arion" "pass any command to arion" ''${cmd} "$@" '' {})
(mkCommand currentSystem "up" "arion up" [pkgs.arion] ''${cmd} up "$@" '' {})
(mkCommand currentSystem "ps" "exec this arion task to ps" [pkgs.arion] ''${cmd} ps "$@" '' {})
(mkCommand currentSystem "stop" "arion stop" [pkgs.arion] ''${cmd} stop "$@" '' {})
(mkCommand currentSystem "rm" "arion rm" [pkgs.arion] ''${cmd} rm "$@" '' {})
(mkCommand currentSystem "config" "check the docker-compose yaml file" [pkgs.arion] ''${cmd} config "$@" '' {})
(mkCommand currentSystem "arion" "pass any command to arion" [pkgs.arion] ''${cmd} "$@" '' {})
];
}
16 changes: 6 additions & 10 deletions lib/blockTypes/containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,18 @@ in

tags' =
builtins.toFile "${target.name}-tags.json" (builtins.concatStringsSep "\n" target.image.tags);
copyFn = let
skopeo = "skopeo --insecure-policy";
in ''
export PATH=${skopeo-nix2container}/bin:$PATH
copyFn = ''
copy() {
local uri prev_tag
uri=$1
shift
for tag in $(<${tags'}); do
if ! [[ -v prev_tag ]]; then
${skopeo} copy nix:${target} "$uri:$tag" "$@"
skopeo --insecure-policy copy nix:${target} "$uri:$tag" "$@"
else
# speedup: copy from the previous tag to avoid superflous network bandwidth
${skopeo} copy "$uri:$prev_tag" "$uri:$tag" "$@"
skopeo --insecure-policy copy "$uri:$prev_tag" "$uri:$tag" "$@"
fi
echo "Done: $uri:$tag"
Expand All @@ -66,20 +62,20 @@ in
'';
in [
(actions.build currentSystem target)
(mkCommand currentSystem "print-image" "print out the image.repo with all tags" ''
(mkCommand currentSystem "print-image" "print out the image.repo with all tags" [] ''
echo
for tag in $(<${tags'}); do
echo "${target.image.repo}:$tag"
done
'' {})
(mkCommand currentSystem "publish" "copy the image to its remote registry" ''
(mkCommand currentSystem "publish" "copy the image to its remote registry" [skopeo-nix2container] ''
${copyFn}
copy docker://${target.image.repo}
'' {
meta.image = target.image.name;
inherit proviso;
})
(mkCommand currentSystem "load" "load image to the local docker daemon" ''
(mkCommand currentSystem "load" "load image to the local docker daemon" [skopeo-nix2container] ''
${copyFn}
if command -v podman &> /dev/null; then
echo "Podman detected: copy to local podman"
Expand Down
8 changes: 4 additions & 4 deletions lib/blockTypes/data.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ in
else target
);
};
jq = ["${pkgs.jq}/bin/jq" "-r" "'.'" "${json}"];
fx = ["|" "${pkgs.fx}/bin/fx"];
in [
(mkCommand currentSystem "write" "write to file" "echo ${json}" {})
(mkCommand currentSystem "explore" "interactively explore" (concatStringsSep "\t" (jq ++ fx)) {})
(mkCommand currentSystem "write" "write to file" [] "echo ${json}" {})
(mkCommand currentSystem "explore" "interactively explore" [pkgs.fx] (
concatStringsSep "\t" ["fx" json]
) {})
];
}
2 changes: 1 addition & 1 deletion lib/blockTypes/devshells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ in
developDrv = devshellDrv target;
in [
(actions.build currentSystem target)
(mkCommand currentSystem "enter" "enter this devshell" ''
(mkCommand currentSystem "enter" "enter this devshell" [] ''
profile_path="$PRJ_DATA_HOME/${fragmentRelPath}"
mkdir -p "$profile_path"
# ${developDrv}
Expand Down
6 changes: 3 additions & 3 deletions lib/blockTypes/files.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ in
target,
}: let
file = toString target;
bat = "${nixpkgs.legacyPackages.${currentSystem}.bat}/bin/bat";
pkgs = nixpkgs.legacyPackages.${currentSystem};
in [
(mkCommand currentSystem "explore" "interactively explore with bat" ''
${bat} ${file}
(mkCommand currentSystem "explore" "interactively explore with bat" [pkgs.bat] ''
bat ${file}
'' {})
];
}
12 changes: 6 additions & 6 deletions lib/blockTypes/installables.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ in
}: [
(actions.build currentSystem target)
# profile commands require a flake ref
(mkCommand currentSystem "install" "install this target" ''
(mkCommand currentSystem "install" "install this target" [] ''
# ${target}
nix profile install $PRJ_ROOT#${fragment}
'' {})
(mkCommand currentSystem "upgrade" "upgrade this target" ''
(mkCommand currentSystem "upgrade" "upgrade this target" [] ''
# ${target}
nix profile upgrade $PRJ_ROOT#${fragment}
'' {})
(mkCommand currentSystem "remove" "remove this target" ''
(mkCommand currentSystem "remove" "remove this target" [] ''
# ${target}
nix profile remove $PRJ_ROOT#${fragment}
'' {})
# TODO: use target. `nix bundle` requires a flake ref, but we may be able to use nix-bundle instead as a workaround
(mkCommand currentSystem "bundle" "bundle this target" ''
(mkCommand currentSystem "bundle" "bundle this target" [] ''
# ${target}
nix bundle --bundler github:Ninlives/relocatable.nix --refresh $PRJ_ROOT#${fragment}
'' {})
(mkCommand currentSystem "bundleImage" "bundle this target to image" ''
(mkCommand currentSystem "bundleImage" "bundle this target to image" [] ''
# ${target}
nix bundle --bundler github:NixOS/bundlers#toDockerImage --refresh $PRJ_ROOT#${fragment}
'' {})
(mkCommand currentSystem "bundleAppImage" "bundle this target to AppImage" ''
(mkCommand currentSystem "bundleAppImage" "bundle this target to AppImage" [] ''
# ${target}
nix bundle --bundler github:ralismark/nix-appimage --refresh $PRJ_ROOT#${fragment}
'' {})
Expand Down
6 changes: 3 additions & 3 deletions lib/blockTypes/microvms.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ in
fragmentRelPath,
target,
}: [
(mkCommand currentSystem "run" "run the microvm" ''
(mkCommand currentSystem "run" "run the microvm" [] ''
${target.config.microvm.runner.${target.config.microvm.hypervisor}}/bin/microvm-run
'' {})
(mkCommand currentSystem "console" "enter the microvm console" ''
(mkCommand currentSystem "console" "enter the microvm console" [] ''
${target.config.microvm.runner.${target.config.microvm.hypervisor}}/bin/microvm-console
'' {})
(mkCommand currentSystem "microvm" "pass any command to microvm" ''
(mkCommand currentSystem "microvm" "pass any command to microvm" [] ''
${target.config.microvm.runner.${target.config.microvm.hypervisor}}/bin/microvm-"$@"
'' {})
];
Expand Down
10 changes: 6 additions & 4 deletions lib/blockTypes/nixago.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ in
fragment,
fragmentRelPath,
target,
}: [
(mkCommand currentSystem "populate" "populate this nixago file into the repo" ''
}: let
pkgs = nixpkgs.legacyPackages.${currentSystem};
in [
(mkCommand currentSystem "populate" "populate this nixago file into the repo" [] ''
${target.install}/bin/nixago_shell_hook
'' {})
(mkCommand currentSystem "explore" "interactively explore the nixago file" ''
${nixpkgs.legacyPackages.${currentSystem}.bat}/bin/bat "${target.config}"
(mkCommand currentSystem "explore" "interactively explore the nixago file" [pkgs.bat] ''
bat "${target.config}"
'' {})
];
}
17 changes: 4 additions & 13 deletions lib/blockTypes/nomadJobManifests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ in
fragmentRelPath,
target,
}: let
fx = "${nixpkgs.legacyPackages.${currentSystem}.fx}/bin";
nomad = "${nixpkgs.legacyPackages.${currentSystem}.nomad}/bin";
jq = "${nixpkgs.legacyPackages.${currentSystem}.jq}/bin";
pkgs = nixpkgs.legacyPackages.${currentSystem};
job = baseNameOf fragmentRelPath;
nixExpr = ''
x: let
Expand All @@ -40,9 +38,6 @@ in
'';
layout = ''
job_path="$PRJ_DATA_HOME/${dirOf fragmentRelPath}/${job}.json"
# use Nomad bin in path if it exists, and only fallback on nixpkgs if it doesn't
PATH="$PATH:${nomad}"
'';
render = ''
echo "Rendering to $job_path..."
Expand All @@ -65,20 +60,18 @@ in
inject the git revision validate the manifest, after which it can be run or
planned with the Nomad cli or the `deploy` action.
*/
(mkCommand currentSystem "render" "build the JSON job description" ''
(mkCommand currentSystem "render" "build the JSON job description" [pkgs.nomad] ''
set -e
${layout}
${render}
'' {})
(mkCommand currentSystem "deploy" "Deploy the job to Nomad" ''
(mkCommand currentSystem "deploy" "Deploy the job to Nomad" [pkgs.nomad pkgs.jq] ''
set -e
${layout}
PATH=$PATH:${jq}
if ! [[ -h "$job_path" ]] \
|| [[ "$(jq -r '.job[].meta.rev' "$job_path")" != "$(git rev-parse --short HEAD)" ]]
then ${render}
Expand Down Expand Up @@ -108,7 +101,7 @@ in
echo "Job hasn't changed since last deployment, nothing to deploy"
fi
'' {})
(mkCommand currentSystem "explore" "interactively explore the Job defintion" ''
(mkCommand currentSystem "explore" "interactively explore the Job defintion" [pkgs.nomad pkgs.fx] ''
set -e
${layout}
Expand All @@ -117,8 +110,6 @@ in
${render}
fi
PATH=$PATH:${fx}
fx "$job_path"
'' {})
];
Expand Down
2 changes: 1 addition & 1 deletion src/local/configs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ in {
nixpkgs.go
];
devshell.startup.prettier-plugin-toml = l.stringsWithDeps.noDepEntry ''
export NODE_PATH=${nixpkgs.nodePackages.prettier-plugin-toml}/lib/node_modules:$NODE_PATH
export NODE_PATH=${nixpkgs.nodePackages.prettier-plugin-toml}/lib/node_modules:''${NODE_PATH-}
'';
};
editorconfig = {
Expand Down
2 changes: 1 addition & 1 deletion src/std/templates/minimal/nix/repo/configs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ A: (1) dotfile proliferation
inputs.nixpkgs.shfmt
];
devshell.startup.prettier-plugin-toml = inputs.nixpkgs.lib.stringsWithDeps.noDepEntry ''
export NODE_PATH=${inputs.nixpkgs.nodePackages.prettier-plugin-toml}/lib/node_modules:$NODE_PATH
export NODE_PATH=${inputs.nixpkgs.nodePackages.prettier-plugin-toml}/lib/node_modules:''${NODE_PATH-}
'';
data = {
formatter = {
Expand Down

0 comments on commit 5ffdd93

Please sign in to comment.