monochromatic

monochromatic blog: http://blog.z3bra.org
git clone git://z3bra.org/monochromatic
Log | Files | Refs

shred-configh.txt (5505B)


      1 # [shred config.h](#)
      2 ## — 27 August, 2014
      3 
      4 As an accomplished Linux user, you have, at some point, had to deal with
      5 softwares that are configured at compilation time.
      6 
      7 If you never ran into those kind of programs, don't worry, you will one day.
      8 The problem is that configuring this kind of software can be really frustrating,
      9 so I'll show you a way to make these a bit more bearable.
     10 
     11 
     12 ### Some background
     13 
     14 Since the beginning, many programs running on Linux have been written in C. C is
     15 a low-level programming language, which means that the machine can *almost*
     16 understand it. For the machine to run the program, you have to translate the
     17 source code into an executable binary file: This is called "**Compilation**".
     18 
     19 The compilation is irreversible, so you can't modify a program once it's
     20 compiled. It means that if you want to change the configuration of a software,
     21 you have two possibilities:
     22 
     23 * Modify the cofiguration file
     24 * Modify the source code
     25 
     26 Most of the time, softwares include an external configuration (like
     27 `~/.bashrc`) to enable on-the-fly configuration. change config file, restart
     28 software, and you're done.  
     29 But in some special cases, such a file can't be used for configuration. the
     30 reason behind that is: speed, simplicity, stability and reliability.
     31 
     32 
     33 ### How does it work ?
     34 
     35 In order to make things easier for the user, the developpers (usually) create a
     36 file named `config.h` where the configuration happens. You don't need
     37 programming knowledge to edit it.
     38 
     39 I like those programs, even though it can be a pain to handle sometimes. The
     40 typical approach with these is the following:
     41 
     42     ─── cd ~/src/c/program
     43     ─── cp config.def.h config.h
     44     ─── vim config.h
     45     ─── make
     46     CC program.c
     47     LD program
     48     ─── ./program
     49 
     50 And you repeat these tasks until you get the behavior you want.
     51 It does not seem hard to do. But on the long term, it can be painful... Also, if
     52 you maintain some packages, it's even harder, because you always have to switch
     53 between your own file, and the default one. Because you use your version, but
     54 you package the software with the default values. So you keep doing this all the
     55 time (on Archlinux for example) :
     56 
     57     ─── cp src/<pkgname>/config.def.h z3bra.h
     58     ─── vim z3bra.h
     59     ─── cp z3bra.h config.h
     60     ─── makepkg -si
     61     ─── vim PKGBUILD
     62     ─── cp src/<pkgname>/config.def.h config.h
     63     ─── makepkg -S
     64 
     65 This. Over and over. You replace your config with the default one, send the
     66 package, recompile the package for you... *sigh* It's **really** boring !
     67 
     68 
     69 
     70 ### Deal with it
     71 
     72 First of all, I had to find an easy way to recompile and reinstall the software
     73 on my computer. I use source-based distributions on my computers (Archlinux,
     74 Crux, Alpine Linux), so it was not really hard. All I had to do was to create a
     75 port for the said software, and include my own files in the building process.
     76 For example, here is the typical `PKGBUILD` I use:
     77 
     78     pkgname=<name>
     79     pkgver=<version>
     80     pkgrel=1
     81     pkgdesc=<description>
     82     arch=('i686' 'x86_64')
     83     url=<url>
     84     license=('GPL')
     85     depends=
     86     conflicts=
     87     makedepends=('git')
     88     source=("$pkgname::git+<gitsource>" 'config.h')
     89     md5sums=('SKIP' 'SKIP')
     90 
     91     build() {
     92         cp config.h $pkgname/config.h
     93         cd $pkgname
     94         make
     95     }
     96     package() {
     97         cd $pkgname
     98         make DESTDIR=$pkgdir install
     99     }
    100 
    101 Now my file will be copied over before compilation, so all I have to do now is
    102 `makepkg -fi`, and the sources will be updated, compiled and installed.
    103 
    104 The compilation/installation part being set up
    105 
    106 The first problem I had is that you need to keep the sources on your
    107 computer, or at least the `config.h` of all the software you use. For this, I
    108 created a special directory in my `$HOME` to hold all the different versions of
    109 those files:
    110 
    111     ─── tree ~/.hm.d
    112     /home/z3bra/.hm.d/
    113     ├── 2bwm
    114     │   ├── config.def.h
    115     │   └── z3bra.h
    116     └── tmenu
    117         ├── bright.h
    118         ├── config.def.h
    119         └── dark.h
    120 
    121     2 directories, 5 files
    122 
    123 Each directory is related to a software, and each file a specific configuration.
    124 Now, I can keep track of all my config files, and they are all in the same
    125 place.
    126 
    127 PROTIP: you can even version your ~/.hm.d directory, to keep track of the
    128 changes you make !
    129 
    130 I then wrote a small script: [hm](http://git.z3bra.org/cgit.cgi/scripts/tree/hm)
    131 (for "Header Manager") to automate the process of saving/restoring
    132 configurations.
    133 
    134 It will browse the `~/.hm.d` directory for specific config files, or the default
    135 ones and replace them in the current folder, automating the config replacement.
    136 Now my workflow is the following:
    137 
    138     ─── cd ~/usr/ports/<pkgname>
    139     ─── vim ~/.hm.d/<pkgname>/$USER.h   # edit personnal config
    140     ─── hm -r <pkgname>/$USER.h         # restore user's file
    141     ─── makepkg -fi                     # build package for me
    142     ─── hm -r <pkgname>                 # restore default config
    143     ─── makepkg -S                      # create package for the community
    144 
    145 As the process is always the same, one could create a wrapper script to automate
    146 all this even more, but I just don't bother.
    147 
    148 
    149 And now you don't want to kill yourself anymore when dealing with these kind of
    150 software ;)  
    151 I hope this trick will help somebody. If not, thanks for reading it anyway !
    152 
    153 <!-- vim: set ft=markdown ts=4 et tw=80: -->