1source $stdenv/setup
2
3# When no modules are built, the $out/lib/modules directory will not
4# exist. Because the rest of the script assumes it does exist, we
5# handle this special case first.
6if ! test -d "$kernel/lib/modules"; then
7 if test -z "$rootModules" || test -n "$allowMissing"; then
8 mkdir -p "$out"
9 exit 0
10 else
11 echo "Required modules: $rootModules"
12 echo "Can not derive a closure of kernel modules because no modules were provided."
13 exit 1
14 fi
15fi
16
17version=$(cd $kernel/lib/modules && ls -d *)
18
19echo "kernel version is $version"
20
21# Determine the dependencies of each root module.
22mkdir -p $out/lib/modules/"$version"
23touch closure
24for module in $rootModules; do
25 echo "root module: $module"
26 modprobe --config no-config -d $kernel --set-version "$version" --show-depends "$module" \
27 | while read cmd module args; do
28 case "$cmd" in
29 builtin)
30 touch found
31 echo "$module" >>closure
32 echo " builtin dependency: $module";;
33 insmod)
34 touch found
35 if ! test -e "$module"; then
36 echo " dependency not found: $module"
37 exit 1
38 fi
39 target=$(echo "$module" | sed "s^$NIX_STORE.*/lib/modules/^$out/lib/modules/^")
40 if test -e "$target"; then
41 echo " dependency already copied: $module"
42 continue
43 fi
44 echo "$module" >>closure
45 echo " copying dependency: $module"
46 mkdir -p $(dirname $target)
47 cp "$module" "$target"
48 # If the kernel is compiled with coverage instrumentation, it
49 # contains the paths of the *.gcda coverage data output files
50 # (which it doesn't actually use...). Get rid of them to prevent
51 # the whole kernel from being included in the initrd.
52 nuke-refs "$target"
53 echo "$target" >> $out/insmod-list;;
54 *)
55 echo " unexpected modprobe output: $cmd $module"
56 exit 1;;
57 esac
58 done || test -n "$allowMissing"
59 if ! test -e found; then
60 echo " not found"
61 if test -z "$allowMissing"; then
62 exit 1
63 fi
64 else
65 rm found
66 fi
67done
68
69mkdir -p $out/lib/firmware
70for module in $(cat closure); do
71 # for builtin modules, modinfo will reply with a wrong output looking like:
72 # $ modinfo -F firmware unix
73 # name: unix
74 #
75 # There is a pending attempt to fix this:
76 # https://github.com/NixOS/nixpkgs/pull/96153
77 # https://lore.kernel.org/linux-modules/20200823215433.j5gc5rnsmahpf43v@blumerang/T/#u
78 #
79 # For now, the workaround is just to filter out the extraneous lines out
80 # of its output.
81 for i in $(modinfo -b $kernel --set-version "$version" -F firmware $module | grep -v '^name:'); do
82 mkdir -p "$out/lib/firmware/$(dirname "$i")"
83 echo "firmware for $module: $i"
84 for name in "$i" "$i.xz" ""; do
85 [ -z "$name" ] && echo "WARNING: missing firmware $i for module $module"
86 if cp "$firmware/lib/firmware/$name" "$out/lib/firmware/$name" 2>/dev/null; then
87 break
88 fi
89 done
90 done
91done
92
93# copy module ordering hints for depmod
94cp $kernel/lib/modules/"$version"/modules.order $out/lib/modules/"$version"/.
95cp $kernel/lib/modules/"$version"/modules.builtin $out/lib/modules/"$version"/.
96
97depmod -b $out -a $version
98
99# remove original hints from final derivation
100rm $out/lib/modules/"$version"/modules.order
101rm $out/lib/modules/"$version"/modules.builtin