How to statically compile a program and control the compilation parameters and preserve dependency binaries

I want to compile the program statically, and control the compilation parameters, and keep the binary file of the program and the binary file of the program dependencies.

If possible, it is better to keep the source code.

How should I do this? If it’s too complicated, it’s okay to tell me the direction or the method to use.

I’m a college student from Japan and I’m doing some experiments. Thanks to the community friends.

I would just write a derivation normally (see eg my tutorial here packaging - How to package my software in nix or write my own package derivation for nixpkgs - Unix & Linux Stack Exchange), and replace pkgs with pkgs.pkgsStatic, it should work directly.