summary refs log tree commit diff
path: root/nixos/modules/system/activation/activatable-system.nix
blob: b179fce417e7048870b53b1511445b94b29d296b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
  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)
    optionalString
    ;

  perlWrapped = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]);

in
{
  config = {
    system.systemBuilderArgs = {
      activationScript = config.system.activationScripts.script;
      dryActivationScript = config.system.dryActivationScript;
    };

    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
      ''}
    '';
  };
}