Download the kernel source, once unpacked you can configure it to build only the modules you have currently loaded via "make localmodconfig".
Of course if you've never built a kernel before you'll need to read the documentation, and you'll probably want to look at the Ubuntu documentation too - since you'll want to build a .deb package, rather than a raw kernel. You'll want to search for "ubuntu make-kpkg".
But really you're not going to gain much, except learning. Sure a kernel with less stuff available might save disk space, but there are easier ways to save disk-space on Ubuntu systems..
I'm looking to improve the security posture by stripping the kernel off unwanted stuff (ebpf, zillion different file systems, drivers) that introduce CVEs into the kernel.
There are probably better approaches; for example you could disable loading modules for code you don't use - without the need to rebuild the whole thing.
I guess it comes down to understanding your threat model.
Of course if you've never built a kernel before you'll need to read the documentation, and you'll probably want to look at the Ubuntu documentation too - since you'll want to build a .deb package, rather than a raw kernel. You'll want to search for "ubuntu make-kpkg".
But really you're not going to gain much, except learning. Sure a kernel with less stuff available might save disk space, but there are easier ways to save disk-space on Ubuntu systems..