From 4536ebad69010f41df3c67c18bff3d85513faf86 Mon Sep 17 00:00:00 2001 From: figsoda Date: Wed, 16 Nov 2022 11:47:36 -0500 Subject: [PATCH 1/4] lib/attrsets: simplify chooseDevOutputs --- lib/attrsets.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index de88763854d6..0a4c3c8ebcf4 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -606,7 +606,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. From f993f8a18659bb15bc5697b6875caf0cba8b1825 Mon Sep 17 00:00:00 2001 From: figsoda Date: Wed, 16 Nov 2022 12:20:38 -0500 Subject: [PATCH 2/4] lib/attrsets: add concatMapAttrs --- lib/attrsets.nix | 21 ++++++++++++++++++++- lib/default.nix | 2 +- lib/tests/misc.nix | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 0a4c3c8ebcf4..8b5c0ef4cea6 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -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 @@ -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. diff --git a/lib/default.nix b/lib/default.nix index 8bb06954518b..cc4bedc5869b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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 diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 31c938a8ffda..b73da4f1010d 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -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"]) { From f4828b40fde0b39afc5dc9b147baa60cb2a6ca47 Mon Sep 17 00:00:00 2001 From: figsoda Date: Wed, 16 Nov 2022 12:54:49 -0500 Subject: [PATCH 3/4] vimPlugins.nvim-treesitter: use lib.concatMapAttrs --- .../vim/plugins/nvim-treesitter/overrides.nix | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkgs/applications/editors/vim/plugins/nvim-treesitter/overrides.nix b/pkgs/applications/editors/vim/plugins/nvim-treesitter/overrides.nix index 97218844730f..f5e5556eaee2 100644 --- a/pkgs/applications/editors/vim/plugins/nvim-treesitter/overrides.nix +++ b/pkgs/applications/editors/vim/plugins/nvim-treesitter/overrides.nix @@ -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; From d1dd00b61823e8e98fe44262090b560eee45b344 Mon Sep 17 00:00:00 2001 From: figsoda Date: Wed, 16 Nov 2022 12:57:04 -0500 Subject: [PATCH 4/4] nixos/vaultwarden: use lib.concatMapAttrs --- nixos/modules/services/security/vaultwarden/default.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/security/vaultwarden/default.nix b/nixos/modules/services/security/vaultwarden/default.nix index 81423e57fd2c..3ef0bfb090ac 100644 --- a/nixos/modules/services/security/vaultwarden/default.nix +++ b/nixos/modules/services/security/vaultwarden/default.nix @@ -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;