-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify attrs flattening (#379)
this reduces unneeded generality. we probably shouldn't do the premature attribute flattening at all, it's not clear to me why it's even done to begin with. the only place where it seems unavoidable is for the `checks` flake output.
- Loading branch information
1 parent
4a23df0
commit 91276d2
Showing
3 changed files
with
26 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,12 @@ | ||
# Functions to lay on top of Nixpkgs' lib for convenience. | ||
{lib}: let | ||
inherit | ||
(builtins) | ||
isAttrs | ||
concatStringsSep | ||
; | ||
|
||
inherit | ||
(lib) | ||
concatMapAttrs | ||
; | ||
in rec { | ||
# Takes an attrset of arbitrary nesting (attrset containing attrset) | ||
# and flattens it into an attrset that is *not* nested, i.e., does | ||
# *not* contain attrsets. | ||
# This is done by concatenating the names of nested values using a | ||
# separator. | ||
# | ||
# Type: flattenAttrs :: string -> [string] -> AttrSet -> AttrSet | ||
# | ||
# Example: | ||
# flattenAttrs "~" ["1" "2"] { a = { b = "x"; }; c = { d = { e = "y"; }; }; f = "z"; } | ||
# => { "1~2~a~b" = "x"; "1~2~c~d~e" = "y"; "1~2~f" = "z"; } | ||
flattenAttrs = | ||
# Separator to use to join names of different nesting levels. | ||
separator: | ||
# Prefix to be prepended to all names in the generated attrset, | ||
# as a list that is joined by the separator. | ||
prefix: let | ||
initPath = | ||
if prefix == [] | ||
then "" | ||
else (concatStringsSep separator prefix) + separator; | ||
f = path: | ||
concatMapAttrs ( | ||
name: value: | ||
if isAttrs value | ||
then f (path + name + separator) value | ||
else {${path + name} = value;} | ||
); | ||
in | ||
f initPath; | ||
|
||
flattenAttrsSlash = flattenAttrs "/" []; | ||
flattenAttrsDot = flattenAttrs "." []; | ||
{lib}: { | ||
# Take an attrset of arbitrary nesting and make it flat | ||
# by concatenating the nested names with the given separator. | ||
flattenAttrs = separator: let | ||
f = path: lib.concatMapAttrs (flatten path); | ||
flatten = path: name: value: | ||
if lib.isAttrs value | ||
then f (path + name + separator) value | ||
else {${path + name} = value;}; | ||
in | ||
f ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters