1# Activation script {#sec-activation-script} 2 3The activation script is a bash script called to activate the new 4configuration which resides in a NixOS system in `$out/activate`. Since its 5contents depend on your system configuration, the contents may differ. 6This chapter explains how the script works in general and some common NixOS 7snippets. Please be aware that the script is executed on every boot and system 8switch, so tasks that can be performed in other places should be performed 9there (for example letting a directory of a service be created by systemd using 10mechanisms like `StateDirectory`, `CacheDirectory`, ... or if that's not 11possible using `preStart` of the service). 12 13Activation scripts are defined as snippets using 14[](#opt-system.activationScripts). They can either be a simple multiline string 15or an attribute set that can depend on other snippets. The builder for the 16activation script will take these dependencies into account and order the 17snippets accordingly. As a simple example: 18 19```nix 20system.activationScripts.my-activation-script = { 21 deps = [ "etc" ]; 22 # supportsDryActivation = true; 23 text = '' 24 echo "Hallo i bims" 25 ''; 26}; 27``` 28 29This example creates an activation script snippet that is run after the `etc` 30snippet. The special variable `supportsDryActivation` can be set so the snippet 31is also run when `nixos-rebuild dry-activate` is run. To differentiate between 32real and dry activation, the `$NIXOS_ACTION` environment variable can be 33read which is set to `dry-activate` when a dry activation is done. 34 35An activation script can write to special files instructing 36`switch-to-configuration` to restart/reload units. The script will take these 37requests into account and will incorporate the unit configuration as described 38above. This means that the activation script will "fake" a modified unit file 39and `switch-to-configuration` will act accordingly. By doing so, configuration 40like [systemd.services.\<name\>.restartIfChanged](#opt-systemd.services) is 41respected. Since the activation script is run **after** services are already 42stopped, [systemd.services.\<name\>.stopIfChanged](#opt-systemd.services) 43cannot be taken into account anymore and the unit is always restarted instead 44of being stopped and started afterwards. 45 46The files that can be written to are `/run/nixos/activation-restart-list` and 47`/run/nixos/activation-reload-list` with their respective counterparts for 48dry activation being `/run/nixos/dry-activation-restart-list` and 49`/run/nixos/dry-activation-reload-list`. Those files can contain 50newline-separated lists of unit names where duplicates are being ignored. These 51files are not create automatically and activation scripts must take the 52possibility into account that they have to create them first. 53 54## NixOS snippets {#sec-activation-script-nixos-snippets} 55 56There are some snippets NixOS enables by default because disabling them would 57most likely break your system. This section lists a few of them and what they 58do: 59 60- `binsh` creates `/bin/sh` which points to the runtime shell 61- `etc` sets up the contents of `/etc`, this includes systemd units and 62 excludes `/etc/passwd`, `/etc/group`, and `/etc/shadow` (which are managed by 63 the `users` snippet) 64- `hostname` sets the system's hostname in the kernel (not in `/etc`) 65- `modprobe` sets the path to the `modprobe` binary for module auto-loading 66- `nix` prepares the nix store and adds a default initial channel 67- `specialfs` is responsible for mounting filesystems like `/proc` and `sys` 68- `users` creates and removes users and groups by managing `/etc/passwd`, 69 `/etc/group` and `/etc/shadow`. This also creates home directories 70- `usrbinenv` creates `/usr/bin/env` 71- `var` creates some directories in `/var` that are not service-specific 72- `wrappers` creates setuid wrappers like `ping` and `sudo`