diff options
author | Alyssa Ross <hi@alyssa.is> | 2020-01-22 15:26:58 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2020-01-22 16:56:33 +0000 |
commit | 542f80867c380b0ac79250b6e5358dda8bc49e0d (patch) | |
tree | aa5903f228b2f32c524a9a6e4f6dd94f8c5b869e /nixpkgs/lib/types.nix | |
parent | f69898c7d5b757342e6ab90d5fcc7c9aea9f5ff4 (diff) | |
parent | 90441b4b47fc7280de6a5bd1a228017caaa0f97f (diff) | |
download | nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.tar nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.tar.gz nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.tar.bz2 nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.tar.lz nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.tar.xz nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.tar.zst nixlib-542f80867c380b0ac79250b6e5358dda8bc49e0d.zip |
Merge commit '90441b4b47fc7280de6a5bd1a228017caaa0f97f'
Diffstat (limited to 'nixpkgs/lib/types.nix')
-rw-r--r-- | nixpkgs/lib/types.nix | 78 |
1 files changed, 65 insertions, 13 deletions
diff --git a/nixpkgs/lib/types.nix b/nixpkgs/lib/types.nix index e86f6d364761..6fd6de7e1fd9 100644 --- a/nixpkgs/lib/types.nix +++ b/nixpkgs/lib/types.nix @@ -340,29 +340,79 @@ rec { let padWidth = stringLength (toString (length def.value)); unnamed = i: unnamedPrefix + fixedWidthNumber padWidth i; + anyString = placeholder "name"; + nameAttrs = [ + { path = [ "environment" "etc" ]; + name = "target"; + } + { path = [ "containers" anyString "bindMounts" ]; + name = "mountPoint"; + } + { path = [ "programs" "ssh" "knownHosts" ]; + # hostNames is actually a list so we would need to handle it only when singleton + name = "hostNames"; + } + { path = [ "fileSystems" ]; + name = "mountPoint"; + } + { path = [ "boot" "specialFileSystems" ]; + name = "mountPoint"; + } + { path = [ "services" "znapzend" "zetup" ]; + name = "dataset"; + } + { path = [ "services" "znapzend" "zetup" anyString "destinations" ]; + name = "label"; + } + { path = [ "services" "geoclue2" "appConfig" ]; + name = "desktopID"; + } + ]; + matched = let + equals = a: b: b == anyString || a == b; + fallback = { name = "name"; }; + in findFirst ({ path, ... }: all (v: v == true) (zipListsWith equals loc path)) fallback nameAttrs; + nameAttr = matched.name; + nameValueOld = value: + if isList value then + if length value > 0 then + "[ " + concatMapStringsSep " " escapeNixString value + " ]" + else + "[ ]" + else + escapeNixString value; + nameValueNew = value: unnamed: + if isList value then + if length value > 0 then + head value + else + unnamed + else + value; res = { inherit (def) file; value = listToAttrs ( imap1 (elemIdx: elem: - { name = elem.name or (unnamed elemIdx); + { name = nameValueNew (elem.${nameAttr} or (unnamed elemIdx)) (unnamed elemIdx); value = elem; }) def.value); }; option = concatStringsSep "." loc; sample = take 3 def.value; - list = concatMapStrings (x: ''{ name = "${x.name or "unnamed"}"; ...} '') sample; - set = concatMapStrings (x: ''${x.name or "unnamed"} = {...}; '') sample; + more = lib.optionalString (length def.value > 3) "... "; + list = concatMapStrings (x: ''{ ${nameAttr} = ${nameValueOld (x.${nameAttr} or "unnamed")}; ...} '') sample; + set = concatMapStrings (x: ''${nameValueNew (x.${nameAttr} or "unnamed") "unnamed"} = {...}; '') sample; msg = '' In file ${def.file} a list is being assigned to the option config.${option}. This will soon be an error as type loaOf is deprecated. - See https://git.io/fj2zm for more information. + See https://github.com/NixOS/nixpkgs/pull/63103 for more information. Do ${option} = - { ${set}...} + { ${set}${more}} instead of ${option} = - [ ${list}...] + [ ${list}${more}] ''; in lib.warn msg res @@ -430,14 +480,16 @@ rec { else unify (if shorthandOnlyDefinesConfig then { config = value; } else value); allModules = defs: modules ++ imap1 (n: { value, file }: - # Annotate the value with the location of its definition for better error messages - coerce (lib.modules.unifyModuleSyntax file "${toString file}-${toString n}") value + if isAttrs value || isFunction value then + # Annotate the value with the location of its definition for better error messages + coerce (lib.modules.unifyModuleSyntax file "${toString file}-${toString n}") value + else value ) defs; in mkOptionType rec { name = "submodule"; - check = x: isAttrs x || isFunction x; + check = x: isAttrs x || isFunction x || path.check x; merge = loc: defs: (evalModules { modules = allModules defs; @@ -538,7 +590,7 @@ rec { tail' = tail ts; in foldl' either head' tail'; - # Either value of type `finalType` or `coercedType`, the latter is + # Either value of type `coercedType` or `finalType`, the former is # converted to `finalType` using `coerceFunc`. coercedTo = coercedType: coerceFunc: finalType: assert lib.assertMsg (coercedType.getSubModules == null) @@ -547,12 +599,12 @@ rec { mkOptionType rec { name = "coercedTo"; description = "${finalType.description} or ${coercedType.description} convertible to it"; - check = x: finalType.check x || (coercedType.check x && finalType.check (coerceFunc x)); + check = x: (coercedType.check x && finalType.check (coerceFunc x)) || finalType.check x; merge = loc: defs: let coerceVal = val: - if finalType.check val then val - else coerceFunc val; + if coercedType.check val then coerceFunc val + else val; in finalType.merge loc (map (def: def // { value = coerceVal def.value; }) defs); emptyValue = finalType.emptyValue; getSubOptions = finalType.getSubOptions; |