Skip to content

Commit e511fc2

Browse files
committed
feat(nix/lazy-deps): add override pattern for deps
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 <[email protected]>
1 parent 91f1e4f commit e511fc2

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

lazy-deps/default.nix

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
# evaluation, and expects both `git` and `nix-build` to exist in the
1010
# user's $PATH. If required, this can be done in the shell
1111
# configuration invoking this function.
12-
{ pkgs, ... }:
12+
{ pkgs, lib, ... }:
1313

1414
let
1515
inherit (builtins) attrNames attrValues mapAttrs;
16-
inherit (pkgs.lib) concatStringsSep;
16+
inherit (lib) fix concatStringsSep;
1717

1818
# Create the case statement for a command invocations, optionally
1919
# overriding the `TARGET_TOOL` variable.
@@ -28,62 +28,64 @@ let
2828

2929
invocations = tools: concatStringsSep "\n" (attrValues (mapAttrs invoke tools));
3030
in
31+
fix (self:
3132

3233
# Attribute set of tools that should be lazily-added to the $PATH.
33-
34-
# The name of each attribute is used as the command name (on $PATH).
35-
# It must contain the keys 'attr' (containing the Nix attribute path
36-
# to the tool's derivation from the top-level), and may optionally
37-
# contain the key 'cmd' to override the name of the binary inside the
38-
# derivation.
34+
#
35+
# The name of each attribute is used as the command name (on $PATH).
36+
# It must contain the keys 'attr' (containing the Nix attribute path
37+
# to the tool's derivation from the top-level), and may optionally
38+
# contain the key 'cmd' to override the name of the binary inside the
39+
# derivation.
3940
tools:
4041

41-
let
42-
self = pkgs.runCommandNoCC "lazy-dispatch"
43-
{
44-
text = ''
45-
#!${pkgs.runtimeShell}
46-
set -ue
42+
pkgs.runCommandNoCC "lazy-dispatch"
43+
{
44+
passthru.overrideDeps = newTools: self (tools // newTools);
45+
passthru.tools = tools;
4746

48-
if ! type git>/dev/null || ! type nix-build>/dev/null; then
49-
echo "The 'git' and 'nix-build' commands must be available." >&2
50-
exit 127
51-
fi
47+
text = ''
48+
#!${pkgs.runtimeShell}
49+
set -ue
5250
53-
readonly REPO_ROOT=$(git rev-parse --show-toplevel)
54-
TARGET_TOOL=$(basename "$0")
51+
if ! type git>/dev/null || ! type nix-build>/dev/null; then
52+
echo "The 'git' and 'nix-build' commands must be available." >&2
53+
exit 127
54+
fi
5555
56-
case "''${TARGET_TOOL}" in
57-
${invocations tools}
58-
*)
59-
echo "''${TARGET_TOOL} is currently not installed in this repository." >&2
60-
exit 127
61-
;;
62-
esac
56+
readonly REPO_ROOT=$(git rev-parse --show-toplevel)
57+
TARGET_TOOL=$(basename "$0")
6358
64-
result=$(nix-build --no-out-link --attr "''${attr}" "''${REPO_ROOT}")
65-
PATH="''${result}/bin:$PATH"
66-
exec "''${TARGET_TOOL}" "''${@}"
67-
'';
59+
case "''${TARGET_TOOL}" in
60+
${invocations tools}
61+
*)
62+
echo "''${TARGET_TOOL} is currently not installed in this repository." >&2
63+
exit 127
64+
;;
65+
esac
6866
69-
# Access this to get a compatible nix-shell
70-
passthru.devShell = pkgs.mkShellNoCC {
71-
name = "${self.name}-shell";
72-
packages = [ self ];
73-
};
74-
}
75-
''
76-
# Write the dispatch code
77-
target=$out/bin/__dispatch
78-
mkdir -p "$(dirname "$target")"
79-
echo "$text" > $target
80-
chmod +x $target
67+
result=$(nix-build --no-out-link --attr "''${attr}" "''${REPO_ROOT}")
68+
PATH="''${result}/bin:$PATH"
69+
exec "''${TARGET_TOOL}" "''${@}"
70+
'';
8171

82-
# Add symlinks from all the tools to the dispatch
83-
${concatStringsSep "\n" (map link (attrNames tools))}
72+
# Access this to get a compatible nix-shell
73+
passthru.devShell = pkgs.mkShellNoCC {
74+
name = "${self.name}-shell";
75+
packages = [ self ];
76+
};
77+
}
78+
''
79+
# Write the dispatch code
80+
target=$out/bin/__dispatch
81+
mkdir -p "$(dirname "$target")"
82+
echo "$text" > $target
83+
chmod +x $target
8484
85-
# Check that it's working-ish
86-
${pkgs.stdenv.shellDryRun} $target
87-
'';
88-
in
89-
self
85+
# Add symlinks from all the tools to the dispatch
86+
${concatStringsSep "\n" (map link (attrNames tools))}
87+
88+
# Check that it's working-ish
89+
${pkgs.stdenv.shellDryRun} $target
90+
''
91+
)

0 commit comments

Comments
 (0)