about summary refs log tree commit diff
path: root/nix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-04-21T14·14+0300
committertazjin <tazjin@tvl.su>2023-04-24T10·56+0000
commit159646787a9696bbea4a1274c18b2b72ee501100 (patch)
tree7789f6c162d4bc4ed4552d8f61cc9d267fb4b6c3 /nix
parent7b133987a443423f41bc405546cb8878e27c710a (diff)
feat(nix/lazy-deps): add override pattern for deps r/6106
Introduces a `.overrideDeps` attribute with which additional tools can
be supplied. This works like `.override` in nixpkgs.

Change-Id: I69a009b51f7f073a2d030eda5e3b5310e0f8e883
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8491
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'nix')
-rw-r--r--nix/lazy-deps/default.nix102
1 files changed, 52 insertions, 50 deletions
diff --git a/nix/lazy-deps/default.nix b/nix/lazy-deps/default.nix
index 1b88f66225..fbdb30b38e 100644
--- a/nix/lazy-deps/default.nix
+++ b/nix/lazy-deps/default.nix
@@ -9,11 +9,11 @@
 # evaluation, and expects both `git` and `nix-build` to exist in the
 # user's $PATH. If required, this can be done in the shell
 # configuration invoking this function.
-{ pkgs, ... }:
+{ pkgs, lib, ... }:
 
 let
   inherit (builtins) attrNames attrValues mapAttrs;
-  inherit (pkgs.lib) concatStringsSep;
+  inherit (lib) fix concatStringsSep;
 
   # Create the case statement for a command invocations, optionally
   # overriding the `TARGET_TOOL` variable.
@@ -28,62 +28,64 @@ let
 
   invocations = tools: concatStringsSep "\n" (attrValues (mapAttrs invoke tools));
 in
+fix (self:
 
 # Attribute set of tools that should be lazily-added to the $PATH.
-
-  # The name of each attribute is used as the command name (on $PATH).
-  # It must contain the keys 'attr' (containing the Nix attribute path
-  # to the tool's derivation from the top-level), and may optionally
-  # contain the key 'cmd' to override the name of the binary inside the
-  # derivation.
+#
+# The name of each attribute is used as the command name (on $PATH).
+# It must contain the keys 'attr' (containing the Nix attribute path
+# to the tool's derivation from the top-level), and may optionally
+# contain the key 'cmd' to override the name of the binary inside the
+# derivation.
 tools:
 
-let
-  self = pkgs.runCommandNoCC "lazy-dispatch"
-    {
-      text = ''
-        #!${pkgs.runtimeShell}
-        set -ue
+pkgs.runCommandNoCC "lazy-dispatch"
+{
+  passthru.overrideDeps = newTools: self (tools // newTools);
+  passthru.tools = tools;
 
-        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
+  text = ''
+    #!${pkgs.runtimeShell}
+    set -ue
 
-        readonly REPO_ROOT=$(git rev-parse --show-toplevel)
-        TARGET_TOOL=$(basename "$0")
+    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
 
-        case "''${TARGET_TOOL}" in
-        ${invocations tools}
-        *)
-          echo "''${TARGET_TOOL} is currently not installed in this repository." >&2
-          exit 127
-          ;;
-        esac
+    readonly REPO_ROOT=$(git rev-parse --show-toplevel)
+    TARGET_TOOL=$(basename "$0")
 
-        result=$(nix-build --no-out-link --attr "''${attr}" "''${REPO_ROOT}")
-        PATH="''${result}/bin:$PATH"
-        exec "''${TARGET_TOOL}" "''${@}"
-      '';
+    case "''${TARGET_TOOL}" in
+    ${invocations tools}
+    *)
+      echo "''${TARGET_TOOL} is currently not installed in this repository." >&2
+      exit 127
+      ;;
+    esac
 
-      # Access this to get a compatible nix-shell
-      passthru.devShell = pkgs.mkShellNoCC {
-        name = "${self.name}-shell";
-        packages = [ self ];
-      };
-    }
-    ''
-      # 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))}
+  # Access this to get a compatible nix-shell
+  passthru.devShell = pkgs.mkShellNoCC {
+    name = "${self.name}-shell";
+    packages = [ self ];
+  };
+}
+  ''
+    # Write the dispatch code
+    target=$out/bin/__dispatch
+    mkdir -p "$(dirname "$target")"
+    echo "$text" > $target
+    chmod +x $target
 
-      # Check that it's working-ish
-      ${pkgs.stdenv.shellDryRun} $target
-    '';
-in
-self
+    # Add symlinks from all the tools to the dispatch
+    ${concatStringsSep "\n" (map link (attrNames tools))}
+
+    # Check that it's working-ish
+    ${pkgs.stdenv.shellDryRun} $target
+  ''
+)