From 3cfa7ed0e1910123d78d5a371e785166c40107a4 Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Wed, 30 Apr 2025 00:43:40 -0400 Subject: [PATCH] New command features for try and update, and yay_run improvement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Replaces raw eval with a safer fish shell command execution • Improves try commands with '--' separator for running one-shot commands • Adds support for updating a specific flake input via flag or positional argument • Updates documentation and completions for consistency and clarity --- flake.lock | 61 ------------- functions/__yay_run.fish | 18 ++-- functions/__yay_try.fish | 53 +++++++++-- functions/__yay_update.fish | 37 ++++++-- readme.md | 44 +++++---- share/fish/completions/yay.fish | 152 +++++++++++++++++++++++++------- 6 files changed, 234 insertions(+), 131 deletions(-) delete mode 100644 flake.lock diff --git a/flake.lock b/flake.lock deleted file mode 100644 index 1f98193..0000000 --- a/flake.lock +++ /dev/null @@ -1,61 +0,0 @@ -{ - "nodes": { - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1731533236, - "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "nixpkgs": { - "locked": { - "lastModified": 1745930157, - "narHash": "sha256-y3h3NLnzRSiUkYpnfvnS669zWZLoqqI6NprtLQ+5dck=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "46e634be05ce9dc6d4db8e664515ba10b78151ae", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "root": { - "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - } - }, - "root": "root", - "version": 7 -} diff --git a/functions/__yay_run.fish b/functions/__yay_run.fish index e365c67..3fd2ebc 100644 --- a/functions/__yay_run.fish +++ b/functions/__yay_run.fish @@ -1,11 +1,13 @@ function __yay_run - set cmd $argv[1] - __yay_yellow "→ $cmd" - eval $cmd - if test $status -eq 0 - __yay_green "✔ $cmd" + set -l cmd_string $argv[1] + __yay_yellow "→ $cmd_string" + # Use fish -c for slightly safer execution than raw eval + fish -c "$cmd_string" + set -l run_status $status + if test $run_status -eq 0 + __yay_green "✔ $cmd_string" else - __yay_red "✘ $cmd (exit $status)" - return $status + __yay_red "✘ $cmd_string (exit $run_status)" + return $run_status end -end +end \ No newline at end of file diff --git a/functions/__yay_try.fish b/functions/__yay_try.fish index fce748c..2b4c2de 100644 --- a/functions/__yay_try.fish +++ b/functions/__yay_try.fish @@ -1,12 +1,55 @@ function __yay_try + set -l all_args $argv + set -l opts h/help - argparse $opts -- $argv; or return - if set -q _flag_help; or test (count $argv) -eq 0 - echo "Usage: yay try PACKAGE [PACKAGE...]" + argparse $opts -- $all_args; or return + + if set -q _flag_help; or test (count $all_args) -eq 0 + echo "Usage: yay try PACKAGE [PACKAGE...] [-- COMMAND [ARGS...]]" echo " -h, --help Show this help message" return end + + # Initialize variables in function scope + set -l pkgs + set -l cmd_str + set -l sep_index (contains --index -- '--' $all_args) + + if test $status -eq 0 # Found '--' separator + # Extract packages before '--' + if test $sep_index -gt 1 + set pkgs $all_args[1..(math $sep_index - 1)] + end + + # Extract and escape command after '--' + if test $sep_index -lt (count $all_args) + set -l raw_cmd $all_args[(math $sep_index + 1)..-1] + set -l escaped_cmd + for arg in $raw_cmd + set escaped_cmd $escaped_cmd (string escape -- "$arg") + end + set cmd_str (string join ' ' -- $escaped_cmd) + else + echo "Error: no command specified after --" >&2 + return 1 + end + else # No '--' found + set pkgs $all_args + end + + if test (count $pkgs) -eq 0 + echo "Error: no packages specified" >&2 + return 1 + end + __yay_green "««« CREATING NIX SHELL »»»" - __yay_yellow "Loading packages: $argv" - __yay_run "nix-shell -p $argv --command fish" + __yay_yellow "Loading packages: $pkgs" + + set -l pkg_str (string join ' ' -- $pkgs) + if test -n "$cmd_str" + __yay_yellow "Running: $cmd_str" + __yay_run "nix-shell -p $pkg_str --run \"$cmd_str\"" + else + __yay_run "nix-shell -p $pkg_str --command fish" + end end \ No newline at end of file diff --git a/functions/__yay_update.fish b/functions/__yay_update.fish index 0018e06..f078361 100644 --- a/functions/__yay_update.fish +++ b/functions/__yay_update.fish @@ -1,16 +1,35 @@ function __yay_update - set -l opts h/help 'p/path=' + set -l opts h/help 'p/path=' 'i/input=' argparse $opts -- $argv; or return if set -q _flag_help - echo "Usage: yay update [OPTIONS]" + echo "Usage: yay update [OPTIONS] [INPUT]" echo " -p, --path PATH Path to the Nix configuration (overrides FLAKE)" - echo " -h, --help Show this help message" + echo " -i, --input INPUT Name of the specific input to update" + echo " -h, --help Show this help message" return end + set flake_path (__yay_get_flake_path $_flag_path); or return - __yay_green "««« UPDATING FLAKE INPUTS »»»" - set orig (pwd) - cd $flake_path - __yay_run "nix flake update" - cd $orig -end + + # Check if a specific input was provided either as flag or positional arg + set -l input_name "" + if test -n "$_flag_input" + set input_name $_flag_input + else if test (count $argv) -gt 0 + set input_name $argv[1] + end + + if test -n "$input_name" + __yay_green "««« UPDATING FLAKE INPUT: $input_name »»»" + set orig (pwd) + cd $flake_path + __yay_run "nix flake lock --update-input $input_name" + cd $orig + else + __yay_green "««« UPDATING FLAKE INPUTS »»»" + set orig (pwd) + cd $flake_path + __yay_run "nix flake update" + cd $orig + end +end \ No newline at end of file diff --git a/readme.md b/readme.md index 717c325..895f83c 100644 --- a/readme.md +++ b/readme.md @@ -52,7 +52,7 @@ Add to your flake.nix: nix shell github:Tophc7/yay.nix ## run any yay command -yay try fastfetch +yay try fastfetch -- fastfetch --config examples/24 yay garbage ``` @@ -75,25 +75,33 @@ Options: ### update -Update flake inputs. +Update flake inputs. You can update all inputs or specify a single input to update. ```bash -yay update [OPTIONS] +yay update [OPTIONS] [INPUT] ``` Options: - `-p, --path PATH`: Path to the Nix configuration (overrides FLAKE) +- `-i, --input INPUT`: Name of the specific input to update (alternative to positional argument) - `-h, --help`: Show help message +Examples: +```bash +# Update all inputs +yay update -TODO: -- update specific inputs -- autocomplete for those inputs +# Update only the 'nixpkgs' input +yay update nixpkgs + +# Update 'nixpkgs' using the flag +yay update -i nixpkgs +``` ### garbage Clean up the Nix store and home-manager backups. -Super overkill don't come for me if something goes wrong. Regardless I use it all the time. +Super overkill don't come for me if something goes wrong. Regardless I use it all the time. ```bash yay garbage @@ -101,25 +109,29 @@ yay garbage This command: 1. Cleans using `nh clean all` (with and without sudo) -2. Runs `nix-collect-garbage --delete-old` (with and without sudo) +2. Runs `nix-collect-garbage --delete-old` (with and without sudo) 3. Executes `nix-store --gc` (with and without sudo) 4. Removes home-manager backup files ### try -Create a shell with specified packages. +Create a temporary shell with specified packages. You can either drop into an interactive shell or run a command directly within the environment using `--`. ```bash -yay try PACKAGE [PACKAGE...] +yay try PACKAGE [PACKAGE...] [-- COMMAND [ARGS...]] ``` -Example: +Examples: ```bash +# Enter an interactive shell with fastfetch and cowsay available yay try fastfetch cowsay -``` -TODO: -- allow `--` to run command directly +# Run 'fastfetch' directly using the packages in the temporary shell +yay try fastfetch -- fastfetch + +# Run 'cowsay moo' directly using the cowsay package +yay try cowsay -- cowsay moo +``` ### tar @@ -154,7 +166,7 @@ yay untar [OPTIONS] ARCHIVE [OUTPUT_DIR] ``` Options: -- `-o, --output DIR`: Output directory +- `-o, --output DIR`: Output directory - `-v, --verbose`: Enable verbose output - `-h, --help`: Show help message @@ -177,7 +189,7 @@ Yay.nix is implemented as a collection of fish functions that are installed into 1. Creates a temporary fish script 2. Sets up the fish function path to include the installed functions -3. Sources the main yay.fish file +3. Sources the main yay.fish file 4. Passes all command-line arguments to the appropriate fish function 5. Cleans up after execution diff --git a/share/fish/completions/yay.fish b/share/fish/completions/yay.fish index a987445..95e30b9 100644 --- a/share/fish/completions/yay.fish +++ b/share/fish/completions/yay.fish @@ -1,3 +1,7 @@ +###################### +# UTILITY FUNCTIONS # +###################### + # Fallback functions for older fish versions function __fish_seen_subcommand_from_fallback set -l cmd (commandline -poc) @@ -49,34 +53,38 @@ function __yay_is_token_n end end -# Complete the main command -complete -c yay -f +###################### +# HELPER FUNCTIONS # +###################### -# Complete the top-level subcommands -complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a rebuild -d "Rebuild the NixOS configuration" -complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a update -d "Update flake inputs" -complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a garbage -d "Clean up the Nix store" -complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a try -d "Create a shell with the specified package(s)" -complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a tar -d "Create compressed tar archives" -complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a untar -d "Extract tar archives" +# Flake input handling +function __yay_list_flake_inputs + set -l flake_path (__yay_get_flake_path $argv[1]); or return + + if not test -f "$flake_path/flake.lock" + return 1 + end + + # Extract input names from the flake.lock file using jq + jq -r '.nodes.root.inputs | keys[]' "$flake_path/flake.lock" 2>/dev/null +end -# Options for 'rebuild' -complete -c yay -n "__yay_seen_subcommand_from rebuild" -s p -l path -r -d "Path to the Nix configuration" -complete -c yay -n "__yay_seen_subcommand_from rebuild" -s H -l host -r -d "Hostname to build for" -complete -c yay -n "__yay_seen_subcommand_from rebuild" -s t -l trace -d "Enable trace output" -complete -c yay -n "__yay_seen_subcommand_from rebuild" -s h -l help -d "Show help message" +function __yay_get_flake_inputs + set -l cmd (commandline -poc) + set -l path "" + + # Check for --path or -p option + for i in (seq 1 (count $cmd)) + if test "$cmd[$i]" = "--path" -o "$cmd[$i]" = "-p"; and test (count $cmd) -ge (math $i + 1) + set path $cmd[(math $i + 1)] + break + end + end + + __yay_list_flake_inputs $path +end -# Options for 'update' -complete -c yay -n "__yay_seen_subcommand_from update" -s p -l path -r -d "Path to the Nix configuration" -complete -c yay -n "__yay_seen_subcommand_from update" -s h -l help -d "Show help message" - -# Options for 'garbage' -complete -c yay -n "__yay_seen_subcommand_from garbage" -s h -l help -d "Show help message" - -# Options for 'try' -complete -c yay -n "__yay_seen_subcommand_from try" -s h -l help -d "Show help message" - -# Package suggestions for 'try' +# Package listing with caching function __yay_list_packages # Use persistent cache file in /tmp (lasts until reboot) set -l cache_file /tmp/yay_packages_cache @@ -107,6 +115,85 @@ function __yay_list_packages printf "%s\n" $cleaned_packages end +###################### +# MAIN COMMAND # +###################### + +# Complete the main command +complete -c yay -f + +# Complete the top-level subcommands +complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a rebuild -d "Rebuild the NixOS configuration" +complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a update -d "Update flake inputs" +complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a garbage -d "Clean up the Nix store" +complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a try -d "Create a shell with the specified package(s)" +complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a tar -d "Create compressed tar archives" +complete -c yay -n "not __yay_seen_subcommand_from rebuild update garbage try tar untar" -a untar -d "Extract tar archives" + +###################### +# REBUILD SUBCOMMAND # +###################### + +# Options for 'rebuild' +complete -c yay -n "__yay_seen_subcommand_from rebuild" -s p -l path -r -d "Path to the Nix configuration" +complete -c yay -n "__yay_seen_subcommand_from rebuild" -s H -l host -r -d "Hostname to build for" +complete -c yay -n "__yay_seen_subcommand_from rebuild" -s t -l trace -d "Enable trace output" +complete -c yay -n "__yay_seen_subcommand_from rebuild" -s h -l help -d "Show help message" + +###################### +# UPDATE SUBCOMMAND # +###################### + +# Options for 'update' +complete -c yay -n "__yay_seen_subcommand_from update" -s p -l path -r -d "Path to the Nix configuration" +complete -c yay -n "__yay_seen_subcommand_from update" -s i -l input -r -a "(__yay_get_flake_inputs)" -d "Specific input to update" +complete -c yay -n "__yay_seen_subcommand_from update" -s h -l help -d "Show help message" + +# Positional argument for update (input name) +complete -c yay -n "__yay_seen_subcommand_from update; and not __yay_seen_option -i --input -h --help; and test (count (commandline -poc)) -eq 2" -a "(__yay_get_flake_inputs)" -d "Input to update" + +###################### +# GARBAGE SUBCOMMAND # +###################### + +# Options for 'garbage' +complete -c yay -n "__yay_seen_subcommand_from garbage" -s h -l help -d "Show help message" + +###################### +# TRY SUBCOMMAND # +###################### + +# Options for 'try' +complete -c yay -n "__yay_seen_subcommand_from try" -s h -l help -d "Show help message" + +# Package completion for try (before --) +function __yay_try_no_dash_dash_yet + __yay_seen_subcommand_from try + and not contains -- -- (commandline -poc) + and not string match -r -- '^-' (commandline -ct) +end +complete -c yay -n "__yay_try_no_dash_dash_yet" -a "(__yay_list_packages)" -d "Nix package" + +# Double dash separator completion for try +function __yay_try_can_add_dash_dash + __yay_seen_subcommand_from try + and not contains -- -- (commandline -poc) + and test (count (commandline -poc)) -gt 2 +end +complete -c yay -n "__yay_try_can_add_dash_dash" -a "--" -d "Separator for command to run" + +# Command completion after -- +function __yay_try_after_dash_dash + set -l tokens (commandline -poc) + contains -- -- $tokens + and __yay_seen_subcommand_from try +end +complete -c yay -n "__yay_try_after_dash_dash" -a "(__fish_complete_command)" -d "Command to run" + +###################### +# TAR SUBCOMMAND # +###################### + # Options for 'tar' complete -c yay -n "__yay_seen_subcommand_from tar" -s o -l output -r -d "Output file path" complete -c yay -n "__yay_seen_subcommand_from tar" -s c -l compression -r -a "zstd gzip bzip2 bzip3 7zip tar" -d "Compression type" @@ -115,7 +202,7 @@ complete -c yay -n "__yay_seen_subcommand_from tar" -s t -l threads -r -d "Threa complete -c yay -n "__yay_seen_subcommand_from tar" -s v -l verbose -d "Enable verbose output" complete -c yay -n "__yay_seen_subcommand_from tar" -s h -l help -d "Show help message" -# File path completion for tar input (simplified) +# File path completion for tar input function __yay_tar_needs_input __yay_seen_subcommand_from tar and count (commandline -poc) = 2 @@ -123,7 +210,7 @@ function __yay_tar_needs_input end complete -c yay -n __yay_tar_needs_input -r -d "Input path" -# File path completion for tar output (simplified) +# File path completion for tar output function __yay_tar_needs_output __yay_seen_subcommand_from tar and count (commandline -poc) = 3 @@ -131,12 +218,16 @@ function __yay_tar_needs_output end complete -c yay -n __yay_tar_needs_output -r -d "Output file" +###################### +# UNTAR SUBCOMMAND # +###################### + # Options for 'untar' complete -c yay -n "__yay_seen_subcommand_from untar" -s o -l output -r -d "Output directory" complete -c yay -n "__yay_seen_subcommand_from untar" -s v -l verbose -d "Enable verbose output" complete -c yay -n "__yay_seen_subcommand_from untar" -s h -l help -d "Show help message" -# File path completion for untar with archive extensions (simplified) +# File path completion for untar input with archive extensions function __yay_untar_needs_input __yay_seen_subcommand_from untar and count (commandline -poc) = 2 @@ -144,13 +235,10 @@ function __yay_untar_needs_input end complete -c yay -n __yay_untar_needs_input -r -a "*.tar *.tar.gz *.tgz *.tar.zst *.tzst *.tar.bz2 *.tbz *.tbz2 *.tb2 *.tz2 *.tar.bz3 *.7z *.tar.7z *.rar" -d "Archive file" -# Directory completion for untar output directory (simplified) +# Directory completion for untar output directory function __yay_untar_needs_output __yay_seen_subcommand_from untar and count (commandline -poc) = 3 and not __yay_seen_option -o --output end complete -c yay -n __yay_untar_needs_output -r -a "(__fish_complete_directories)" -d "Output directory" - -# Package completion for try -complete -c yay -n "__yay_seen_subcommand_from try; and not string match -r -- '^-' (commandline -ct)" -a "(__yay_list_packages)" -d "Nix package"