-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixos: Add system.activatable flag for images that are pre-activated
- Loading branch information
Showing
3 changed files
with
132 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,92 @@ | ||
/* | ||
This module adds the activation script to toplevel, so that any previously | ||
built configuration can be activated again, as long as they're available in | ||
the store, e.g. through the profile's older generations. | ||
Alternate applications of the NixOS modules may omit this module, e.g. to | ||
build images that are pre-activated and omit the activation script and its | ||
dependencies. | ||
*/ | ||
{ config, lib, pkgs, ... }: | ||
|
||
let | ||
inherit (lib) | ||
mkOption | ||
optionalString | ||
types | ||
; | ||
|
||
perlWrapped = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]); | ||
|
||
systemBuilderArgs = { | ||
activationScript = config.system.activationScripts.script; | ||
dryActivationScript = config.system.dryActivationScript; | ||
}; | ||
|
||
systemBuilderCommands = '' | ||
echo "$activationScript" > $out/activate | ||
echo "$dryActivationScript" > $out/dry-activate | ||
substituteInPlace $out/activate --subst-var-by out ''${!toplevelVar} | ||
substituteInPlace $out/dry-activate --subst-var-by out ''${!toplevelVar} | ||
chmod u+x $out/activate $out/dry-activate | ||
unset activationScript dryActivationScript | ||
mkdir $out/bin | ||
substitute ${./switch-to-configuration.pl} $out/bin/switch-to-configuration \ | ||
--subst-var out \ | ||
--subst-var-by toplevel ''${!toplevelVar} \ | ||
--subst-var-by coreutils "${pkgs.coreutils}" \ | ||
--subst-var-by distroId ${lib.escapeShellArg config.system.nixos.distroId} \ | ||
--subst-var-by installBootLoader ${lib.escapeShellArg config.system.build.installBootLoader} \ | ||
--subst-var-by localeArchive "${config.i18n.glibcLocales}/lib/locale/locale-archive" \ | ||
--subst-var-by perl "${perlWrapped}" \ | ||
--subst-var-by shell "${pkgs.bash}/bin/sh" \ | ||
--subst-var-by su "${pkgs.shadow.su}/bin/su" \ | ||
--subst-var-by systemd "${config.systemd.package}" \ | ||
--subst-var-by utillinux "${pkgs.util-linux}" \ | ||
; | ||
chmod +x $out/bin/switch-to-configuration | ||
${optionalString (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) '' | ||
if ! output=$(${perlWrapped}/bin/perl -c $out/bin/switch-to-configuration 2>&1); then | ||
echo "switch-to-configuration syntax is not valid:" | ||
echo "$output" | ||
exit 1 | ||
fi | ||
''} | ||
''; | ||
|
||
in | ||
{ | ||
config = { | ||
system.systemBuilderArgs = { | ||
activationScript = config.system.activationScripts.script; | ||
dryActivationScript = config.system.dryActivationScript; | ||
options = { | ||
system.activatable = mkOption { | ||
type = types.bool; | ||
default = true; | ||
description = '' | ||
Whether to add the activation script to the system profile. | ||
The default, to have the script available all the time, is what we normally | ||
do, but for image based systems, this may not be needed or not be desirable. | ||
''; | ||
}; | ||
system.build.separateActivationScript = mkOption { | ||
type = types.package; | ||
description = '' | ||
A separate activation script package that's not part of the system profile. | ||
This is useful for configurations where `system.activatable` is `false`. | ||
Otherwise, you can just use `system.build.toplevel`. | ||
''; | ||
}; | ||
}; | ||
config = { | ||
system.systemBuilderCommands = lib.mkIf config.system.activatable systemBuilderCommands; | ||
system.systemBuilderArgs = lib.mkIf config.system.activatable | ||
(systemBuilderArgs // { | ||
toplevelVar = "out"; | ||
}); | ||
|
||
system.systemBuilderCommands = '' | ||
echo "$activationScript" > $out/activate | ||
echo "$dryActivationScript" > $out/dry-activate | ||
substituteInPlace $out/activate --subst-var out | ||
substituteInPlace $out/dry-activate --subst-var out | ||
chmod u+x $out/activate $out/dry-activate | ||
unset activationScript dryActivationScript | ||
mkdir $out/bin | ||
substitute ${./switch-to-configuration.pl} $out/bin/switch-to-configuration \ | ||
--subst-var out \ | ||
--subst-var-by coreutils "${pkgs.coreutils}" \ | ||
--subst-var-by distroId ${lib.escapeShellArg config.system.nixos.distroId} \ | ||
--subst-var-by installBootLoader ${lib.escapeShellArg config.system.build.installBootLoader} \ | ||
--subst-var-by localeArchive "${config.i18n.glibcLocales}/lib/locale/locale-archive" \ | ||
--subst-var-by perl "${perlWrapped}" \ | ||
--subst-var-by shell "${pkgs.bash}/bin/sh" \ | ||
--subst-var-by su "${pkgs.shadow.su}/bin/su" \ | ||
--subst-var-by systemd "${config.systemd.package}"\ | ||
--subst-var-by utillinux "${pkgs.util-linux}" \ | ||
; | ||
chmod +x $out/bin/switch-to-configuration | ||
${optionalString (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) '' | ||
if ! output=$(${perlWrapped}/bin/perl -c $out/bin/switch-to-configuration 2>&1); then | ||
echo "switch-to-configuration syntax is not valid:" | ||
echo "$output" | ||
exit 1 | ||
fi | ||
''} | ||
''; | ||
system.build.separateActivationScript = | ||
pkgs.runCommand | ||
"separate-activation-script" | ||
(systemBuilderArgs // { | ||
toplevelVar = "toplevel"; | ||
toplevel = config.system.build.toplevel; | ||
}) | ||
'' | ||
mkdir $out | ||
${systemBuilderCommands} | ||
''; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters