Skip to content

Instantly share code, notes, and snippets.

@danielphan2003
Created August 21, 2022 01:27
Show Gist options
  • Save danielphan2003/5aae67b9ad06c2f5c05d378bf0a2e563 to your computer and use it in GitHub Desktop.
Save danielphan2003/5aae67b9ad06c2f5c05d378bf0a2e563 to your computer and use it in GitHub Desktop.

Revisions

  1. danielphan2003 created this gist Aug 21, 2022.
    49 changes: 49 additions & 0 deletions splitNamespaceSep.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    {inputs,cell}:
    {
    /*
    A function that split namespaces by a separator.
    Example:
    ```nix
    # src
    {
    packagesA_helper1 = ...;
    packagesA_helper2 = ...;
    packagesB_helper1 = ...;
    }
    # splitNamespaceSep "_" src
    {
    packagesA = {
    helper1 = ...;
    helper2 = ...;
    };
    packagesB = {
    helper1 = ...;
    };
    }
    */
    splitNamespaceSep = sep: src: let
    namespaces = l.foldl'
    (all: ns: let
    matches = l.splitString sep ns;
    namespace = l.head matches;
    pname = l.last matches;
    attrs =
    if l.length matches == 2
    then {
    "${pname}" = l.getAttrFromPath [ns] src;
    }
    else cell.lib.splitNamespaceSep sep (l.mapAttrs' (name:
    l.nameValuePair (l.removePrefix "${namespace}." name)
    ) src)
    ;
    in
    l.recursiveUpdate all {
    "${namespace}" = attrs;
    })
    {}
    (l.attrNames src)
    ;
    in
    namespaces
    ;
    }