Writing Installation Programs using NSIS

 

 

Nullsoft makes a great free installation program for Windows called NSIS.

There’s a simple two step process to making an installation program.

First you write a .nsi script.

Then you type the following command and a yourscript.exe installation program will be created for you:

 

Ø       makensis yourscript.nsi

 

 

 

Let’s start with an example.

Say you have a program called Dummy.

Make a new file called “dummy.nsi” and type the following in:

 

 

; dummy.nsi

;

; This script is based on example1.nsi, but adds uninstall support

; and (optionally) start menu shortcuts.

;

; It will install dummy.exe into a directory that the user selects

;

 

; The name of the installer

Name "Dummy"

 

 

Okay, now we need the filename of the .exe file that’s going to be created:

 

; The file to write

OutFile "install_dummy.exe"

 

 

Where do we install Dummy? Probably in a directory in “Program Files” called “dummy”. But that’s up to you.

The InstallDir option sets that up for you.

 

 

; The default installation directory

InstallDir $PROGRAMFILES\dummy

 

 

We save this directory for later, so we can uninstall this program:

 

 

; Registry key to check for directory (so if you install again, it will

; overwrite the old one automatically)

InstallDirRegKey HKLM SOFTWARE\NSIS_dummy "Install_Dir"

 

 

The following lines are what the user will see when they run your installation program. Be informative.

 

 

; The text to prompt the user to enter a directory

ComponentText "This will install Dummy onto your computer."

; The text to prompt the user to enter a directory

DirText "Choose a directory to install in to:"

 

 

Okay, here’s the first real Section. A Section is a checkbox the user can select/deselect that supposedly installs a

bunch of files. To go with convention, you’ll probably want the first checkbox to be a required one that installs

the minimum files necessary for the program to run. Notice I stored some values in the registry so we can undo everything

later, if the user uninstalls.

 

 

; The stuff to install

Section "Dummy Program (required)"

  ; Set output path to the installation directory.

  SetOutPath $INSTDIR

  ; Put file there

  File /r dummy

  ; Write the installation path into the registry

  WriteRegStr HKLM SOFTWARE\NSIS_dummy "Install_Dir" "$INSTDIR"

  ; Write the uninstall keys for Windows

  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\dummy" "DisplayName" "Dummy (remove only)"

  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\dummy" "UninstallString" $INSTDIR\uninstall.exe"'

  WriteUninstaller "uninstall.exe"

SectionEnd

 

 

You can have as many Section’s as you want. Here I’ve added one for Start Menu shortcuts:

 

; optional section

Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\Dummy"

  CreateShortCut "$SMPROGRAMS\Dummy\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0

  CreateShortCut "$SMPROGRAMS\Dummy\Dummy.lnk" "$INSTDIR\dummy\Dummy.exe" "" "" 0

SectionEnd

 

 

You can execute arbitrary programs in a section. There are many commands, in fact, at your disposal.

Refer to the NSIS documentation for more details. Here we execute another installation program and wait for it to

finish before quitting.

 

Section "Microsoft Speech Recognition required files"

  SetOutPath $INSTDIR

  File "spchapi.exe"

 

  ExecWait "$INSTDIR\spchapi.exe"

SectionEnd

 

 

You have already seen a bunch of uninstall code. The following is the real meat and potatoes of it—where everything is

actually deleted and such. You can probably keep the following almost exactly the same for your program.

 

 

; uninstall stuff

 

UninstallText "This will uninstall Dummy. Hit next to continue."

 

; special uninstall section.

Section "Uninstall"

  ; remove registry keys

  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\dummy"

  DeleteRegKey HKLM SOFTWARE\NSIS_dummy

  ; remove files

  Delete $INSTDIR\makensisw.exe

  ; MUST REMOVE UNINSTALLER, too

  Delete $INSTDIR\uninstall.exe

  ; remove shortcuts, if any.

  Delete "$SMPROGRAMS\Dummy\*.*"

  ; remove directories used.

  RMDir "$SMPROGRAMS\Dummy"

  RMDir "$INSTDIR"

SectionEnd

 

 

That concludes this brief tour of NSIS.