apt-src is an apt tool that can be used to download, build, and install debian packages from debianized source code. Reasons for building your own package could include changing configure options or trying to optimize certain packages for your architecture.
Note: Using apt-src to build your entire system is not recommended. For a task like that, you're better off with apt-build.
First step (after installation of apt-src) is to make sure you have the appropriate deb-src lines in your sources.list.
deb http://ftp.debian.org/debian main deb-src http://ftp.debian.org/debian main
The next step is to configure the default build options for dpkg-buildpackage. This is done /etc/apt/apt.conf.d/02dpkg-buildpackage
APT::Src::BuildCommand "dpkg-buildpackage <options go here>";
Downloading, Building, and Installing
First you need update, and then download the desired package's source.
#apt-src update #apt-src install <package>
This will download the vanilla source code (tarballed),the .dsc, a .changes file, and the unpacked debian source tree.
Now you should modify the debian/rules in the unpacked source tree to set the desired configuration options. You'll also want to set any CFLAGS that you need for gcc. Once you've done that, you can build the package,
#apt-src build <package>
That will build your source package, and leave the .deb in the same directory as the files apt-src downloaded. Simply use dpkg to install the package.
#dpkg --install <path to compiled .deb>
Now if you really don't wanna modify the source tree, or modify the debian/rules file, you can download and build the source in one command.
#apt-src --build install <package>
Ensuring your changes persist
After compiling, aptitude will probably try and upgrade your custom package with the standard Debian ones, obliterating your changes! Two ways to maintain the same package version are creating a diversion with dpkg-divert or by having aptitude hold the package. Just note though, that dpkg will respect your diversion when you try and install your newest compiled version, so you might want to remove the diversion before installing a new version of your custom compiled package.