Skip to content

Commit

Permalink
Merge pull request #1332 from emilazy/push-vyxwvkuuvvvw
Browse files Browse the repository at this point in the history
treewide: add checks for `nix.enable`
  • Loading branch information
emilazy authored Feb 14, 2025
2 parents 7206892 + 731910a commit 678b226
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 34 deletions.
43 changes: 26 additions & 17 deletions modules/nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ let
})
];

managedDefault = name: default: {
default = if cfg.enable then default else throw ''
${name}: accessed when `nix.enable` is off; this is a bug in
nix-darwin or a third‐party module
'';
defaultText = default;
};

in

{
Expand Down Expand Up @@ -224,9 +232,7 @@ in

package = mkOption {
type = types.package;
default = warnIf (!cfg.enable)
"nix.package: accessed when `nix.enable` is off; this is a bug"
pkgs.nix;
inherit (managedDefault "nix.package" pkgs.nix) default;
defaultText = literalExpression "pkgs.nix";
description = ''
This option specifies the Nix package instance to use throughout the system.
Expand All @@ -235,7 +241,7 @@ in

distributedBuilds = mkOption {
type = types.bool;
default = false;
inherit (managedDefault "nix.distributedBuilds" false) default defaultText;
description = ''
Whether to distribute builds to the machines listed in
{option}`nix.buildMachines`.
Expand All @@ -245,7 +251,7 @@ in
# Not in NixOS module
daemonProcessType = mkOption {
type = types.enum [ "Background" "Standard" "Adaptive" "Interactive" ];
default = "Standard";
inherit (managedDefault "nix.daemonProcessType" "Standard") default defaultText;
description = ''
Nix daemon process resource limits class. These limits propagate to
build processes. `Standard` is the default process type
Expand All @@ -260,7 +266,7 @@ in
# Not in NixOS module
daemonIOLowPriority = mkOption {
type = types.bool;
default = false;
inherit (managedDefault "nix.daemonIOLowPriority" false) default defaultText;
description = ''
Whether the Nix daemon process should considered to be low priority when
doing file system I/O.
Expand Down Expand Up @@ -388,7 +394,7 @@ in
};
};
});
default = [ ];
inherit (managedDefault "nix.buildMachines" [ ]) default defaultText;
description = ''
This option lists the machines to be used if distributed builds are
enabled (see {option}`nix.distributedBuilds`).
Expand All @@ -402,12 +408,13 @@ in
envVars = mkOption {
type = types.attrs;
internal = true;
default = { };
inherit (managedDefault "nix.envVars" { }) default defaultText;
description = "Environment variables used by Nix.";
};

nrBuildUsers = mkOption {
type = types.int;
inherit (managedDefault "nix.nrBuildUsers" 0) default defaultText;
description = ''
Number of `nixbld` user accounts created to
perform secure concurrent builds. If you receive an error
Expand Down Expand Up @@ -435,11 +442,13 @@ in
# Definition differs substantially from NixOS module
nixPath = mkOption {
type = nixPathType;
default = lib.optionals cfg.channel.enable [
# Include default path <darwin-config>.
{ darwin-config = "${config.environment.darwinConfig}"; }
"/nix/var/nix/profiles/per-user/root/channels"
];
inherit (managedDefault "nix.nixPath" (
lib.optionals cfg.channel.enable [
# Include default path <darwin-config>.
{ darwin-config = "${config.environment.darwinConfig}"; }
"/nix/var/nix/profiles/per-user/root/channels"
]
)) default;

defaultText = lib.literalExpression ''
lib.optionals cfg.channel.enable [
Expand All @@ -461,7 +470,7 @@ in

checkConfig = mkOption {
type = types.bool;
default = true;
inherit (managedDefault "nix.checkConfig" true) default defaultText;
description = ''
If enabled (the default), checks for data type mismatches and that Nix
can parse the generated nix.conf.
Expand Down Expand Up @@ -522,15 +531,15 @@ in
};
}
));
default = { };
inherit (managedDefault "nix.registry" { }) default defaultText;
description = ''
A system-wide flake registry.
'';
};

