Skip to content

command-not-found: disable by default, simplify, document - #416425

Merged
Mic92 merged 7 commits into
NixOS:masterfrom
jessie-ross:disable-command-not-found-by-default
Jun 17, 2025
Merged

Mic92 merged 7 commits into
NixOS:masterfrom
jessie-ross:disable-command-not-found-by-default

Conversation

@jessie-ross

@jessie-ross jessie-ross commented Jun 13, 2025 •

Copy link
Copy Markdown
Contributor

(From my comment here.)

Hi all, I'm new to nix, this is my first PR, please let me know if it is too controversial a change, or if I'm missing anything.

This PR simplifies command-not-found, adds documentation and disables it by default as it is currently failing silently and depends on nix-channels that won't be in use for many users, and requires undocumented management and initialisation for other users.

Context:
command-not-found is most important for new users (so they can find packages,) but for new users (using a flakes setup) it will be enabled and broken, at best failing silently and they won't know about its existence, at worst creating annoying and confusing error messages.

There are some rewrites and alternate approaches, the most recommended one is programs.nix-index which provides its own command-not-found (but is slow to generate the index, and requires running a command to explicitly generate that index.)

This is a 5-10 year old issue, is broken for new users using flakes, users using custom setups, and is broken for users using nix-channels without having run sudo nix-channels --update. It will also produce very old results for users that have switched to a flakes-based system, and will suddenly break for other users who have changed their channels.

The ultimate issue is command-not-found does not provide or manage its package database.

Problems:

  • Noisy errors in Fish which will automatically use this script without a wrapper.
  • Silent failure in bash and zsh.
  • Won't work for a flakes setup, it depends entirely on nix-channels.
  • Requires initial setup that isn't documented and is confusing - requires a nix-channel explicitly named nixos, won't check other channels.

See:

Changes:

  • BREAKING: Disables programs.command-not-found by default. There are a number of other alternatives including nix-index, for auto-run you can use comma.
  • Removed the "rerun" mechanism from the wrapper, the script will not install the command but rather run it if NIX_AUTO_RUN is set, so this seems to left over from some legacy behaviour.
  • Removes the script existence check from the wrapper script - in a nix system the script should definitely exist.
  • Moves the database check from the wrapper script into the main script.
  • The wrappers are now only simple passthroughs.
  • Adds an error message that explains how to initialize the database.
  • Docs: Requirement for nix-channels usage.
  • Docs: NIX_AUTO_RUN feature.

Related PRs:

Manual testing

Missing database

# bash
[j@jrpi:~]$ asdf
asdf: command not found

command-not-found: Missing package database
This likely means the database hasn't been generated yet.
This tool requires nix-channels to generate the database.

If you are using nix-channels you can run:
    sudo nix-channels --update

If you are using flakes, see nix-index and nix-index-database.

If you would like to disable this message you can set:
    programs.command-not-found.enable = false;

# zsh
j@jrpi:~/ > asdf
asdf: command not found

command-not-found: Missing package database
This likely means the database hasn't been generated yet.
This tool requires nix-channels to generate the database.

If you are using nix-channels you can run:
    sudo nix-channels --update

If you are using flakes, see nix-index and nix-index-database.

If you would like to disable this message you can set:
    programs.command-not-found.enable = false;
# fish
j@jrpi ~ [127]> asdf
asdf: command not found

command-not-found: Missing package database
This likely means the database hasn't been generated yet.
This tool requires nix-channels to generate the database.

If you are using nix-channels you can run:
    sudo nix-channels --update

If you are using flakes, see nix-index and nix-index-database.

If you would like to disable this message you can set:
    programs.command-not-found.enable = false;

With database

j@jrpi ~> sudo nix-channel --add https://nixos.org/channels/nixos-25.05 nixos
j@jrpi ~> sudo nix-channel --update
# bash
[j@jrpi:~]$ asdf
The program 'asdf' is not in your PATH. You can make it available in an
ephemeral shell by typing:
  nix-shell -p asdf-vm

# zsh
j@jrpi:~/ > asdf
The program 'asdf' is not in your PATH. You can make it available in an
ephemeral shell by typing:
  nix-shell -p asdf-vm
