Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions home-manager/services/dolt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,14 @@ let
repoDir = "${homeDir}/dotfiles";
beadsDir = "${repoDir}/.beads";
enabled = isKyber || isGalactica;
startScript = pkgs.writeShellScript "dotfiles-dolt-start.sh" ''
set -euo pipefail

mkdir -p "${beadsDir}"

if [ -d "${beadsDir}/dolt" ] && [ ! -L "${beadsDir}/dolt" ]; then
if [ -e "${beadsDir}/df" ]; then
echo "Refusing to migrate ${beadsDir}/dolt because ${beadsDir}/df already exists" >&2
exit 1
fi

mv -f "${beadsDir}/dolt" "${beadsDir}/df"
fi

if [ -d "${beadsDir}/df" ]; then
ln -sfn df "${beadsDir}/dolt"
fi

exec "${pkgs.dolt}/bin/dolt" sql-server \
-H 127.0.0.1 \
-P 3307 \
--data-dir "${beadsDir}" \
--loglevel info
'';
startScript = pkgs.writeShellScript "dotfiles-dolt-start.sh" (
builtins.readFile (
pkgs.replaceVars ./start.sh {
inherit beadsDir;
inherit (pkgs) dolt;
}
)
);
Comment on lines +14 to +21

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This implementation uses builtins.readFile on a derivation produced by pkgs.replaceVars, which is an Import From Derivation (IFD). IFD can cause performance issues during Nix evaluation because it forces a build to happen before evaluation can continue.

Since this follows an existing pattern in the repository, it may be acceptable, but a more efficient alternative would be to use pkgs.substituteAll to create the script derivation directly, avoiding the need to read the file back into Nix memory.

in
lib.mkIf enabled {
launchd.agents.dolt = lib.mkIf pkgs.stdenv.isDarwin {
Expand Down
24 changes: 24 additions & 0 deletions home-manager/services/dolt/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# @beadsDir@ and @dolt@ are substituted by pkgs.replaceVars.
set -euo pipefail

mkdir -p "@beadsDir@"

if [ -d "@beadsDir@/dolt" ] && [ ! -L "@beadsDir@/dolt" ]; then
if [ -e "@beadsDir@/df" ]; then
echo "Refusing to migrate @beadsDir@/dolt because @beadsDir@/df already exists" >&2
exit 1
fi

mv -f "@beadsDir@/dolt" "@beadsDir@/df"
fi

if [ -d "@beadsDir@/df" ]; then
ln -sfn df "@beadsDir@/dolt"
fi

exec "@dolt@/bin/dolt" sql-server \
-H 127.0.0.1 \
-P 3307 \
--data-dir "@beadsDir@" \
--loglevel info
6 changes: 3 additions & 3 deletions scripts/check-nix-inline-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ while IFS= read -r nix_file; do
return line ~ /^(\$DRY_RUN_CMD[[:space:]]+)?[^[:space:];|&<>`]*bin\/bash([[:space:]]+(".*"|\$\{.*\}|[^[:space:];|&<>`]+))+([[:space:]]+\|\|[[:space:]]+true)?[[:space:]]*\\?$/
}

function block_has_violation( i, line, saw_bash_call, saw_content) {
function block_has_violation( i, n, line, saw_bash_call, saw_content) {
saw_bash_call = 0
saw_content = 0
split(body, lines, "\n")
n = split(body, lines, "\n")

for (i in lines) {
for (i = 1; i <= n; i++) {
line = trim(lines[i])

if (line == "" || line ~ /^#/) continue
Expand Down
5 changes: 5 additions & 0 deletions spec/coverage_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ End
It 'has spec file for home-manager/services/docker/docker-setup.sh'
The path "spec/docker_setup_wrapper_spec.sh" should be exist
End

It 'has spec file for home-manager/services/dolt/start.sh'
The path "spec/dolt_start_spec.sh" should be exist
End
It 'has spec file for nix-darwin/services/pmset-battery-policy/power-policy.sh'
The path "spec/pmset_battery_policy_spec.sh" should be exist
End
Expand Down Expand Up @@ -402,6 +406,7 @@ home-manager/services/docker-postgres/start-postgres-wrapper.sh
home-manager/services/docker-postgres/start-postgres.sh
home-manager/services/docker/docker-setup.sh
home-manager/services/docker/setup-docker.sh
home-manager/services/dolt/start.sh
home-manager/services/dotfiles-updater/update.sh
home-manager/services/gas-town/start.sh
home-manager/services/make-updater/update.sh
Expand Down
70 changes: 70 additions & 0 deletions spec/dolt_start_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
# shellcheck disable=SC2329

Describe 'home-manager/services/dolt/start.sh'
SCRIPT="$PWD/home-manager/services/dolt/start.sh"

Describe 'script properties'
It 'uses bash shebang'
When run bash -c "head -1 '$SCRIPT'"
The output should include '#!/usr/bin/env bash'
End

It 'uses strict mode'
When run bash -c "grep 'set -euo pipefail' '$SCRIPT'"
The output should include 'set -euo pipefail'
End

It 'passes bash syntax check after stripping placeholders'
When run bash -c "sed 's|@[a-z_]*@|/usr|g' '$SCRIPT' | bash -n"
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
The status should be success
End
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
End

Describe 'placeholder substitutions'
It 'references @beadsDir@'
When run bash -c "grep '@beadsDir@' '$SCRIPT'"
The output should include '@beadsDir@'
End

It 'references @dolt@'
When run bash -c "grep '@dolt@' '$SCRIPT'"
The output should include '@dolt@'
End
End

Describe 'dolt migration behavior'
It 'migrates legacy dolt directory to df'
When run bash -c "grep 'mv -f' '$SCRIPT'"
The output should include 'mv -f'
End

It 'symlinks dolt to df for backwards compatibility'
When run bash -c "grep 'ln -sfn df' '$SCRIPT'"
The output should include 'ln -sfn df'
End

It 'refuses to overwrite an existing df directory'
When run bash -c "grep 'Refusing to migrate' '$SCRIPT'"
The output should include 'Refusing to migrate'
End
End

Describe 'sql-server invocation'
It 'binds to localhost'
When run bash -c "grep -- '-H 127.0.0.1' '$SCRIPT'"
The output should include '-H 127.0.0.1'
End

It 'listens on port 3307'
When run bash -c "grep -- '-P 3307' '$SCRIPT'"
The output should include '-P 3307'
End

It 'points --data-dir at the beads directory'
When run bash -c "grep -- '--data-dir' '$SCRIPT'"
The output should include '--data-dir'
End
End

End
Loading