extraOptions = mkOption {
type = types.lines;
default = "";
inherit (managedDefault "nix.extraOptions" "") default defaultText;
example = ''
keep-outputs = true
keep-derivations = true
Expand Down Expand Up @@ -699,7 +708,7 @@ in
};
};
};
default = { };
inherit (managedDefault "nix.settings" { }) default defaultText;
description = ''
Configuration for Nix, see
<https://nixos.org/manual/nix/stable/#sec-conf-file>
Expand Down
7 changes: 7 additions & 0 deletions modules/nix/linux-builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ in
};

config = mkIf cfg.enable {
assertions = [
{
assertion = config.nix.enable;
message = ''`nix.linux-builder.enable` requires `nix.enable`'';
}
];

system.activationScripts.preActivation.text = ''
mkdir -p ${cfg.workingDirectory}
'';
Expand Down
2 changes: 1 addition & 1 deletion modules/nix/nix-darwin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let
nix-tools = pkgs.callPackage ../../pkgs/nix-tools {
inherit (config.system) profile;
inherit (config.environment) systemPath;
nixPath = lib.concatStringsSep ":" config.nix.nixPath;
nixPath = lib.optionalString config.nix.enable (lib.concatStringsSep ":" config.nix.nixPath);
};

darwin-uninstaller = pkgs.callPackage ../../pkgs/darwin-uninstaller { };
Expand Down
20 changes: 16 additions & 4 deletions modules/nix/nixpkgs-flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ in
setNixPath = mkOption {
type = types.bool;

default = cfg.source != null;
defaultText = "config.nixpkgs.flake.source != null";
default = config.nix.enable && cfg.source != null;
defaultText = literalExpression ''config.nix.enable && nixpkgs.flake.source != null'';

description = ''
Whether to set {env}`NIX_PATH` to include `nixpkgs=flake:nixpkgs` such that `<nixpkgs>`
Expand All @@ -57,8 +57,8 @@ in
setFlakeRegistry = mkOption {
type = types.bool;

default = cfg.source != null;
defaultText = "config.nixpkgs.flake.source != null";
default = config.nix.enable && cfg.source != null;
defaultText = literalExpression ''config.nix.enable && config.nixpkgs.flake.source != null'';

description = ''
Whether to pin nixpkgs in the system-wide flake registry (`/etc/nix/registry.json`) to the
Expand All @@ -85,6 +85,18 @@ in
be set, since it is implemented in terms of indirection through the flake registry.
'';
}

# TODO: Upstream these to NixOS.

{
assertion = cfg.setNixPath -> config.nix.enable;
message = ''`nixpkgs.flake.setNixPath` requires `nix.enable`'';
}

{
assertion = cfg.setFlakeRegistry -> config.nix.enable;
message = ''`nixpkgs.flake.setFlakeRegistry` requires `nix.enable`'';
}
];
}
(mkIf cfg.setFlakeRegistry {
Expand Down
4 changes: 3 additions & 1 deletion modules/services/activate-system/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
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
if [[ -d /nix/var/nix/gcroots ]]; then
ln -sfn /run/current-system /nix/var/nix/gcroots/current-system
fi
${config.system.activationScripts.etcChecks.text}
${config.system.activationScripts.etc.text}
Expand Down
8 changes: 8 additions & 0 deletions modules/services/cachix-agent.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ in {
};

config = mkIf cfg.enable {
# TODO: Upstream this to NixOS.
assertions = [
{
assertion = config.nix.enable;
message = ''`services.cachix-agent.enable` requires `nix.enable`'';
}
];

launchd.daemons.cachix-agent = {
script = ''
. ${cfg.credentialsFile}
Expand Down
5 changes: 5 additions & 0 deletions modules/services/github-runner/service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ in
{
config.assertions = flatten (
flip mapAttrsToList config.services.github-runners (name: cfg: map (mkIf cfg.enable) [
# TODO: Upstream this to NixOS.
{
assertion = config.nix.enable;
message = ''`services.github-runners.${name}.enable` requires `nix.enable`'';
}
{
assertion = (cfg.user == null && cfg.group == null) || (cfg.user != null);
message = "`services.github-runners.${name}`: Either set `user` and `group` to `null` to have nix-darwin manage them or set at least `user` explicitly";
Expand Down
8 changes: 8 additions & 0 deletions modules/services/hercules-ci-agent/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ in
};

config = mkIf cfg.enable {
# TODO: Upstream this to NixOS.
assertions = [
{
assertion = config.nix.enable;
message = ''`services.hercules-ci-agent.enable` requires `nix.enable`'';
}
];

launchd.daemons.hercules-ci-agent = {
script = "exec ${cfg.package}/bin/hercules-ci-agent --config ${cfg.tomlFile}";

Expand Down
10 changes: 9 additions & 1 deletion modules/services/lorri.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ in
};

config = mkIf cfg.enable {
# TODO: Upstream this to NixOS.
assertions = [
{
assertion = config.nix.enable;
message = ''`services.lorri.enable` requires `nix.enable`'';
}
];

environment.systemPackages = [ pkgs.lorri ];
launchd.user.agents.lorri = {
command = with pkgs; "${lorri}/bin/lorri daemon";
Expand All @@ -43,4 +51,4 @@ in
};
};
};
}
}
13 changes: 9 additions & 4 deletions modules/services/nix-gc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ in

###### implementation

config = mkIf cfg.automatic {

launchd.daemons.nix-gc = {
config = {
assertions = [
{
assertion = cfg.automatic -> config.nix.enable;
message = ''nix.gc.automatic requires nix.enable'';
}
];

launchd.daemons.nix-gc = mkIf cfg.automatic {
command = "${config.nix.package}/bin/nix-collect-garbage ${cfg.options}";
serviceConfig.RunAtLoad = false;
serviceConfig.StartCalendarInterval = cfg.interval;
};

};
}
13 changes: 9 additions & 4 deletions modules/services/nix-optimise/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ in

###### implementation

config = mkIf cfg.automatic {

launchd.daemons.nix-optimise = {
config = {
assertions = [
{
assertion = cfg.automatic -> config.nix.enable;
message = ''nix.optimise.automatic requires nix.enable'';
}
];

launchd.daemons.nix-optimise = mkIf cfg.automatic {
command = "${lib.getExe' config.nix.package "nix-store"} --optimise";
serviceConfig = {
RunAtLoad = false;
StartCalendarInterval = cfg.interval;
};
};

};
}
7 changes: 7 additions & 0 deletions modules/services/ofborg/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ in
};

config = mkIf cfg.enable {
assertions = [
{
assertion = config.nix.enable;
message = ''`services.ofborg.enable` requires `nix.enable`'';
}
];

warnings = mkIf (isDerivation cfg.configFile) [
"services.ofborg.configFile is a derivation, credentials will be world readable"
];
Expand Down
4 changes: 3 additions & 1 deletion modules/system/activation-scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ in
ln -sfn "$(readlink -f "$systemConfig")" /run/current-system
# Prevent the current configuration from being garbage-collected.
ln -sfn /run/current-system /nix/var/nix/gcroots/current-system
if [[ -d /nix/var/nix/gcroots ]]; then
ln -sfn /run/current-system /nix/var/nix/gcroots/current-system
fi
'';

# FIXME: activationScripts.checks should be system level
Expand Down
1 change: 0 additions & 1 deletion tests/nix-enable.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

{
nix.enable = false;
nix.package = throw "`nix.package` used when `nix.enable` is turned off";

test = ''
printf >&2 'checking for unexpected Nix binary in /sw/bin\n'
Expand Down

0 comments on commit 678b226

Please sign in to comment.