From 10c82665cb197b68ff0d9bb02e12a4287f1b8925 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 24 Dec 2024 16:49:21 +0100 Subject: feat(by-name-overlay.nix): Support non-sharded by-name directories --- by-name-overlay.nix | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/by-name-overlay.nix b/by-name-overlay.nix index 4d700a8..80aebf4 100644 --- a/by-name-overlay.nix +++ b/by-name-overlay.nix @@ -7,6 +7,7 @@ finalizeFunction, coImportsNameFunction ? null, coImportsWarnMessageObject ? null, + useShards ? true, }: let # Takes a list of attrs as input and returns one merged attr set. flattenAttrs = list: @@ -43,27 +44,35 @@ namesForShard = shard: type: if type != "directory" then warn "Ignored non-directory, whilst importing by-name directory (${fileName}): '${shard}'" {} - else let - mkPath = name: _type: let - path = baseDirectory + "/${shard}/${name}" + "/${fileName}"; - coImportPath = - if coImportsNameFunction != null - then - coImportsNameFunction - {inherit shard name;} - else path; - in - if builtins.pathExists path + else if useShards + then namesForElement shard type + else namesForElement "." type; + + namesForElement = shard: _type: let + mkPath = name: type: let + path = baseDirectory + "/${shard}/${name}" + "/${fileName}"; + coImportPath = + if coImportsNameFunction != null then - if builtins.pathExists coImportPath - then path - else warn "'${builtins.toString coImportPath}' does not exist. Should include ${coImportsWarnMessageObject} for '${shard}/${name}'" path - else warn "'${builtins.toString path}' does not exist. Skipped" null; + coImportsNameFunction + {inherit shard name;} + else path; in - filterAttrs (name: value: value != null) - (builtins.mapAttrs - mkPath - (builtins.readDir (baseDirectory + "/${shard}"))); + if builtins.pathExists path + then + if type != "directory" + then + # The `namesForShard` function should have already printed a warning. + null + else if builtins.pathExists coImportPath + then path + else warn "'${builtins.toString coImportPath}' does not exist. Should include ${coImportsWarnMessageObject} for '${shard}/${name}'" path + else warn "'${builtins.toString path}' does not exist. Skipped" null; + in + filterAttrs (name: value: value != null) + (builtins.mapAttrs + mkPath + (builtins.readDir (baseDirectory + "/${shard}"))); # A list of all module paths. # These can the be simply injected into `import` -- cgit 1.4.1