diff --git a/Makefile.am b/Makefile.am
index 99ec7f741209fcf98666b97ac88b86cd84aaf85d..2b44a56a6064c6c836282f3b69dbb89a66266231 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,10 @@
 SUBDIRS = externals src scripts corepkgs doc
 
-EXTRA_DIST = substitute.mk nix.spec
+EXTRA_DIST = substitute.mk nix.spec nix.spec.in
 
-rpm: dist
+include ./substitute.mk
+
+nix.spec: nix.spec.in
+
+rpm: nix.spec dist
 	rpm $(EXTRA_RPM_FLAGS) -ta $(distdir).tar.gz
diff --git a/configure.ac b/configure.ac
index 85244ebabfaf6d52bc81be9d55041713617bfaa2..ecd97b1e779d414e927bf8c5cebee4267f924384 100644
--- a/configure.ac
+++ b/configure.ac
@@ -5,9 +5,9 @@ AM_INIT_AUTOMAKE
 
 # Put the revision number in the version.
 if REVISION=`test -d $srcdir/.svn && svnversion $srcdir 2> /dev/null`; then
-   VERSION="$VERSION-r$REVISION"
+   VERSION="${VERSION}-pre${REVISION}"
 elif REVISION=`cat $srcdir/svn-revision 2> /dev/null`; then
-   VERSION="$VERSION-r$REVISION"
+   VERSION="${VERSION}-pre${REVISION}"
 fi
 
 AC_PREFIX_DEFAULT(/nix)
diff --git a/nix.spec b/nix.spec.in
similarity index 92%
rename from nix.spec
rename to nix.spec.in
index 74fc18ef28cadddba46db86727cb4c8fa1bb1abd..388c4075196c9e7a8a8622943b105850dac35c03 100644
--- a/nix.spec
+++ b/nix.spec.in
@@ -1,11 +1,11 @@
 Summary: The Nix software deployment system
 Name: nix
-Version: 0.5
+Version: @version@
 Release: 1
 License: GPL
 Group: WeetNiet
 URL: http://www.cs.uu.nl/groups/ST/twiki/bin/view/Trace/NixDeploymentSystem
-Source0: %{name}-%{version}.tar.gz
+Source0: %{name}-@version@.tar.gz
 BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
 %define _prefix /nix
 Prefix: %{_prefix}
diff --git a/substitute.mk b/substitute.mk
index cc21eba402459e0b17ada103849fe8cea155d5fb..bbdb9b617b4f0f553d48e33d62e6d61f352e27cb 100644
--- a/substitute.mk
+++ b/substitute.mk
@@ -8,5 +8,6 @@
 	 -e "s^@libexecdir\@^$(libexecdir)^g" \
 	 -e "s^@system\@^$(system)^g" \
 	 -e "s^@wget\@^$(wget)^g" \
+	 -e "s^@version\@^$(VERSION)^g" \
 	 < $< > $@ || rm $@
-	chmod +x $@
+	if test -x $<; then chmod +x $@; fi