Thursday, June 11, 2009

Getting Started with NSIS(Nullsoft Scriptable Install System)

NSIS (Nullsoft Scriptable Install System) is a tool that allows programmers to create such installers for Windows.It is released under an open source license and is completely free for any use.

NSIS creates installers that are capable of installing, uninstalling, setting system settings, extracting files, etc. Because it's based on script files, you can fully control every part of your installers. The script language supports variables, functions, string manipulation, just like a normal programming language - but designed for the creation of installers. Even with all these features, NSIS is still the smallest installer system available. With the default options, it has an overhead of only 34 KB. Let's now try a very simple installer with the NSIS as below

1. Download latest NSIS and install

2. Open any text editor and copy the following snippet. Save this file as .nsi

;-------------------------------------------------------------------------
; Installer script for My Installer 1.0
;-------------------------------------------------------------------------
;--------------------------------------------
; General definitions
!define PRODUCT_NAME "My Installer"
!define PRODUCT_VERSION_MAJOR 1
!define PRODUCT_VERSION_MINOR 0
!define PRODUCT_DISPLAY_VERSION "1.0"
!define PRODUCT_PUBLISHER "XYZ, Inc."
!define PRODUCT_WEB_SITE "http://www.XYZ.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME} 1.0"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_INSTALL_DIR "$PROGRAMFILES\XYZ\${PRODUCT_NAME} 1.0"

;--------------------------------------------
; Maximum compression
SetCompressor /SOLID lzma

;--------------------------------------------
; Modern UI (MUI 1.67 compatible) definitions
!include "MUI2.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "resources\icon.ico"
!define MUI_UNICON "resources\icon.ico"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "resources\header.bmp"
!define MUI_HEADERIMAGE_RIGHT
!define MUI_HEADER_TRANSPARENT_TEXT
!define MUI_WELCOMEFINISHPAGE_BITMAP "resources\welcome.bmp"
!define MUI_WELCOMEFINISHPAGE_BITMAP_NOSTRETCH
;!define MUI_FINISHPAGE_NOAUTOCLOSE

; Welcome page
!insertmacro MUI_PAGE_WELCOME

; License page
!define MUI_LICENSEPAGE_CHECKBOX
!insertmacro MUI_PAGE_LICENSE "resources\license.rtf"

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES

; Finish page
!define MUI_FINISHPAGE_SHOWREADME "$INSTDIR\README.txt"
!define MUI_FINISHPAGE_NOREBOOTSUPPORT
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "StrFunc.nsh"
${StrLoc}

;--------------------------------------------
; Installer Settings
Name "${PRODUCT_NAME} v1.0"
OutFile "XYZ_1.0.exe"
InstallDir "${PRODUCT_INSTALL_DIR}"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "${PRODUCT_PUBLISHER}"
RequestExecutionLevel admin

;-------------------------------------------------------------------------
; Initialize Function
Function .onInit

; Determine if installed
ReadRegStr $R0 ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString"
StrCmp $R0 "" Proceed EqualMinor

EqualMinor:
MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
"${PRODUCT_NAME} is already installed. $\n$\nClick `OK` to remove the \
previous installation or `Cancel` to cancel." \
IDOK UninstallPrevious
Abort

;--------------------------------------------
; Run the uninstaller
UninstallPrevious:
HideWindow
ClearErrors
ExecWait '$R0 _?=$INSTDIR' ;Do not copy the uninstaller to a temp file
Delete '$R0'
BringToFront
Goto Proceed

Proceed:

FunctionEnd

;-------------------------------------------------------------------------
; Main Install Section
Section "MainSection" MainSection

DetailPrint "Installing Components..."
SetOutPath "${PRODUCT_INSTALL_DIR}"
SetOverwrite ifnewer
File "/oname=abc.png" "Build\abc.png"
File "/oname=icon.ico" "resources\blendables1.ico"

SectionEnd

;-------------------------------------------------------------------------
; Installer Finished callbacks
Function .onInstSuccess
FunctionEnd

Function .onInstFailed
FunctionEnd

;-------------------------------------------------------------------------
; Post Install
Section -Post
WriteUninstaller "$INSTDIR\uninst.exe"

;--------------------------------------------
; Add/Remove registry settings
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\icon.ico"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_DISPLAY_VERSION}"
WriteRegDWORD ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "VersionMajor" "${PRODUCT_VERSION_MAJOR}"
WriteRegDWORD ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "VersionMinor" "${PRODUCT_VERSION_MINOR}"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

;-------------------------------------------------------------------------
; Uninstaller
Function un.onUninstSuccess
HideWindow
MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
Abort
FunctionEnd

Section Uninstall

;--------------------------------------------
; Registry
DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
Delete "$INSTDIR\icon.ico"
Delete "$INSTDIR\uninst.exe"
Delete "${PRODUCT_INSTALL_DIR}\abc.png"
RMDir /r "${PRODUCT_INSTALL_DIR}"
SetAutoClose True

SectionEnd



3. Create a folder named Resources and copy/create the files like .ico and .rtf as mentioned in the script. This folder should be created in the same location where the .nsi file is saved



4. Right click the ".nsi" file and select "Compile NSIS Script"



5. A compilation window will pop up and will list the errors with line numbers if the compilation failed. I would recommend using Notepad++ as the editor for nsi script as it is very easy to navigate to the line number mentioned in the compiler window (for errors in the script) . If the compilation went with out any errors the installer will be ready and available at the same location as the .nsi file.

1 comment:

  1. Remember using NSIS few years back, for one of my shareware projects, Smart Editor http://www.tucows.com/preview/362587 :).

    It's smart and easy

    ReplyDelete