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