From 5988f8f841d7fde752edf611cd0a9c2ea942b4bd Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 12 Mar 2024 08:32:54 -0700 Subject: [PATCH 1/6] lib.systems: use explicit attrset instead of `rec` This allows refactoring in the file without accidentally modifying the public interface of the file. Also, pull in symbols consistently from `lib` instead of `builtins`. --- lib/systems/default.nix | 74 ++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/lib/systems/default.nix b/lib/systems/default.nix index 6137d47e91a2..518c9f66f5ba 100644 --- a/lib/systems/default.nix +++ b/lib/systems/default.nix @@ -1,7 +1,25 @@ { lib }: - let inherit (lib.attrsets) mapAttrs; in -rec { +let + inherit (lib) + any + filterAttrs + foldl + hasInfix + isFunction + isList + isString + mapAttrs + optional + optionalAttrs + optionalString + removeSuffix + replaceStrings + toUpper + ; + + inherit (lib.strings) toJSON; + doubles = import ./doubles.nix { inherit lib; }; parse = import ./parse.nix { inherit lib; }; inspect = import ./inspect.nix { inherit lib; }; @@ -24,7 +42,7 @@ rec { both arguments have been `elaborate`-d. */ equals = - let removeFunctions = a: lib.filterAttrs (_: v: !builtins.isFunction v) a; + let removeFunctions = a: filterAttrs (_: v: !isFunction v) a; in a: b: removeFunctions a == removeFunctions b; /* List of all Nix system doubles the nixpkgs flake will expose the package set @@ -41,7 +59,7 @@ rec { # clearly preferred, and to prevent cycles. A simpler fixed point where the RHS # always just used `final.*` would fail on both counts. elaborate = args': let - args = if lib.isString args' then { system = args'; } + args = if isString args' then { system = args'; } else args'; # TODO: deprecate args.rustc in favour of args.rust after 23.05 is EOL. @@ -96,7 +114,7 @@ rec { then "lib64" else "lib" else null; - extensions = lib.optionalAttrs final.hasSharedLibraries { + extensions = optionalAttrs final.hasSharedLibraries { sharedLibrary = if final.isDarwin then ".dylib" else if final.isWindows then ".dll" @@ -134,9 +152,9 @@ rec { # uname -m processor = if final.isPower64 - then "ppc64${lib.optionalString final.isLittleEndian "le"}" + then "ppc64${optionalString final.isLittleEndian "le"}" else if final.isPower - then "ppc${lib.optionalString final.isLittleEndian "le"}" + then "ppc${optionalString final.isLittleEndian "le"}" else if final.isMips64 then "mips64" # endianness is *not* included on mips64 else final.parsed.cpu.name; @@ -202,8 +220,8 @@ rec { else if final.isS390 && !final.isS390x then null else if final.isx86_64 then "x86_64" else if final.isx86 then "i386" - else if final.isMips64n32 then "mipsn32${lib.optionalString final.isLittleEndian "el"}" - else if final.isMips64 then "mips64${lib.optionalString final.isLittleEndian "el"}" + else if final.isMips64n32 then "mipsn32${optionalString final.isLittleEndian "el"}" + else if final.isMips64 then "mips64${optionalString final.isLittleEndian "el"}" else final.uname.processor; # Name used by UEFI for architectures. @@ -255,7 +273,7 @@ rec { if pkgs.stdenv.hostPlatform.canExecute final then "${pkgs.runtimeShell} -c '\"$@\"' --" else if final.isWindows - then "${wine}/bin/wine${lib.optionalString (final.parsed.cpu.bits == 64) "64"}" + then "${wine}/bin/wine${optionalString (final.parsed.cpu.bits == 64) "64"}" else if final.isLinux && pkgs.stdenv.hostPlatform.isLinux && final.qemuArch != null then "${qemu-user}/bin/qemu-${final.qemuArch}" else if final.isWasi @@ -306,10 +324,10 @@ rec { let f = args.rustc.platform.target-family; in - if builtins.isList f then f else [ f ] + if isList f then f else [ f ] ) - else lib.optional final.isUnix "unix" - ++ lib.optional final.isWindows "windows"; + else optional final.isUnix "unix" + ++ optional final.isWindows "windows"; # https://doc.rust-lang.org/reference/conditional-compilation.html#target_vendor vendor = let @@ -333,13 +351,13 @@ rec { vendor_ = final.rust.platform.vendor; # TODO: deprecate args.rustc in favour of args.rust after 23.05 is EOL. in args.rust.rustcTarget or args.rustc.config - or "${cpu_}-${vendor_}-${kernel.name}${lib.optionalString (abi.name != "unknown") "-${abi.name}"}"; + or "${cpu_}-${vendor_}-${kernel.name}${optionalString (abi.name != "unknown") "-${abi.name}"}"; # The name of the rust target if it is standard, or the json file # containing the custom target spec. rustcTargetSpec = rust.rustcTargetSpec or ( /**/ if rust ? platform - then builtins.toFile (final.rust.rustcTarget + ".json") (builtins.toJSON rust.platform) + then builtins.toFile (final.rust.rustcTarget + ".json") (toJSON rust.platform) else final.rust.rustcTarget); # The name of the rust target if it is standard, or the @@ -348,7 +366,7 @@ rec { # # This is the name used by Cargo for target subdirectories. cargoShortTarget = - lib.removeSuffix ".json" (baseNameOf "${final.rust.rustcTargetSpec}"); + removeSuffix ".json" (baseNameOf "${final.rust.rustcTargetSpec}"); # When used as part of an environment variable name, triples are # uppercased and have all hyphens replaced by underscores: @@ -356,17 +374,17 @@ rec { # https://github.com/rust-lang/cargo/pull/9169 # https://github.com/rust-lang/cargo/issues/8285#issuecomment-634202431 cargoEnvVarTarget = - lib.strings.replaceStrings ["-"] ["_"] - (lib.strings.toUpper final.rust.cargoShortTarget); + replaceStrings ["-"] ["_"] + (toUpper final.rust.cargoShortTarget); # True if the target is no_std # https://github.com/rust-lang/rust/blob/2e44c17c12cec45b6a682b1e53a04ac5b5fcc9d2/src/bootstrap/config.rs#L415-L421 isNoStdTarget = - builtins.any (t: lib.hasInfix t final.rust.rustcTarget) ["-none" "nvptx" "switch" "-uefi"]; + any (t: hasInfix t final.rust.rustcTarget) ["-none" "nvptx" "switch" "-uefi"]; }; }; in assert final.useAndroidPrebuilt -> final.isAndroid; - assert lib.foldl + assert foldl (pass: { assertion, message }: if assertion final then pass @@ -374,4 +392,20 @@ rec { true (final.parsed.abi.assertions or []); final; + +in + +# Everything in this attrset is the public interface of the file. +{ + inherit + architectures + doubles + elaborate + equals + examples + flakeExposed + inspect + parse + platforms + ; } From 123a2f0fcc96cc7828ef942a88d3608b55d6d81c Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 12 Mar 2024 08:37:53 -0700 Subject: [PATCH 2/6] lib/systems: use lib.systems.parse and lib.systems.inspect.predicates instead of re-importing --- lib/systems/inspect.nix | 2 +- lib/systems/parse.nix | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/systems/inspect.nix b/lib/systems/inspect.nix index c6a33781ae28..3ce1db2b8987 100644 --- a/lib/systems/inspect.nix +++ b/lib/systems/inspect.nix @@ -1,5 +1,5 @@ { lib }: -with import ./parse.nix { inherit lib; }; +with lib.systems.parse; with lib.attrsets; with lib.lists; diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix index b69ad669e187..7161b30dc571 100644 --- a/lib/systems/parse.nix +++ b/lib/systems/parse.nix @@ -19,7 +19,7 @@ with lib.lists; with lib.types; with lib.attrsets; with lib.strings; -with (import ./inspect.nix { inherit lib; }).predicates; +with lib.systems.inspect.predicates; let inherit (lib.options) mergeOneOption; From af634f14baa740e26664a9643788a35b8385b437 Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 12 Mar 2024 08:41:59 -0700 Subject: [PATCH 3/6] lib/systems: inherit from lib.systems.parse in lib/systems/inspect.nix --- lib/systems/inspect.nix | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/systems/inspect.nix b/lib/systems/inspect.nix index 3ce1db2b8987..1a402d0c1c72 100644 --- a/lib/systems/inspect.nix +++ b/lib/systems/inspect.nix @@ -1,10 +1,19 @@ { lib }: -with lib.systems.parse; + with lib.attrsets; with lib.lists; -let abis_ = abis; in -let abis = lib.mapAttrs (_: abi: builtins.removeAttrs abi [ "assertions" ]) abis_; in +let + inherit (lib.systems.parse) + kernels + kernelFamilies + significantBytes + cpuTypes + execFormats + ; + + abis = lib.mapAttrs (_: abi: builtins.removeAttrs abi [ "assertions" ]) lib.systems.parse.abis; +in rec { # these patterns are to be matched against {host,build,target}Platform.parsed From 07d3270dbccfc520cc4ffc85e8dee5efe2fe1676 Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 12 Mar 2024 08:44:52 -0700 Subject: [PATCH 4/6] lib/systems: inherit from lib.systems.inspect.predicates in lib/systems/parse.nix --- lib/systems/parse.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix index 7161b30dc571..9721024e700a 100644 --- a/lib/systems/parse.nix +++ b/lib/systems/parse.nix @@ -19,9 +19,16 @@ with lib.lists; with lib.types; with lib.attrsets; with lib.strings; -with lib.systems.inspect.predicates; let + inherit (lib.systems.inspect.predicates) + isAarch32 + isBigEndian + isDarwin + isLinux + isPower64 + isWindows + ; inherit (lib.options) mergeOneOption; setTypes = type: From c02fcc946a1786cad114f9ec04686e19642b5f48 Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 12 Mar 2024 09:01:18 -0700 Subject: [PATCH 5/6] Avoid top-level `with ...;` in lib/systems/inspect.nix --- lib/systems/inspect.nix | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/systems/inspect.nix b/lib/systems/inspect.nix index 1a402d0c1c72..ebc7ab366876 100644 --- a/lib/systems/inspect.nix +++ b/lib/systems/inspect.nix @@ -1,9 +1,21 @@ { lib }: -with lib.attrsets; -with lib.lists; - let + inherit (lib) + any + attrValues + concatMap + filter + hasPrefix + isList + mapAttrs + matchAttrs + recursiveUpdateUntil + toList + ; + + inherit (lib.strings) toJSON; + inherit (lib.systems.parse) kernels kernelFamilies @@ -12,7 +24,7 @@ let execFormats ; - abis = lib.mapAttrs (_: abi: builtins.removeAttrs abi [ "assertions" ]) lib.systems.parse.abis; + abis = mapAttrs (_: abi: removeAttrs abi [ "assertions" ]) lib.systems.parse.abis; in rec { @@ -41,8 +53,8 @@ rec { isx86 = { cpu = { family = "x86"; }; }; isAarch32 = { cpu = { family = "arm"; bits = 32; }; }; isArmv7 = map ({ arch, ... }: { cpu = { inherit arch; }; }) - (lib.filter (cpu: lib.hasPrefix "armv7" cpu.arch or "") - (lib.attrValues cpuTypes)); + (filter (cpu: hasPrefix "armv7" cpu.arch or "") + (attrValues cpuTypes)); isAarch64 = { cpu = { family = "arm"; bits = 64; }; }; isAarch = { cpu = { family = "arm"; }; }; isMicroBlaze = { cpu = { family = "microblaze"; }; }; @@ -120,19 +132,19 @@ rec { let # patterns can be either a list or a (bare) singleton; turn # them into singletons for uniform handling - pat1 = lib.toList pat1_; - pat2 = lib.toList pat2_; + pat1 = toList pat1_; + pat2 = toList pat2_; in - lib.concatMap (attr1: + concatMap (attr1: map (attr2: - lib.recursiveUpdateUntil + recursiveUpdateUntil (path: subattr1: subattr2: if (builtins.intersectAttrs subattr1 subattr2) == {} || subattr1 == subattr2 then true else throw '' pattern conflict at path ${toString path}: - ${builtins.toJSON subattr1} - ${builtins.toJSON subattr2} + ${toJSON subattr1} + ${toJSON subattr2} '') attr1 attr2 @@ -141,7 +153,7 @@ rec { pat1; matchAnyAttrs = patterns: - if builtins.isList patterns then attrs: any (pattern: matchAttrs pattern attrs) patterns + if isList patterns then attrs: any (pattern: matchAttrs pattern attrs) patterns else matchAttrs patterns; predicates = mapAttrs (_: matchAnyAttrs) patterns; From 79ce46fe49f27bf034f1d8792c92120018a50dc3 Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 12 Mar 2024 08:49:46 -0700 Subject: [PATCH 6/6] Avoid top-level `with ...;` in lib/systems/parse.nix --- lib/systems/parse.nix | 59 +++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix index 9721024e700a..191e9734b879 100644 --- a/lib/systems/parse.nix +++ b/lib/systems/parse.nix @@ -15,12 +15,26 @@ # systems that overlap with existing ones and won't notice something amiss. # { lib }: -with lib.lists; -with lib.types; -with lib.attrsets; -with lib.strings; let + inherit (lib) + all + any + attrValues + elem + elemAt + hasPrefix + id + length + mapAttrs + mergeOneOption + optionalString + splitString + versionAtLeast + ; + + inherit (lib.strings) match; + inherit (lib.systems.inspect.predicates) isAarch32 isBigEndian @@ -29,7 +43,17 @@ let isPower64 isWindows ; - inherit (lib.options) mergeOneOption; + + inherit (lib.types) + enum + float + isType + mkOptionType + number + setType + string + types + ; setTypes = type: mapAttrs (name: value: @@ -40,10 +64,10 @@ let # regex `e?abi.*$` when determining the validity of a triple. In # other words, `i386-linuxabichickenlips` is a valid triple. removeAbiSuffix = x: - let match = builtins.match "(.*)e?abi.*" x; - in if match==null + let found = match "(.*)e?abi.*" x; + in if found == null then x - else lib.elemAt match 0; + else elemAt found 0; in @@ -83,7 +107,7 @@ rec { types.cpuType = enum (attrValues cpuTypes); - cpuTypes = with significantBytes; setTypes types.openCpuType { + cpuTypes = let inherit (significantBytes) bigEndian littleEndian; in setTypes types.openCpuType { arm = { bits = 32; significantByte = littleEndian; family = "arm"; }; armv5tel = { bits = 32; significantByte = littleEndian; family = "arm"; version = "5"; arch = "armv5t"; }; armv6m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; arch = "armv6-m"; }; @@ -173,7 +197,7 @@ rec { # Note: Since 22.11 the archs of a mode switching CPU are no longer considered # pairwise compatible. Mode switching implies that binaries built for A # and B respectively can't be executed at the same time. - isCompatible = a: b: with cpuTypes; lib.any lib.id [ + isCompatible = with cpuTypes; a: b: any id [ # x86 (b == i386 && isCompatible a i486) (b == i486 && isCompatible a i586) @@ -294,7 +318,10 @@ rec { types.kernel = enum (attrValues kernels); - kernels = with execFormats; with kernelFamilies; setTypes types.openKernel { + kernels = let + inherit (execFormats) elf pe wasm unknown macho; + inherit (kernelFamilies) bsd darwin; + in setTypes types.openKernel { # TODO(@Ericson2314): Don't want to mass-rebuild yet to keeping 'darwin' as # the normalized name for macOS. macos = { execFormat = macho; families = { inherit darwin; }; name = "darwin"; }; @@ -366,7 +393,7 @@ rec { The "gnu" ABI is ambiguous on 32-bit ARM. Use "gnueabi" or "gnueabihf" instead. ''; } - { assertion = platform: with platform; !(isPower64 && isBigEndian); + { assertion = platform: !(platform.isPower64 && platform.isBigEndian); message = '' The "gnu" ABI is ambiguous on big-endian 64-bit PowerPC. Use "gnuabielfv2" or "gnuabielfv1" instead. ''; @@ -487,7 +514,7 @@ rec { /**/ if args ? abi then getAbi args.abi else if isLinux parsed || isWindows parsed then if isAarch32 parsed then - if lib.versionAtLeast (parsed.cpu.version or "0") "6" + if versionAtLeast (parsed.cpu.version or "0") "6" then abis.gnueabihf else abis.gnueabi # Default ppc64 BE to ELFv2 @@ -498,7 +525,7 @@ rec { in mkSystem parsed; - mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (lib.splitString "-" s)); + mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (splitString "-" s)); kernelName = kernel: kernel.name + toString (kernel.version or ""); @@ -510,10 +537,10 @@ rec { tripleFromSystem = { cpu, vendor, kernel, abi, ... } @ sys: assert isSystem sys; let optExecFormat = - lib.optionalString (kernel.name == "netbsd" && + optionalString (kernel.name == "netbsd" && gnuNetBSDDefaultExecFormat cpu != kernel.execFormat) kernel.execFormat.name; - optAbi = lib.optionalString (abi != abis.unknown) "-${abi.name}"; + optAbi = optionalString (abi != abis.unknown) "-${abi.name}"; in "${cpu.name}-${vendor.name}-${kernelName kernel}${optExecFormat}${optAbi}"; ################################################################################