I have a mkIf to enable / disable some configuration in home-manager.
{ options, config, systemConfig, inputs, lib, pkgs, ... }:
with lib;
let
cfg = systemConfig.my_modules.desktopEnvironment;
condition = (cfg == "hyprland");
in
{
config = mkIf condition {
services.udiskie = trace "Hyprland home module loaded: ${boolToString condition}" {
enable = true;
};
};
}
I cannot understand how this can trace out
trace: Hyprland home module loaded: false
when the module is loaded. In my mind, the code within the attrset after the mkIf should not get evaluated if the condition was false.
Stzx
June 4, 2023, 10:59am
2
Since mkIf
is not actually implemented using if
, this behavior is justified.
These should help you:
https://nixos.org/manual/nixos/stable/#sec-option-definitions-delaying-conditionals
Hi folks,
It’s obvious to me that mkIf makes more sense when I don’t need else:
config = {
environment.systemPackages =
mkIf config.my.custom.opt.installvim
[ pkgs.vim ];
}
and if / then makes more sense when I do need else:
config = {
services.resolved.dnssec =
if config.my.custom.opt.dnssecforced
then "true"
else "allow-downgrade";
}
or
config = {
services.resolved =
if config.my.custom.opt.resolved
then {
enable = true;
llmnr = "false";
…
I think I understand now, but it took a while . But does that mean that if I lock some configuration behind a mkIf, the entire configuration in the mkIf will still be evaluated even if it never ends up being used? So it could be considered that there is some amount of unnecessary compute going on?