Skip to content

Instantly share code, notes, and snippets.

@infinisil
Created September 5, 2018 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infinisil/871441af8ee5db2dec505f9581d139a2 to your computer and use it in GitHub Desktop.
Save infinisil/871441af8ee5db2dec505f9581d139a2 to your computer and use it in GitHub Desktop.
commit 08b02d4f0b70168a8032c6bc1332fecabfd8c6d0
Author: Silvan Mosberger <infinisil@icloud.com>
Date: Wed Sep 5 09:07:57 2018 +0200
pkgs: Add __path for attribute path
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index 1e4142562fa..8f84dfd9d8d 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -271,6 +271,25 @@ rec {
in mapAttrs g set;
in recurse [] set;
+ # Recursively adds a __path attribute to all attribute sets, corresponding to
+ # the attribute path to this value
+ mapPaths = { __path ? [], ... }@attrs:
+ if lib.isDerivation attrs then attrs
+ else lib.mapAttrs (n: v:
+ let pathSet = { __path = __path ++ [ n ]; }; in
+ if lib.isAttrs v then
+ if v ? __type && v.__type == "functionTakingPath" then v.fun (v.args // pathSet)
+ else mapPaths (v // pathSet)
+ else v
+ ) attrs;
+
+ # An attrset representing a function that takes a single argument. When it is
+ # accessed through mapPaths, it is evaluated with the argument extended with
+ # the __path argument corresponding to the path where the function was called
+ functionTakingPath = fun: args: {
+ __type = "functionTakingPath";
+ inherit args fun;
+ };
/* Generate an attribute set by mapping a function over a list of
attribute names.
diff --git a/lib/default.nix b/lib/default.nix
index c3640914405..01ce9351e98 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -69,8 +69,8 @@ let
getAttrFromPath attrVals attrValues catAttrs filterAttrs
filterAttrsRecursive foldAttrs collect nameValuePair mapAttrs
mapAttrs' mapAttrsToList mapAttrsRecursive mapAttrsRecursiveCond
- genAttrs isDerivation toDerivation optionalAttrs
- zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
+ mapPaths functionTakingPath genAttrs isDerivation toDerivation
+ optionalAttrs zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
recursiveUpdate matchAttrs overrideExisting getOutput getBin
getLib getDev chooseDevOutputs zipWithNames zip;
inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix
index 06978d1067b..7fdfcd1f8a4 100644
--- a/pkgs/top-level/stage.nix
+++ b/pkgs/top-level/stage.nix
@@ -174,4 +174,4 @@ let
in
# Return the complete set of packages.
- lib.fix toFix
+ lib.fix (x: lib.mapPaths (toFix x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment