From 4241fb5eaeb33e8a673a547d1eb1bb2d3fc7d34f Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Fri, 13 Jun 2025 23:25:17 +0700 Subject: [PATCH 1/7] command-not-found: Disable by default This tool is broken for users that use flakes and not nix-channels, and fails silently so few would be aware. --- nixos/modules/programs/command-not-found/command-not-found.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/programs/command-not-found/command-not-found.nix b/nixos/modules/programs/command-not-found/command-not-found.nix index 47c5e0de3f8b..a102c43cd57b 100644 --- a/nixos/modules/programs/command-not-found/command-not-found.nix +++ b/nixos/modules/programs/command-not-found/command-not-found.nix @@ -33,7 +33,7 @@ in enable = lib.mkOption { type = lib.types.bool; - default = true; + default = false; description = '' Whether interactive shells should show which Nix package (if any) provides a missing command. From b8bbe7706be9aca2190e9e3f78be895a81171dd5 Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Fri, 13 Jun 2025 23:27:39 +0700 Subject: [PATCH 2/7] command-not-found: Add database check to script We must check for the database every time we use the script so lets add it to the script itself. --- .../command-not-found/command-not-found.pl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/nixos/modules/programs/command-not-found/command-not-found.pl b/nixos/modules/programs/command-not-found/command-not-found.pl index 72e246c81ae9..4a6b7fa22381 100644 --- a/nixos/modules/programs/command-not-found/command-not-found.pl +++ b/nixos/modules/programs/command-not-found/command-not-found.pl @@ -10,6 +10,23 @@ my $program = $ARGV[0]; my $dbPath = "@dbPath@"; +if (! -e $dbPath) { + print STDERR "$program: command not found\n"; + print STDERR "\n"; + print STDERR "command-not-found: Missing package database\n"; + print STDERR "This likely means the database hasn't been generated yet.\n"; + print STDERR "This tool requires nix-channels to generate the database.\n"; + print STDERR "\n"; + print STDERR "If you are using nix-channels you can run:\n"; + print STDERR " sudo nix-channels --update\n"; + print STDERR "\n"; + print STDERR "If you are using flakes, see nix-index and nix-index-database.\n"; + print STDERR "\n"; + print STDERR "If you would like to disable this message you can set:\n"; + print STDERR " programs.command-not-found.enable = false;\n"; + exit 127; +} + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "") or die "cannot open database `$dbPath'"; $dbh->{RaiseError} = 0; From be5b0ef6999f90470eaa42a1f3a4db364d7e6e4c Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Fri, 13 Jun 2025 23:30:55 +0700 Subject: [PATCH 3/7] command-not-found: Simplify the wrappers - Now we check for database existence in the script. - Nix ensures the script exists. - The 126 error code check and retry is leftover from an old version. --- .../command-not-found/command-not-found.nix | 34 ++----------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/nixos/modules/programs/command-not-found/command-not-found.nix b/nixos/modules/programs/command-not-found/command-not-found.nix index a102c43cd57b..65ed7366b8a3 100644 --- a/nixos/modules/programs/command-not-found/command-not-found.nix +++ b/nixos/modules/programs/command-not-found/command-not-found.nix @@ -54,44 +54,14 @@ in config = lib.mkIf cfg.enable { programs.bash.interactiveShellInit = '' - # This function is called whenever a command is not found. command_not_found_handle() { - local p='${commandNotFound}/bin/command-not-found' - if [ -x "$p" ] && [ -f '${cfg.dbPath}' ]; then - # Run the helper program. - "$p" "$@" - # Retry the command if we just installed it. - if [ $? = 126 ]; then - "$@" - else - return 127 - fi - else - echo "$1: command not found" >&2 - return 127 - fi + '${commandNotFound}/bin/command-not-found' "$@" } ''; programs.zsh.interactiveShellInit = '' - # This function is called whenever a command is not found. command_not_found_handler() { - local p='${commandNotFound}/bin/command-not-found' - if [ -x "$p" ] && [ -f '${cfg.dbPath}' ]; then - # Run the helper program. - "$p" "$@" - - # Retry the command if we just installed it. - if [ $? = 126 ]; then - "$@" - else - return 127 - fi - else - # Indicate than there was an error so ZSH falls back to its default handler - echo "$1: command not found" >&2 - return 127 - fi + '${commandNotFound}/bin/command-not-found' "$@" } ''; From ce0a4828723617b23f7fc2973a9b2e53a33904a5 Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Fri, 13 Jun 2025 23:32:03 +0700 Subject: [PATCH 4/7] command-not-found: Add explicit fish shell integration Fish completely separate of nixos will look for command-not-found provided by nixos, this was surprising so lets make it explicit. --- .../programs/command-not-found/command-not-found.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/nixos/modules/programs/command-not-found/command-not-found.nix b/nixos/modules/programs/command-not-found/command-not-found.nix index 65ed7366b8a3..ec2927805d59 100644 --- a/nixos/modules/programs/command-not-found/command-not-found.nix +++ b/nixos/modules/programs/command-not-found/command-not-found.nix @@ -65,6 +65,13 @@ in } ''; + # NOTE: Fish by itself checks for nixos command-not-found, let's instead makes it explicit. + programs.fish.interactiveShellInit = '' + function fish_command_not_found + "${commandNotFound}/bin/command-not-found" $argv + end + ''; + environment.systemPackages = [ commandNotFound ]; }; From 0309303c7ee90c076f7db18ad4fe63abf690de49 Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Fri, 13 Jun 2025 23:32:46 +0700 Subject: [PATCH 5/7] command-not-found: Improve documentation --- .../programs/command-not-found/command-not-found.nix | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nixos/modules/programs/command-not-found/command-not-found.nix b/nixos/modules/programs/command-not-found/command-not-found.nix index ec2927805d59..40ee958a0a49 100644 --- a/nixos/modules/programs/command-not-found/command-not-found.nix +++ b/nixos/modules/programs/command-not-found/command-not-found.nix @@ -37,6 +37,12 @@ in description = '' Whether interactive shells should show which Nix package (if any) provides a missing command. + + Requires nix-channels to be set and downloaded (sudo nix-channels --update.) + + See also nix-index and nix-index-database as an alternative for flakes-based systems. + + Additionally, having the env var NIX_AUTO_RUN set will automatically run the matching package, and with NIX_AUTO_RUN_INTERACTIVE it will confirm the package before running. ''; }; From 9f9bd4f36f5c22b92f1947eb687602930fe97745 Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Sun, 15 Jun 2025 16:00:05 +0700 Subject: [PATCH 6/7] release-notes: Mention disable-by-default of command-not-found --- doc/release-notes/rl-2511.section.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/release-notes/rl-2511.section.md b/doc/release-notes/rl-2511.section.md index d87d4348d8fb..32d77abf746e 100644 --- a/doc/release-notes/rl-2511.section.md +++ b/doc/release-notes/rl-2511.section.md @@ -18,6 +18,7 @@ - `base16-builder` node package has been removed due to lack of upstream maintenance. - `gentium` package now provides `Gentium-*.ttf` files, and not `GentiumPlus-*.ttf` files like before. The font identifiers `Gentium Plus*` are available in the `gentium-plus` package, and if you want to use the more recently updated package `gentium` [by sil](https://software.sil.org/gentium/), you should update your configuration files to use the `Gentium` font identifier. - `space-orbit` package has been removed due to lack of upstream maintenance. Debian upstream stopped tracking it in 2011. +- `command-not-found` package is now disabled by default; it works only for nix-channels based systems, and requires setup for it to work. ## Other Notable Changes {#sec-nixpkgs-release-25.11-notable-changes} From 0e144f8f2723dccaa646d256a018ab8d8515f39b Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Tue, 17 Jun 2025 11:11:39 +0700 Subject: [PATCH 7/7] command-not-found: Add more context to error msg --- .../modules/programs/command-not-found/command-not-found.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nixos/modules/programs/command-not-found/command-not-found.pl b/nixos/modules/programs/command-not-found/command-not-found.pl index 4a6b7fa22381..64f0f1a49877 100644 --- a/nixos/modules/programs/command-not-found/command-not-found.pl +++ b/nixos/modules/programs/command-not-found/command-not-found.pl @@ -14,8 +14,9 @@ if (! -e $dbPath) { print STDERR "$program: command not found\n"; print STDERR "\n"; print STDERR "command-not-found: Missing package database\n"; - print STDERR "This likely means the database hasn't been generated yet.\n"; - print STDERR "This tool requires nix-channels to generate the database.\n"; + print STDERR "command-not-found is a tool for searching for missing packages.\n"; + print STDERR "No database was found, this likely means the database hasn't been generated yet.\n"; + print STDERR "This tool requires nix-channels to generate the database for the `nixos` channel.\n"; print STDERR "\n"; print STDERR "If you are using nix-channels you can run:\n"; print STDERR " sudo nix-channels --update\n";