diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/attrsets.nix | 115 | ||||
-rw-r--r-- | lib/default.nix | 10 | ||||
-rw-r--r-- | lib/lists.nix | 17 | ||||
-rw-r--r-- | lib/modules.nix | 94 | ||||
-rw-r--r-- | lib/options.nix | 2 | ||||
-rw-r--r-- | lib/systems/default.nix | 3 | ||||
-rw-r--r-- | lib/systems/doubles.nix | 8 | ||||
-rw-r--r-- | lib/systems/examples.nix | 20 | ||||
-rw-r--r-- | lib/systems/inspect.nix | 6 | ||||
-rw-r--r-- | lib/systems/parse.nix | 7 | ||||
-rw-r--r-- | lib/systems/platforms.nix | 47 | ||||
-rw-r--r-- | lib/tests/misc.nix | 152 | ||||
-rwxr-xr-x | lib/tests/modules.sh | 13 | ||||
-rw-r--r-- | lib/tests/modules/declare-bare-submodule-deep-option-duplicate.nix | 10 | ||||
-rw-r--r-- | lib/tests/modules/declare-bare-submodule-deep-option.nix | 10 | ||||
-rw-r--r-- | lib/tests/modules/declare-bare-submodule-nested-option.nix | 19 | ||||
-rw-r--r-- | lib/tests/modules/declare-bare-submodule.nix | 18 | ||||
-rw-r--r-- | lib/tests/modules/declare-set.nix | 12 | ||||
-rw-r--r-- | lib/tests/modules/define-bare-submodule-values.nix | 4 | ||||
-rw-r--r-- | lib/tests/modules/define-shorthandOnlyDefinesConfig-true.nix | 1 | ||||
-rw-r--r-- | lib/tests/systems.nix | 4 | ||||
-rw-r--r-- | lib/trivial.nix | 24 | ||||
-rw-r--r-- | lib/types.nix | 22 |
23 files changed, 578 insertions, 40 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index c0d3ede73d0e6..516fdd8d33fdd 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -4,8 +4,8 @@ let inherit (builtins) head tail length; inherit (lib.trivial) id; - inherit (lib.strings) concatStringsSep sanitizeDerivationName; - inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all; + inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName; + inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all partition groupBy take foldl; in rec { @@ -78,6 +78,103 @@ rec { in attrByPath attrPath (abort errorMsg); + /* Update or set specific paths of an attribute set. + + Takes a list of updates to apply and an attribute set to apply them to, + and returns the attribute set with the updates applied. Updates are + represented as { path = ...; update = ...; } values, where `path` is a + list of strings representing the attribute path that should be updated, + and `update` is a function that takes the old value at that attribute path + as an argument and returns the new + value it should be. + + Properties: + - Updates to deeper attribute paths are applied before updates to more + shallow attribute paths + - Multiple updates to the same attribute path are applied in the order + they appear in the update list + - If any but the last `path` element leads into a value that is not an + attribute set, an error is thrown + - If there is an update for an attribute path that doesn't exist, + accessing the argument in the update function causes an error, but + intermediate attribute sets are implicitly created as needed + + Example: + updateManyAttrsByPath [ + { + path = [ "a" "b" ]; + update = old: { d = old.c; }; + } + { + path = [ "a" "b" "c" ]; + update = old: old + 1; + } + { + path = [ "x" "y" ]; + update = old: "xy"; + } + ] { a.b.c = 0; } + => { a = { b = { d = 1; }; }; x = { y = "xy"; }; } + */ + updateManyAttrsByPath = let + # When recursing into attributes, instead of updating the `path` of each + # update using `tail`, which needs to allocate an entirely new list, + # we just pass a prefix length to use and make sure to only look at the + # path without the prefix length, so that we can reuse the original list + # entries. + go = prefixLength: hasValue: value: updates: + let + # Splits updates into ones on this level (split.right) + # And ones on levels further down (split.wrong) + split = partition (el: length el.path == prefixLength) updates; + + # Groups updates on further down levels into the attributes they modify + nested = groupBy (el: elemAt el.path prefixLength) split.wrong; + + # Applies only nested modification to the input value + withNestedMods = + # Return the value directly if we don't have any nested modifications + if split.wrong == [] then + if hasValue then value + else + # Throw an error if there is no value. This `head` call here is + # safe, but only in this branch since `go` could only be called + # with `hasValue == false` for nested updates, in which case + # it's also always called with at least one update + let updatePath = (head split.right).path; in + throw + ( "updateManyAttrsByPath: Path '${showAttrPath updatePath}' does " + + "not exist in the given value, but the first update to this " + + "path tries to access the existing value.") + else + # If there are nested modifications, try to apply them to the value + if ! hasValue then + # But if we don't have a value, just use an empty attribute set + # as the value, but simplify the code a bit + mapAttrs (name: go (prefixLength + 1) false null) nested + else if isAttrs value then + # If we do have a value and it's an attribute set, override it + # with the nested modifications + value // + mapAttrs (name: go (prefixLength + 1) (value ? ${name}) value.${name}) nested + else + # However if it's not an attribute set, we can't apply the nested + # modifications, throw an error + let updatePath = (head split.wrong).path; in + throw + ( "updateManyAttrsByPath: Path '${showAttrPath updatePath}' needs to " + + "be updated, but path '${showAttrPath (take prefixLength updatePath)}' " + + "of the given value is not an attribute set, so we can't " + + "update an attribute inside of it."); + + # We get the final result by applying all the updates on this level + # after having applied all the nested updates + # We use foldl instead of foldl' so that in case of multiple updates, + # intermediate values aren't evaluated if not needed + in foldl (acc: el: el.update acc) withNestedMods split.right; + + in updates: value: go 0 true value updates; + /* Return the specified attributes from a set. Example: @@ -477,6 +574,20 @@ rec { overrideExisting = old: new: mapAttrs (name: value: new.${name} or value) old; + /* Turns a list of strings into a human-readable description of those + strings represented as an attribute path. The result of this function is + not intended to be machine-readable. + + Example: + showAttrPath [ "foo" "10" "bar" ] + => "foo.\"10\".bar" + showAttrPath [] + => "<root attribute path>" + */ + showAttrPath = path: + if path == [] then "<root attribute path>" + else concatMapStringsSep "." escapeNixIdentifier path; + /* Get a package output. If no output is found, fallback to `.out` and then to the default. diff --git a/lib/default.nix b/lib/default.nix index 2d231bbd2d92c..0e94e821bea50 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -67,7 +67,7 @@ let inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max importJSON importTOML warn warnIf throwIfNot checkListOfEnum - info showWarnings nixpkgsVersion version + info showWarnings nixpkgsVersion version isInOldestRelease mod compare splitByAndCompare functionArgs setFunctionArgs isFunction toFunction toHexString toBaseDigits; @@ -79,9 +79,10 @@ let mapAttrs' mapAttrsToList mapAttrsRecursive mapAttrsRecursiveCond genAttrs isDerivation toDerivation optionalAttrs zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil - recursiveUpdate matchAttrs overrideExisting getOutput getBin + recursiveUpdate matchAttrs overrideExisting showAttrPath getOutput getBin getLib getDev getMan chooseDevOutputs zipWithNames zip - recurseIntoAttrs dontRecurseIntoAttrs cartesianProductOfSets; + recurseIntoAttrs dontRecurseIntoAttrs cartesianProductOfSets + updateManyAttrsByPath; inherit (self.lists) singleton forEach foldr fold foldl foldl' imap0 imap1 concatMap flatten remove findSingle findFirst any all count optional optionals toList range partition zipListsWith zipLists @@ -120,7 +121,8 @@ let mkOptionDefault mkDefault mkImageMediaOverride mkForce mkVMOverride mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule - mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule + mkRenamedOptionModule mkRenamedOptionModuleWith + mkMergedOptionModule mkChangedOptionModule mkAliasOptionModule mkDerivedConfig doRename; inherit (self.options) isOption mkEnableOption mkSinkUndeclaredOptions mergeDefaultOption mergeOneOption mergeEqualOption mergeUniqueOption diff --git a/lib/lists.nix b/lib/lists.nix index 1dbff7668d75d..a030280c8dcc4 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -4,6 +4,7 @@ let inherit (lib.strings) toInt; inherit (lib.trivial) compare min; + inherit (lib.attrsets) mapAttrs; in rec { @@ -340,15 +341,15 @@ rec { groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ] => { true = 12; false = 3; } */ - groupBy' = op: nul: pred: lst: - foldl' (r: e: - let - key = pred e; - in - r // { ${key} = op (r.${key} or nul) e; } - ) {} lst; + groupBy' = op: nul: pred: lst: mapAttrs (name: foldl op nul) (groupBy pred lst); - groupBy = groupBy' (sum: e: sum ++ [e]) []; + groupBy = builtins.groupBy or ( + pred: foldl' (r: e: + let + key = pred e; + in + r // { ${key} = (r.${key} or []) ++ [e]; } + ) {}); /* Merges two lists of the same size together. If the sizes aren't the same the merging stops at the shortest. How both lists are merged is defined diff --git a/lib/modules.nix b/lib/modules.nix index 46739746e6275..35c93d22baf90 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -9,7 +9,7 @@ let catAttrs concatLists concatMap - count + concatStringsSep elem filter findFirst @@ -47,6 +47,20 @@ let showOption unknownModule ; + + showDeclPrefix = loc: decl: prefix: + " - option(s) with prefix `${showOption (loc ++ [prefix])}' in module `${decl._file}'"; + showRawDecls = loc: decls: + concatStringsSep "\n" + (sort (a: b: a < b) + (concatMap + (decl: map + (showDeclPrefix loc decl) + (attrNames decl.options) + ) + decls + )); + in rec { @@ -474,26 +488,61 @@ rec { [{ inherit (module) file; inherit value; }] ) configs; + # Convert an option tree decl to a submodule option decl + optionTreeToOption = decl: + if isOption decl.options + then decl + else decl // { + options = mkOption { + type = types.submoduleWith { + modules = [ { options = decl.options; } ]; + # `null` is not intended for use by modules. It is an internal + # value that means "whatever the user has declared elsewhere". + # This might become obsolete with https://github.com/NixOS/nixpkgs/issues/162398 + shorthandOnlyDefinesConfig = null; + }; + }; + }; + resultsByName = mapAttrs (name: decls: # We're descending into attribute ‘name’. let loc = prefix ++ [name]; defns = defnsByName.${name} or []; defns' = defnsByName'.${name} or []; - nrOptions = count (m: isOption m.options) decls; + optionDecls = filter (m: isOption m.options) decls; in - if nrOptions == length decls then + if length optionDecls == length decls then let opt = fixupOptionType loc (mergeOptionDecls loc decls); in { matchedOptions = evalOptionValue loc opt defns'; unmatchedDefns = []; } - else if nrOptions != 0 then - let - firstOption = findFirst (m: isOption m.options) "" decls; - firstNonOption = findFirst (m: !isOption m.options) "" decls; - in - throw "The option `${showOption loc}' in `${firstOption._file}' is a prefix of options in `${firstNonOption._file}'." + else if optionDecls != [] then + if all (x: x.options.type.name == "submodule") optionDecls + # Raw options can only be merged into submodules. Merging into + # attrsets might be nice, but ambiguous. Suppose we have + # attrset as a `attrsOf submodule`. User declares option + # attrset.foo.bar, this could mean: + # a. option `bar` is only available in `attrset.foo` + # b. option `foo.bar` is available in all `attrset.*` + # c. reject and require "<name>" as a reminder that it behaves like (b). + # d. magically combine (a) and (c). + # All of the above are merely syntax sugar though. + then + let opt = fixupOptionType loc (mergeOptionDecls loc (map optionTreeToOption decls)); + in { + matchedOptions = evalOptionValue loc opt defns'; + unmatchedDefns = []; + } + else + let + firstNonOption = findFirst (m: !isOption m.options) "" decls; + nonOptions = filter (m: !isOption m.options) decls; + in + throw "The option `${showOption loc}' in module `${(lib.head optionDecls)._file}' would be a parent of the following options, but its type `${(lib.head optionDecls).options.type.description or "<no description>"}' does not support nested options.\n${ + showRawDecls loc nonOptions + }" else mergeModules' loc decls defns) declsByName; @@ -753,13 +802,14 @@ rec { compare = a: b: (a.priority or 1000) < (b.priority or 1000); in sort compare defs'; - /* Hack for backward compatibility: convert options of type - optionSet to options of type submodule. FIXME: remove - eventually. */ fixupOptionType = loc: opt: let options = opt.options or (throw "Option `${showOption loc}' has type optionSet but has no option attribute, in ${showFiles opt.declarations}."); + + # Hack for backward compatibility: convert options of type + # optionSet to options of type submodule. FIXME: remove + # eventually. f = tp: if tp.name == "option set" || tp.name == "submodule" then throw "The option ${showOption loc} uses submodules without a wrapping type, in ${showFiles opt.declarations}." @@ -904,6 +954,26 @@ rec { use = builtins.trace "Obsolete option `${showOption from}' is used. It was renamed to `${showOption to}'."; }; + mkRenamedOptionModuleWith = { + /* Old option path as list of strings. */ + from, + /* New option path as list of strings. */ + to, + + /* + Release number of the first release that contains the rename, ignoring backports. + Set it to the upcoming release, matching the nixpkgs/.version file. + */ + sinceRelease, + + }: doRename { + inherit from to; + visible = false; + warn = lib.isInOldestRelease sinceRelease; + use = lib.warnIf (lib.isInOldestRelease sinceRelease) + "Obsolete option `${showOption from}' is used. It was renamed to `${showOption to}'."; + }; + /* Return a module that causes a warning to be shown if any of the "from" option is defined; the defined values can be used in the "mergeFn" to set the "to" value. diff --git a/lib/options.nix b/lib/options.nix index 627aac24d2fb2..9efc1249e58e9 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -231,7 +231,7 @@ rec { then true else opt.visible or true; readOnly = opt.readOnly or false; - type = opt.type.description or null; + type = opt.type.description or "unspecified"; } // optionalAttrs (opt ? example) { example = scrubOptionValue opt.example; } // optionalAttrs (opt ? default) { default = scrubOptionValue opt.default; } diff --git a/lib/systems/default.nix b/lib/systems/default.nix index 529eeb6514b9e..7ddd5b8a58129 100644 --- a/lib/systems/default.nix +++ b/lib/systems/default.nix @@ -105,7 +105,8 @@ rec { else if final.isAarch64 then "arm64" else if final.isx86_32 then "i386" else if final.isx86_64 then "x86_64" - else if final.isMips then "mips" + else if final.isMips32 then "mips" + else if final.isMips64 then "mips" # linux kernel does not distinguish mips32/mips64 else if final.isPower then "powerpc" else if final.isRiscV then "riscv" else if final.isS390 then "s390" diff --git a/lib/systems/doubles.nix b/lib/systems/doubles.nix index 00e57339a3102..27cdaf6a7233b 100644 --- a/lib/systems/doubles.nix +++ b/lib/systems/doubles.nix @@ -26,7 +26,7 @@ let # Linux "aarch64-linux" "armv5tel-linux" "armv6l-linux" "armv7a-linux" - "armv7l-linux" "i686-linux" "m68k-linux" "mipsel-linux" + "armv7l-linux" "i686-linux" "m68k-linux" "mipsel-linux" "mips64el-linux" "powerpc64-linux" "powerpc64le-linux" "riscv32-linux" "riscv64-linux" "s390-linux" "s390x-linux" "x86_64-linux" @@ -87,7 +87,11 @@ in { darwin = filterDoubles predicates.isDarwin; freebsd = filterDoubles predicates.isFreeBSD; # Should be better, but MinGW is unclear. - gnu = filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnu; }) ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnueabi; }) ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnueabihf; }); + gnu = filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnu; }) + ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnueabi; }) + ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnueabihf; }) + ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnuabin32; }) + ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnuabi64; }); illumos = filterDoubles predicates.isSunOS; linux = filterDoubles predicates.isLinux; netbsd = filterDoubles predicates.isNetBSD; diff --git a/lib/systems/examples.nix b/lib/systems/examples.nix index 9c0c91617e8a1..997a7a8c273ae 100644 --- a/lib/systems/examples.nix +++ b/lib/systems/examples.nix @@ -93,6 +93,26 @@ rec { config = "mipsel-unknown-linux-gnu"; } // platforms.fuloong2f_n32; + # MIPS ABI table transcribed from here: https://wiki.debian.org/Multiarch/Tuples + + # can execute on 32bit chip + mips-linux-gnu = { config = "mips-linux-gnu"; } // platforms.gcc_mips32r2_o32; + mipsel-linux-gnu = { config = "mipsel-linux-gnu"; } // platforms.gcc_mips32r2_o32; + mipsisa32r6-linux-gnu = { config = "mipsisa32r6-linux-gnu"; } // platforms.gcc_mips32r6_o32; + mipsisa32r6el-linux-gnu = { config = "mipsisa32r6el-linux-gnu"; } // platforms.gcc_mips32r6_o32; + + # require 64bit chip (for more registers, 64-bit floating point, 64-bit "long long") but use 32bit pointers + mips64-linux-gnuabin32 = { config = "mips64-linux-gnuabin32"; } // platforms.gcc_mips64r2_n32; + mips64el-linux-gnuabin32 = { config = "mips64el-linux-gnuabin32"; } // platforms.gcc_mips64r2_n32; + mipsisa64r6-linux-gnuabin32 = { config = "mipsisa64r6-linux-gnuabin32"; } // platforms.gcc_mips64r6_n32; + mipsisa64r6el-linux-gnuabin32 = { config = "mipsisa64r6el-linux-gnuabin32"; } // platforms.gcc_mips64r6_n32; + + # 64bit pointers + mips64-linux-gnuabi64 = { config = "mips64-linux-gnuabi64"; } // platforms.gcc_mips64r2_64; + mips64el-linux-gnuabi64 = { config = "mips64el-linux-gnuabi64"; } // platforms.gcc_mips64r2_64; + mipsisa64r6-linux-gnuabi64 = { config = "mipsisa64r6-linux-gnuabi64"; } // platforms.gcc_mips64r6_64; + mipsisa64r6el-linux-gnuabi64 = { config = "mipsisa64r6el-linux-gnuabi64"; } // platforms.gcc_mips64r6_64; + muslpi = raspberryPi // { config = "armv6l-unknown-linux-musleabihf"; }; diff --git a/lib/systems/inspect.nix b/lib/systems/inspect.nix index 718954e0839a1..89cac575c67d3 100644 --- a/lib/systems/inspect.nix +++ b/lib/systems/inspect.nix @@ -17,6 +17,10 @@ rec { isAarch32 = { cpu = { family = "arm"; bits = 32; }; }; isAarch64 = { cpu = { family = "arm"; bits = 64; }; }; isMips = { cpu = { family = "mips"; }; }; + isMips32 = { cpu = { family = "mips"; bits = 32; }; }; + isMips64 = { cpu = { family = "mips"; bits = 64; }; }; + isMips64n32 = { cpu = { family = "mips"; bits = 64; }; abi = { abi = "n32"; }; }; + isMips64n64 = { cpu = { family = "mips"; bits = 64; }; abi = { abi = "64"; }; }; isMmix = { cpu = { family = "mmix"; }; }; isRiscV = { cpu = { family = "riscv"; }; }; isSparc = { cpu = { family = "sparc"; }; }; @@ -57,7 +61,7 @@ rec { isAndroid = [ { abi = abis.android; } { abi = abis.androideabi; } ]; isGnu = with abis; map (a: { abi = a; }) [ gnuabi64 gnu gnueabi gnueabihf ]; - isMusl = with abis; map (a: { abi = a; }) [ musl musleabi musleabihf ]; + isMusl = with abis; map (a: { abi = a; }) [ musl musleabi musleabihf muslabin32 muslabi64 ]; isUClibc = with abis; map (a: { abi = a; }) [ uclibc uclibceabi uclibceabihf ]; isEfi = map (family: { cpu.family = family; }) diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix index f0e87c30e473b..3ceddbb599b9c 100644 --- a/lib/systems/parse.nix +++ b/lib/systems/parse.nix @@ -359,6 +359,13 @@ rec { ]; }; gnuabi64 = { abi = "64"; }; + muslabi64 = { abi = "64"; }; + + # NOTE: abi=n32 requires a 64-bit MIPS chip! That is not a typo. + # It is basically the 64-bit abi with 32-bit pointers. Details: + # https://www.linux-mips.org/pub/linux/mips/doc/ABI/MIPS-N32-ABI-Handbook.pdf + gnuabin32 = { abi = "n32"; }; + muslabin32 = { abi = "n32"; }; musleabi = { float = "soft"; }; musleabihf = { float = "hard"; }; diff --git a/lib/systems/platforms.nix b/lib/systems/platforms.nix index b2a8dbedef4fc..04d55416242e1 100644 --- a/lib/systems/platforms.nix +++ b/lib/systems/platforms.nix @@ -1,3 +1,10 @@ +# Note: lib/systems/default.nix takes care of producing valid, +# fully-formed "platform" values (e.g. hostPlatform, buildPlatform, +# targetPlatform, etc) containing at least the minimal set of attrs +# required (see types.parsedPlatform in lib/systems/parse.nix). This +# file takes an already-valid platform and further elaborates it with +# optional fields such as linux-kernel, gcc, etc. + { lib }: rec { pc = { @@ -482,6 +489,43 @@ rec { }; }; + # can execute on 32bit chip + gcc_mips32r2_o32 = { gcc = { arch = "mips32r2"; abi = "o32"; }; }; + gcc_mips32r6_o32 = { gcc = { arch = "mips32r6"; abi = "o32"; }; }; + gcc_mips64r2_n32 = { gcc = { arch = "mips64r2"; abi = "n32"; }; }; + gcc_mips64r6_n32 = { gcc = { arch = "mips64r6"; abi = "n32"; }; }; + gcc_mips64r2_64 = { gcc = { arch = "mips64r2"; abi = "64"; }; }; + gcc_mips64r6_64 = { gcc = { arch = "mips64r6"; abi = "64"; }; }; + + # based on: + # https://www.mail-archive.com/qemu-discuss@nongnu.org/msg05179.html + # https://gmplib.org/~tege/qemu.html#mips64-debian + mips64el-qemu-linux-gnuabi64 = (import ./examples).mips64el-linux-gnuabi64 // { + linux-kernel = { + name = "mips64el"; + baseConfig = "64r2el_defconfig"; + target = "vmlinuz"; + autoModules = false; + DTB = true; + # for qemu 9p passthrough filesystem + extraConfig = '' + MIPS_MALTA y + PAGE_SIZE_4KB y + CPU_LITTLE_ENDIAN y + CPU_MIPS64_R2 y + 64BIT y + CPU_MIPS64_R2 y + + NET_9P y + NET_9P_VIRTIO y + 9P_FS y + 9P_FS_POSIX_ACL y + PCI y + VIRTIO_PCI y + ''; + }; + }; + ## ## Other ## @@ -499,6 +543,9 @@ rec { }; }; + # This function takes a minimally-valid "platform" and returns an + # attrset containing zero or more additional attrs which should be + # included in the platform in order to further elaborate it. select = platform: # x86 /**/ if platform.isx86 then pc diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 5fa95828df691..2711190313956 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -761,4 +761,156 @@ runTests { { a = 3; b = 30; c = 300; } ]; }; + + # The example from the showAttrPath documentation + testShowAttrPathExample = { + expr = showAttrPath [ "foo" "10" "bar" ]; + expected = "foo.\"10\".bar"; + }; + + testShowAttrPathEmpty = { + expr = showAttrPath []; + expected = "<root attribute path>"; + }; + + testShowAttrPathVarious = { + expr = showAttrPath [ + "." + "foo" + "2" + "a2-b" + "_bc'de" + ]; + expected = ''".".foo."2".a2-b._bc'de''; + }; + + testGroupBy = { + expr = groupBy (n: toString (mod n 5)) (range 0 16); + expected = { + "0" = [ 0 5 10 15 ]; + "1" = [ 1 6 11 16 ]; + "2" = [ 2 7 12 ]; + "3" = [ 3 8 13 ]; + "4" = [ 4 9 14 ]; + }; + }; + + testGroupBy' = { + expr = groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ]; + expected = { false = 3; true = 12; }; + }; + + # The example from the updateManyAttrsByPath documentation + testUpdateManyAttrsByPathExample = { + expr = updateManyAttrsByPath [ + { + path = [ "a" "b" ]; + update = old: { d = old.c; }; + } + { + path = [ "a" "b" "c" ]; + update = old: old + 1; + } + { + path = [ "x" "y" ]; + update = old: "xy"; + } + ] { a.b.c = 0; }; + expected = { a = { b = { d = 1; }; }; x = { y = "xy"; }; }; + }; + + # If there are no updates, the value is passed through + testUpdateManyAttrsByPathNone = { + expr = updateManyAttrsByPath [] "something"; + expected = "something"; + }; + + # A single update to the root path is just like applying the function directly + testUpdateManyAttrsByPathSingleIncrement = { + expr = updateManyAttrsByPath [ + { + path = [ ]; + update = old: old + 1; + } + ] 0; + expected = 1; + }; + + # Multiple updates can be applied are done in order + testUpdateManyAttrsByPathMultipleIncrements = { + expr = updateManyAttrsByPath [ + { + path = [ ]; + update = old: old + "a"; + } + { + path = [ ]; + update = old: old + "b"; + } + { + path = [ ]; + update = old: old + "c"; + } + ] ""; + expected = "abc"; + }; + + # If an update doesn't use the value, all previous updates are not evaluated + testUpdateManyAttrsByPathLazy = { + expr = updateManyAttrsByPath [ + { + path = [ ]; + update = old: old + throw "nope"; + } + { + path = [ ]; + update = old: "untainted"; + } + ] (throw "start"); + expected = "untainted"; + }; + + # Deeply nested attributes can be updated without affecting others + testUpdateManyAttrsByPathDeep = { + expr = updateManyAttrsByPath [ + { + path = [ "a" "b" "c" ]; + update = old: old + 1; + } + ] { + a.b.c = 0; + + a.b.z = 0; + a.y.z = 0; + x.y.z = 0; + }; + expected = { + a.b.c = 1; + + a.b.z = 0; + a.y.z = 0; + x.y.z = 0; + }; + }; + + # Nested attributes are updated first + testUpdateManyAttrsByPathNestedBeforehand = { + expr = updateManyAttrsByPath [ + { + path = [ "a" ]; + update = old: old // { x = old.b; }; + } + { + path = [ "a" "b" ]; + update = old: old + 1; + } + ] { + a.b = 0; + }; + expected = { + a.b = 1; + a.x = 1; + }; + }; + } diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 350fe85e7487f..8050c6539fc20 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -62,6 +62,13 @@ checkConfigError() { checkConfigOutput '^false$' config.enable ./declare-enable.nix checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix +checkConfigOutput '^1$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix +checkConfigOutput '^2$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix +checkConfigOutput '^42$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix ./declare-bare-submodule-deep-option.nix ./define-bare-submodule-values.nix +checkConfigOutput '^420$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix ./declare-bare-submodule-deep-option.nix ./define-bare-submodule-values.nix +checkConfigOutput '^2$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix ./define-shorthandOnlyDefinesConfig-true.nix +checkConfigError 'The option .bare-submodule.deep. in .*/declare-bare-submodule-deep-option.nix. is already declared in .*/declare-bare-submodule-deep-option-duplicate.nix' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix ./declare-bare-submodule-deep-option-duplicate.nix + # Check integer types. # unsigned checkConfigOutput '^42$' config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix @@ -304,6 +311,12 @@ checkConfigOutput "10" config.processedToplevel ./raw.nix checkConfigError "The option .multiple. is defined multiple times" config.multiple ./raw.nix checkConfigOutput "bar" config.priorities ./raw.nix +## Option collision +checkConfigError \ + 'The option .set. in module .*/declare-set.nix. would be a parent of the following options, but its type .attribute set of signed integers. does not support nested options.\n\s*- option[(]s[)] with prefix .set.enable. in module .*/declare-enable-nested.nix.' \ + config.set \ + ./declare-set.nix ./declare-enable-nested.nix + # Test that types.optionType merges types correctly checkConfigOutput '^10$' config.theOption.int ./optionTypeMerging.nix checkConfigOutput '^"hello"$' config.theOption.str ./optionTypeMerging.nix diff --git a/lib/tests/modules/declare-bare-submodule-deep-option-duplicate.nix b/lib/tests/modules/declare-bare-submodule-deep-option-duplicate.nix new file mode 100644 index 0000000000000..06ad1f6e0a517 --- /dev/null +++ b/lib/tests/modules/declare-bare-submodule-deep-option-duplicate.nix @@ -0,0 +1,10 @@ +{ lib, ... }: +let + inherit (lib) mkOption types; +in +{ + options.bare-submodule.deep = mkOption { + type = types.int; + default = 2; + }; +} diff --git a/lib/tests/modules/declare-bare-submodule-deep-option.nix b/lib/tests/modules/declare-bare-submodule-deep-option.nix new file mode 100644 index 0000000000000..06ad1f6e0a517 --- /dev/null +++ b/lib/tests/modules/declare-bare-submodule-deep-option.nix @@ -0,0 +1,10 @@ +{ lib, ... }: +let + inherit (lib) mkOption types; +in +{ + options.bare-submodule.deep = mkOption { + type = types.int; + default = 2; + }; +} diff --git a/lib/tests/modules/declare-bare-submodule-nested-option.nix b/lib/tests/modules/declare-bare-submodule-nested-option.nix new file mode 100644 index 0000000000000..da125c84b25d4 --- /dev/null +++ b/lib/tests/modules/declare-bare-submodule-nested-option.nix @@ -0,0 +1,19 @@ +{ config, lib, ... }: +let + inherit (lib) mkOption types; +in +{ + options.bare-submodule = mkOption { + type = types.submoduleWith { + shorthandOnlyDefinesConfig = config.shorthandOnlyDefinesConfig; + modules = [ + { + options.nested = mkOption { + type = types.int; + default = 1; + }; + } + ]; + }; + }; +} diff --git a/lib/tests/modules/declare-bare-submodule.nix b/lib/tests/modules/declare-bare-submodule.nix new file mode 100644 index 0000000000000..5402f4ff5a503 --- /dev/null +++ b/lib/tests/modules/declare-bare-submodule.nix @@ -0,0 +1,18 @@ +{ config, lib, ... }: +let + inherit (lib) mkOption types; +in +{ + options.bare-submodule = mkOption { + type = types.submoduleWith { + modules = [ ]; + shorthandOnlyDefinesConfig = config.shorthandOnlyDefinesConfig; + }; + default = {}; + }; + + # config-dependent options: won't recommend, but useful for making this test parameterized + options.shorthandOnlyDefinesConfig = mkOption { + default = false; + }; +} diff --git a/lib/tests/modules/declare-set.nix b/lib/tests/modules/declare-set.nix new file mode 100644 index 0000000000000..853418531a812 --- /dev/null +++ b/lib/tests/modules/declare-set.nix @@ -0,0 +1,12 @@ +{ lib, ... }: + +{ + options.set = lib.mkOption { + default = { }; + example = { a = 1; }; + type = lib.types.attrsOf lib.types.int; + description = '' + Some descriptive text + ''; + }; +} diff --git a/lib/tests/modules/define-bare-submodule-values.nix b/lib/tests/modules/define-bare-submodule-values.nix new file mode 100644 index 0000000000000..00ede929ee663 --- /dev/null +++ b/lib/tests/modules/define-bare-submodule-values.nix @@ -0,0 +1,4 @@ +{ + bare-submodule.nested = 42; + bare-submodule.deep = 420; +} diff --git a/lib/tests/modules/define-shorthandOnlyDefinesConfig-true.nix b/lib/tests/modules/define-shorthandOnlyDefinesConfig-true.nix new file mode 100644 index 0000000000000..bd3a73dce3400 --- /dev/null +++ b/lib/tests/modules/define-shorthandOnlyDefinesConfig-true.nix @@ -0,0 +1 @@ +{ shorthandOnlyDefinesConfig = true; } diff --git a/lib/tests/systems.nix b/lib/tests/systems.nix index 2646e792682ba..c88adbf4651aa 100644 --- a/lib/tests/systems.nix +++ b/lib/tests/systems.nix @@ -17,7 +17,7 @@ with lib.systems.doubles; lib.runTests { testarm = mseteq arm [ "armv5tel-linux" "armv6l-linux" "armv6l-netbsd" "armv6l-none" "armv7a-linux" "armv7a-netbsd" "armv7l-linux" "armv7l-netbsd" "arm-none" "armv7a-darwin" ]; testi686 = mseteq i686 [ "i686-linux" "i686-freebsd" "i686-genode" "i686-netbsd" "i686-openbsd" "i686-cygwin" "i686-windows" "i686-none" "i686-darwin" ]; - testmips = mseteq mips [ "mipsel-linux" "mipsel-netbsd" ]; + testmips = mseteq mips [ "mips64el-linux" "mipsel-linux" "mipsel-netbsd" ]; testmmix = mseteq mmix [ "mmix-mmixware" ]; testx86_64 = mseteq x86_64 [ "x86_64-linux" "x86_64-darwin" "x86_64-freebsd" "x86_64-genode" "x86_64-redox" "x86_64-openbsd" "x86_64-netbsd" "x86_64-cygwin" "x86_64-solaris" "x86_64-windows" "x86_64-none" ]; @@ -28,7 +28,7 @@ with lib.systems.doubles; lib.runTests { testredox = mseteq redox [ "x86_64-redox" ]; testgnu = mseteq gnu (linux /* ++ kfreebsd ++ ... */); testillumos = mseteq illumos [ "x86_64-solaris" ]; - testlinux = mseteq linux [ "aarch64-linux" "armv5tel-linux" "armv6l-linux" "armv7a-linux" "armv7l-linux" "i686-linux" "mipsel-linux" "riscv32-linux" "riscv64-linux" "x86_64-linux" "powerpc64-linux" "powerpc64le-linux" "m68k-linux" "s390-linux" "s390x-linux" ]; + testlinux = mseteq linux [ "aarch64-linux" "armv5tel-linux" "armv6l-linux" "armv7a-linux" "armv7l-linux" "i686-linux" "mips64el-linux" "mipsel-linux" "riscv32-linux" "riscv64-linux" "x86_64-linux" "powerpc64-linux" "powerpc64le-linux" "m68k-linux" "s390-linux" "s390x-linux" ]; testnetbsd = mseteq netbsd [ "aarch64-netbsd" "armv6l-netbsd" "armv7a-netbsd" "armv7l-netbsd" "i686-netbsd" "m68k-netbsd" "mipsel-netbsd" "powerpc-netbsd" "riscv32-netbsd" "riscv64-netbsd" "x86_64-netbsd" ]; testopenbsd = mseteq openbsd [ "i686-openbsd" "x86_64-openbsd" ]; testwindows = mseteq windows [ "i686-cygwin" "x86_64-cygwin" "i686-windows" "x86_64-windows" ]; diff --git a/lib/trivial.nix b/lib/trivial.nix index 52648125059d0..1a8c113b4221e 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -166,6 +166,30 @@ rec { /* Returns the current nixpkgs release number as string. */ release = lib.strings.fileContents ../.version; + /* The latest release that is supported, at the time of release branch-off, + if applicable. + + Ideally, out-of-tree modules should be able to evaluate cleanly with all + supported Nixpkgs versions (master, release and old release until EOL). + So if possible, deprecation warnings should take effect only when all + out-of-tree expressions/libs/modules can upgrade to the new way without + losing support for supported Nixpkgs versions. + + This release number allows deprecation warnings to be implemented such that + they take effect as soon as the oldest release reaches end of life. */ + oldestSupportedRelease = + # Update on master only. Do not backport. + 2111; + + /* Whether a feature is supported in all supported releases (at the time of + release branch-off, if applicable). See `oldestSupportedRelease`. */ + isInOldestRelease = + /* Release number of feature introduction as an integer, e.g. 2111 for 21.11. + Set it to the upcoming release, matching the nixpkgs/.version file. + */ + release: + release <= lib.trivial.oldestSupportedRelease; + /* Returns the current nixpkgs release code name. On each release the first letter is bumped and a new animal is chosen diff --git a/lib/types.nix b/lib/types.nix index 3078615f5ddc0..00d97bf572372 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -572,14 +572,18 @@ rec { let inherit (lib.modules) evalModules; - coerce = unify: value: if isFunction value - then setFunctionArgs (args: unify (value args)) (functionArgs value) - else unify (if shorthandOnlyDefinesConfig then { config = value; } else value); + shorthandToModule = if shorthandOnlyDefinesConfig == false + then value: value + else value: { config = value; }; allModules = defs: imap1 (n: { value, file }: - 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 + if isFunction value + then setFunctionArgs + (args: lib.modules.unifyModuleSyntax file "${toString file}-${toString n}" (value args)) + (functionArgs value) + else if isAttrs value + then + lib.modules.unifyModuleSyntax file "${toString file}-${toString n}" (shorthandToModule value) else value ) defs; @@ -647,7 +651,11 @@ rec { then lhs.specialArgs // rhs.specialArgs else throw "A submoduleWith option is declared multiple times with the same specialArgs \"${toString (attrNames intersecting)}\""; shorthandOnlyDefinesConfig = - if lhs.shorthandOnlyDefinesConfig == rhs.shorthandOnlyDefinesConfig + if lhs.shorthandOnlyDefinesConfig == null + then rhs.shorthandOnlyDefinesConfig + else if rhs.shorthandOnlyDefinesConfig == null + then lhs.shorthandOnlyDefinesConfig + else if lhs.shorthandOnlyDefinesConfig == rhs.shorthandOnlyDefinesConfig then lhs.shorthandOnlyDefinesConfig else throw "A submoduleWith option is declared multiple times with conflicting shorthandOnlyDefinesConfig values"; }; |