vuls: init at 0.27.0 (#348530)
[NixPkgs.git] / maintainers / scripts / nixpkgs-lint.pl
blob43fb39413613947105605e460922cd7225dd30c3
1 #! /usr/bin/env nix-shell
2 #! nix-shell -i perl -p perl perlPackages.XMLSimple
4 use strict;
5 use List::Util qw(min);
6 use XML::Simple qw(:strict);
7 use Getopt::Long qw(:config gnu_getopt);
9 # Parse the command line.
10 my $path = "<nixpkgs>";
11 my $filter = "*";
12 my $maintainer;
14 sub showHelp {
15 print <<EOF;
16 Usage: $0 [--package=NAME] [--maintainer=REGEXP] [--file=PATH]
18 Check Nixpkgs for common errors/problems.
20 -p, --package filter packages by name (default is ‘*’)
21 -m, --maintainer filter packages by maintainer (case-insensitive regexp)
22 -f, --file path to Nixpkgs (default is ‘<nixpkgs>’)
24 Examples:
25 \$ nixpkgs-lint -f /my/nixpkgs -p firefox
26 \$ nixpkgs-lint -f /my/nixpkgs -m eelco
27 EOF
28 exit 0;
31 GetOptions("package|p=s" => \$filter,
32 "maintainer|m=s" => \$maintainer,
33 "file|f=s" => \$path,
34 "help" => sub { showHelp() }
35 ) or exit 1;
37 # Evaluate Nixpkgs into an XML representation.
38 my $xml = `nix-env -f '$path' --arg overlays '[]' -qa '$filter' --xml --meta --drv-path`;
39 die "$0: evaluation of ‘$path’ failed\n" if $? != 0;
41 my $info = XMLin($xml, KeyAttr => { 'item' => '+attrPath', 'meta' => 'name' }, ForceArray => 1, SuppressEmpty => '' ) or die "cannot parse XML output";
43 # Check meta information.
44 print "=== Package meta information ===\n\n";
45 my $nrBadNames = 0;
46 my $nrMissingMaintainers = 0;
47 my $nrMissingPlatforms = 0;
48 my $nrMissingDescriptions = 0;
49 my $nrBadDescriptions = 0;
50 my $nrMissingLicenses = 0;
52 foreach my $attr (sort keys %{$info->{item}}) {
53 my $pkg = $info->{item}->{$attr};
55 my $pkgName = $pkg->{name};
56 my $pkgVersion = "";
57 if ($pkgName =~ /(.*)(-[0-9].*)$/) {
58 $pkgName = $1;
59 $pkgVersion = $2;
62 # Check the maintainers.
63 my @maintainers;
64 my $x = $pkg->{meta}->{maintainers};
65 if (defined $x && $x->{type} eq "strings") {
66 @maintainers = map { $_->{value} } @{$x->{string}};
67 } elsif (defined $x->{value}) {
68 @maintainers = ($x->{value});
71 if (defined $maintainer && scalar(grep { $_ =~ /$maintainer/i } @maintainers) == 0) {
72 delete $info->{item}->{$attr};
73 next;
76 if (scalar @maintainers == 0) {
77 print "$attr: Lacks a maintainer\n";
78 $nrMissingMaintainers++;
81 # Check the platforms.
82 if (!defined $pkg->{meta}->{platforms}) {
83 print "$attr: Lacks a platform\n";
84 $nrMissingPlatforms++;
87 # Package names should not be capitalised.
88 if ($pkgName =~ /^[A-Z]/) {
89 print "$attr: package name ‘$pkgName’ should not be capitalised\n";
90 $nrBadNames++;
93 if ($pkgVersion eq "") {
94 print "$attr: package has no version\n";
95 $nrBadNames++;
98 # Check the license.
99 if (!defined $pkg->{meta}->{license}) {
100 print "$attr: Lacks a license\n";
101 $nrMissingLicenses++;
104 # Check the description.
105 my $description = $pkg->{meta}->{description}->{value};
106 if (!$description) {
107 print "$attr: Lacks a description\n";
108 $nrMissingDescriptions++;
109 } else {
110 my $bad = 0;
111 if ($description =~ /^\s/) {
112 print "$attr: Description starts with whitespace\n";
113 $bad = 1;
115 if ($description =~ /\s$/) {
116 print "$attr: Description ends with whitespace\n";
117 $bad = 1;
119 if ($description =~ /\.$/) {
120 print "$attr: Description ends with a period\n";
121 $bad = 1;
123 if (index(lc($description), lc($attr)) != -1) {
124 print "$attr: Description contains package name\n";
125 $bad = 1;
127 $nrBadDescriptions++ if $bad;
131 print "\n";
133 # Find packages that have the same name.
134 print "=== Package name collisions ===\n\n";
136 my %pkgsByName;
138 foreach my $attr (sort keys %{$info->{item}}) {
139 my $pkg = $info->{item}->{$attr};
140 #print STDERR "attr = $attr, name = $pkg->{name}\n";
141 $pkgsByName{$pkg->{name}} //= [];
142 push @{$pkgsByName{$pkg->{name}}}, $pkg;
145 my $nrCollisions = 0;
146 foreach my $name (sort keys %pkgsByName) {
147 my @pkgs = @{$pkgsByName{$name}};
149 # Filter attributes that are aliases of each other (e.g. yield the
150 # same derivation path).
151 my %drvsSeen;
152 @pkgs = grep { my $x = $drvsSeen{$_->{drvPath}}; $drvsSeen{$_->{drvPath}} = 1; !defined $x } @pkgs;
154 # Filter packages that have a lower priority.
155 my $highest = min (map { $_->{meta}->{priority}->{value} // 0 } @pkgs);
156 @pkgs = grep { ($_->{meta}->{priority}->{value} // 0) == $highest } @pkgs;
158 next if scalar @pkgs == 1;
160 $nrCollisions++;
161 print "The following attributes evaluate to a package named ‘$name’:\n";
162 print " ", join(", ", map { $_->{attrPath} } @pkgs), "\n\n";
165 print "=== Bottom line ===\n";
166 print "Number of packages: ", scalar(keys %{$info->{item}}), "\n";
167 print "Number of bad names: $nrBadNames\n";
168 print "Number of missing maintainers: $nrMissingMaintainers\n";
169 print "Number of missing platforms: $nrMissingPlatforms\n";
170 print "Number of missing licenses: $nrMissingLicenses\n";
171 print "Number of missing descriptions: $nrMissingDescriptions\n";
172 print "Number of bad descriptions: $nrBadDescriptions\n";
173 print "Number of name collisions: $nrCollisions\n";