Client Management Suite

 View Only

Using NSIS to Build Application Installers 

May 30, 2008 12:47 PM

NSIS (Nullsoft Scriptable Install System) is an open source system to create Windows installers. It is designed to be as small and flexible as possible and is therefore very suitable for internet distribution.

I took it for a spin and really enjoyed the ride. This article covers some of what I picked up.

Here's some of what makes NSIS handy:

  • It has a very small (34 KB) overhead size.
  • It supports LZMA compression, which is the best compression method available. LZMA has a high compression ratio (about double the compression ratio of zip) and fast decompression speed.
  • All installers and uninstallers built by NSIS have a silent mode (/S) for silent, automated installs and uninstalls.
  • It is completely free for any use.

Using NSIS, you will be able to repackage your installers that have multiple files into a single, highly compressed executable. Your installers will complete faster, and consume less bandwidth. You will also be able to script administrative changes to your machines, such as deploying a standard desktop, or updating a specific registry value, or deploying an in-house application.

While NSIS is infinitely extensible and can do anything from creating services to checking for and installing prerequisites to configuring the Windows firewall, it is also extremely easy to start using for simple file and registry changes.

This article will guide you through the process of installing NSIS and making your first NSIS install package that displays a license agreement, allows the user to select which components to install, prompts for a destination directory, applies file changes, and adds a Start Menu group with shortcuts.

Installing NSIS

NSIS itself will only read .nsi (Install System Script) files and follow the instructions in the .nsi file to build a self-installing .exe file. You will also need an NSI Editor/IDE in order to create the .nsi file for NSIS. My favorite NSI Editor is HM NIS Edit.

First we will install NSIS. You can download the latest version of NSIS at http://nsis.sourceforge.net/Download. Download and run the installer, accepting all the defaults. When the installer says that it has completed, uncheck both "Run NSIS" and "Show release notes" and click Finish.

Next we will install HM NIS Edit. You can download the latest version of HM NIS Edit at http://hmne.sourceforge.net/index.php#Download. Download and run nisedit2.x.x.exe, accepting all the defaults. When the installer says that it has completed, make sure "Run HM NIS Edit" is checked and click Finish.

Making your first NSIS install package

The first step to making an install package is to create a directory for your project gather all of the files you need to install into a structure similar to the locations you want them installed to by your installer.

  1. Open Windows explorer and go to the "My Documents" folder.
  2. Create a new folder named "NSIS". This will be where you keep all of your NSIS projects.
  3. Create a folder under the new "NSIS" folder for your project. We will call this folder "MyFirst".
  4. In your new project folder ("MyFirst"), create a text file named license.txt.
  5. Edit license.txt to contain your license agreement for the application.
  6. Create a folder under your new project folder ("MyFirst") with the same name you want as the name for your installer's default directory. We will call this folder "MyFirstApp".
  7. Now add all of the folders and files of your application to this default directory folder ("MyFirstApp"). For your fist installer, we will create the following simple folder and file structure:
    • MyFirstApp
      • notepad.exe (Copy from C:\WINDOWS\system32\notepad.exe)
      • notepad.chm (Copy from C:\WINDOWS\Help\notepad.chm)
      • AFolder
        • AnotherFile.txt
      • AnotherFolder
        • YetAnotherFile.txt

