From 209294075c4bb449010722a2fc72fd437a029fe9 Mon Sep 17 00:00:00 2001 From: cswimr Date: Mon, 2 Dec 2024 15:05:24 -0500 Subject: [PATCH] improved automatic script alias creation and switched to lookup paths for nix-shell invocations --- config/xonsh/aliases.py | 23 +++++++++++++++---- scripts/common/__init__.py | 0 .../nix/python.nix | 4 +--- scripts/{ => py/common}/__init__.py | 0 scripts/{ => py}/common/common.py | 0 scripts/{ => py}/spectacle-screenshot.py | 2 +- scripts/{ => py}/zipline.py | 2 +- 7 files changed, 21 insertions(+), 10 deletions(-) delete mode 100644 scripts/common/__init__.py rename nixos/script-dependencies.nix => scripts/nix/python.nix (61%) rename scripts/{ => py/common}/__init__.py (100%) rename scripts/{ => py}/common/common.py (100%) rename scripts/{ => py}/spectacle-screenshot.py (98%) rename scripts/{ => py}/zipline.py (98%) diff --git a/config/xonsh/aliases.py b/config/xonsh/aliases.py index d8ed841..9d6188b 100644 --- a/config/xonsh/aliases.py +++ b/config/xonsh/aliases.py @@ -128,13 +128,26 @@ alias_dictionary = { "s": "sudo", } -# Create aliases for scripts with the .py extension stripped +# Create aliases for scripts with the file extension stripped script_path = Path("/etc/nixos/scripts") if script_path.exists(): - for script in script_path.glob("*.py"): - if script.name == "__init__.py": + for sub_dir in script_path.iterdir(): + # ignore files within the nix subdirectory, since they are just nix-shell expressions + # if you aren't using nix, you can remove this if statement + if sub_dir.name == "nix": continue - script_name = script.name.strip(".py") - aliases.update({script_name: [str(script)]}) + if not sub_dir.is_dir(): + c = colors.Colors + print(f"{c.YELLOW}{c.BOLD}WARNING: The path {c.PURPLE}'{sub_dir}'{c.YELLOW} is not a directory. Skipping alias creation for this path.{c.END}") + continue + + extension = f".{sub_dir.name}" + + for script in sub_dir.glob(f"*{extension}"): + if script.name == "__init__.py": + continue + + script_name = script.stem + aliases[script_name] = [str(script)] aliases.update(alias_dictionary) diff --git a/scripts/common/__init__.py b/scripts/common/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/nixos/script-dependencies.nix b/scripts/nix/python.nix similarity index 61% rename from nixos/script-dependencies.nix rename to scripts/nix/python.nix index 75a54cd..7d7a71a 100644 --- a/nixos/script-dependencies.nix +++ b/scripts/nix/python.nix @@ -1,7 +1,5 @@ let - nixpkgs = fetchTarball - "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz"; - pkgs = import nixpkgs { + pkgs = import { config = { }; overlays = [ ]; }; diff --git a/scripts/__init__.py b/scripts/py/common/__init__.py similarity index 100% rename from scripts/__init__.py rename to scripts/py/common/__init__.py diff --git a/scripts/common/common.py b/scripts/py/common/common.py similarity index 100% rename from scripts/common/common.py rename to scripts/py/common/common.py diff --git a/scripts/spectacle-screenshot.py b/scripts/py/spectacle-screenshot.py similarity index 98% rename from scripts/spectacle-screenshot.py rename to scripts/py/spectacle-screenshot.py index cc6d1fd..0c94b16 100755 --- a/scripts/spectacle-screenshot.py +++ b/scripts/py/spectacle-screenshot.py @@ -1,5 +1,5 @@ #! /usr/bin/env nix-shell -#! nix-shell /etc/nixos/nixos/script-dependencies.nix -i python +#! nix-shell /etc/nixos/scripts/nix/python.nix -i python import argparse import os diff --git a/scripts/zipline.py b/scripts/py/zipline.py similarity index 98% rename from scripts/zipline.py rename to scripts/py/zipline.py index b6e0aa3..d17f745 100755 --- a/scripts/zipline.py +++ b/scripts/py/zipline.py @@ -1,5 +1,5 @@ #! /usr/bin/env nix-shell -#! nix-shell /etc/nixos/nixos/script-dependencies.nix -i python +#! nix-shell /etc/nixos/scripts/nix/python.nix -i python import argparse import mimetypes