Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/attrsets: add concatMapAttrs #201527

Merged
merged 4 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

let
inherit (builtins) head tail length;
inherit (lib.trivial) id;
inherit (lib.trivial) flip id mergeAttrs pipe;
inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName;
inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all partition groupBy take foldl;
in
Expand Down Expand Up @@ -77,6 +77,25 @@ rec {
let errorMsg = "cannot find attribute `" + concatStringsSep "." attrPath + "'";
in attrByPath attrPath (abort errorMsg);

/* Map each attribute in the given set and merge them into a new attribute set.

Type:
concatMapAttrs ::
(String -> a -> AttrSet)
-> AttrSet
-> AttrSet

Example:
concatMapAttrs
(name: value: {
${name} = value;
${name + value} = value;
})
{ x = "a"; y = "b"; }
=> { x = "a"; xa = "a"; y = "b"; yb = "b"; }
*/
concatMapAttrs = f: flip pipe [ (mapAttrs f) attrValues (foldl' mergeAttrs { }) ];


/* Update or set specific paths of an attribute set.

Expand Down Expand Up @@ -606,7 +625,7 @@ rec {
getMan = getOutput "man";

/* Pick the outputs of packages to place in buildInputs */
chooseDevOutputs = drvs: builtins.map getDev drvs;
chooseDevOutputs = builtins.map getDev;

/* Make various Nix tools consider the contents of the resulting
attribute set when looking for what to build, find, etc.
Expand Down
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let
inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath
getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs
filterAttrsRecursive foldAttrs collect nameValuePair mapAttrs
mapAttrs' mapAttrsToList mapAttrsRecursive mapAttrsRecursiveCond
mapAttrs' mapAttrsToList concatMapAttrs mapAttrsRecursive mapAttrsRecursiveCond
genAttrs isDerivation toDerivation optionalAttrs
zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
recursiveUpdate matchAttrs overrideExisting showAttrPath getOutput getBin
Expand Down
17 changes: 17 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,23 @@ runTests {

# ATTRSETS

testConcatMapAttrs = {
expr = concatMapAttrs
(name: value: {
${name} = value;
${name + value} = value;
})
{
foo = "bar";
foobar = "baz";
};
expected = {
foo = "bar";
foobar = "baz";
foobarbaz = "baz";
};
};

# code from the example
testRecursiveUpdateUntil = {
expr = recursiveUpdateUntil (path: l: r: path == ["foo"]) {
Expand Down
6 changes: 3 additions & 3 deletions nixos/modules/services/security/vaultwarden/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ let
# we can only check for values consistently after converting them to their corresponding environment variable name.
configEnv =
let
configEnv = listToAttrs (concatLists (mapAttrsToList (name: value:
if value != null then [ (nameValuePair (nameToEnvVar name) (if isBool value then boolToString value else toString value)) ] else []
) cfg.config));
configEnv = concatMapAttrs (name: value: optionalAttrs (value != null) {
${nameToEnvVar name} = if isBool value then boolToString value else toString value;
}) cfg.config;
in { DATA_FOLDER = "/var/lib/bitwarden_rs"; } // optionalAttrs (!(configEnv ? WEB_VAULT_ENABLED) || configEnv.WEB_VAULT_ENABLED == "true") {
WEB_VAULT_FOLDER = "${cfg.webVaultPackage}/share/vaultwarden/vault";
} // configEnv;
Expand Down
24 changes: 12 additions & 12 deletions pkgs/applications/editors/vim/plugins/nvim-treesitter/overrides.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ let
# ocaml-interface
# tree-sitter-ocaml-interface
# tree-sitter-ocaml_interface
builtGrammars = generatedGrammars // lib.listToAttrs
(lib.concatLists (lib.mapAttrsToList
(k: v:
let
replaced = lib.replaceStrings [ "_" ] [ "-" ] k;
in
map (lib.flip lib.nameValuePair v)
([ "tree-sitter-${k}" ] ++ lib.optionals (k != replaced) [
replaced
"tree-sitter-${replaced}"
]))
generatedDerivations));
builtGrammars = generatedGrammars // lib.concatMapAttrs
(k: v:
let
replaced = lib.replaceStrings [ "_" ] [ "-" ] k;
in
{
"tree-sitter-${k}" = v;
} // lib.optionalAttrs (k != replaced) {
${replaced} = v;
"tree-sitter-${replaced}" = v;
})
generatedDerivations;

allGrammars = lib.attrValues generatedDerivations;

Expand Down