Skip to content

Commit

Permalink
scripts: Tidy up
Browse files Browse the repository at this point in the history
- Use `git rev-parse` for `cd` to repo root instead of being relative to script path
- Add shebang to scripts where possible, and add shellcheck shell directive to nix-shell scripts
- Remove nix-format.sh due to treefmt (to be provided by `nix fmt`) respecting exclude lists
  • Loading branch information
RandomSpaceship committed Nov 28, 2024
1 parent 2c67e40 commit bc20978
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 74 deletions.
6 changes: 4 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#! /usr/bin/env bash
# use nix direnv to make evaluation faster
if ! has nix_direnv_version || ! nix_direnv_version 3.0.5; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.5/direnvrc" "sha256-RuwIS+QKFj/T9M2TFXScjBsLR6V3A17YVoEW/Q6AZ1w=" &>/dev/null
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.5/direnvrc" "sha256-RuwIS+QKFj/T9M2TFXScjBsLR6V3A17YVoEW/Q6AZ1w=" &>/dev/null
fi

use flake
# print out autocomplete instructions
echo --------------------
echo "Run this command to set up autocomplete in your shell:"
echo "eval \"\$(mk-workspace-shell-setup)\""
# shellcheck disable=SC2016
echo 'eval "$(mk-workspace-shell-setup)"'
echo --------------------
1 change: 1 addition & 0 deletions software/scripts/autocomplete.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/usr/bin/env bash
# source file this to set up autocomplete
eval "$(mk-workspace-shell-setup)"
6 changes: 3 additions & 3 deletions software/scripts/cachix-push-aarch64.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#! /usr/bin/env nix-shell
#! nix-shell -p cachix jq -i bash
# shellcheck shell=bash

set -e

# cd to the Software root so it picks the flake properly
SCRIPT_DIR="$(dirname "$(readlink -f $0)")"
cd $SCRIPT_DIR/..
# cd to the repo root
cd "$(git rev-parse --show-toplevel)"

# push built packages
nix build .#packages.aarch64-linux.rosCore --json | jq -r '.[].outputs | to_entries[].value' | cachix push qutrc-roar
Expand Down
6 changes: 3 additions & 3 deletions software/scripts/cachix-push.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#! /usr/bin/env nix-shell
#! nix-shell -p cachix jq -i bash
# shellcheck shell=bash

set -e

# cd to the Software root so it picks the flake properly
SCRIPT_DIR="$(dirname "$(readlink -f $0)")"
cd $SCRIPT_DIR/..
# cd to the repo root
cd "$(git rev-parse --show-toplevel)"

# push built packages
nix build .#rosCore --json | jq -r '.[].outputs | to_entries[].value' | cachix push qutrc-roar
Expand Down
17 changes: 8 additions & 9 deletions software/scripts/clean.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#! /usr/bin/env bash

set -e

shopt -s globstar # Enable globstar for recursive globbing
SCRIPT_DIR="$(dirname "$(readlink -f $0)")"
PROJECT_ROOT="$SCRIPT_DIR/../.."
cd $PROJECT_ROOT

rm -rf "$PROJECT_ROOT"/**/result
rm -rf "$PROJECT_ROOT"/**/build
rm -rf "$PROJECT_ROOT"/**/log
rm -rf "$PROJECT_ROOT"/**/install
rm -rf "$PROJECT_ROOT"/**/generated
# cd to the repo root
cd "$(git rev-parse --show-toplevel)"

rm -rf ./**/result
rm -rf ./**/build
rm -rf ./**/log
rm -rf ./**/install
rm -rf ./**/generated
13 changes: 0 additions & 13 deletions software/scripts/nix-format.sh

This file was deleted.

32 changes: 17 additions & 15 deletions software/scripts/nix-package.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env bash

set -e

# cd to the script location so everything searches in and goes to the right place
SCRIPT_DIR="$(dirname "$(readlink -f $0)")"
WORKSPACE_ROOT="$(dirname "$SCRIPT_DIR")"
cd $WORKSPACE_ROOT
# cd to the software directory
cd "$(git rev-parse --show-toplevel)"/software

ROS_WS="$WORKSPACE_ROOT"/ros_ws
ROS_WS="ros_ws"
OUTPUT_DIR="$ROS_WS"/nix-packages

echo "Cleaning old packaging files"
Expand All @@ -17,28 +17,30 @@ echo "Running ros2nix to generate new packaging files..."
# -i (--ignore-environment) clears all environment variables and drops you into a shell
# --command automatically runs the following command and exits the shell once it completes
# nix run is the "better" way to do this, but it doesn't allow for complete environment isolation like the shell -i flag.
# we want word splitting here to get all the package files in one go, so disable the shellcheck warning
# shellcheck disable=SC2046
nix shell -i github:wentasah/ros2nix --command ros2nix --output-dir="$OUTPUT_DIR" --output-as-nix-pkg-name --no-default --distro humble $(find "$ROS_WS" -name package.xml)
echo "Formatting generated files"
cd "$OUTPUT_DIR"
nix fmt --quiet

