feat: add option to install tool

This commit is contained in:
eric
2026-03-07 06:54:30 +01:00
parent 80cc529de7
commit 53e498ca45
3 changed files with 42 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ env = devshell-lib.lib.mkDevShell {
inherit system; inherit system;
src = ./.; src = ./.;
extraPackages = [ ]; extraPackages = [ ];
preToolHook = "";
tools = [ ]; tools = [ ];
additionalHooks = { }; additionalHooks = { };
}; };
@@ -46,6 +47,30 @@ Expose it in `devShells` as `default` and run:
nix develop nix develop
``` ```
Use `preToolHook` when a tool needs bootstrap work before the shell prints tool versions. This is useful for tools you install outside `nixpkgs`, as long as the hook is idempotent.
```nix
env = devshell-lib.lib.mkDevShell {
inherit system;
src = ./.;
# assumes `go` is already available in PATH, for example via `extraPackages`
preToolHook = ''
export GOBIN="$PWD/.tools/bin"
export PATH="$GOBIN:$PATH"
if ! command -v golangci-lint >/dev/null 2>&1; then
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
fi
'';
tools = [
{ name = "golangci-lint"; bin = "golangci-lint"; versionCmd = "version"; color = "YELLOW"; }
];
};
```
## Common commands ## Common commands
```bash ```bash

View File

@@ -34,12 +34,15 @@
system, system,
src ? ./., src ? ./.,
extraPackages ? [ ], extraPackages ? [ ],
preToolHook ? "",
extraShellHook ? "", extraShellHook ? "",
additionalHooks ? { }, additionalHooks ? { },
tools ? [ ], tools ? [ ],
includeStandardPackages ? true, includeStandardPackages ? true,
# tools = list of { name, bin, versionCmd, color? } # tools = list of { name, bin, versionCmd, color? }
# e.g. { name = "Bun"; bin = "${pkgs.bun}/bin/bun"; versionCmd = "--version"; color = "YELLOW"; } # e.g. { name = "Bun"; bin = "${pkgs.bun}/bin/bun"; versionCmd = "--version"; color = "YELLOW"; }
# preToolHook = shell snippet that runs before the ready banner and tool logs
# e.g. install tools outside nixpkgs, export PATH updates, warm caches
formatters ? { }, formatters ? { },
# formatters = treefmt-nix programs attrset, merged over { nixfmt.enable = true; } # formatters = treefmt-nix programs attrset, merged over { nixfmt.enable = true; }
# e.g. { gofmt.enable = true; shfmt.enable = true; } # e.g. { gofmt.enable = true; shfmt.enable = true; }
@@ -144,6 +147,8 @@
UNDERLINE='\033[4m' UNDERLINE='\033[4m'
RESET='\033[0m' RESET='\033[0m'
${preToolHook}
printf "\n$GREEN 🚀 Dev shell ready$RESET\n\n" printf "\n$GREEN 🚀 Dev shell ready$RESET\n\n"
${toolBannerScript} ${toolBannerScript}
printf "\n" printf "\n"

View File

@@ -66,8 +66,20 @@
# { name = "Bun"; bin = "${pkgs.bun}/bin/bun"; versionCmd = "--version"; color = "YELLOW"; } # { name = "Bun"; bin = "${pkgs.bun}/bin/bun"; versionCmd = "--version"; color = "YELLOW"; }
# { name = "Go"; bin = "${pkgs.go}/bin/go"; versionCmd = "version"; color = "CYAN"; } # { name = "Go"; bin = "${pkgs.go}/bin/go"; versionCmd = "version"; color = "CYAN"; }
# { name = "Rust"; bin = "${pkgs.rustc}/bin/rustc"; versionCmd = "--version"; color = "YELLOW"; } # { name = "Rust"; bin = "${pkgs.rustc}/bin/rustc"; versionCmd = "--version"; color = "YELLOW"; }
# { name = "golangci-lint"; bin = "golangci-lint"; versionCmd = "version"; color = "YELLOW"; }
]; ];
preToolHook = ''
# runs before the ready banner + tool version logs
# useful for installing tools outside nixpkgs and updating PATH first
#
# export GOBIN="$PWD/.tools/bin"
# export PATH="$GOBIN:$PATH"
# if ! command -v golangci-lint >/dev/null 2>&1; then
# go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# fi
'';
extraShellHook = '' extraShellHook = ''
# any repo-specific shell setup here # any repo-specific shell setup here
''; '';