1#! @perl@/bin/perl -w
2
3use strict;
4use DBI;
5use DBD::SQLite;
6use String::ShellQuote;
7use Config;
8
9my $program = $ARGV[0];
10
11my $dbPath = "@dbPath@";
12
13if (! -e $dbPath) {
14 print STDERR "$program: command not found\n";
15 print STDERR "\n";
16 print STDERR "command-not-found: Missing package database\n";
17 print STDERR "command-not-found is a tool for searching for missing packages.\n";
18 print STDERR "No database was found, this likely means the database hasn't been generated yet.\n";
19 print STDERR "This tool requires nix-channels to generate the database for the `nixos` channel.\n";
20 print STDERR "\n";
21 print STDERR "If you are using nix-channels you can run:\n";
22 print STDERR " sudo nix-channels --update\n";
23 print STDERR "\n";
24 print STDERR "If you are using flakes, see nix-index and nix-index-database.\n";
25 print STDERR "\n";
26 print STDERR "If you would like to disable this message you can set:\n";
27 print STDERR " programs.command-not-found.enable = false;\n";
28 exit 127;
29}
30
31my $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "")
32 or die "cannot open database `$dbPath'";
33$dbh->{RaiseError} = 0;
34$dbh->{PrintError} = 0;
35
36my $system = $ENV{"NIX_SYSTEM"} // $Config{myarchname};
37
38my $res = $dbh->selectall_arrayref(
39 "select package from Programs where system = ? and name = ?",
40 { Slice => {} }, $system, $program);
41
42my $len = !defined $res ? 0 : scalar @$res;
43
44if ($len == 0) {
45 print STDERR "$program: command not found\n";
46} elsif ($len == 1) {
47 my $package = @$res[0]->{package};
48 if ($ENV{"NIX_AUTO_RUN"} // "") {
49 if ($ENV{"NIX_AUTO_RUN_INTERACTIVE"} // "") {
50 while (1) {
51 print STDERR "'$program' from package '$package' will be run, confirm? [yn]: ";
52 chomp(my $comfirm = <STDIN>);
53 if (lc $comfirm eq "n") {
54 exit 0;
55 } elsif (lc $comfirm eq "y") {
56 last;
57 }
58 }
59 }
60 exec("nix-shell", "-p", $package, "--run", shell_quote("exec", @ARGV));
61 } else {
62 print STDERR <<EOF;
63The program '$program' is not in your PATH. You can make it available in an
64ephemeral shell by typing:
65 nix-shell -p $package
66EOF
67 }
68} else {
69 if ($ENV{"NIX_AUTO_RUN"} // "") {
70 print STDERR "Select a package that provides '$program':\n";
71 for my $i (0 .. $len - 1) {
72 print STDERR " [", $i + 1, "]: @$res[$i]->{package}\n";
73 }
74 my $choice = 0;
75 while (1) { # exec will break this loop
76 no warnings "numeric";
77 print STDERR "Your choice [1-${len}]: ";
78 # 0 can be invalid user input like non-number string
79 # so we start from 1
80 $choice = <STDIN> + 0;
81 if (1 <= $choice && $choice <= $len) {
82 exec("nix-shell", "-p", @$res[$choice - 1]->{package},
83 "--run", shell_quote("exec", @ARGV));
84 }
85 }
86 } else {
87 print STDERR <<EOF;
88The program '$program' is not in your PATH. It is provided by several packages.
89You can make it available in an ephemeral shell by typing one of the following:
90EOF
91 print STDERR " nix-shell -p $_->{package}\n" foreach @$res;
92 }
93}
94
95exit 127;