|
9 | 9 | # evaluation, and expects both `git` and `nix-build` to exist in the
|
10 | 10 | # user's $PATH. If required, this can be done in the shell
|
11 | 11 | # configuration invoking this function.
|
12 |
| -{ pkgs, ... }: |
| 12 | +{ pkgs, lib, ... }: |
13 | 13 |
|
14 | 14 | let
|
15 | 15 | inherit (builtins) attrNames attrValues mapAttrs;
|
16 |
| - inherit (pkgs.lib) concatStringsSep; |
| 16 | + inherit (lib) fix concatStringsSep; |
17 | 17 |
|
18 | 18 | # Create the case statement for a command invocations, optionally
|
19 | 19 | # overriding the `TARGET_TOOL` variable.
|
|
28 | 28 |
|
29 | 29 | invocations = tools: concatStringsSep "\n" (attrValues (mapAttrs invoke tools));
|
30 | 30 | in
|
| 31 | +fix (self: |
31 | 32 |
|
32 | 33 | # 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. |
39 | 40 | tools:
|
40 | 41 |
|
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; |
47 | 46 |
|
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 |
52 | 50 |
|
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 |
55 | 55 |
|
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") |
63 | 58 |
|
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 |
68 | 66 |
|
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 | + ''; |
81 | 71 |
|
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 |
84 | 84 |
|
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