SitePackage.lua and hooks

Sites may wish to alter the behavior of Lmod to suit their needs. A good place to do this is the SitePackage.lua. Anything in this file will automatically be loaded every time the Lmod command is run. This file can be used to provide common functions that can be used in a sites modulefiles. It is also a place where a site can implement their hook functions.

Hook functions are normal functions that if implemented and registered with Lmod will be called when certain operations happen inside Lmod. For example, there is a load hook. A site can register a function that is called every time a module is loaded. There are several hook functions that are discussed in Hook functions.

How to set up SitePackage.lua

Here are two suggestions on how to use your SitePackage.lua file:

  1. Install Lmod normally and then overwrite your SitePackage.lua file over this one in the install directory. This is good for testing but it is recommended that sites use the second method for permanent changes. Otherwise changes may be lost when upgrading Lmod.

  2. Create a file named “SitePackage.lua” in a different directory separate from the Lmod installed directory and it will override the one in the Lmod install directory. Then you should modify your and z00_lmod.csh (or however you initialize the “module” command) with:

    (for bash, zsh, etc)
    export LMOD_PACKAGE_PATH=/path/to/the/Site/Directory
    (for csh)
    setenv LMOD_PACKAGE_PATH /path/to/the/Site/Directory

Implementing functions in SitePackage.lua

For example your site might wish to provide the following function to set MODULEPATH inside your SitePackage:

function prependModulePath(subdir)
   local mroot = os.getenv("MODULEPATH_ROOT")
   local mdir  = pathJoin(mroot, subdir)
   prepend_path("MODULEPATH", mdir)

This function must be registered with the sandbox so that Lmod modulefiles can call it:

sandbox_registration{ prependModulePath    = prependModulePath }

Hook tips

# If you want to know which modules are being loaded, use the FrameStack:

local frameStk = FrameStk:singleton()
-- check if anything else gets loaded
if not frameStk:empty() then
    print("This modules has no dependencies loaded.")

-- the module at the top of the FrameStack is the one the user requested
local userload = (frameStk:atTop()) and "yes" or "no"

# If you want to know the short name or path of a loaded module, you can use the ModuleTable:

local mname = MName:new("mt", FullModuleName)
local sn         = mname:sn()
local version = mname:version()

Hook functions

This function is called after a modulefile is loaded in “load” mode.
This function is called after a modulefile is unloaded in “unload” mode.
This hook returns the time on the timestamp file.
This hook return whether a cache should be written.
This hook is used to specify Site Name. It is used to generate family prefix: site_FAMILY_
Hook to print messages after avail, list, spider.
Hook to print messages after LmodError, LmodWarning, LmodMessage.
This hook adds the arch and os name to moduleT.lua to make it safe on shared filesystems.
Map directory names to labels
This hook is run after restore operation
This hook is run when Lmod is called
**packagebasename**(s_patDir, s_patLib):
This hook gives you a table with the current patterns that spider uses to construct the reverse map.
This hook is called when spider is evaluating a modulefile.