1 #! /usr/bin/env nix-shell
2 #! nix-shell -i perl -p perl perlPackages.XMLSimple
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>";
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>’)
25 \$ nixpkgs-lint -f /my/nixpkgs -p firefox
26 \$ nixpkgs-lint -f /my/nixpkgs -m eelco
31 GetOptions
("package|p=s" => \
$filter,
32 "maintainer|m=s" => \
$maintainer,
34 "help" => sub { showHelp
() }
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";
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
};
57 if ($pkgName =~ /(.*)(-[0-9].*)$/) {
62 # Check the 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};
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";
93 if ($pkgVersion eq "") {
94 print "$attr: package has no version\n";
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
};
107 print "$attr: Lacks a description\n";
108 $nrMissingDescriptions++;
111 if ($description =~ /^\s/) {
112 print "$attr: Description starts with whitespace\n";
115 if ($description =~ /\s$/) {
116 print "$attr: Description ends with whitespace\n";
119 if ($description =~ /\.$/) {
120 print "$attr: Description ends with a period\n";
123 if (index(lc($description), lc($attr)) != -1) {
124 print "$attr: Description contains package name\n";
127 $nrBadDescriptions++ if $bad;
133 # Find packages that have the same name.
134 print "=== Package name collisions ===\n\n";
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).
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;
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";