How to specify `programs.sqlite` for `command-not-found` from flakes?

I’ve got rid of all nix channels and just use flakes for everything. However, command-not-found now complains it can not find programs.sqlite because that file is expected in default root channel. How do I specify that that for command-not-found? (preferrably from home-manager option programs.command-not-found.dbPath)

I’m not aware of a way to pull that database in with flakes.

I therefore still maintain an irregularly updated nixos channel for the root user.

You can either replace the current command-not-found module with the one provided by nix-index (it provides both a module for nixos and home manager, thanks K900).

The second approach I can imagine is to manually download the database from https://channels.nixos.org (code not tested)

{ pgks, ... }:
let
  # To find the channel url I went in:
  # https://channels.nixos.org/ > nixos-22.05 > nixexprs.tar.gz
  # All the available channels you can browse https://releases.nixos.org
  nixos_tarball = pkgs.fetchzip {
    url = "https://releases.nixos.org/nixos/22.05/nixos-22.05.3737.3933d8bb912/nixexprs.tar.xz";
    sha256 = "sha256-+xhJb0vxEAnF3hJ6BZ1cbndYKZwlqYJR/PWeJ3aVU8k=";}
  ;
  # extract only the sqlite file
  programs-sqlite = pkgs.runCommand "program.sqlite" {} ''
    cp ${nixos_tarball}/programs.sqlite $out
  '';
in
{
  programs.command-not-found.dbPath = programs-sqlite;
}
1 Like

Thank you all, especially @tobiasBora for spending the time to write the code, really appreciated.

Can you elaborate on the nix-index? I’ve seen the nix-index option in home manager, but when I enable it I face the same complaints (missing programs.sqlite), and I’m not sure how the first approach would work.

No problems. The readme provides a code snippet to use in your configuration.nix here GitHub - nix-community/nix-index: Quickly locate nix packages with specific files [maintainers=@bennofs @figsoda @raitobezarius]

programs.command-not-found.enable = false;
programs.bash.interactiveShellInit = ''
      source ${pkgs.nix-index}/etc/profile.d/command-not-found.sh
    '';

It also links to the home manager module here https://nix-community.github.io/home-manager/options.html#opt-programs.nix-index.enable I don’t have a computer to test but I guess you want to have

programs.nix-index.enable = true;
programs.nix-index.enableBashIntegration = true;

(If you use zsh/fish adapt this code of course)
Also make sure to disable the command-not-found option to avoid conflicts as they are vifferent implementations of the same functionality(it is disabled by default but you may have enabled it). If it fails maybe try to disable system-wide to see if it helps. If you are still out of luck please post your config :wink:

that was what I had in my home-manager configuration as well. However, nix-index doesn’t know where the nixpkgs is either.

I ended up writing a custom script to call it with the right nixpkgs path:

(pkgs.writeScriptBin "nix-index-update-db" "nix-index -f ${pkgs.path}")

and run the script when I update my flake nixpkgs input.

Thank you very much for the pointer.

1 Like

Oh I thought that nix-index would just pick the latest stuff from Hydra irrespective of the current channel. Good to know then!

@NobbZ @myguidingstar @tobiasBora
I spent a bit of spare time on GitHub - wamserma/flake-programs-sqlite: A simple flake to automagically specify programs.sqlite for command-not-found. - maybe you could have a look and test-drive it.

2 Likes

Seems to be a nice idea and thank you for the effort, though:

$ nixos-rebuild build --flake .#mimas
building the system configuration...
warning: Git tree '/home/nmelzer/Projects/nixos-config' is dirty
error: attribute '757b82211463dd5ba1475b6851d3731dfe14d377' missing

And that commit is from 2022-12-16T14:28:48Z (unstable)

If the system can’t sustain even that pace for updates, I wont be able to do any test-drive for you, sorry.

I just realized that you simply might miss the old commit because your started scraping at some later moment in time.

Therefore I updated my local clone to be the same as on GitHub :smiley:

That lockfiles commit actually got recognized by your system and now I am building my system to start testing your flake under real life conditions.

I already found some issue with your README and hope to be able to do a PR later today.

1 Like

Yes, I only kept some older revisions for testing, but proper scraping started less than 24 hours ago.

1 Like

I have made my first tries and some testing and draw a first conclusion:

  • Works well so far, but setup steps are currently lacking documentation (will try to PR today)
  • There should be a module that handles the setup and also goes through a redirection (I hope to be able to do a PR before christmas)
  • I do not really like how overriding the input is part of the contract (I have an idea, but that has to be postponed for now, I don’t have the time nor energy to even verbalise my thoughts before christmas)

I need to change something it seems, as I have posted this in the wrong thread initially and simply reposting seems to be denied by the spam filter or something…

No need to hurry, most like I won’t be available to review the module before next year.

1 Like

Worked like a charm. As opposed to nix-index. Thank you very much!

1 Like

I now really like nix-index-database (no need to worry about anything, the database is downloaded automatically) GitHub - nix-community/nix-index-database: Weekly updated nix-index database [maintainer=@Mic92]. With flake installed:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
  
    nix-index-database.url = "github:nix-community/nix-index-database"; # <--- Add this
    nix-index-database.inputs.nixpkgs.follows = "nixpkgs"; # <--- Add this
  };

  outputs = { self,
             , nixpkgs
             , nix-index-database # <--- Add this
             , ... }: {
    nixosConfigurations = {
      my-nixos = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
          nix-index-database.nixosModules.nix-index # <--- Add this
          { programs.command-not-found.enable = false; } # <--- Add this
        ];
      };
    };
  };
}

Then, it will automatically propose you the name of the program to install if not available, and you can use nix-locate to search which package provides which file.

Btw I recently covered this topic in a Nix Hour :slight_smile: