add basic nix alternative for stubs

Mon, 08 May 2023 02:36:32 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Mon, 08 May 2023 02:36:32 -0500
changeset 1112
0ae7864c4ad8
parent 1111
7f6a94dd56ee
child 1113
9b4ca32ec390

add basic nix alternative for stubs

.lib/sh/stubexec.sh file | annotate | diff | comparison | revisions
.lib/stubs/direnv file | annotate | diff | comparison | revisions
.lib/stubs/fzf file | annotate | diff | comparison | revisions
.lib/stubs/just file | annotate | diff | comparison | revisions
.lib/stubs/stow file | annotate | diff | comparison | revisions
.lib/stubs/tectonic file | annotate | diff | comparison | revisions
--- a/.lib/sh/stubexec.sh
+++ b/.lib/sh/stubexec.sh
@@ -38,11 +38,23 @@ stubexec() {
   if [ -x "$real_bin" ]; then
     exec "$real_bin" "$@"
   fi
+  try_nix_run "$@"
   install_it
   touch "$(realbin "$0")"  # In case of no updates
   stubexec "$@"
 }
 
+has() {
+  type "$1" >/dev/null 2>&1
+}
+
+try_nix_run() {
+  if [ "${nix_ref:-}" ] && has nix; then
+    # FIXME: this can be GC'd and doesn't allow running alternate commands
+    exec nix run "$nix_ref" -- "$@"
+  fi
+}
+
 bina_install() {
   local github_repo="$1"
   mkdir -p ~/.local/bin
--- a/.lib/stubs/direnv
+++ b/.lib/stubs/direnv
@@ -1,6 +1,8 @@
 #!/bin/sh
 set -eu
 
+nix_ref='nixpkgs#direnv'
+
 install_it() {
   mkdir -p ~/.local/bin
   curl -fsSL https://direnv.net/install.sh |
--- a/.lib/stubs/fzf
+++ b/.lib/stubs/fzf
@@ -1,6 +1,8 @@
 #!/bin/sh
 set -eu
 
+nix_ref='nixpkgs#fzf'
+
 install_it() {
   bina_install "junegunn/fzf"
 }
--- a/.lib/stubs/just
+++ b/.lib/stubs/just
@@ -1,6 +1,8 @@
 #!/bin/sh
 set -eu
 
+nix_ref='nixpkgs#just'
+
 install_it() {
   bina_install "casey/just"
   # cargo install just
--- a/.lib/stubs/stow
+++ b/.lib/stubs/stow
@@ -1,6 +1,8 @@
 #!/bin/sh
 set -eu
 
+nix_ref='nixpkgs#stow'
+
 install_it() {
   cpanm -n Stow
   asdf reshim perl || :
--- a/.lib/stubs/tectonic
+++ b/.lib/stubs/tectonic
@@ -2,6 +2,7 @@
 set -eu
 
 age_limit=30
+nix_ref='nixpkgs#tectonic'
 
 install_it() {
   mkdir -p ~/.local/bin

mercurial