summaryrefslogtreecommitdiff
path: root/modules/services/activate-system.nix
blob: 5ee7cfb5964dcaf1b8cffd03cf7502b95f8107c0 (plain)
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
{ config, lib, pkgs, ... }:

with lib;

let

  inherit (pkgs) stdenv;

  cfg = config.services.activate-system;

  activateScript = pkgs.writeScript "activate-system" ''
    #! ${stdenv.shell}

    # 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 $(cat ${config.system.profile}/systemConfig) /run/current-system

    # Prevent the current configuration from being garbage-collected.
    ln -sfn /run/current-system /nix/var/nix/gcroots/current-system

    ${config.system.activationScripts.nix.text}
  '';

in

{
  options = {
    services.activate-system = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to activate system at boot time.
        '';
      };

    };
  };

  config = mkIf cfg.enable {

    launchd.daemons.activate-system = {
      command = activateScript;
      serviceConfig.RunAtLoad = true;
      serviceConfig.KeepAlive.SuccessfulExit = false;
    };

  };
}