From 1d933f1a458c510be9329edaab2b1381e098ac48 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sun, 23 Aug 2026 18:31:35 +0200 Subject: modules/fish: Init --- .../by-name/ni/nix-index/command_not_found.fish | 111 +++++++++++++++++++++ modules/by-name/ni/nix-index/command_not_found.sh | 74 -------------- modules/by-name/ni/nix-index/command_not_found.zsh | 74 ++++++++++++++ modules/by-name/ni/nix-index/module.nix | 6 +- 4 files changed, 190 insertions(+), 75 deletions(-) create mode 100644 modules/by-name/ni/nix-index/command_not_found.fish delete mode 100644 modules/by-name/ni/nix-index/command_not_found.sh create mode 100644 modules/by-name/ni/nix-index/command_not_found.zsh (limited to 'modules/by-name/ni') diff --git a/modules/by-name/ni/nix-index/command_not_found.fish b/modules/by-name/ni/nix-index/command_not_found.fish new file mode 100644 index 00000000..bbdb2230 --- /dev/null +++ b/modules/by-name/ni/nix-index/command_not_found.fish @@ -0,0 +1,111 @@ +#!/usr/bin/env fish + +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see . + +# This was taken from the +# `${pkgs.nix-index}/etc/profile.d/command-not-found.sh` file on 2024-02-28 + +function _log + printf $argv >&2 +end + +function _bail + __fish_default_command_not_found_handler $argv + return "$status" +end + +function _common_prefix + set --function prefix + + for str in $argv + set position 1 + for c in (string split '' "$str") + if test -z $prefix[$position] + set prefix[$position] "$c" + else if test $prefix[$position] != "$c" + if test $position = 1 + # The first letter of the prefix mis-matched. + # As such there is no shared prefix, and we can simple bail early. + echo "" + return + else + set prefix $prefix[1..(math $position - 1)] + end + break + end + + set position (math $position + 1) + end + end + + echo "$(string join "" $prefix)" +end + +function fish_command_not_found + # Only run if it's an interactive shell + if not status is-interactive + return (_bail $argv) + end + + # The nixpkgs flake should always be available on NixOS + set --local toplevel nixpkgs + set --local cmd "$argv[1]" + + set --local attrs "$(nix-locate --minimal --no-group --type x --type s --whole-name --at-root "/bin/$cmd")" + + if test -n "$attrs" + set --function len "$(echo "$attrs" | count)" + else + set --function len 0 + end + + switch "$len" + case 0 + case 1 or '*' + _log "The program '%s' is currently not installed.\n" "$cmd" + end + + switch "$len" + case 0 + return (_bail $argv) + case 1 + # If only one package provides this, then we can invoke it + # without asking the user. + + # These will not return 127 if they worked correctly. + + _log "A shell will be opened with it.\n" + + if nix build "$toplevel#$attrs" --no-link + nix shell "$toplevel#$attrs" + return "$status" + else + _log "Failed to build: '%s#%s'\n" "$toplevel" "$attrs" + return (_bail $argv) + end + case '*' + _log "It is provided by several packages. You can run it load it with:\n" + + set --local counter 0 + set --local attrs $(echo $attrs) + for attr in $attrs + _log "%3s)" "$counter" + _log " nix shell %s#%s\n" "$toplevel" "$attr"; + + set counter "$(math "$counter" + 1)" + end + + commandline "nix shell $toplevel#$(_common_prefix $attrs)" + end + + # command not found should always exit with 127 + return 127 +end diff --git a/modules/by-name/ni/nix-index/command_not_found.sh b/modules/by-name/ni/nix-index/command_not_found.sh deleted file mode 100644 index 579f9db4..00000000 --- a/modules/by-name/ni/nix-index/command_not_found.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env dash - -# nixos-config - My current NixOS configuration -# -# Copyright (C) 2025 Benedikt Peetz -# SPDX-License-Identifier: GPL-3.0-or-later -# -# This file is part of my nixos-config. -# -# You should have received a copy of the License along with this program. -# If not, see . - -# This was taken from the -# `${pkgs.nix-index}/etc/profile.d/command-not-found.sh` file on 2024-02-28 - -# for bash 4 -# this will be called when a command is entered -# but not found in the user’s path + environment -command_not_found_handle() { - # taken from http://www.linuxjournal.com/content/bash-command-not-found - # - do not run when inside Midnight Commander or within a Pipe - if [ -n "${MC_SID-}" ] || ! [ -t 1 ]; then - >&2 echo "$1: command not found" - return 127 - fi - - toplevel=nixpkgs # nixpkgs should always be available even in NixOS - cmd="$1" - attrs=$(nix-locate --minimal --no-group --type x --type s --whole-name --at-root "/bin/$cmd") - len=$(if [ -n "$attrs" ]; then echo "$attrs" | wc -l; else echo 0; fi) - - case "$len" in - 0) - printf "%s: command not found\n" "$cmd" >&2 - ;; - 1) - # If only one package provides this, then we can invoke it - # without asking the user. - - # These will not return 127 if they worked correctly. - - >&2 cat <&2 cat <&2 cat < +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see . + +# This was taken from the +# `${pkgs.nix-index}/etc/profile.d/command-not-found.sh` file on 2024-02-28 + +# for bash 4 +# this will be called when a command is entered +# but not found in the user’s path + environment +command_not_found_handle() { + # taken from http://www.linuxjournal.com/content/bash-command-not-found + # - do not run when inside Midnight Commander or within a Pipe + if [ -n "${MC_SID-}" ] || ! [ -t 1 ]; then + >&2 echo "$1: command not found" + return 127 + fi + + toplevel=nixpkgs # nixpkgs should always be available even in NixOS + cmd="$1" + attrs=$(nix-locate --minimal --no-group --type x --type s --whole-name --at-root "/bin/$cmd") + len=$(if [ -n "$attrs" ]; then echo "$attrs" | wc -l; else echo 0; fi) + + case "$len" in + 0) + printf "%s: command not found\n" "$cmd" >&2 + ;; + 1) + # If only one package provides this, then we can invoke it + # without asking the user. + + # These will not return 127 if they worked correctly. + + >&2 cat <&2 cat <&2 cat <