From 6082de62b094363bf2e693d9eabcf9ab26de8047 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Wed, 31 Jul 2024 18:00:00 +0100
Subject: [PATCH] Look for `.ignored/run` when running commands
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Make the `run` command cleverer by also searching for a `.ignored/run`
file, following the convention of always ignoring a `.ignored` directory
from Git.

This allows me to have a local `run` file that doesn't need to be
committed and pushed to the repo (e.g. it's too specific to me), and
being able to keep it outside of the root of the project directory where
it could be committed accidentally.

A similar approach was done by Andreas Möller using Makefiles in this
article:

https://localheinz.com/articles/2020/05/07/using-makefiles-in-projects-where-i-can-not-use-them

With this function, the `.ignored/run` file is executed if it's found
and exits with the status code of the command.

If not, it will fall back to using `./run` as before.

I considered reversing these and checking for `./run` first as there
could be a performance benefit but, if neither file is found, I want the
error to show `./run` and not `.ignored/run` as that's the main use
case.
---
 lib/shared/home-manager-packages.nix |  2 ++
 lib/shared/modules/zsh/aliases.nix   |  1 -
 lib/shared/scripts/run.nix           | 16 ++++++++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 lib/shared/scripts/run.nix

diff --git a/lib/shared/home-manager-packages.nix b/lib/shared/home-manager-packages.nix
index 22023f8a..af96f81f 100644
--- a/lib/shared/home-manager-packages.nix
+++ b/lib/shared/home-manager-packages.nix
@@ -7,12 +7,14 @@ let
 
   scripts = {
     notetaker = writeShellApplication (import ./scripts/notetaker.nix);
+    run = writeShellApplication (import ./scripts/run.nix { inherit pkgs; });
     t = writeShellApplication (import ./scripts/t.nix { inherit pkgs; });
   };
 in
 with pkgs;
 [
   scripts.notetaker
+  scripts.run
   scripts.t
 
   awscli2
diff --git a/lib/shared/modules/zsh/aliases.nix b/lib/shared/modules/zsh/aliases.nix
index bb3bd438..c7f58135 100644
--- a/lib/shared/modules/zsh/aliases.nix
+++ b/lib/shared/modules/zsh/aliases.nix
@@ -5,7 +5,6 @@
   "...." = "cd ../../..";
   "....." = "cd ../../../..";
   cat = "bat";
-  run = "./run";
   s = "secrets";
   secrets = ''doppler --project "$(whoami)" run'';
   tag = "tag-release";
diff --git a/lib/shared/scripts/run.nix b/lib/shared/scripts/run.nix
new file mode 100644
index 00000000..8533c4e8
--- /dev/null
+++ b/lib/shared/scripts/run.nix
@@ -0,0 +1,16 @@
+{ pkgs }:
+
+{
+  name = "run";
+
+  runtimeInputs = with pkgs; [ bashInteractive ];
+
+  text = ''
+    if [[ -e .ignored/run ]]; then
+      .ignored/run "$@"
+      exit $?
+    fi
+
+    ./run "$@"
+  '';
+}