diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c8366ed --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Emil Lerch + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..17b5a88 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +zvm-direnv +========== + +A way to use [zvm](https://zvm.app) with [direnv](https://direnv.net) + +Installing +---------- + +Put the following lines in your `.envrc`: + +```sh +if ! has zvm_direnv_version || ! zvm_direnv_version 1.0.0; then + source_url "https://git.lerch.org/lobo/zvm-direnv/1.0.0/direnvrc" #"sha256-RuwIS+QKFj/T9M2TFXScjBsLR6V3A17YVoEW/Q6AZ1w=" +fi +``` + +Usage +----- + +```sh +$ echo "use zig 0.13.0" >> .envrc +$ direnv allow +``` + +If you haven't used direnv before, make sure to [hook it into your shell](https://direnv.net/docs/hook.html) first. + +How it works +------------ + +This implementation utilizes zvm for installation of both [zig](https://ziglang.org) +and [zls](https://github.com/zigtools/zls). It avoids the use of `zvm use` as +this will change the zig version system-wide, possibly resulting in some +confusing behavior. Instead, it uses zvm to download the appropriate versions +if necessary, then prepends the desired version to the path used in the direnv +directory. diff --git a/direnvrc b/direnvrc new file mode 100644 index 0000000..7741cb4 --- /dev/null +++ b/direnvrc @@ -0,0 +1,54 @@ +# vi: ft=sh +# shellcheck shell=bash + +ZVM_DIRENV_VERSION=1.0.0 + +# Usage: zvm_direnv_version +# +# Checks that the nix-direnv version is at least as old as . +zvm_direnv_version() { + _require_version zvm-direnv $ZVM_DIRENV_VERSION "$1" +} + +_require_version() { + local cmd=$1 version=$2 required=$3 + if ! printf "%s\n" "$required" "$version" | LC_ALL=C sort -c -V 2>/dev/null; then + log_error \ + "zvm_direnv: minimum required $(basename "$cmd") version is $required (installed: $version)" + return 1 + fi +} + +use_zig() { + if [[ -z $1 ]]; then + log_error "Must specify a zig version!" + return 1 + fi + if [[ "$1" == "master" ]]; then + log_error "Please don't use master" + return 1 + fi + + if ! command -v "$1" > /dev/null 2>&1; then + log_error "zvm not installed - please install it from https://www.zvm.app/" + return 1 + fi + local version="$1" + # This is a bad idea, because it will make a global change + # zvm use $1 + if [[ "$(zvm ls | grep "$version")" != "$version" ]]; then + local current + # zvm 0.7.9 does not indicate which version is in use, so we have to find + # the version that was hard linked into the bin directory + current="$(find "${ZVM_INSTALL}/.." -samefile "${ZVM_INSTALL}/../bin/zig" | \ + rev | \ + cut -d/ -f2 | \ + rev)" + zvm install --zls "$1" # I think this is safe for all except master + + # zvm install will automatically use the installed version, which is normally + # cool, but in this case, we don't want to do that, so we will revert + zvm use "$current" + fi + PATH_add "${ZVM_INSTALL}/../$1" +}