Add aliases for autoloading root module attrs

This commit is contained in:
Archit Gupta 2023-04-22 11:44:44 -07:00
parent f9967894bb
commit f4f7a8cd37

View File

@ -7,9 +7,9 @@ let
inherit (builtins) intersectAttrs isPath readDir; inherit (builtins) intersectAttrs isPath readDir;
inherit (localInputs.nixpkgs.lib) attrNames attrVals attrValues inherit (localInputs.nixpkgs.lib) attrNames attrVals attrValues
callPackageWith composeManyExtensions concat concatStringsSep filter callPackageWith composeManyExtensions concat concatStringsSep filter
filterAttrs foldAttrs foldl functionArgs genAttrs hasSuffix isFunction filterAttrs findFirst foldAttrs foldl functionArgs genAttrs hasSuffix
isList isString listToAttrs mapAttrs mapAttrsToList mapAttrs' mergeAttrs isFunction isList isString listToAttrs mapAttrs mapAttrsToList mapAttrs'
nameValuePair optional optionalAttrs parseDrvName pathExists pipe mergeAttrs nameValuePair optional optionalAttrs parseDrvName pathExists pipe
recursiveUpdate removePrefix removeSuffix zipAttrsWith; recursiveUpdate removePrefix removeSuffix zipAttrsWith;
/* Attributes in flakelite's lib output. /* Attributes in flakelite's lib output.
@ -74,20 +74,23 @@ let
(p: import (path + (if pathExists (p: import (path + (if pathExists
(path + "/_${p}.nix") then "/_${p}.nix" else "/${p}.nix"))); (path + "/_${p}.nix") then "/_${p}.nix" else "/${p}.nix")));
/* Try to load an attr from a directory. If a nix file by that name exits, /* Try to load a name from a directory. If a nix file by that name exits,
import it. If an importable directory with that name exists, import it. import it. If an importable directory with that name exists, import it.
Else, if a non-importable directory with that name exists, load the nix Else, if a non-importable directory with that name exists, load the nix
files in that dir as an attrset. Returns null if the attr could not be files in that dir as an attrset. Returns null if the name could not be
loaded. loaded. If name is a list, tries all names in that list.
*/ */
autoImport = dir: attr: autoImport = dir: name:
if pathExists (dir + "/${attr}.nix") if isList name
then import (dir + "/${attr}.nix") then findFirst (x: x != null) null (map (autoImport dir) name)
else if pathExists (dir + "/${attr}/default.nix") else
then import (dir + "/${attr}") if pathExists (dir + "/${name}.nix")
else if pathExists (dir + "/${attr}") then import (dir + "/${name}.nix")
then importDir (dir + "/${attr}") else if pathExists (dir + "/${name}/default.nix")
else null; then import (dir + "/${name}")
else if pathExists (dir + "/${name}")
then importDir (dir + "/${name}")
else null;
/* List of attrs that can be provided by a module. /* List of attrs that can be provided by a module.
*/ */
@ -128,10 +131,21 @@ let
"nixDir" "nixDir"
]; ];
/* Generate an attrset by importing attrs from dir. Filters null values. /* Alternative names for autoloading root attrs.
*/ */
autoImportAttrs = dir: attrs: attrAliases = {
filterAttrs (_: v: v != null) (genAttrs attrs (autoImport dir)); "nixosConfigurations" = [ "nixos" ];
"homeConfigurations" = [ "home" ];
# Lets `nix-shell` shell.nix be used automatically if no ./nix dir.
"devShell" = [ "shell" ];
};
/* Generate an attrset by importing attrs from dir. Filters null values.
Alternative names from aliases are used.
*/
autoImportAttrs = dir: attrs: aliases:
filterAttrs (_: v: v != null) (genAttrs attrs
(attr: autoImport dir ([ attr ] ++ aliases.${attr} or [ ])));
/* Makes the parameter callable, if it isn't. This allows values that are not /* Makes the parameter callable, if it isn't. This allows values that are not
always functions to be applied to parameters. always functions to be applied to parameters.
@ -353,9 +367,10 @@ let
# Root module with autoloads, normalization, and additional attrs. # Root module with autoloads, normalization, and additional attrs.
root' = root' =
let let
resolvedRoot = applyNonSysArgs root; appliedRoot = applyNonSysArgs root;
nixDir = resolvedRoot.nixDir or (src + /nix); nixDir = appliedRoot.nixDir or (src + /nix);
fullRoot = (autoImportAttrs nixDir rootAttrs) // resolvedRoot; fullRoot = (autoImportAttrs nixDir rootAttrs attrAliases)
// appliedRoot;
in in
normalizeModule fullRoot // { normalizeModule fullRoot // {
modules = fullRoot.modules or modules = fullRoot.modules or