The second step to making an install package is to use HM NIS Edit's "NSIS Script Wizard" to generate an NSIS Script based on the folder structure we created earlier.

  1. Open HM NIS Edit.
  2. Click File>New Script From Wizard...
  3. Click Next.
  4. Fill out the application's name, version, publisher, and website information and click Next.
  5. On the "Setup options" screen, under "Setup file:" give your installer the file name "InstallMyFirst.exe". Under "Compress:" choose LZMA.

    Click Next.

  6. On the "Application directory and license" screen, specify the name of the default directory folder we created earlier ("MyFirstApp") in the format $PROGRAMFILES\MyFirstApp. Choose whether or not you want the user to be able to change the application directory. Then browse to the license file ("license.txt") you created earlier.

    Click Next.

  7. On the "Application Files" screen, we will create two groups "MainSection" and "Optional". MainSection will contain everything except for YetAnotherFile.txt, and Optional will contain YetAnotherFile.txt.
    1. Remove any of the sample files that are listed by highlighting them and pressing the 'X' button.
    2. When the Files pane is empty, click on the "Add directory tree" button.
    3. Click the Browse button.
    4. Select your MyFirstApp folder and click OK.
    5. Click OK Again.
    6. Highlight YetAnotherFile.txt and remove it from the list.
    7. Click the "New group" button.
    8. Name the group "Optional" and click OK.
    9. Select the Optional group and click the "Add file" button.
    10. For the source file, choose YetAnotherFile.txt.
    11. For the destination directory, specify "$INSTDIR\AnotherFolder"
    12. Click OK.
    13. Enable "Allow user to select the components to install"
    14. Enter a description for both MainSection and Optional.
      • Highlight MainSection.
      • Enter the Description "Installs the main application"
      • Highlight Optional
      • Enter the Description "Installs optional components"
      • Click Next.
  8. On the "Application Icons" screen, specify the Start Menu folder name "My First Appplication". The Wizard will automatically define some shortcuts for you pointing to the .exe and .chm (help) files of your application. Select the "$Desktop" shortcut and click "Remove shortcut". Click New Shortcut. For "Shortcut:" specify "$ICONS_GROUP\AnotherFile.lnk". For "Destination:" choose "$INSTDIR\AFolder\AnotherFile.txt".

    Click OK.

    Click Next.

  9. On the "Execute after setup" screen, you have the option to allow the customer to launch a program, and/or launch a readme file after the install is complete. For this exercise, we will leave this section blank. Click Next. Click Next again.
  10. On the "Wizard finished" screen, make sure "Save script" and "Convert file paths to relative paths" are checked.

    Click Finish.

  11. In the "Save As" dialog, browse to your project directory ("NSIS\MyFirst") and give the file a name. For this exercise, we will name the file MyFirst.nsi.

    Click Save.

You now have a .nsi file that will generate an installer that will display a license agreement, prompt for a destination directory, add the files and folders you specified, and adds a Start Menu group with shortcuts. If you want your installer to do something more, you can now add any action to the script using the text editor.

  1. To add an action to the MainSection of your installer, modify the lines between "Section "MainSection"" and "SectionEnd".
  2. To add an action to the Optional Section of your installer, modify the lines between "Section "Optional"" and "SectionEnd".
  3. To add an action to you uninstaller, modify the lines between "Section Uninstall" and "SectionEnd".
  4. Always remember that when you add any permanent action to the installer, that you must add the opposite action to your uninstaller. For example, if you add "CreateShortCut "$SMPROGRAMS\My First Application\YetAnotherFile.lnk" "$INSTDIR\AnotherFolder\YetAnotherFile.txt"" to the MainSection of your installer, you must add "Delete "$SMPROGRAMS\My First Application\YetAnotherFile.lnk"" to your uninstaller.

Before I consider any install script complete, I always make sure that the installer and uninstaller can be run silently. NSIS automatically supports the '/S' switch (notice that it is capital S) to perform silent installs and uninstalls. However, this switch will not suppress Message Boxes by default. We will need to find any Message Box and add the logic that will skip the message box if the installer or uninstaller is being run in silent mode.

  1. Hit Ctrl-F and search for all instances of the word MessageBox.
  2. For each MessageBox command you find, add the following as a new line just before it:
  3. IfSilent +2
  4. "IfSilent +2" instructs NSIS to move two lines forward from the current line (or skip the next line) if we are running in silent mode.

Now click the Save button to save all the changes you have made. Click "NSIS>Compile script" to build your installer executable.

When the compilation has completed, you can run InstallMyFirst.exe to run the installer you just created. To launch the installer silently, run the command "InstallMyFirst.exe /S".

After the install completes, it will write an uninstaller in the directory it was installed to named uninst.exe. It also adds a reference to that uninstaller in your "Add or Remove Programs" Control Panel Applet. You can uninstall this application in one of three ways.

  1. You can open the "Add or Remove Programs" Control Panel Applet, find the application in the list, choose to remove it, and follow the prompts to remove the application.
  2. You can run the command "C:\Program Files\MyFirstApp\uninst.exe" and follow the prompts to remove the application.
  3. You can run the command ""C:\Program Files\MyFirstApp\uninst.exe" /S" to silently remove the application.

Statistics
0 Favorited
0 Views
1 Files
0 Shares
0 Downloads
Attachment(s)
doc file
How to use NSIS to build application installers.doc   163 KB   1 version
Uploaded - Feb 25, 2020

Tags and Keywords

Comments

Nov 05, 2014 10:39 AM

Thank you for the detailed information regarding NSIS. would like to know if NSIS used programs getting detected as a false positive via Symantec

Related Entries and Links

No Related Resource entered.