From 5ffdd932af9577eb97f9cae0983e7493868ba542 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 21 Jun 2023 14:30:55 -0500 Subject: [PATCH] refactor: make mkCommand signature more ergonomic in block type development --- flake.lock | 6 +-- lib/_mkCommand.nix | 48 ++++++++++++++----- lib/actions/build.nix | 2 +- lib/actions/run.nix | 2 +- lib/blockTypes/arion.nix | 14 +++--- lib/blockTypes/containers.nix | 16 +++---- lib/blockTypes/data.nix | 8 ++-- lib/blockTypes/devshells.nix | 2 +- lib/blockTypes/files.nix | 6 +-- lib/blockTypes/installables.nix | 12 ++--- lib/blockTypes/microvms.nix | 6 +-- lib/blockTypes/nixago.nix | 10 ++-- lib/blockTypes/nomadJobManifests.nix | 17 ++----- src/local/configs.nix | 2 +- .../templates/minimal/nix/repo/configs.nix | 2 +- 15 files changed, 82 insertions(+), 71 deletions(-) diff --git a/flake.lock b/flake.lock index 7cc76a60..4bb7f9fb 100644 --- a/flake.lock +++ b/flake.lock @@ -229,11 +229,11 @@ ] }, "locked": { - "lastModified": 1683210100, - "narHash": "sha256-bhGDOlkWtlhVECpoOog4fWiFJmLCpVEg09a40aTjCbw=", + "lastModified": 1687381756, + "narHash": "sha256-IUMIlYfrvj7Yli4H2vvyig8HEPpfCeMaE6+kBGPzFyk=", "owner": "nix-community", "repo": "nixago", - "rev": "1da60ad9412135f9ed7a004669fdcf3d378ec630", + "rev": "dacceb10cace103b3e66552ec9719fa0d33c0dc9", "type": "github" }, "original": { diff --git a/lib/_mkCommand.nix b/lib/_mkCommand.nix index cfd2a255..b118eafb 100644 --- a/lib/_mkCommand.nix +++ b/lib/_mkCommand.nix @@ -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; + }; } diff --git a/lib/actions/build.nix b/lib/actions/build.nix index 1750d140..3f334bc0 100644 --- a/lib/actions/build.nix +++ b/lib/actions/build.nix @@ -25,7 +25,7 @@ in inherit proviso; }; in - mkCommand currentSystem "build" "build it" '' + mkCommand currentSystem "build" "build it" [] '' # ${target} nix build ${contextFreeDrv target} '' diff --git a/lib/actions/run.nix b/lib/actions/run.nix index d61b953d..9cb1e379 100644 --- a/lib/actions/run.nix +++ b/lib/actions/run.nix @@ -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}"} "$@" '' {} diff --git a/lib/blockTypes/arion.nix b/lib/blockTypes/arion.nix index 742ef270..a2a6e676 100644 --- a/lib/blockTypes/arion.nix +++ b/lib/blockTypes/arion.nix @@ -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} "$@" '' {}) ]; } diff --git a/lib/blockTypes/containers.nix b/lib/blockTypes/containers.nix index ea0b9d11..683a7094 100644 --- a/lib/blockTypes/containers.nix +++ b/lib/blockTypes/containers.nix @@ -41,11 +41,7 @@ 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 @@ -53,10 +49,10 @@ in 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" @@ -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" diff --git a/lib/blockTypes/data.nix b/lib/blockTypes/data.nix index 198570c5..9f3aa092 100644 --- a/lib/blockTypes/data.nix +++ b/lib/blockTypes/data.nix @@ -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] + ) {}) ]; } diff --git a/lib/blockTypes/devshells.nix b/lib/blockTypes/devshells.nix index 96a354fb..be1cbc24 100644 --- a/lib/blockTypes/devshells.nix +++ b/lib/blockTypes/devshells.nix @@ -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} diff --git a/lib/blockTypes/files.nix b/lib/blockTypes/files.nix index 5106ac64..a595b69f 100644 --- a/lib/blockTypes/files.nix +++ b/lib/blockTypes/files.nix @@ -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} '' {}) ]; } diff --git a/lib/blockTypes/installables.nix b/lib/blockTypes/installables.nix index dd793a53..1c8dec63 100644 --- a/lib/blockTypes/installables.nix +++ b/lib/blockTypes/installables.nix @@ -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} '' {}) diff --git a/lib/blockTypes/microvms.nix b/lib/blockTypes/microvms.nix index 21997a34..599f258a 100644 --- a/lib/blockTypes/microvms.nix +++ b/lib/blockTypes/microvms.nix @@ -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-"$@" '' {}) ]; diff --git a/lib/blockTypes/nixago.nix b/lib/blockTypes/nixago.nix index 288ed1bb..523c7bf2 100644 --- a/lib/blockTypes/nixago.nix +++ b/lib/blockTypes/nixago.nix @@ -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}" '' {}) ]; } diff --git a/lib/blockTypes/nomadJobManifests.nix b/lib/blockTypes/nomadJobManifests.nix index febd280e..50fbe62c 100644 --- a/lib/blockTypes/nomadJobManifests.nix +++ b/lib/blockTypes/nomadJobManifests.nix @@ -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 @@ -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..." @@ -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} @@ -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} @@ -117,8 +110,6 @@ in ${render} fi - PATH=$PATH:${fx} - fx "$job_path" '' {}) ]; diff --git a/src/local/configs.nix b/src/local/configs.nix index d556058b..ca2b3c97 100644 --- a/src/local/configs.nix +++ b/src/local/configs.nix @@ -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 = { diff --git a/src/std/templates/minimal/nix/repo/configs.nix b/src/std/templates/minimal/nix/repo/configs.nix index bbb9b780..7a4949e2 100644 --- a/src/std/templates/minimal/nix/repo/configs.nix +++ b/src/std/templates/minimal/nix/repo/configs.nix @@ -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 = {