diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/attrsets.nix | 30 | ||||
-rw-r--r-- | lib/default.nix | 4 | ||||
-rw-r--r-- | lib/fileset/README.md | 2 | ||||
-rw-r--r-- | lib/licenses.nix | 8 | ||||
-rw-r--r-- | lib/strings.nix | 27 | ||||
-rw-r--r-- | lib/tests/misc.nix | 24 |
6 files changed, 80 insertions, 15 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index b8960cf73f208..b0460ab139e8f 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -542,6 +542,36 @@ rec { attrs: map (name: f name attrs.${name}) (attrNames attrs); + /* + Deconstruct an attrset to a list of name-value pairs as expected by [`builtins.listToAttrs`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-listToAttrs). + Each element of the resulting list is an attribute set with these attributes: + - `name` (string): The name of the attribute + - `value` (any): The value of the attribute + + The following is always true: + ```nix + builtins.listToAttrs (attrsToList attrs) == attrs + ``` + + :::{.warning} + The opposite is not always true. In general expect that + ```nix + attrsToList (builtins.listToAttrs list) != list + ``` + + This is because the `listToAttrs` removes duplicate names and doesn't preserve the order of the list. + ::: + + Example: + attrsToList { foo = 1; bar = "asdf"; } + => [ { name = "bar"; value = "asdf"; } { name = "foo"; value = 1; } ] + + Type: + attrsToList :: AttrSet -> [ { name :: String; value :: Any; } ] + + */ + attrsToList = mapAttrsToList nameValuePair; + /* Like `mapAttrs`, except that it recursively applies itself to the *leaf* attributes of a potentially-nested attribute set: diff --git a/lib/default.nix b/lib/default.nix index 169f013191b82..fe737a125e680 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -81,8 +81,8 @@ let inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs filterAttrsRecursive foldlAttrs foldAttrs collect nameValuePair mapAttrs - mapAttrs' mapAttrsToList concatMapAttrs mapAttrsRecursive mapAttrsRecursiveCond - genAttrs isDerivation toDerivation optionalAttrs + mapAttrs' mapAttrsToList attrsToList concatMapAttrs mapAttrsRecursive + mapAttrsRecursiveCond genAttrs isDerivation toDerivation optionalAttrs zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil recursiveUpdate matchAttrs overrideExisting showAttrPath getOutput getBin getLib getDev getMan chooseDevOutputs zipWithNames zip diff --git a/lib/fileset/README.md b/lib/fileset/README.md index c439e449544e5..ebe13f08fdef8 100644 --- a/lib/fileset/README.md +++ b/lib/fileset/README.md @@ -205,7 +205,7 @@ File sets do not support Nix store paths in strings such as `"/nix/store/...-sou Arguments: - (+) Such paths are usually produced by derivations, which means `toSource` would either: - - Require IFD if `builtins.path` is used as the underlying primitive + - Require [Import From Derivation](https://nixos.org/manual/nix/unstable/language/import-from-derivation) (IFD) if `builtins.path` is used as the underlying primitive - Require importing the entire `root` into the store such that derivations can be used to do the filtering - (+) The convenient path coercion like `union ./foo ./bar` wouldn't work for absolute paths, requiring more verbose alternate interfaces: - `let root = "/nix/store/...-source"; in union "${root}/foo" "${root}/bar"` diff --git a/lib/licenses.nix b/lib/licenses.nix index b9d6dc44e6d00..d9555ca66cb96 100644 --- a/lib/licenses.nix +++ b/lib/licenses.nix @@ -30,6 +30,14 @@ in mkLicense lset) ({ fullName = "Abstyles License"; }; + acsl14 = { + fullName = "Anti-Capitalist Software License v1.4"; + url = "https://anticapitalist.software/"; + /* restrictions on corporations apply for both use and redistribution */ + free = false; + redistributable = false; + }; + afl20 = { spdxId = "AFL-2.0"; fullName = "Academic Free License v2.0"; diff --git a/lib/strings.nix b/lib/strings.nix index d7642ce10fafe..628669d86bbd4 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -854,7 +854,7 @@ rec { assert (lib.isBool flag); mesonOption feature (if flag then "enabled" else "disabled"); - /* Create an --{enable,disable}-<feat> string that can be passed to + /* Create an --{enable,disable}-<feature> string that can be passed to standard GNU Autoconf scripts. Example: @@ -863,11 +863,12 @@ rec { enableFeature false "shared" => "--disable-shared" */ - enableFeature = enable: feat: - assert isString feat; # e.g. passing openssl instead of "openssl" - "--${if enable then "enable" else "disable"}-${feat}"; + enableFeature = flag: feature: + assert lib.isBool flag; + assert lib.isString feature; # e.g. passing openssl instead of "openssl" + "--${if flag then "enable" else "disable"}-${feature}"; - /* Create an --{enable-<feat>=<value>,disable-<feat>} string that can be passed to + /* Create an --{enable-<feature>=<value>,disable-<feature>} string that can be passed to standard GNU Autoconf scripts. Example: @@ -876,9 +877,10 @@ rec { enableFeatureAs false "shared" (throw "ignored") => "--disable-shared" */ - enableFeatureAs = enable: feat: value: enableFeature enable feat + optionalString enable "=${value}"; + enableFeatureAs = flag: feature: value: + enableFeature flag feature + optionalString flag "=${value}"; - /* Create an --{with,without}-<feat> string that can be passed to + /* Create an --{with,without}-<feature> string that can be passed to standard GNU Autoconf scripts. Example: @@ -887,11 +889,11 @@ rec { withFeature false "shared" => "--without-shared" */ - withFeature = with_: feat: - assert isString feat; # e.g. passing openssl instead of "openssl" - "--${if with_ then "with" else "without"}-${feat}"; + withFeature = flag: feature: + assert isString feature; # e.g. passing openssl instead of "openssl" + "--${if flag then "with" else "without"}-${feature}"; - /* Create an --{with-<feat>=<value>,without-<feat>} string that can be passed to + /* Create an --{with-<feature>=<value>,without-<feature>} string that can be passed to standard GNU Autoconf scripts. Example: @@ -900,7 +902,8 @@ rec { withFeatureAs false "shared" (throw "ignored") => "--without-shared" */ - withFeatureAs = with_: feat: value: withFeature with_ feat + optionalString with_ "=${value}"; + withFeatureAs = flag: feature: value: + withFeature flag feature + optionalString flag "=${value}"; /* Create a fixed width string with additional prefix to match required width. diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index ec306acbb765f..6527c31e49c2e 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -20,6 +20,10 @@ let expr = (builtins.tryEval (builtins.seq expr "didn't throw")); expected = { success = false; value = false; }; }; + testingEval = expr: { + expr = (builtins.tryEval expr).success; + expected = true; + }; testingDeepThrow = expr: testingThrow (builtins.deepSeq expr expr); testSanitizeDerivationName = { name, expected }: @@ -816,6 +820,26 @@ runTests { expected = { a = 1; b = 2; }; }; + testListAttrsReverse = let + exampleAttrs = {foo=1; bar="asdf"; baz = [1 3 3 7]; fnord=null;}; + exampleSingletonList = [{name="foo"; value=1;}]; + in { + expr = { + isReverseToListToAttrs = builtins.listToAttrs (attrsToList exampleAttrs) == exampleAttrs; + isReverseToAttrsToList = attrsToList (builtins.listToAttrs exampleSingletonList) == exampleSingletonList; + testDuplicatePruningBehaviour = attrsToList (builtins.listToAttrs [{name="a"; value=2;} {name="a"; value=1;}]); + }; + expected = { + isReverseToAttrsToList = true; + isReverseToListToAttrs = true; + testDuplicatePruningBehaviour = [{name="a"; value=2;}]; + }; + }; + + testAttrsToListsCanDealWithFunctions = testingEval ( + attrsToList { someFunc= a: a + 1;} + ); + # GENERATORS # these tests assume attributes are converted to lists # in alphabetical order |