This is taken directly from Figure 5.2 in http://nixos.org/~eelco/pubs/phd-thesis.pdf. It is presented here to be more directly linkable.
serialise(fso) = str("nix-archive-1") + serialise1(fso)
serialise1(fso) = str("(") + seralise2(fso) + str(")")
serialise2(type=Regular, exec, contents) =
str("type") + str("regular")
+ (