Skip to content

Commit 1fc72eb

Browse files
committed
languages: replace nix-hash with faster checksum check
nix-hash creates and hashes a NAR, which we don't need at any of the call sites. There is significant overhead to running this command (100+ms if not more sometimes, daemon xpc?).
1 parent f30a244 commit 1fc72eb

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/modules/languages/javascript.nix

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ let
1111
lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''
1212
}node_modules";
1313

14+
dirPrefix = lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"'';
15+
1416
initNpmScript = pkgs.writeShellScript "init-npm.sh" ''
1517
function _devenv-npm-install()
1618
{
1719
# Avoid running "npm install" for every shell.
1820
# Only run it when the "package-lock.json" file or nodejs version has changed.
1921
# We do this by storing the nodejs version and a hash of "package-lock.json" in node_modules.
20-
local ACTUAL_NPM_CHECKSUM="${cfg.npm.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${
21-
lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''
22-
}package-lock.json)"
22+
local ACTUAL_NPM_CHECKSUM="${cfg.npm.package.version}:${config.lib._fileChecksum "${dirPrefix}package-lock.json"}"
2323
local NPM_CHECKSUM_FILE="${nodeModulesPath}/package-lock.json.checksum"
2424
if [ -f "$NPM_CHECKSUM_FILE" ]
2525
then
@@ -61,9 +61,7 @@ let
6161
# Avoid running "pnpm install" for every shell.
6262
# Only run it when the "package-lock.json" file or nodejs version has changed.
6363
# We do this by storing the nodejs version and a hash of "package-lock.json" in node_modules.
64-
local ACTUAL_PNPM_CHECKSUM="${cfg.pnpm.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${
65-
lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''
66-
}pnpm-lock.yaml)"
64+
local ACTUAL_PNPM_CHECKSUM="${cfg.pnpm.package.version}:${config.lib._fileChecksum "${dirPrefix}pnpm-lock.yaml"}"
6765
local PNPM_CHECKSUM_FILE="${nodeModulesPath}/pnpm-lock.yaml.checksum"
6866
if [ -f "$PNPM_CHECKSUM_FILE" ]
6967
then
@@ -105,9 +103,7 @@ let
105103
# Avoid running "yarn install" for every shell.
106104
# Only run it when the "yarn.lock" file or nodejs version has changed.
107105
# We do this by storing the nodejs version and a hash of "yarn.lock" in node_modules.
108-
local ACTUAL_YARN_CHECKSUM="${cfg.yarn.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${
109-
lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''
110-
}yarn.lock)"
106+
local ACTUAL_YARN_CHECKSUM="${cfg.yarn.package.version}:${config.lib._fileChecksum "${dirPrefix}yarn.lock"}"
111107
local YARN_CHECKSUM_FILE="${nodeModulesPath}/yarn.lock.checksum"
112108
if [ -f "$YARN_CHECKSUM_FILE" ]
113109
then
@@ -150,9 +146,7 @@ let
150146
# Avoid running "bun install" for every shell.
151147
# Only run it when the "bun.lock" file or nodejs version has changed.
152148
# We do this by storing the nodejs version and a hash of "bun.lock" in node_modules.
153-
local ACTUAL_BUN_CHECKSUM="${cfg.bun.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${
154-
lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''
155-
}bun.lock)"
149+
local ACTUAL_BUN_CHECKSUM="${cfg.bun.package.version}:${config.lib._fileChecksum "${dirPrefix}bun.lock"}"
156150
local BUN_CHECKSUM_FILE="${nodeModulesPath}/bun.lock.checksum"
157151
if [ -f "$BUN_CHECKSUM_FILE" ]
158152
then
@@ -187,9 +181,7 @@ let
187181
# Avoid running "bun install --yarn" for every shell.
188182
# Only run it when the "yarn.lock" file or nodejs version has changed.
189183
# We do this by storing the nodejs version and a hash of "yarn.lock" in node_modules.
190-
local ACTUAL_BUN_CHECKSUM="${cfg.bun.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${
191-
lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''
192-
}yarn.lock)"
184+
local ACTUAL_BUN_CHECKSUM="${cfg.bun.package.version}:${config.lib._fileChecksum "${dirPrefix}yarn.lock"}"
193185
local BUN_CHECKSUM_FILE="${nodeModulesPath}/yarn.lock.checksum"
194186
if [ -f "$BUN_CHECKSUM_FILE" ]
195187
then

src/modules/languages/python/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ let
190190
191191
# Avoid running "uv sync" for every shell.
192192
# Only run it when the "pyproject.toml" file or Python interpreter has changed.
193-
local ACTUAL_UV_CHECKSUM="${cfg.package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):''${UV_SYNC_COMMAND[@]}"
193+
local ACTUAL_UV_CHECKSUM="${cfg.package.interpreter}:${config.lib._fileChecksum "pyproject.toml"}:''${UV_SYNC_COMMAND[@]}"
194194
local UV_CHECKSUM_FILE="$VENV_PATH/uv.sync.checksum"
195195
if [ -f "$UV_CHECKSUM_FILE" ]
196196
then
@@ -250,7 +250,7 @@ let
250250
# Avoid running "poetry install" for every shell.
251251
# Only run it when the "poetry.lock" file or Python interpreter has changed.
252252
# We do this by storing the interpreter path and a hash of "poetry.lock" in venv.
253-
local ACTUAL_POETRY_CHECKSUM="${cfg.package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):$(${pkgs.nix}/bin/nix-hash --type sha256 poetry.lock):''${POETRY_INSTALL_COMMAND[@]}"
253+
local ACTUAL_POETRY_CHECKSUM="${cfg.package.interpreter}:${config.lib._fileChecksum "pyproject.toml"}:${config.lib._fileChecksum "poetry.lock"}:''${POETRY_INSTALL_COMMAND[@]}"
254254
local POETRY_CHECKSUM_FILE=".venv/poetry.lock.checksum"
255255
if [ -f "$POETRY_CHECKSUM_FILE" ]
256256
then

src/modules/lib.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ lib, config, inputs, ... }:
1+
{ pkgs, lib, config, inputs, ... }:
22

33
{
44
# freestyle
@@ -76,6 +76,10 @@
7676
})
7777
results);
7878

79+
# Generate a shell expression that checksums a file for change detection.
80+
# Returns only the checksum value (no filename).
81+
_fileChecksum = path: "$(${pkgs.coreutils}/bin/cksum ${lib.escapeShellArg path} | ${pkgs.coreutils}/bin/cut -f1 -d' ')";
82+
7983
mkTests = folder:
8084
let
8185
mk = dir: {

0 commit comments

Comments
 (0)