Skip to content

Commit

Permalink
Merge pull request #263203 from nikstur/replace-activation
Browse files Browse the repository at this point in the history
Replace simple activationScripts
  • Loading branch information
lheckemann authored Oct 28, 2023
2 parents 0643b92 + d300940 commit 8670794
Show file tree
Hide file tree
Showing 23 changed files with 232 additions and 222 deletions.
4 changes: 2 additions & 2 deletions nixos/modules/config/mysql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,11 @@ in
'';
};

# Activation script to append the password from the password file
# preStart script to append the password from the password file
# to the configuration files. It also fixes the owner of the
# libnss-mysql-root.cfg because it is changed to root after the
# password is appended.
system.activationScripts.mysql-auth-passwords = ''
systemd.services.mysql.preStart = ''
if [[ -r ${cfg.passwordFile} ]]; then
org_umask=$(umask)
umask 0077
Expand Down
11 changes: 4 additions & 7 deletions nixos/modules/config/nix-channel.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,9 @@ in

nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault "");

system.activationScripts.nix-channel = mkIf cfg.channel.enable
(stringAfter [ "etc" "users" ] ''
# Subscribe the root user to the NixOS channel by default.
if [ ! -e "/root/.nix-channels" ]; then
echo "${config.system.defaultChannel} nixos" > "/root/.nix-channels"
fi
'');
systemd.tmpfiles.rules = lib.mkIf cfg.channel.enable [
"f /root/.nix-channels -"
''w "/root/.nix-channels" - - - - "${config.system.defaultChannel} nixos\n"''
];
};
}
15 changes: 12 additions & 3 deletions nixos/modules/security/duosec.nix
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ in
source = "${pkgs.duo-unix.out}/bin/login_duo";
};

