1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-06 16:57:08 +00:00
nix-darwin/modules/system/activation-scripts.nix

144 lines
4.3 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
2016-12-07 23:06:18 +01:00
inherit (pkgs) stdenv;
cfg = config.system;
2017-01-02 08:21:27 +01:00
script = import ../lib/write-text.nix {
inherit lib;
mkTextDerivation = name: text: pkgs.writeScript "activate-${name}" text;
};
in
{
options = {
system.activationScripts = mkOption {
internal = true;
type = types.attrsOf (types.submodule script);
default = {};
2024-04-14 23:02:32 +02:00
description = ''
A set of shell script fragments that are executed when a NixOS
system configuration is activated. Examples are updating
/etc, creating accounts, and so on. Since these are executed
every time you boot the system or run
{command}`nixos-rebuild`, it's important that they are
idempotent and fast.
'';
};
};
config = {
system.activationScripts.script.text = ''
{activation-scripts,activate-system}: purify environment This ensures that system activation does not depend on various details of its process environment, ensuring uniformity across various invocation contexts and with the `activate-system` daemon. This becomes more important in a post‐user‐activation world to avoid problematic dependencies like `$SUDO_USER`, but is a good idea in general. The `sudoers(5)` defaults on my Sequoia system are: Defaults env_reset Defaults env_keep += "BLOCKSIZE" Defaults env_keep += "COLORFGBG COLORTERM" Defaults env_keep += "__CF_USER_TEXT_ENCODING" Defaults env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults env_keep += "LINES COLUMNS" Defaults env_keep += "LSCOLORS" Defaults env_keep += "SSH_AUTH_SOCK" Defaults env_keep += "TZ" Defaults env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults env_keep += "EDITOR VISUAL" Defaults env_keep += "HOME MAIL" Of these preserved environment variables, the ones that are set in practice when I run `sudo env` that aren’t set in the activation script here are: * `$COLORTERM` * `$DISPLAY` * `$EDITOR` * `$MAIL` * `$SSH_AUTH_SOCK` * `$TERM` * `$__CF_USER_TEXT_ENCODING` Most of these seem either pointless or actively harmful to set for the purpose of the system activation script. This will mean that tools run during activation won’t print output in the user’s preferred language, but that’s probably the right trade‐off overall, as that is likely to break activation scripts that parse command output anyway.
2025-01-11 15:44:41 +00:00
#!/usr/bin/env -i ${stdenv.shell}
# shellcheck shell=bash
# shellcheck disable=SC2096
2016-12-12 17:34:43 +01:00
set -e
set -o pipefail
{activation-scripts,activate-system}: purify environment This ensures that system activation does not depend on various details of its process environment, ensuring uniformity across various invocation contexts and with the `activate-system` daemon. This becomes more important in a post‐user‐activation world to avoid problematic dependencies like `$SUDO_USER`, but is a good idea in general. The `sudoers(5)` defaults on my Sequoia system are: Defaults env_reset Defaults env_keep += "BLOCKSIZE" Defaults env_keep += "COLORFGBG COLORTERM" Defaults env_keep += "__CF_USER_TEXT_ENCODING" Defaults env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults env_keep += "LINES COLUMNS" Defaults env_keep += "LSCOLORS" Defaults env_keep += "SSH_AUTH_SOCK" Defaults env_keep += "TZ" Defaults env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults env_keep += "EDITOR VISUAL" Defaults env_keep += "HOME MAIL" Of these preserved environment variables, the ones that are set in practice when I run `sudo env` that aren’t set in the activation script here are: * `$COLORTERM` * `$DISPLAY` * `$EDITOR` * `$MAIL` * `$SSH_AUTH_SOCK` * `$TERM` * `$__CF_USER_TEXT_ENCODING` Most of these seem either pointless or actively harmful to set for the purpose of the system activation script. This will mean that tools run during activation won’t print output in the user’s preferred language, but that’s probably the right trade‐off overall, as that is likely to break activation scripts that parse command output anyway.
2025-01-11 15:44:41 +00:00
export PATH="${pkgs.gnugrep}/bin:${pkgs.coreutils}/bin:@out@/sw/bin:/usr/bin:/bin:/usr/sbin:/sbin"
{activation-scripts,activate-system}: purify environment This ensures that system activation does not depend on various details of its process environment, ensuring uniformity across various invocation contexts and with the `activate-system` daemon. This becomes more important in a post‐user‐activation world to avoid problematic dependencies like `$SUDO_USER`, but is a good idea in general. The `sudoers(5)` defaults on my Sequoia system are: Defaults env_reset Defaults env_keep += "BLOCKSIZE" Defaults env_keep += "COLORFGBG COLORTERM" Defaults env_keep += "__CF_USER_TEXT_ENCODING" Defaults env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults env_keep += "LINES COLUMNS" Defaults env_keep += "LSCOLORS" Defaults env_keep += "SSH_AUTH_SOCK" Defaults env_keep += "TZ" Defaults env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults env_keep += "EDITOR VISUAL" Defaults env_keep += "HOME MAIL" Of these preserved environment variables, the ones that are set in practice when I run `sudo env` that aren’t set in the activation script here are: * `$COLORTERM` * `$DISPLAY` * `$EDITOR` * `$MAIL` * `$SSH_AUTH_SOCK` * `$TERM` * `$__CF_USER_TEXT_ENCODING` Most of these seem either pointless or actively harmful to set for the purpose of the system activation script. This will mean that tools run during activation won’t print output in the user’s preferred language, but that’s probably the right trade‐off overall, as that is likely to break activation scripts that parse command output anyway.
2025-01-11 15:44:41 +00:00
export USER=root
export LOGNAME=root
export HOME=~root
export SHELL=$BASH
export LANG=C
export LC_CTYPE=UTF-8
systemConfig=@out@
# Ensure a consistent umask.
umask 0022
{activation-scripts,activate-system}: purify environment This ensures that system activation does not depend on various details of its process environment, ensuring uniformity across various invocation contexts and with the `activate-system` daemon. This becomes more important in a post‐user‐activation world to avoid problematic dependencies like `$SUDO_USER`, but is a good idea in general. The `sudoers(5)` defaults on my Sequoia system are: Defaults env_reset Defaults env_keep += "BLOCKSIZE" Defaults env_keep += "COLORFGBG COLORTERM" Defaults env_keep += "__CF_USER_TEXT_ENCODING" Defaults env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults env_keep += "LINES COLUMNS" Defaults env_keep += "LSCOLORS" Defaults env_keep += "SSH_AUTH_SOCK" Defaults env_keep += "TZ" Defaults env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults env_keep += "EDITOR VISUAL" Defaults env_keep += "HOME MAIL" Of these preserved environment variables, the ones that are set in practice when I run `sudo env` that aren’t set in the activation script here are: * `$COLORTERM` * `$DISPLAY` * `$EDITOR` * `$MAIL` * `$SSH_AUTH_SOCK` * `$TERM` * `$__CF_USER_TEXT_ENCODING` Most of these seem either pointless or actively harmful to set for the purpose of the system activation script. This will mean that tools run during activation won’t print output in the user’s preferred language, but that’s probably the right trade‐off overall, as that is likely to break activation scripts that parse command output anyway.
2025-01-11 15:44:41 +00:00
cd /
if [[ $(id -u) -ne 0 ]]; then
printf >&2 '\e[1;31merror: `activate` must be run as root\e[0m\n'
exit 2
fi
${cfg.activationScripts.preActivation.text}
# We run `etcChecks` again just in case someone runs `activate`
# directly without `activate-user`.
${cfg.activationScripts.etcChecks.text}
${cfg.activationScripts.extraActivation.text}
${cfg.activationScripts.groups.text}
2018-01-13 13:33:54 +01:00
${cfg.activationScripts.users.text}
${cfg.activationScripts.applications.text}
${cfg.activationScripts.pam.text}
2020-05-26 23:15:07 +02:00
${cfg.activationScripts.patches.text}
${cfg.activationScripts.etc.text}
${cfg.activationScripts.defaults.text}
${cfg.activationScripts.launchd.text}
${cfg.activationScripts.nix-daemon.text}
${cfg.activationScripts.time.text}
${cfg.activationScripts.networking.text}
${cfg.activationScripts.power.text}
${cfg.activationScripts.keyboard.text}
2018-09-30 13:22:24 +02:00
${cfg.activationScripts.fonts.text}
2024-02-27 17:21:19 -08:00
${cfg.activationScripts.nvram.text}
${cfg.activationScripts.postActivation.text}
# Make this configuration the current configuration.
# The readlink is there to ensure that when $systemConfig = /system
# (which is a symlink to the store), /run/current-system is still
# used as a garbage collection root.
ln -sfn "$(readlink -f "$systemConfig")" /run/current-system
# Prevent the current configuration from being garbage-collected.
ln -sfn /run/current-system /nix/var/nix/gcroots/current-system
'';
2024-10-26 12:31:53 +11:00
# FIXME: activationScripts.checks should be system level
2016-12-14 12:32:20 +01:00
system.activationScripts.userScript.text = ''
#! ${stdenv.shell}
set -e
set -o pipefail
export PATH="${pkgs.gnugrep}/bin:${pkgs.coreutils}/bin:@out@/sw/bin:/usr/bin:/bin"
2016-12-14 12:32:20 +01:00
systemConfig=@out@
_status=0
trap "_status=1" ERR
# Ensure a consistent umask.
umask 0022
${cfg.activationScripts.preUserActivation.text}
2017-07-23 16:05:46 +02:00
2024-07-27 10:39:57 +10:00
# This should be running at the system level, but as user activation runs first
# we run it here with sudo
${cfg.activationScripts.createRun.text}
${cfg.activationScripts.checks.text}
${cfg.activationScripts.etcChecks.text}
${cfg.activationScripts.extraUserActivation.text}
${cfg.activationScripts.userDefaults.text}
2017-01-25 22:35:06 +01:00
${cfg.activationScripts.userLaunchd.text}
2020-12-17 13:03:57 -08:00
${cfg.activationScripts.homebrew.text}
2016-12-14 12:32:20 +01:00
${cfg.activationScripts.postUserActivation.text}
2016-12-14 12:32:20 +01:00
exit $_status
'';
2016-12-19 20:31:59 +01:00
# Extra activation scripts, that can be customized by users
# don't use this unless you know what you are doing.
system.activationScripts.extraActivation.text = mkDefault "";
system.activationScripts.preActivation.text = mkDefault "";
system.activationScripts.postActivation.text = mkDefault "";
2016-12-19 20:31:59 +01:00
system.activationScripts.extraUserActivation.text = mkDefault "";
system.activationScripts.preUserActivation.text = mkDefault "";
system.activationScripts.postUserActivation.text = mkDefault "";
2016-12-19 20:31:59 +01:00
};
}