8.nix-darwin/modules/system/default.nix
Robert Hensing acf6b46011 system.build: Treat as variables, make lazy
This fixes an unnecessary evaluation dependency that prevented the
custom and much appreciated primaryUser error from popping up.

Specifically:

       … while evaluating the option `system.build':

       … while evaluating definitions from `/nix/store/lc6n4bhxj9255kzfn9pnpx65583a8cgc-source/modules/environment':

       … while evaluating definitions from `/nix/store/lc6n4bhxj9255kzfn9pnpx65583a8cgc-source/modules/nix':

       … while evaluating the option `environment.darwinConfig':

       … while evaluating the option `system.primaryUserHome':

       error: expected a string but found null: null
       at /nix/store/lc6n4bhxj9255kzfn9pnpx65583a8cgc-source/modules/system/primary-user.nix:26:30:
           25|       default =
           26|         config.users.users.${config.system.primaryUser}.home or "/Users/${config.system.primaryUser}";
             |                              ^
           27|     };

While it did have some indication as to the cause, it lets the good
error message go to waste.

**Context**

`lazyAttrsOf` is the better choice when you use an attrset as individual
variables instead of in aggregate (e.g. `attrNames`, `toJSON`).

The reason is that an expression like `a.b` is strict in `a`, which
entails the evaluating the _whole_ set of attribute _names_ in `a`.
In the `attrsOf` this means evaluating all `mkIf` conditions, which
in turn also means evaluating all the regular definitions to the
smallest degree (WHNF) to determine that they're not `mkIf`s.

`lazyAttrsOf` simply assumes that all attributes aren't `mkIf false`,
and throws an error in the attribute value if necessary.
This would be a problem with `toJSON` and such, but is completely
fine when the attributes are treated as variables of a lazy program,
as is the case here.

**NixOS**

NixOS made `system.build` a submodule with a `freeformType`, allowing
the things inside of it to be declared, and for them to have niceties
like documentation and merging behavior.
nix-darwin could probably adopt this.
2025-05-23 12:00:51 +02:00

197 lines
6.8 KiB
Nix
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{ 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: fold (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.path = mkOption {
internal = true;
type = types.package;
description = ''
The packages you want in the system environment.
'';
};
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
# thirdparty 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 nonstandard 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 postuseractivation 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 preuseractivation\n'
printf >&2 ' configuration this should be run as a normal user, and for a\n'
printf >&2 ' postuseractivation configuration it should be run as `root`.\n'
printf >&2 '\n'
printf >&2 ' If you cant or dont want to use `darwin-rebuild activate`, then you\n'
printf >&2 ' should skip running `activate-user` for postuseractivation\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 dont use a thirdparty 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 singlequote strings and
# nonASCII quotation marks as they are noisy and lead to a lot
# of false positives in our userfacing 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)));
};
}