Allow nixDir auto-loads to set a value to null

Previously null was used when a value could not be loaded. This
prevented setting a value to null using a nixDir auto-load.

This change removes the use of null as a special value when loading from
nixDir, allowing it to be used as a normal value.
This commit is contained in:
Archit Gupta 2024-01-15 01:07:55 -08:00
parent 3dd1d890a3
commit a4440382bb

View File

@ -5,23 +5,24 @@
{ config, options, src, lib, flakelight, ... }:
let
inherit (builtins) attrNames;
inherit (lib) findFirst genAttrs isList mkIf mkOption pathExists
subtractLists;
inherit (lib) findFirst genAttrs mkIf mkOption pathExists subtractLists;
inherit (lib.types) attrsOf listOf str;
inherit (flakelight) importDir;
inherit (flakelight.types) path;
autoImport = dir: name:
if isList name
then findFirst (x: x != null) null (map (autoImport dir) name)
else
if pathExists (dir + "/${name}.nix")
then import (dir + "/${name}.nix")
else if pathExists (dir + "/${name}/default.nix")
then import (dir + "/${name}")
else if pathExists (dir + "/${name}")
then importDir (dir + "/${name}")
else null;
inherit (config) nixDir;
importName = name:
if pathExists (nixDir + "/${name}.nix")
then { success = true; value = import (nixDir + "/${name}.nix"); }
else if pathExists (nixDir + "/${name}/default.nix")
then { success = true; value = import (nixDir + "/${name}"); }
else if pathExists (nixDir + "/${name}")
then { success = true; value = importDir (nixDir + "/${name}"); }
else { success = false; };
importNames = names:
findFirst (x: x.success) { success = false; } (map importName names);
in
{
options = {
@ -40,10 +41,10 @@ in
(name:
let
internal = options.${name}.internal or false;
val = autoImport config.nixDir
(if name == "nixDirAliases" then name else
val = importNames
(if name == "nixDirAliases" then [ name ] else
([ name ] ++ config.nixDirAliases.${name} or [ ]));
cond = !internal && (val != null);
cond = !internal && val.success;
in
mkIf cond (if cond then val else { }));
mkIf cond (if cond then val.value else { }));
}