can't rebuild configuration from a script

I wrote a rebuild script which opens my config in nvim, checks for changes, and rebuilds if changes were made.

When I run the script on my desktop everything is fine, but when I run it from my laptop (nix-darwin if that matters), I get error: opening lock file '/nix/var/nix/profiles/system.lock': Permission denied. The weird thing is that I can rebuild manually, it’s just when it’s running in this script.

set -e

red=$(tput setaf 1)
reset_color=$(tput setaf 9)

force=false
git_push=false
notify=true

reset() {
popd >/dev/null
exit "$1"
}

show_help() {
cat <<-EOF
dot: a command to rebuild configs
flags:
-f  force rebuild even when there weren't changes
-h  help
-N  disable notificaitons
-p  run git pull before editing config
-P  git push after config is done
-r  rebuild without editing configs
EOF
}

pushd ~/dotfiles/ >/dev/null

while getopts "fhNpPr" arg; do
case "$arg" in
f) force=true ;;
h) show_help ;;
N) notify=false ;;
p) git pull ;;
P) git_push=true ;;
r)
eval "$REBUILD_COMMAND" 2>&1 || (
printf "\n%s\n" "${red}rebuild failed!${reset_color}" && reset 1
)
reset 0
;;
*)
echo "invalid flag: $arg"
show_help
reset 1
;;
esac
done

eval "$EDITOR"

if [[ $force == false ]]; then
if git diff --quiet; then
reset 0
fi
fi

if [[ ($force == true) || (! $(git diff --quiet "./nix/*")) ]]; then

alejandra ./nix/* &>/dev/null || (
printf "\n%s%s" "$red" "formatting failed!" && reset 1
)

git diff -U0 nix/*

printf "\n%s\n" "rebuilding nixos"

eval "$REBUILD_COMMAND" 2>&1 || (
printf "\n%s%s\n" "$red" "rebuild failed!" && reset 1
)

current=$(eval "$CURRENT_COMMAND")

git commit -am "nix - $NAME gen $current" || (
printf "\n%s" "commit failed"
reset 1
)

if [[ $git_push == true ]]; then
git push
fi

if $notify; then
eval "$NOTIFY_COMMAND"
fi

fi

reset 0

I have two computers so I made a nix wrapper around this script and pass the correct commands as environment variables. I’ve tried having the rebuild command be the standard darwin-rebuild as well as using nh.

my full config is here in case you think it might be something else. I appreciate any help with this, it’s incredibly inconvenient.

I fixed it, the issue was my CURRENT_COMMAND needed sudo.