From 75705ebced46d21085a72f258124ea56daedd3c5 Mon Sep 17 00:00:00 2001 From: Cian Hughes Date: Tue, 7 Apr 2026 13:18:12 +0100 Subject: [PATCH] Fixed bugs and improved justfile UX --- justfile | 16 ++++++++++++---- scripts/{boostrap.py => bootstrap.py} | 2 +- scripts/edit.py | 12 ++++++++---- scripts/packages.bb | 25 +++++++++++++++++-------- 4 files changed, 38 insertions(+), 17 deletions(-) rename scripts/{boostrap.py => bootstrap.py} (98%) diff --git a/justfile b/justfile index 5a16531..86d6c5e 100644 --- a/justfile +++ b/justfile @@ -10,11 +10,15 @@ default: _git-sync: @if [ -n "$(git status --porcelain)" ]; then \ - echo ">> Error: Git working directory is not clean. Stash or commit your changes first."; \ - exit 1; \ + echo ">> Stashing local changes..."; \ + git stash; \ + git pull --ff-only --recurse-submodules; \ + git submodule update --remote --recursive; \ + git stash pop; \ + else \ + git pull --ff-only --recurse-submodules; \ + git submodule update --remote --recursive; \ fi - git pull --ff-only --recurse-submodules - git submodule update --remote --recursive _flake-update: nix flake update @@ -83,6 +87,10 @@ repl: # Fully update the system, home-manager, and flatpaks update: prebuild _update-root _update-home update-flatpaks +# Preview system changes without activating them +dry-run: prebuild + sudo nixos-rebuild dry-activate --flake .?submodules=1#$(hostname) + # Run Nix and Flatpak garbage collection. Optionally specify age (e.g., 'just cleanup 7d') cleanup days="": @if [ -n "{{days}}" ]; then \ diff --git a/scripts/boostrap.py b/scripts/bootstrap.py similarity index 98% rename from scripts/boostrap.py rename to scripts/bootstrap.py index 117f6c8..2047d5b 100755 --- a/scripts/boostrap.py +++ b/scripts/bootstrap.py @@ -35,7 +35,7 @@ def clone_repository(repo_url: str, target_dir: str): """Clone the configuration repository and enter the directory.""" print(f">> Cloning {repo_url} into {target_dir}...") try: - subprocess.run(["git", "clone", repo_url, target_dir], check=True) + subprocess.run(["git", "clone", "--recursive", repo_url, target_dir], check=True) except subprocess.CalledProcessError: print("Error: Failed to clone repository.", file=sys.stderr) sys.exit(1) diff --git a/scripts/edit.py b/scripts/edit.py index d94f0c1..2135a4f 100755 --- a/scripts/edit.py +++ b/scripts/edit.py @@ -5,21 +5,24 @@ from pathlib import Path import mimetypes -def is_text_file(filepath): +def is_text_file(filepath: str): mime_type, _ = mimetypes.guess_type(filepath) if mime_type: return mime_type.startswith("text/") try: - with open(filepath, "tr") as f: - f.read(1024) + with open(filepath, "rb") as f: + chunk = f.read(1024) + if b"\0" in chunk: + return False # Null bytes usually mean binary return True - except (UnicodeDecodeError, IsADirectoryError): + except IsADirectoryError: return False app = typer.Typer() + @app.command() def edit(target: Path = typer.Argument(..., help="File or directory to edit")): if not target.exists(): @@ -35,5 +38,6 @@ def edit(target: Path = typer.Argument(..., help="File or directory to edit")): else: subprocess.run(["heh", str(target)]) + if __name__ == "__main__": app() diff --git a/scripts/packages.bb b/scripts/packages.bb index 95fe3bb..349c7d2 100755 --- a/scripts/packages.bb +++ b/scripts/packages.bb @@ -1,6 +1,7 @@ #!/usr/bin/env bb (require '[babashka.cli :as cli] + '[babashka.fs :as fs] '[babashka.process :refer [shell]] '[clojure.string :as str]) @@ -12,7 +13,7 @@ (def cli-spec {:spec {:home {:coerce :boolean :desc "Edit home-manager config instead of NixOS config"} - :sys {:desc "Specify system (defaults to current hostname)"} ; Removed :default from spec + :sys {:desc "Specify system (defaults to current hostname)"} :user {:default (System/getProperty "user.name") :desc "Specify user (defaults to current user)"} :update {:coerce :boolean :default true @@ -34,11 +35,19 @@ (str "home-manager/" sys "/packages.nix") (str "nixos/" sys "/packages.nix"))] - (println ">> Editing" target "...") - (shell "just" "edit" target) - - (if (:update opts) + (if-not (fs/exists? target) (do - (println ">> Applying updates...") - (shell "just" "quick-update")) - (println ">> Skipping update (no-update provided).")))) + (binding [*out* *err*] + (println (str ">> Error: Target file does not exist: " target)) + (println ">> Please ensure this system profile has been bootstrapped or configured first.")) + (System/exit 1)) + + (do + (println ">> Editing" target "...") + (shell "just" "edit" target) + + (if (:update opts) + (do + (println ">> Applying updates...") + (shell "just" "quick-update")) + (println ">> Skipping update (no-update provided)."))))))