213 lines
6.5 KiB
Bash
213 lines
6.5 KiB
Bash
#!/usr/bin/env bash
|
|
# dev: enter, combine and auto-activate (direnv) the development shells of a
|
|
# flake, by default this NixOS configuration (modules/dev/shells). See --help.
|
|
set -euo pipefail
|
|
|
|
die() {
|
|
printf 'dev: %s\n' "$*" >&2
|
|
exit 1
|
|
}
|
|
|
|
usage() {
|
|
cat <<'USAGE'
|
|
Usage:
|
|
dev [list] list the dev shells
|
|
dev SHELL... [-p PKG]... [-c CMD [ARG]...]
|
|
enter SHELL in the current directory; several
|
|
shells and -p nixpkgs packages are merged into
|
|
one ad-hoc shell; -c runs CMD instead of $SHELL
|
|
dev init SHELL [DIR] write DIR/.envrc (default: .) so that direnv
|
|
enters SHELL there on cd, and allow it
|
|
|
|
Options:
|
|
--flake DIR flake with the shells (default: $DEV_FLAKE, else $NH_FLAKE,
|
|
else ~/nixos-config); the top of a git checkout
|
|
-f, --force init: replace an existing .envrc
|
|
-h, --help show this help
|
|
|
|
Examples:
|
|
dev rust
|
|
dev rust cpp -p python3
|
|
dev cpp -c cmake --build build
|
|
dev init rust-cpp ~/src/project
|
|
USAGE
|
|
}
|
|
|
|
# Flake inputs are git+file: only tracked files are copied to the store. A plain
|
|
# directory would be copied whole (keys/ included), so insist on a checkout.
|
|
resolve_flake() {
|
|
local dir=$1 top
|
|
[ -d "$dir" ] || die "no flake directory $dir (use --flake DIR or set DEV_FLAKE)"
|
|
dir=$(cd "$dir" && pwd -P)
|
|
[ -f "$dir/flake.nix" ] || die "no flake.nix in $dir"
|
|
top=$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null) ||
|
|
die "$dir is not a git checkout (flakes only see tracked files)"
|
|
[ "$top" = "$dir" ] || die "$dir is inside the checkout $top; pass its top directory"
|
|
case $dir in
|
|
*[!A-Za-z0-9._/+-]*) die "unsupported character in the flake path $dir" ;;
|
|
esac
|
|
printf '%s\n' "$dir"
|
|
}
|
|
|
|
check_name() {
|
|
case $1 in
|
|
'' | -* | *[!A-Za-z0-9_+-]*) die "invalid shell name: '$1'" ;;
|
|
esac
|
|
}
|
|
|
|
check_package() {
|
|
case $1 in
|
|
'' | [.-]* | *[!A-Za-z0-9_.+-]*) die "invalid package attribute: '$1'" ;;
|
|
esac
|
|
}
|
|
|
|
# The system is asked of Nix once and kept in $system.
|
|
find_system() {
|
|
system=$(nix eval --raw --impure --expr builtins.currentSystem)
|
|
}
|
|
|
|
# The flake's devShells.<system> as name<TAB>description lines.
|
|
shell_table() {
|
|
nix eval --json "git+file://$flake#devShells.$system" \
|
|
--apply 'builtins.mapAttrs (_: s: s.meta.description or "")' |
|
|
jq -r 'to_entries[] | "\(.key)\t\(.value)"'
|
|
}
|
|
|
|
list_shells() {
|
|
printf 'Dev shells of %s:\n' "$flake"
|
|
shell_table | column -t -s $'\t' | sed 's/^/ /'
|
|
}
|
|
|
|
nix_strings() {
|
|
local s
|
|
for s in "$@"; do
|
|
printf '"%s" ' "$s"
|
|
done
|
|
}
|
|
|
|
enter() {
|
|
local name expr known
|
|
for name in "$@"; do check_name "$name"; done
|
|
for name in "${packages[@]}"; do check_package "$name"; done
|
|
[ "${#command[@]}" -gt 0 ] || command=("${SHELL:-bash}")
|
|
# nix develop makes SHELL the stdenv's bash; give the command the user's own.
|
|
command=(env "SHELL=${SHELL:-bash}" "${command[@]}")
|
|
if [ -n "${DEV_SHELL:-}" ]; then
|
|
printf 'dev: note: nesting in the dev shell %s\n' "$DEV_SHELL" >&2
|
|
fi
|
|
# Spelled devShells.<system>.NAME: a bare #NAME would also match a package.
|
|
if [ "$#" -eq 1 ] && [ "${#packages[@]}" -eq 0 ]; then
|
|
known=$(nix eval --json "git+file://$flake#devShells.$system" --apply "s: s ? \"$1\"") ||
|
|
die "could not read the dev shells of $flake"
|
|
[ "$known" = true ] || die "no dev shell '$1' in $flake (see: dev list)"
|
|
exec nix develop "git+file://$flake#devShells.$system.$1" --command "${command[@]}"
|
|
fi
|
|
# shellcheck disable=SC2016 # ${builtins.currentSystem} is Nix, not shell
|
|
expr=$(printf '(builtins.getFlake "git+file://%s").legacyPackages.${builtins.currentSystem}.composeShells { shells = [ %s]; packages = [ %s]; }' \
|
|
"$flake" "$(nix_strings "$@")" "$(nix_strings "${packages[@]}")")
|
|
exec nix develop --impure --expr "$expr" --command "${command[@]}"
|
|
}
|
|
|
|
init_envrc() {
|
|
[ "$#" -ge 1 ] && [ "$#" -le 2 ] || die "usage: dev init SHELL [DIR]"
|
|
local name=$1 dir=${2:-.} envrc table names
|
|
check_name "$name"
|
|
[ "${#packages[@]}" -eq 0 ] && [ "${#command[@]}" -eq 0 ] || die "init takes no -p or -c"
|
|
[ -d "$dir" ] || die "no directory $dir (init takes one shell; combine shells with extends in modules/dev/shells/)"
|
|
dir=$(cd "$dir" && pwd -P)
|
|
table=$(shell_table) || die "could not list the dev shells of $flake"
|
|
names=$(cut -f1 <<<"$table")
|
|
grep -qxF "$name" <<<"$names" || die "no dev shell '$name' in $flake (see: dev list)"
|
|
envrc=$dir/.envrc
|
|
# -L: a dangling symlink is "there" too.
|
|
if { [ -e "$envrc" ] || [ -L "$envrc" ]; } && [ "$force" -ne 1 ]; then
|
|
die "$envrc exists (--force replaces it)"
|
|
fi
|
|
# Written next to it and moved over it: a symlink is replaced, never written through.
|
|
envrc_tmp=$(mktemp "$dir/.envrc.XXXXXX")
|
|
trap 'rm -f -- "$envrc_tmp"' EXIT
|
|
cat >"$envrc_tmp" <<ENVRC
|
|
# Written by 'dev init $name': direnv enters the "$name" dev shell of
|
|
# $flake here. Editing the shells (modules/dev/shells/) reloads it.
|
|
watch_dir "$flake/modules/dev/shells"
|
|
watch_file "$flake/modules/flake/shells.nix"
|
|
use flake "$flake#$name"
|
|
ENVRC
|
|
chmod 0644 "$envrc_tmp"
|
|
mv -fT -- "$envrc_tmp" "$envrc" || die "could not write $envrc"
|
|
printf 'dev: wrote %s\n' "$envrc"
|
|
if command -v direnv >/dev/null 2>&1; then
|
|
direnv allow "$dir"
|
|
else
|
|
printf 'dev: direnv is not on PATH; run "direnv allow %s" once it is\n' "$dir" >&2
|
|
fi
|
|
}
|
|
|
|
flake=${DEV_FLAKE:-${NH_FLAKE:-$HOME/nixos-config}}
|
|
system=
|
|
envrc_tmp=
|
|
force=0
|
|
args=()
|
|
packages=()
|
|
command=()
|
|
while [ "$#" -gt 0 ]; do
|
|
case $1 in
|
|
--flake)
|
|
[ "$#" -ge 2 ] || die "--flake needs a directory"
|
|
flake=$2
|
|
shift 2
|
|
;;
|
|
--flake=*)
|
|
flake=${1#--flake=}
|
|
shift
|
|
;;
|
|
-p | --package)
|
|
[ "$#" -ge 2 ] || die "$1 needs a package"
|
|
packages+=("$2")
|
|
shift 2
|
|
;;
|
|
-c | --command)
|
|
shift
|
|
[ "$#" -ge 1 ] || die "-c needs a command"
|
|
command=("$@")
|
|
break
|
|
;;
|
|
-f | --force)
|
|
force=1
|
|
shift
|
|
;;
|
|
-h | --help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
--)
|
|
shift
|
|
args+=("$@")
|
|
break
|
|
;;
|
|
-*) die "unknown option $1 (see dev --help)" ;;
|
|
*)
|
|
args+=("$1")
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
flake=$(resolve_flake "$flake")
|
|
find_system
|
|
|
|
if [ "${#args[@]}" -eq 0 ] && [ "${#packages[@]}" -eq 0 ]; then
|
|
[ "${#command[@]}" -eq 0 ] || die "-c needs a shell: dev SHELL -c CMD"
|
|
list_shells
|
|
exit 0
|
|
fi
|
|
|
|
case ${args[0]:-} in
|
|
list | ls)
|
|
[ "${#args[@]}" -eq 1 ] || die "list takes no arguments"
|
|
list_shells
|
|
;;
|
|
init) init_envrc "${args[@]:1}" ;;
|
|
*) enter "${args[@]}" ;;
|
|
esac
|