From 470831a100867fb3cb4e68b828ee144a1571c1f0 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Wed, 19 Apr 2023 10:58:03 +0200 Subject: [PATCH] nushell: Add command_not_found hook --- home-manager/modules/nushell/config.nu | 23 +++++++++++++++++++---- home-manager/modules/nushell/default.nix | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/home-manager/modules/nushell/config.nu b/home-manager/modules/nushell/config.nu index 9be4915..18d31ef 100644 --- a/home-manager/modules/nushell/config.nu +++ b/home-manager/modules/nushell/config.nu @@ -86,9 +86,9 @@ let dark_theme = { } # External completer example -let carapace_completer = {|spans| - carapace $spans.0 nushell $spans | from json -} +# let carapace_completer = {|spans| +# carapace $spans.0 nushell $spans | from json +# } # The default config record. This is where much of your global configuration is setup. @@ -183,7 +183,7 @@ let-env config = { external: { enable: true # set to false to prevent nushell looking into $env.PATH to find more suggestions, `false` recommended for WSL users as this look up my be very slow max_results: 100 # setting it lower can improve completion performance at the cost of omitting some options - completer: $carapace_completer # check 'carapace_completer' above as an example + completer: null # $carapace_completer # check 'carapace_completer' above as an example } } filesize: { @@ -222,6 +222,21 @@ let-env config = { display_output: {|| if (term size).columns >= 100 { table -e } else { table } } + command_not_found: { |cmdname| ( + try { + let candidates = (open $env.command_not_found_db | query db $"select package from Programs where system = '($env.NIX_SYSTEM)' and name = '($cmdname)'") + if ($candidates | is-empty) { + return null + } + let packages = ($candidates | each {|pkg| $"\tnix-shell -p ($pkg.package)" } | str join "\n") + let multiple = if ($candidates | length) > 1 { " one of the following" } else { "" } + ( + $"The program (ansi $env.config.color_config.shape_external)($cmdname)(ansi reset) " + + $"is not in your PATH. You can make it available in an ephemeral shell by typing($multiple):\n" + + $"($packages)" + ) + } + )} } menus: [ # Configuration for default nushell menus # Note the lack of source parameter diff --git a/home-manager/modules/nushell/default.nix b/home-manager/modules/nushell/default.nix index 865bc68..9832748 100644 --- a/home-manager/modules/nushell/default.nix +++ b/home-manager/modules/nushell/default.nix @@ -18,6 +18,30 @@ in package = pkgs.nushell.override { additionalFeatures = p: p ++ [ "dataframe" ]; }; configFile.source = ./config.nu; envFile.source = ./env.nu; + + extraEnv = '' + let-env command_not_found_db = ${config.programs.command-not-found.dbPath} + let-env NIX_SYSTEM = "${pkgs.system}" + ''; + # extraConfig = '' + # let-env config = ($env.config | update hooks ($env.config.hooks | default [] command_not_found)) + # let-env config = ($env.config | update hooks.command_not_found ($env.config.hooks.command_not_found | append { |cmdname| ( + # let candidates = (open ${config.programs.command-not-found.dbPath} | query db $"select package from Programs where system = '${pkgs.system}' and name = '($cmdname)'") + # if ($candidates | is-empty) { + # return null + # } + # let packages = ($candidates | each {|pkg| $"\tnix-shell -p ($pkg.package)" } | str join "\n") + # let multiple = if ($candidates | length) > 1 { " one of the following" } else { "" } + # ( + # $"The program (ansi $env.config.color_config.shape_external)($cmdname)(ansi reset) " + + # $"is not in your PATH. You can make it available in an ephemeral shell by typing($multiple):\n" + + # $"($packages)" + # ) + # ) + # } + # ) + # ) + # ''; }; home.packages = with pkgs; [