summaryrefslogtreecommitdiffstats
path: root/modules/system/default.nix
blob: 4592d92a8f55983cf51294d882e922d427c81a3b (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
{ config, lib, pkgs, ... }:

with lib;

let

  inherit (pkgs) stdenvNoCC;

  cfg = config.system;

  failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);

  throwAssertions = res: if (failedAssertions != []) then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}" else res;
  showWarnings = res: foldr (w: x: builtins.trace "warning: ${w}" x) res config.warnings;

in

{
  options = {

    system.build = mkOption {
      internal = true;
      type = types.lazyAttrsOf types.unspecified;
      default = {};
      description = ''
        Attribute set of derivation used to setup the system.
      '';
    };

    system.profile = mkOption {
      type = types.path;
      default = "/nix/var/nix/profiles/system";
      description = ''
        Profile to use for the system.
      '';
    };

    system.systemBuilderCommands = mkOption {
      internal = true;
      type = types.lines;
      default = "";
      description = ''
        This code will be added to the builder creating the system store path.
      '';
    };

    system.systemBuilderArgs = mkOption {
      internal = true;
      type = types.attrsOf types.unspecified;
      default = {};
      description = ''
        `lib.mkDerivation` attributes that will be passed to the top level system builder.
      '';
    };

    assertions = mkOption {
      type = types.listOf types.unspecified;
      internal = true;
      default = [];
      example = [ { assertion = false; message = "you can't enable this for that reason"; } ];
      description = ''
        This option allows modules to express conditions that must
        hold for the evaluation of the system configuration to
        succeed, along with associated error messages for the user.
      '';
    };

    warnings = mkOption {
      internal = true;
      default = [];
      type = types.listOf types.str;
      example = [ "The `foo' service is deprecated and will go away soon!" ];
      description = ''
        This option allows modules to show warnings to users during
        the evaluation of the system configuration.
      '';
    };

  };

  config = {

    system.build.toplevel = throwAssertions (showWarnings (stdenvNoCC.mkDerivation ({
      name = "darwin-system-${cfg.darwinLabel}";
      preferLocalBuild = true;

      nativeBuildInputs = [ pkgs.shellcheck ];

      activationScript = cfg.activationScripts.script.text;

      # This is for compatibility with older `darwin-rebuild`s and
      # third‐party deployment tools.
      #
      # TODO: Remove this in 25.11.
      activationUserScript = ''
        #! ${pkgs.stdenv.shell}
        # nix-darwin: deprecated

        # Hack to handle upgrades.
        if
          [[ -e /run/current-system/activate-user ]] \
          && ! grep -q '^# nix-darwin: deprecated$' \
            /run/current-system/activate-user
        then
          exit
        fi

        printf >&2 '\e[1;31mwarning: `activate-user` is deprecated and will be removed in 25.11\e[0m\n'
        printf >&2 'This is usually due to the use of a non‐standard activation/deployment\n'
        printf >&2 'tool. If you maintain one of these tools, our advice is:\n'
        printf >&2 '\n'
        printf >&2 '  You can identify a post‐user‐activation configuration by the absence\n'
        printf >&2 '  of `activate-user` or the second line of the script being\n'
        printf >&2 '  `# nix-darwin: deprecated`.\n'
        printf >&2 '\n'
        printf >&2 '  We recommend running `$systemConfig/sw/bin/darwin-rebuild activate`\n'
        printf >&2 '  to activate built configurations; for a pre‐user‐activation\n'
        printf >&2 '  configuration this should be run as a normal user, and for a\n'
        printf >&2 '  post‐user‐activation configuration it should be run as `root`.\n'
        printf >&2 '\n'
        printf >&2 '  If you can’t or don’t want to use `darwin-rebuild activate`, then you\n'
        printf >&2 '  should skip running `activate-user` for post‐user‐activation\n'
        printf >&2 '  configurations and continue running `activate` as `root`.\n'
        printf >&2 '\n'
        printf >&2 '  In 25.11, `darwin-rebuild` will stop running `activate-user` and this\n'
        printf >&2 '  transition script will be deleted; you should be able to safely\n'
        printf >&2 '  remove all related logic by then.\n'
        printf >&2 '\n'
        printf >&2 'Otherwise, you should report this to the deployment tool developers. If\n'
        printf >&2 'you don’t use a third‐party deployment tool, please open a bug report\n'
        printf >&2 'at <https://github.com/nix-darwin/nix-darwin/issues/new> and include as much\n'
        printf >&2 'detail about your setup as possible.\n'
      '';

      inherit (cfg) darwinLabel;

      darwinVersionJson = (pkgs.formats.json {}).generate "darwin-version.json" (
        filterAttrs (k: v: v != null) {
          inherit (config.system) darwinRevision nixpkgsRevision configurationRevision darwinLabel;
        }
      );

      buildCommand = ''
        mkdir $out

        systemConfig=$out

        mkdir -p $out/darwin
        cp -f ${../../CHANGELOG} $out/darwin-changes

        ln -s ${cfg.build.patches}/patches $out/patches
        ln -s ${cfg.build.etc}/etc $out/etc
        ln -s ${cfg.path} $out/sw

        mkdir -p $out/Library
        ln -s ${cfg.build.applications}/Applications $out/Applications
        ln -s ${cfg.build.fonts}/Library/Fonts $out/Library/Fonts
        ln -s ${cfg.build.launchd}/Library/LaunchAgents $out/Library/LaunchAgents
        ln -s ${cfg.build.launchd}/Library/LaunchDaemons $out/Library/LaunchDaemons

        mkdir -p $out/user/Library
        ln -s ${cfg.build.launchd}/user/Library/LaunchAgents $out/user/Library/LaunchAgents

        echo "$activationScript" > $out/activate
        substituteInPlace $out/activate --subst-var out
        chmod u+x $out/activate
        unset activationScript

        echo "$activationUserScript" > $out/activate-user
        chmod u+x $out/activate-user
        unset activationUserScript

        # We exclude the warnings for `…` in single‐quote strings and
        # non‐ASCII quotation marks as they are noisy and lead to a lot
        # of false positives in our user‐facing output:
        shellcheck --exclude=SC2016,SC1112 $out/activate $out/activate-user

        echo -n "$systemConfig" > $out/systemConfig

        echo -n "$darwinLabel" > $out/darwin-version
        ln -s $darwinVersionJson $out/darwin-version.json
        echo -n "$system" > $out/system

        ${cfg.systemBuilderCommands}
      '';
    } // cfg.systemBuilderArgs)));

  };
}