1#! @perl@/bin/perl -w @perlFlags@
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_INSTALL"} // "") {
29 print STDERR <<EOF;
30The program ‘$program’ is currently not installed. It is provided by
31the package ‘$package’, which I will now install for you.
32EOF
33 ;
34 exit 126 if system("nix-env", "-iA", "nixos.$package") == 0;
35 } elsif ($ENV{"NIX_AUTO_RUN"} // "") {
36 exec("nix-shell", "-p", $package, "--run", shell_quote("exec", @ARGV));
37 } else {
38 print STDERR <<EOF;
39The program ‘$program’ is currently not installed. You can install it by typing:
40 nix-env -iA nixos.$package
41EOF
42 }
43} else {
44 print STDERR <<EOF;
45The program ‘$program’ is currently not installed. It is provided by
46several packages. You can install it by typing one of the following:
47EOF
48 print STDERR " nix-env -iA nixos.$_->{package}\n" foreach @$res;
49}
50
51exit 127;