Skip to content

Commit

Permalink
refactor: simplify attrs flattening (#379)
Browse files Browse the repository at this point in the history
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
fricklerhandwerk authored Oct 11, 2024
1 parent 4a23df0 commit 91276d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 76 deletions.
32 changes: 11 additions & 21 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,13 @@
lib = import (nixpkgs + "/lib");
lib' = import ./lib.nix {inherit lib;};

inherit
(builtins)
mapAttrs
attrValues
;

inherit
(lib)
attrValues
concatMapAttrs
recursiveUpdate
filterAttrs
;

inherit
(lib')
flattenAttrsDot
flattenAttrsSlash
mapAttrs
recursiveUpdate
;

# Imported from Nixpkgs
Expand All @@ -75,15 +65,15 @@
sources = {inherit inputs;};
};

rawExamples = flattenAttrsSlash (
mapAttrs (_: project: mapAttrs (_: example: example.path) project.nixos.examples) rawNgiProjects
rawExamples = lib'.flattenAttrs "/" (
mapAttrs
(_: project: mapAttrs (_: example: example.path) project.nixos.examples)
rawNgiProjects
);

rawNixosModules = flattenAttrsDot (
lib.foldl recursiveUpdate {} (attrValues (
mapAttrs (_: project: project.nixos.modules) rawNgiProjects
))
);
rawNixosModules = lib'.flattenAttrs "." (lib.foldl recursiveUpdate {} (attrValues (
mapAttrs (_: project: project.nixos.modules) rawNgiProjects
)));

nixosModules =
{
Expand Down Expand Up @@ -207,7 +197,7 @@
ngipkgs
// {
overview = import ./overview {
inherit lib pkgs self;
inherit lib lib' pkgs self;
projects = ngiProjects;
options = optionsDoc.optionsNix;
};
Expand Down
57 changes: 11 additions & 46 deletions lib.nix
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 "";
}
13 changes: 4 additions & 9 deletions overview/default.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
lib,
lib',
options,
pkgs,
projects,
self,
}: let
lib' = import ../lib.nix {inherit lib;};

inherit
(builtins)
any
Expand All @@ -30,11 +29,6 @@
optionalString
;

inherit
(lib')
flattenAttrsDot
;

empty = xs: assert isList xs; xs == [];
heading = i: text: "<h${toString i}>${text}</h${toString i}>";
dottedLoc = option: concatStringsSep "." option.loc;
Expand All @@ -51,7 +45,7 @@

pick = {
options = project: let
spec = attrNames (flattenAttrsDot project.nixos.modules);
spec = attrNames (lib'.flattenAttrs "." project.nixos.modules);
in
filter
(option: any ((flip hasPrefix) (dottedLoc option)) spec)
Expand Down Expand Up @@ -159,7 +153,8 @@
<footer>Version: ${version}, Last Modified: ${lastModified}</footer>
'';
in
pkgs.runCommand "overview" {
pkgs.runCommand "overview"
{
nativeBuildInputs = with pkgs; [jq gnused pandoc validator-nu];
} ''
mkdir -v $out
Expand Down

0 comments on commit 91276d2

Please sign in to comment.