# fish
j@jrpi ~> asdf
The program 'asdf' is not in your PATH. You can make it available in an
ephemeral shell by typing:
  nix-shell -p asdf-vm

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • Nixpkgs 25.11 Release Notes (or backporting 24.11 and 25.05 Nixpkgs Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
  • NixOS 25.11 Release Notes (or backporting 24.11 and 25.05 NixOS Release notes)
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other contributing documentation in corresponding paths.

Add a 👍 reaction to pull requests you find important.

@NixOSInfra NixOSInfra added the 12.first-time contribution This PR is the author's first one; please be gentle! label Jun 13, 2025
Comment on lines 63 to 65

@jessie-ross jessie-ross Jun 13, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

13 year old code that doesn't do anything - today we install and run the command immediately if NIX_AUTO_RUN is set.

@github-actions github-actions Bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Jun 13, 2025
This tool is broken for users that use flakes and not nix-channels, and
fails silently so few would be aware.
@jessie-ross
jessie-ross force-pushed the disable-command-not-found-by-default branch from 71b6c37 to a2d5ceb Compare June 13, 2025 16:36
We must check for the database every time we use the script so lets add
it to the script itself.
- 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.
Fish completely separate of nixos will look for command-not-found
provided by nixos, this was surprising so lets make it explicit.
@jessie-ross
jessie-ross force-pushed the disable-command-not-found-by-default branch from a2d5ceb to 0309303 Compare June 15, 2025 08:57
@github-actions github-actions Bot added the 8.has: documentation This PR adds or changes documentation label Jun 15, 2025
@jessie-ross
jessie-ross marked this pull request as ready for review June 15, 2025 09:09
enable = lib.mkOption {
type = lib.types.bool;
default = true;
default = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatives to disabling for all cases:

  • Detect use of flakes and disable if flakes are used.
  • Only disable by default for systemState >= 25.11.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is config.nixpkgs.source != null which could be used as a default.

Though, there is no guarantee that this will catch all systems using a flake. nixpkgs.flake.source is only set by default if nixpkgs.lib.nixosSystem is used to construct the nixosConfiguration. But this should be the case for most flakes users.

Still worthwhile considering I guess.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only disable by default for systemState >= 25.11

I think this is not needed. It is not breaking anything super important.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, there is no guarantee that this will catch all systems using a flake.

IMO the consistency of just disabling is better, otherwise there is a more complicated bug for those flake systems where this falls through.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense

@DavHau DavHau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

enable = lib.mkOption {
type = lib.types.bool;
default = true;
default = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is config.nixpkgs.source != null which could be used as a default.

Though, there is no guarantee that this will catch all systems using a flake. nixpkgs.flake.source is only set by default if nixpkgs.lib.nixosSystem is used to construct the nixosConfiguration. But this should be the case for most flakes users.

Still worthwhile considering I guess.

if (! -e $dbPath) {
print STDERR "$program: command not found\n";
print STDERR "\n";
print STDERR "command-not-found: Missing package database\n";

@DavHau DavHau Jun 16, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a user that doesn't know about the package-not-found mechanism, this error lacks context.

Suggested change
print STDERR "command-not-found: Missing package database\n";
print STDERR "command-not-found: A package providing $program can possibly be installed, but a package database is missing to tell for sure.\n";

... or the like would probably be more clear

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've added more context in.

@github-actions github-actions Bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Jun 16, 2025
@DavHau

DavHau commented Jun 17, 2025

Copy link
Copy Markdown
Member

I rebuilt my system with this change and verified that it behaves as expected with all 3 shells.

@Mic92
Mic92 merged commit d9a40c9 into NixOS:master Jun 17, 2025
- `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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have go into the NixOS release notes nixos/doc/manual/release-notes, not the nixpkgs ones. Can you make a follow up change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks for letting me know, I've moved it to the correct place in #432023

sternenseemann added a commit to openlab-aux/vuizvui that referenced this pull request Aug 2, 2025
Given that we include programs.sqlite by default we may as well revert
this [upstream change][]. Users can still disable it if they want to.

[upstream change]: NixOS/nixpkgs#416425
@jessie-ross
jessie-ross deleted the disable-command-not-found-by-default branch August 8, 2025 14:26
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/6551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person. 12.first-time contribution This PR is the author's first one; please be gentle!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants