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
13my $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "")
14 or die "cannot open database `$dbPath'";
15$dbh->{RaiseError} = 0;
16$dbh->{PrintError} = 0;
17
18my $system = $ENV{"NIX_SYSTEM"} // $Config{myarchname};
19
20my $res = $dbh->selectall_arrayref(
21 "select package from Programs where system = ? and name = ?",
22 { Slice => {} }, $system, $program);
23
24if (!defined $res || scalar @$res == 0) {
25 print STDERR "$program: command not found\n";
26} elsif (scalar @$res == 1) {
27 my $package = @$res[0]->{package};
28 if ($ENV{"NIX_AUTO_RUN"} // "") {
29 exec("nix-shell", "-p", $package, "--run", shell_quote("exec", @ARGV));
30 } else {
31 print STDERR <<EOF;
32The program '$program' is not in your PATH. You can make it available in an
33ephemeral shell by typing:
34 nix-shell -p $package
35EOF
36 }
37} else {
38 print STDERR <<EOF;
39The program '$program' is not in your PATH. It is provided by several packages.
40You can make it available in an ephemeral shell by typing one of the following:
41EOF
42 print STDERR " nix-shell -p $_->{package}\n" foreach @$res;
43}
44
45exit 127;