1{ pkgs, ... }:
2{
3 name = "restart-by-activation-script";
4 meta = with pkgs.lib.maintainers; {
5 maintainers = [ das_j ];
6 };
7
8 nodes.machine =
9 { pkgs, ... }:
10 {
11 imports = [ ../modules/profiles/minimal.nix ];
12
13 system.switch.enable = true;
14
15 systemd.services.restart-me = {
16 wantedBy = [ "multi-user.target" ];
17 serviceConfig = {
18 Type = "oneshot";
19 RemainAfterExit = true;
20 ExecStart = "${pkgs.coreutils}/bin/true";
21 };
22 };
23
24 systemd.services.reload-me = {
25 wantedBy = [ "multi-user.target" ];
26 serviceConfig = rec {
27 Type = "oneshot";
28 RemainAfterExit = true;
29 ExecStart = "${pkgs.coreutils}/bin/true";
30 ExecReload = ExecStart;
31 };
32 };
33
34 system.activationScripts.test = {
35 supportsDryActivation = true;
36 text = ''
37 if [ -e /test-the-activation-script ]; then
38 if [ "$NIXOS_ACTION" != dry-activate ]; then
39 touch /activation-was-run
40 echo restart-me.service > /run/nixos/activation-restart-list
41 echo reload-me.service > /run/nixos/activation-reload-list
42 else
43 echo restart-me.service > /run/nixos/dry-activation-restart-list
44 echo reload-me.service > /run/nixos/dry-activation-reload-list
45 fi
46 fi
47 '';
48 };
49 };
50
51 testScript = # python
52 ''
53 machine.wait_for_unit("multi-user.target")
54
55 with subtest("nothing happens when the activation script does nothing"):
56 out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate 2>&1")
57 assert 'restart' not in out
58 assert 'reload' not in out
59 out = machine.succeed("/run/current-system/bin/switch-to-configuration test")
60 assert 'restart' not in out
61 assert 'reload' not in out
62
63 machine.succeed("touch /test-the-activation-script")
64
65 with subtest("dry activation"):
66 out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate 2>&1")
67 assert 'would restart the following units: restart-me.service' in out
68 assert 'would reload the following units: reload-me.service' in out
69 machine.fail("test -f /run/nixos/dry-activation-restart-list")
70 machine.fail("test -f /run/nixos/dry-activation-reload-list")
71
72 with subtest("real activation"):
73 out = machine.succeed("/run/current-system/bin/switch-to-configuration test 2>&1")
74 assert 'restarting the following units: restart-me.service' in out
75 assert 'reloading the following units: reload-me.service' in out
76 machine.fail("test -f /run/nixos/activation-restart-list")
77 machine.fail("test -f /run/nixos/activation-reload-list")
78 '';
79}