From 99030d10ce114124e7303231e9bcb382f10bd0e7 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Mon, 30 May 2022 18:59:48 +0200 Subject: refactor(nix/lazy-deps): use runCommand writeTextFile is nice, but not flexible enough to allow the passthru argument needed for a follow-up change. Change-Id: I4f0cffd0f29b2c06b0155101d3806c9c5745c37a Reviewed-on: https://cl.tvl.fyi/c/depot/+/5854 Tested-by: BuildkiteCI Reviewed-by: tazjin --- nix/lazy-deps/default.nix | 66 ++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'nix') diff --git a/nix/lazy-deps/default.nix b/nix/lazy-deps/default.nix index 3cce48d8a53e..bfdf5490ce22 100644 --- a/nix/lazy-deps/default.nix +++ b/nix/lazy-deps/default.nix @@ -38,38 +38,46 @@ in # derivation. tools: -pkgs.writeTextFile { - name = "lazy-dispatch"; - executable = true; - destination = "/bin/__dispatch"; +let + self = pkgs.runCommandNoCC "lazy-dispatch" + { + text = '' + #!${pkgs.runtimeShell} + set -ue - text = '' - #!${pkgs.runtimeShell} - set -ue + if ! type git>/dev/null || ! type nix-build>/dev/null; then + echo "The 'git' and 'nix-build' commands must be available." >&2 + exit 127 + fi - if ! type git>/dev/null || ! type nix-build>/dev/null; then - echo "The 'git' and 'nix-build' commands must be available." >&2 - exit 127 - fi + readonly REPO_ROOT=$(git rev-parse --show-toplevel) + TARGET_TOOL=$(basename "$0") - readonly REPO_ROOT=$(git rev-parse --show-toplevel) - TARGET_TOOL=$(basename "$0") + case "''${TARGET_TOOL}" in + ${invocations tools} + *) + echo "''${TARGET_TOOL} is currently not installed in this repository." >&2 + exit 127 + ;; + esac - case "''${TARGET_TOOL}" in - ${invocations tools} - *) - echo "''${TARGET_TOOL} is currently not installed in this repository." >&2 - exit 127 - ;; - esac + result=$(nix-build --no-out-link --attr "''${attr}" "''${REPO_ROOT}") + PATH="''${result}/bin:$PATH" + exec "''${TARGET_TOOL}" "''${@}" + ''; + } + '' + # Write the dispatch code + target=$out/bin/__dispatch + mkdir -p "$(dirname "$target")" + echo "$text" > $target + chmod +x $target - result=$(nix-build --no-out-link --attr "''${attr}" "''${REPO_ROOT}") - PATH="''${result}/bin:$PATH" - exec "''${TARGET_TOOL}" "''${@}" - ''; + # Add symlinks from all the tools to the dispatch + ${concatStringsSep "\n" (map link (attrNames tools))} - checkPhase = '' - ${pkgs.stdenv.shellDryRun} "$target" - ${concatStringsSep "\n" (map link (attrNames tools))} - ''; -} + # Check that it's working-ish + ${pkgs.stdenv.shellDryRun} $target + ''; +in +self -- cgit 1.4.1