But are there any other considerations? Reading between lines in other posts, it seems that mkIf is prefered when it comes to NixOS configuration. But I’m not sure why…
The implicit else branch of lib.mkIf is the least important difference between that and if-then-else construct. You could achieve the same with lib.optionalAttrs, which has the same interface:
Rather, the main difference is that mkIf produces an attribute set that is handled specially by the NixOS module system and keeps the contents available:
This will allow the module system to remain aware of the contents even when the condition is false and it can validate them.
Just note that mkIf produces a special attribute set so the result will need to be merged with lib.mkMerge – you can no longer just use standard Nix operators:
config = {
environment.systemPackages = [
pkgs.ripgrep
# ⬐ THIS IS WRONG!
] ++ mkIf config.my.custom.opt.installvim [
pkgs.vim
];
}
The issue described in this section can come up in surprising ways sometimes, so it’s best to default to mkIf whenever possible (though it’d be cool if we had a mkIfElse function that took a second argument for the else case)