-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
180 lines (164 loc) · 5.72 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
# Prevent duplicated reports.
config.allowAliases = false;
overlays = [
(final: prev: {
# Ensure we are using the latest version of Go, or we will get
# many findings of vulnerable stdlib packages.
# go_1_22 = prev.go_1_22.overrideAttrs (finalAttrs: _prevAttrs: {
# version = "1.22.3";
# src = final.fetchurl {
# url = "https://go.dev/dl/go${finalAttrs.version}.src.tar.gz";
# hash = "sha256-gGSO80+QMZPXKlnA3/AZ9fmK4MmqE63gsOy/+ZGnb2g=";
# };
# });
})
];
};
lib = pkgs.lib;
# The Go vulnerability database.
# Version is based on the modified field of index/db.json in the archive.
govulndb = pkgs.buildGoModule {
pname = "govuln";
version = "0-unstable-2025-01-29";
src = pkgs.fetchFromGitHub {
owner = "golang";
repo = "vulndb";
rev = "2db00b4cd84ec07683fce47c4fa55aaaf9fe0520";
leaveDotGit = true;
deepClone = true;
hash = "sha256-DRSEA7ZmmUSVWYZ0fxO134nW2IorB4g7WrK5OinPxvo=";
};
vendorHash = "sha256-u2h0zqZvbXFp+CxzZdeRn6ZNZGl1PwMzyqlZgVla0gk=";
subPackages = [ "cmd/gendb" ];
installPhase = ''
go run ./cmd/gendb -out $out
'';
};
# Helper script to govulncheck a module against the downloaded database.
govulncheck-script = pkgs.writeShellApplication {
name = "govulncheck-script";
runtimeInputs = with pkgs; [
govulncheck
go
];
text = ''govulncheck -db file://${govulndb} -C "$@" ./...'';
};
# Filter for Go packages based on some well known attributes buildGoModule will add.
# This is not precise and likely flawed. It doesn't handle nested package sets correctly.
# Number of packages found with this is close enough to the number of findings grepping
# nixpkgs for "buildGoModule", so it's good enough for now.
isGoPkg =
name: pkg:
(
(builtins.tryEval pkg).success
&& lib.isAttrs pkg
&& lib.hasAttr "src" pkg
&& lib.hasAttr "go" pkg
&& lib.hasAttr "goModules" pkg
);
# Construct a file mapping package name to src path.
goPkgs = lib.filterAttrs isGoPkg pkgs;
goPkgsSrcs = lib.mapAttrsToList (
name: pkg:
(lib.concatStringsSep " " [
name
pkg.src
])
) goPkgs;
goPkgsSrcsFile = pkgs.writeText "goPkgsList" (lib.concatStringsSep "\n" goPkgsSrcs);
# Iterate over the list of Go package path srcs and run govulncheck on them.
# Run as 'nix run .#govulncheck-srcs |& tee report.txt'
govulncheck-srcs = pkgs.writeShellApplication {
name = "govulncheck-srcs";
runtimeInputs = with pkgs; [
govulncheck
go
];
text = ''
# Read Go packages from file, line by line, format is "name src"
while IFS= read -r line; do
name=$(echo "$line" | cut -d ' ' -f 1)
src=$(echo "$line" | cut -d ' ' -f 2)
echo "Checking nixpkg $name"
govulncheck -db file://${govulndb} -C "$src" ./... 2>&1 || true
done < ${goPkgsSrcsFile}
'';
};
# Some bash to get something useful out of the govulncheck-srcs report.
report-tool = pkgs.writeShellApplication {
name = "report-tool";
runtimeInputs = with pkgs; [
curl
gawk
gnugrep
gnused
jq
ripgrep
];
text = builtins.readFile ./report-tool.sh;
};
# This tries to count the total number of module dependencies in nixpkgs.
# Main goal here is to get an expectation for the goPackages size of gobuild.nix.
go-modules-total =
pkgs.runCommand "go-count-modules-total"
{
buildInputs = with pkgs; [ go ];
}
''
mkdir -p $out
tmpfile=$(mktemp)
while IFS= read -r line; do
src=$(echo "$line" | cut -d ' ' -f 2)
cat $src/go.mod >> $tmpfile || echo "$src" >> $out/not-included.txt
done < ${goPkgsSrcsFile}
cat $tmpfile |
sed -E 's/^[[:space:]]+//;s/[[:space:]]+$//' |
sed -E 's/[[:space:]]*\/\/ indirect$//' |
grep -v '^go' |
grep -v '^//' |
grep -v '^module' |
grep -v '^require (' |
grep -v '^replace' |
grep -v '^exclude' |
grep -v '^)$' |
grep -v '^retract' |
grep -v '^toolchain' |
grep -v '^v[0-9]\+\.[0-9]\+\.[0-9]\+' |
cut -d' ' -f1 |
sort -u > $out/modules.txt
'';
in
{
packages = {
inherit
govulncheck-script
govulncheck-srcs
govulndb
go-modules-total
report-tool
;
};
devShells = {
default = pkgs.mkShell {
packages = [ report-tool ];
};
};
}
);
}