4.6 Sections

Each NSIS installer contains one or more sections. Each of these sections are created, modified, and ended with the following commands.

  • Each section contains zero or more instructions.
  • Sections are executed in order by the resulting installer, and if a component page is used, the user will have the option of disabling/enabling each visible section.
  • If a section's name is 'Uninstall' or is prefixed with 'un.', it's an uninstaller section.

4.6.1 Section Commands

4.6.1.1 AddSize

  1. size_kb

Tells the installer that the current section needs an additional "size_kb" kilobytes of disk space. Only valid within a section (will have no effect outside of a section or in a function).

  1. Section
  2. AddSize 500
  3. SectionEnd

4.6.1.2 Section

  1. [/o] [([!]|[-])section_name] [section_index_output]

Begins and opens a new section. If sectionname is empty, omitted, or begins with a -, then it is a hidden section and the user will not have the option of disabling it. If the section name is 'Uninstall' or is prefixed with 'un.', then it is a an uninstaller section. If _section_index_output is specified, the parameter will be !defined with the section index (can be used with SectionSetText etc). If the section name begins with a !, the section will be displayed as bold. If the /o switch is specified, the section will be unselected by default.

  1. Section "-hidden section"
  2. SectionEnd
  3.  
  4. Section # hidden section
  5. SectionEnd
  6.  
  7. Section "!bold section"
  8. SectionEnd
  9.  
  10. Section /o "optional"
  11. SectionEnd
  12.  
  13. Section "install something" SEC_IDX
  14. SectionEnd

To access the section index, curly brackets must be used and the code must be located below the section in the script.

  1. Section test1 sec1_id
  2. SectionEnd
  3.  
  4. Section test2 sec2_id
  5. SectionEnd
  6.  
  7. Function .onInit
  8. SectionGetText ${sec2_id} $0
  9. MessageBox MB_OK "name of ${sec2_id}:$\n$0" # will correctly display 'name of 1: test2'
  10. FunctionEnd
  1. Function .onInit
  2. SectionGetText ${sec2_id} $0
  3. MessageBox MB_OK "name of ${sec2_id}:$\n$0" # will incorrectly display 'name of ${sec2_id}: test1'
  4. # plus a warning stating:
  5. # unknown variable/constant "{sec2_id}" detected, ignoring
  6. FunctionEnd
  7.  
  8. Section test1 sec1_id
  9. SectionEnd
  10.  
  11. Section test2 sec2_id
  12. SectionEnd

4.6.1.3 SectionEnd

This command closes the current open section.

4.6.1.4 SectionInstType

  1. insttype_index [insttype_index [...]] [RO]

This command specifies which install types (see InstType) the current section defaults to the enabled state in. Multiple SectionInstType commands can be specified (they are combined). If you specify RO as a parameter, then the section will be read-only, meaning the user won't be able to change its state.

  1. InstType "Full" IT_FULL
  2. InstType "Minimal" IT_MIN
  3.  
  4. Section "Help"
  5. SectionInstType ${IT_FULL} ${IT_MIN}
  6. SectionEnd
  7.  
  8. Section "Bonus content"
  9. SectionInstType ${IT_FULL}
  10. SectionEnd

4.6.1.5 SectionIn

  1. insttype_index [insttype_index [...]] [RO]

Works like SectionInstType except that the first install type defined using InstType is indexed 1, the next 2 and so on.

4.6.1.6 SectionGroup

  1. [/e] section_group_name [index_output]

This command inserts a section group. The section group must be closed with SectionGroupEnd, and should contain 1 or more sections. If the section group name begins with a !, its name will be displayed with a bold font. If /e is present, the section group will be expanded by default. If index_output is specified, the parameter will be !defined with the section index (can be used with SectionSetText etc). If the name is prefixed with 'un.' the section group is an uninstaller section group.

  1. SectionGroup "some stuff"
  2. Section "a section"
  3. SectionEnd
  4. Section "another section"
  5. SectionEnd
  6. SectionGroupEnd

4.6.1.7 SectionGroupEnd

Closes a section group opened with SectionGroup.

4.6.2 Uninstall Section

A special Section named 'Uninstall' must be created in order to generate an uninstaller. This section should remove all files, registry keys etc etc that were installed by the installer, from the system. Here is an example of a simple uninstall section:

  1. Section "Uninstall"
  2. Delete $INSTDIR\Uninst.exe ; delete self (see explanation below why this works)
  3. Delete $INSTDIR\myApp.exe
  4. RMDir $INSTDIR
  5. DeleteRegKey HKLM SOFTWARE\myApp
  6. SectionEnd

The first Delete instruction works (deleting the uninstaller), because the uninstaller is transparently copied to the system temporary directory for the uninstall.

Note that in uninstaller code, $INSTDIR contains the directory where the uninstaller lies. It does not necessarily contain the same value it contained in the installer.