Viperfang [2E0VFN]

Digital Playground

Dynamic APT Sources list selection

Edit 20/05/2009: Realised I didnt attach a licence to this little project, so let it be the lastest version of the GPL, which at present is v3. 🙂

Before I start this, it should be known I am relatively new to shell scripts, and while this may not be the best method by any means, it does work.

Everything needed is available here as a tarball, it should be placed in /etc/apt/ and unpacked using ‘tar -xvf source-switcher.tar’, running will get you set up. You will have to configure your sources files though!

My problem was this. I have a local repo on a machine in my house, which makes updating and installing a lot quicker that it used to be, let alone being fun to set up. My laptop however, is not always in the house for obvious reasons, and even when it is, it’s not always connected to the network.

For this, I wanted a way of switching between sources.list files with as little user intervention as possible.

I already have a folder called ‘’ which contains a different list for each location, and this is a listing so far:


root@ben-laptop:/etc/apt/ ls -1

So I wrote this script which checks to see what repo is available, and copies the right sources.list and overwriting the one that is already there, and then notifies user 1000 (perfect for single user systems).



export DISPLAY=:0.0
sudo -u \#1000 notify-send -u normal -t 10000 -i /etc/apt/apt.png "APT Source: $1" 'Your sources list has been updated for this network!'
cp "/etc/apt/$1" '/etc/apt/sources.list'

if wget -t 1 -T 1 --spider; then
apply 'Home'
elif wget -t 1 -T 1 --spider; then
apply 'Official'
apply 'CD-Rom'

As you can see, the apply lines use the same extension as the sources files in the folder. More scripts can be added by more else statements. This also allows prioritisation. Note that if all else fails it uses the CD-Rom sources list, this should allow me to have something to install from available at all times.

Also in my /etc/apt folder I created this script, which sets up the links in /etc/network/if-up.d and /etc/network/if-down.d so that the script runs when you ‘change location’.



echo 'Setting up links...'

link /etc/apt/ /etc/network/if-up.d/source-swapper
link /etc/apt/ /etc/network/if-down.d/source-swapper

echo 'making main script executable...'

chmod +x /etc/apt/

echo 'Installing dependencies...'

apt-get -y install libnotify-bin

echo 'should be done :-)'

A few things that should probably be noted. First is that the setup script will need to be set executable. If you decide not to use the setup script, make sure you manually apply its changes. The only other thing that have is the ‘package’ icon. That should be places in the apt folder as /etc/apt/apt.png, if you decide to place it elsewhere or which to use another image, you will need to adapt the script accordingly.

Here is the package icon. I got it from here:

Package Icon