system.activationScripts = {
login_duo = mkIf cfg.ssh.enable ''
systemd.services.login-duo = lib.mkIf cfg.ssh.enable {
wantedBy = [ "sysinit.target" ];
before = [ "sysinit.target" ];
unitConfig.DefaultDependencies = false;
script = ''
if test -f "${cfg.secretKeyFile}"; then
mkdir -m 0755 -p /etc/duo
Expand All @@ -209,7 +212,13 @@ in
mv -fT "$conf" /etc/duo/login_duo.conf
fi
'';
pam_duo = mkIf cfg.pam.enable ''
};

systemd.services.pam-duo = lib.mkIf cfg.ssh.enable {
wantedBy = [ "sysinit.target" ];
before = [ "sysinit.target" ];
unitConfig.DefaultDependencies = false;
script = ''
if test -f "${cfg.secretKeyFile}"; then
mkdir -m 0755 -p /etc/duo
Expand Down
57 changes: 31 additions & 26 deletions nixos/modules/security/wrappers/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -275,33 +275,38 @@ in
mrpx ${wrap.source},
'') wrappers;

###### wrappers activation script
system.activationScripts.wrappers =
lib.stringAfter [ "specialfs" "users" ]
''
chmod 755 "${parentWrapperDir}"
# We want to place the tmpdirs for the wrappers to the parent dir.
wrapperDir=$(mktemp --directory --tmpdir="${parentWrapperDir}" wrappers.XXXXXXXXXX)
chmod a+rx "$wrapperDir"
${lib.concatStringsSep "\n" mkWrappedPrograms}
if [ -L ${wrapperDir} ]; then
# Atomically replace the symlink
# See https://axialcorps.com/2013/07/03/atomically-replacing-files-and-directories/
old=$(readlink -f ${wrapperDir})
if [ -e "${wrapperDir}-tmp" ]; then
rm --force --recursive "${wrapperDir}-tmp"
fi
ln --symbolic --force --no-dereference "$wrapperDir" "${wrapperDir}-tmp"
mv --no-target-directory "${wrapperDir}-tmp" "${wrapperDir}"
rm --force --recursive "$old"
else
# For initial setup
ln --symbolic "$wrapperDir" "${wrapperDir}"
systemd.services.suid-sgid-wrappers = {
description = "Create SUID/SGID Wrappers";
wantedBy = [ "sysinit.target" ];
before = [ "sysinit.target" ];
unitConfig.DefaultDependencies = false;
unitConfig.RequiresMountsFor = [ "/nix/store" "/run/wrappers" ];
serviceConfig.Type = "oneshot";
script = ''
chmod 755 "${parentWrapperDir}"
# We want to place the tmpdirs for the wrappers to the parent dir.
wrapperDir=$(mktemp --directory --tmpdir="${parentWrapperDir}" wrappers.XXXXXXXXXX)
chmod a+rx "$wrapperDir"
${lib.concatStringsSep "\n" mkWrappedPrograms}
if [ -L ${wrapperDir} ]; then
# Atomically replace the symlink
# See https://axialcorps.com/2013/07/03/atomically-replacing-files-and-directories/
old=$(readlink -f ${wrapperDir})
if [ -e "${wrapperDir}-tmp" ]; then
rm --force --recursive "${wrapperDir}-tmp"
fi
'';
ln --symbolic --force --no-dereference "$wrapperDir" "${wrapperDir}-tmp"
mv --no-target-directory "${wrapperDir}-tmp" "${wrapperDir}"
rm --force --recursive "$old"
else
# For initial setup
ln --symbolic "$wrapperDir" "${wrapperDir}"
fi
'';
};

###### wrappers consistency checks
system.checks = lib.singleton (pkgs.runCommandLocal
Expand Down
17 changes: 10 additions & 7 deletions nixos/modules/services/mail/mlmmj.nix
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ in

environment.systemPackages = [ pkgs.mlmmj ];

system.activationScripts.mlmmj = ''
${pkgs.coreutils}/bin/mkdir -p ${stateDir} ${spoolDir}/${cfg.listDomain}
${pkgs.coreutils}/bin/chown -R ${cfg.user}:${cfg.group} ${spoolDir}
${concatMapLines (createList cfg.listDomain) cfg.mailLists}
${pkgs.postfix}/bin/postmap /etc/postfix/virtual
${pkgs.postfix}/bin/postmap /etc/postfix/transport
'';
systemd.tmpfiles.rules = [
''d "${stateDir}" -''
''d "${spoolDir}/${cfg.listDomain}" -''
''Z "${spoolDir}" - "${cfg.user}" "${cfg.group}" -''
];

systemd.services.mlmmj-maintd = {
description = "mlmmj maintenance daemon";
Expand All @@ -158,6 +156,11 @@ in
Group = cfg.group;
ExecStart = "${pkgs.mlmmj}/bin/mlmmj-maintd -F -d ${spoolDir}/${cfg.listDomain}";
};
preStart = ''
${concatMapLines (createList cfg.listDomain) cfg.mailLists}
${pkgs.postfix}/bin/postmap /etc/postfix/virtual
${pkgs.postfix}/bin/postmap /etc/postfix/transport
'';
};

systemd.timers.mlmmj-maintd = {
Expand Down
8 changes: 3 additions & 5 deletions nixos/modules/services/monitoring/ups.nix
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,9 @@ in

power.ups.schedulerRules = mkDefault "${pkgs.nut}/etc/upssched.conf.sample";

system.activationScripts.upsSetup = stringAfter [ "users" "groups" ]
''
# Used to store pid files of drivers.
mkdir -p /var/state/ups
'';
systemd.tmpfiles.rules = [
"d /var/state/ups -"
];


/*
Expand Down
36 changes: 19 additions & 17 deletions nixos/modules/services/networking/iscsi/initiator.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,27 @@ in
'';
environment.etc."iscsi/initiatorname.iscsi".text = "InitiatorName=${cfg.name}";

system.activationScripts.iscsid = let
extraCfgDumper = optionalString (cfg.extraConfigFile != null) ''
if [ -f "${cfg.extraConfigFile}" ]; then
printf "\n# The following is from ${cfg.extraConfigFile}:\n"
cat "${cfg.extraConfigFile}"
else
echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2
fi
'';
in ''
(
cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source}
${extraCfgDumper}
) > /etc/iscsi/iscsid.conf
'';

systemd.packages = [ cfg.package ];

systemd.services."iscsid".wantedBy = [ "multi-user.target" ];
systemd.services."iscsid" = {
wantedBy = [ "multi-user.target" ];
preStart =
let
extraCfgDumper = optionalString (cfg.extraConfigFile != null) ''
if [ -f "${cfg.extraConfigFile}" ]; then
printf "\n# The following is from ${cfg.extraConfigFile}:\n"
cat "${cfg.extraConfigFile}"
else
echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2
fi
'';
in ''
(
cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source}
${extraCfgDumper}
) > /etc/iscsi/iscsid.conf
'';
};
systemd.sockets."iscsid".wantedBy = [ "sockets.target" ];

systemd.services."iscsi" = mkIf cfg.enableAutoLoginOut {
Expand Down
5 changes: 3 additions & 2 deletions nixos/modules/services/networking/spiped.nix
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ in
script = "exec ${pkgs.spiped}/bin/spiped -F `cat /etc/spiped/$1.spec`";
};

system.activationScripts.spiped = optionalString (cfg.config != {})
"mkdir -p /var/lib/spiped";
systemd.tmpfiles.rules = lib.mkIf (cfg.config != { }) [
"d /var/lib/spiped -"
];

# Setup spiped config files
environment.etc = mapAttrs' (name: cfg: nameValuePair "spiped/${name}.spec"
Expand Down
30 changes: 15 additions & 15 deletions nixos/modules/services/networking/strongswan-swanctl/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ in {

# The swanctl command complains when the following directories don't exist:
# See: https://wiki.strongswan.org/projects/strongswan/wiki/Swanctldirectory
system.activationScripts.strongswan-swanctl-etc = stringAfter ["etc"] ''
mkdir -p '/etc/swanctl/x509' # Trusted X.509 end entity certificates
mkdir -p '/etc/swanctl/x509ca' # Trusted X.509 Certificate Authority certificates
mkdir -p '/etc/swanctl/x509ocsp'
mkdir -p '/etc/swanctl/x509aa' # Trusted X.509 Attribute Authority certificates
mkdir -p '/etc/swanctl/x509ac' # Attribute Certificates
mkdir -p '/etc/swanctl/x509crl' # Certificate Revocation Lists
mkdir -p '/etc/swanctl/pubkey' # Raw public keys
mkdir -p '/etc/swanctl/private' # Private keys in any format
mkdir -p '/etc/swanctl/rsa' # PKCS#1 encoded RSA private keys
mkdir -p '/etc/swanctl/ecdsa' # Plain ECDSA private keys
mkdir -p '/etc/swanctl/bliss'
mkdir -p '/etc/swanctl/pkcs8' # PKCS#8 encoded private keys of any type
mkdir -p '/etc/swanctl/pkcs12' # PKCS#12 containers
'';
systemd.tmpfiles.rules = [
"d /etc/swanctl/x509 -" # Trusted X.509 end entity certificates
"d /etc/swanctl/x509ca -" # Trusted X.509 Certificate Authority certificates
"d /etc/swanctl/x509ocsp -"
"d /etc/swanctl/x509aa -" # Trusted X.509 Attribute Authority certificates
"d /etc/swanctl/x509ac -" # Attribute Certificates
"d /etc/swanctl/x509crl -" # Certificate Revocation Lists
"d /etc/swanctl/pubkey -" # Raw public keys
"d /etc/swanctl/private -" # Private keys in any format
"d /etc/swanctl/rsa -" # PKCS#1 encoded RSA private keys
"d /etc/swanctl/ecdsa -" # Plain ECDSA private keys
"d /etc/swanctl/bliss -"
"d /etc/swanctl/pkcs8 -" # PKCS#8 encoded private keys of any type
"d /etc/swanctl/pkcs12 -" # PKCS#12 containers
];

systemd.services.strongswan-swanctl = {
description = "strongSwan IPsec IKEv1/IKEv2 daemon using swanctl";
Expand Down
5 changes: 0 additions & 5 deletions nixos/modules/services/system/nix-daemon.nix
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,6 @@ in

services.xserver.displayManager.hiddenUsers = attrNames nixbldUsers;

system.activationScripts.nix = stringAfter [ "etc" "users" ]
''
install -m 0755 -d /nix/var/nix/{gcroots,profiles}/per-user
'';

# Legacy configuration conversion.
nix.settings = mkMerge [
(mkIf (isNixAtLeast "2.3pre") { sandbox-fallback = false; })
Expand Down
6 changes: 3 additions & 3 deletions nixos/modules/services/web-apps/mattermost.nix
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ in

# The systemd service will fail to execute the preStart hook
# if the WorkingDirectory does not exist
system.activationScripts.mattermost = ''
mkdir -p "${cfg.statePath}"
'';
systemd.tmpfiles.rules = [
''d "${cfg.statePath}" -''
];

systemd.services.mattermost = {
description = "Mattermost chat service";
Expand Down
8 changes: 3 additions & 5 deletions nixos/modules/services/web-servers/stargazer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,9 @@ in
};

# Create default cert store
system.activationScripts.makeStargazerCertDir =
lib.optionalAttrs (cfg.store == /var/lib/gemini/certs) ''
mkdir -p /var/lib/gemini/certs
chown -R ${cfg.user}:${cfg.group} /var/lib/gemini/certs
'';
systemd.tmpfiles.rules = lib.mkIf (cfg.store == /var/lib/gemini/certs) [
''d /var/lib/gemini/certs - "${cfg.user}" "${cfg.group}" -''
];

users.users = lib.optionalAttrs (cfg.user == "stargazer") {
stargazer = {
Expand Down
48 changes: 9 additions & 39 deletions nixos/modules/system/activation/activation-script.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ let
# used as a garbage collection root.
ln -sfn "$(readlink -f "$systemConfig")" /run/current-system
# Prevent the current configuration from being garbage-collected.
mkdir -p /nix/var/nix/gcroots
ln -sfn /run/current-system /nix/var/nix/gcroots/current-system
exit $_status
'';

Expand Down Expand Up @@ -233,23 +229,16 @@ in
config = {

system.activationScripts.stdio = ""; # obsolete
system.activationScripts.var = ""; # obsolete
system.activationScripts.specialfs = ""; # obsolete

system.activationScripts.var =
''
# Various log/runtime directories.
mkdir -p /var/tmp
chmod 1777 /var/tmp
# Empty, immutable home directory of many system accounts.
mkdir -p /var/empty
# Make sure it's really empty
${pkgs.e2fsprogs}/bin/chattr -f -i /var/empty || true
find /var/empty -mindepth 1 -delete
chmod 0555 /var/empty
chown root:root /var/empty
${pkgs.e2fsprogs}/bin/chattr -f +i /var/empty || true
'';
systemd.tmpfiles.rules = [
# Prevent the current configuration from being garbage-collected.
"d /nix/var/nix/gcroots -"
"L+ /nix/var/nix/gcroots/current-system - - - - /run/current-system"
"D /var/empty 0555 root root -"
"h /var/empty - - - - +i"
];

system.activationScripts.usrbinenv = if config.environment.usrbinenv != null
then ''
Expand All @@ -263,25 +252,6 @@ in
rmdir --ignore-fail-on-non-empty /usr/bin /usr
'';

system.activationScripts.specialfs =
''
specialMount() {
local device="$1"
local mountPoint="$2"
local options="$3"
local fsType="$4"
if mountpoint -q "$mountPoint"; then
local options="remount,$options"
else
mkdir -p "$mountPoint"
chmod 0755 "$mountPoint"
fi
mount -t "$fsType" -o "$options" "$device" "$mountPoint"
}
source ${config.system.build.earlyMountScript}
'';

systemd.user = {
services.nixos-activation = {
description = "Run user-specific NixOS activation";
Expand Down
Loading

0 comments on commit 8670794

Please sign in to comment.