if [[ "$*" == *--no-commit* ]]; then
echo "Will not commit changes"
exit 0
if [[ $* == *--no-commit* ]]; then
echo "Will not commit changes"
exit 0
fi

git add -N "$INVENTORY_LOCATION"
if ! git diff --cached --quiet >/dev/null; then
HAS_GIT_STAGING=1
echo "Stashing staged changes"
git stash push -S >/dev/null
HAS_GIT_STAGING=1
echo "Stashing staged changes"
git stash push -S >/dev/null
fi

cleanup() {
if [[ -v HAS_GIT_STAGING ]]; then
echo "Restoring git stash"
git stash pop >/dev/null
fi
if [[ -v HAS_GIT_STAGING ]]; then
echo "Restoring git stash"
git stash pop >/dev/null
fi
}
trap cleanup EXIT

Expand Down
59 changes: 30 additions & 29 deletions software/scripts/nix-setup.sh
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
#!/usr/bin/env bash

set -e

echo "Setting up git submodule repos"

SCRIPT_DIR="$(dirname "$(readlink -f $0)")"
# cd to project root so git will work properly
cd $SCRIPT_DIR/../..
# cd to the repo root
cd "$(git rev-parse --show-toplevel)"

# clone submodules if not already done so workspace can actually build
git submodule update --init --recursive

if ! command -v nix &>/dev/null; then
echo "Setting up Nix"
# install nix! https://github.com/DeterminateSystems/nix-installer
# using the Determinate Systems installer rather than the "official" installer for its uninstall options and better error handling
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
echo "Setting up Nix"
# install nix! https://github.com/DeterminateSystems/nix-installer
# using the Determinate Systems installer rather than the "official" installer for its uninstall options and better error handling
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
else
echo "Nix already present on system!"
echo "Nix already present on system!"
fi

# add current user to nix trusted-users so we can do things like configure binary caching
if grep -Fq "$USER" /etc/nix/nix.conf; then
echo "Nix trusted-users already set up!"
echo "Nix trusted-users already set up!"
else
echo "Adding current user to nix trusted-users..."
echo "trusted-users = root $USER" | sudo tee -a /etc/nix/nix.conf
echo "Adding current user to nix trusted-users..."
echo "trusted-users = root $USER" | sudo tee -a /etc/nix/nix.conf

# restart Nix daemon so above changes take effect
echo "Restarting nix daemon"
sudo systemctl restart nix-daemon
# restart Nix daemon so above changes take effect
echo "Restarting nix daemon"
sudo systemctl restart nix-daemon
fi

# add direnv hooks to bash and zsh if not already present
if grep -Fq 'direnv hook bash' ~/.bashrc &>/dev/null; then
echo "bash direnv hook already set up!"
echo "bash direnv hook already set up!"
elif command -v bash &>/dev/null; then
echo "Setting up bash direnv hook..."
echo "eval \"\$(direnv hook bash)\"" >>~/.bashrc
echo "Setting up bash direnv hook..."
# shellcheck disable=SC2016
echo 'eval "$(direnv hook bash)"' >>~/.bashrc
else
echo "bash not detected on system, not setting up .bashrc"
echo "bash not detected on system, not setting up .bashrc"
fi

if grep -Fq 'direnv hook zsh' ~/.zshrc &>/dev/null; then
echo "zsh direnv hook already set up!"
echo "zsh direnv hook already set up!"
elif command -v zsh &>/dev/null; then
echo "Setting up zsh direnv hook..."
echo "eval \"\$(direnv hook zsh)\"" >>~/.zshrc
echo "Setting up zsh direnv hook..."
# shellcheck disable=SC2016
echo 'eval "$(direnv hook zsh)"' >>~/.zshrc
else
echo "zsh not detected on system, not setting up .zshrc"
echo "zsh not detected on system, not setting up .zshrc"
fi

if grep -Fq 'hide_env_diff' ~/.config/direnv/direnv.toml &>/dev/null; then
echo "direnv hide_env_diff already set up!"
echo "direnv hide_env_diff already set up!"
else
mkdir -p ~/.config/direnv
touch ~/.config/direnv/direnv.toml
echo "[global]" >>~/.config/direnv/direnv.toml
echo "hide_env_diff = true" >>~/.config/direnv/direnv.toml
mkdir -p ~/.config/direnv
touch ~/.config/direnv/direnv.toml
echo "[global]" >>~/.config/direnv/direnv.toml
echo "hide_env_diff = true" >>~/.config/direnv/direnv.toml
fi

# cd to Software/ folder for direnv config
cd $SCRIPT_PATH/..
# allow direnv to configure based on the .envrc file in the current directory
direnv allow

Expand Down

0 comments on commit bc20978

Please sign in to comment.