From 9850da8aa9dc9be22e237c9b424a18e801e53ecb Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 16 Jun 2024 02:24:21 -0700 Subject: [PATCH] fix: resolve issue with extending cross package --- lib/src/modules/default.test.nix | 110 ++++++++++++++++++ lib/src/types/default.nix | 20 ++-- tidepool/default.nix | 3 + tidepool/flake.lock | 16 +-- tidepool/src/builders/basic.nix | 9 +- tidepool/src/export.nix | 13 +++ tidepool/src/lib/systems.nix | 32 ++--- tidepool/src/lib/types.nix | 17 ++- tidepool/src/packages/default.nix | 13 ++- .../src/packages/foundation/gcc/default.nix | 57 ++++++--- 10 files changed, 223 insertions(+), 67 deletions(-) diff --git a/lib/src/modules/default.test.nix b/lib/src/modules/default.test.nix index 788e2fa..9e30e79 100644 --- a/lib/src/modules/default.test.nix +++ b/lib/src/modules/default.test.nix @@ -601,5 +601,115 @@ in { }; in evaluated.config.aux.message == evaluated.config.aux.message2; + + "base level submodule" = let + expected = "Hello, World!"; + evaluated = lib.modules.run { + modules = [ + { + options = { + aux = lib.options.create { + type = lib.types.submodule { + options.message = lib.options.create { + type = lib.types.string; + }; + }; + }; + }; + config = { + aux = args: { + options.message2 = lib.options.create { + type = lib.types.string; + }; + + config.message = expected; + }; + }; + } + { + config = { + aux.message2 = expected; + }; + } + ]; + }; + in + evaluated.config.aux.message == evaluated.config.aux.message2; + + "base level submodule (freeform)" = let + expected = "Hello, World!"; + evaluated = lib.modules.run { + modules = [ + { + options = { + aux = lib.options.create { + type = lib.types.submodule { + freeform = lib.types.any; + + options.message = lib.options.create { + type = lib.types.string; + }; + }; + }; + }; + config = { + aux = args: { + options.message2 = lib.options.create { + type = lib.types.string; + }; + + config.message = expected; + }; + }; + } + { + config = { + aux.exists = true; + aux.message2 = expected; + }; + } + ]; + }; + in + (evaluated.config.aux.message == evaluated.config.aux.message2) + && evaluated.config.aux.exists; + + "nested submodules" = let + expected = "Hello, World!"; + evaluated = lib.modules.run { + modules = [ + { + options = { + aux = lib.options.create { + type = lib.types.submodule { + freeform = lib.types.any; + + options.message = lib.options.create { + type = lib.types.string; + }; + }; + }; + }; + config = { + aux = args: { + options.message2 = lib.options.create { + type = lib.types.string; + }; + + config.message = expected; + }; + }; + } + { + config = { + aux.exists = true; + aux.message2 = expected; + }; + } + ]; + }; + in + (evaluated.config.aux.message == evaluated.config.aux.message2) + && evaluated.config.aux.exists; }; } diff --git a/lib/src/types/default.nix b/lib/src/types/default.nix index c6c990f..7582f9a 100644 --- a/lib/src/types/default.nix +++ b/lib/src/types/default.nix @@ -775,16 +775,20 @@ lib: { ["freeform"]; in if definition.value ? config - then { - __file__ = definition.__file__; - config = definition.value.config; - } + then + rest + // { + __file__ = definition.__file__; + config = definition.value.config; + } else let config = builtins.removeAttrs definition.value lib.modules.VALID_KEYS; - in { - __file__ = definition.__file__; - config = config; - } + in + rest + // { + __file__ = definition.__file__; + config = config; + } else { __file__ = definition.__file__; includes = [definition.value]; diff --git a/tidepool/default.nix b/tidepool/default.nix index 5ada914..6b039d1 100644 --- a/tidepool/default.nix +++ b/tidepool/default.nix @@ -22,3 +22,6 @@ }; in result.config.exported + // { + inherit (result) config; + } diff --git a/tidepool/flake.lock b/tidepool/flake.lock index 4572262..9eadd0d 100644 --- a/tidepool/flake.lock +++ b/tidepool/flake.lock @@ -8,10 +8,10 @@ }, "locked": { "dir": "foundation", - "dirtyRev": "6bd35bf4d5f24b8e91d8afa589ea3088796b2baa-dirty", - "dirtyShortRev": "6bd35bf-dirty", - "lastModified": 1718450285, - "narHash": "sha256-/i3RlOudzZdl6UBdhMDRBAgVQTHOZDXEkSfRUw77aK8=", + "dirtyRev": "0312e3c4cc261e2384fcf372c766a0cf245f3213-dirty", + "dirtyShortRev": "0312e3c-dirty", + "lastModified": 1718460525, + "narHash": "sha256-+ToaXY8ISWLx9AtO/CjfY/6SGuKu8A/d9ncqE4h2H20=", "type": "git", "url": "file:../?dir=foundation" }, @@ -24,10 +24,10 @@ "lib": { "locked": { "dir": "lib", - "dirtyRev": "6bd35bf4d5f24b8e91d8afa589ea3088796b2baa-dirty", - "dirtyShortRev": "6bd35bf-dirty", - "lastModified": 1718450285, - "narHash": "sha256-/i3RlOudzZdl6UBdhMDRBAgVQTHOZDXEkSfRUw77aK8=", + "dirtyRev": "0312e3c4cc261e2384fcf372c766a0cf245f3213-dirty", + "dirtyShortRev": "0312e3c-dirty", + "lastModified": 1718460525, + "narHash": "sha256-+ToaXY8ISWLx9AtO/CjfY/6SGuKu8A/d9ncqE4h2H20=", "type": "git", "url": "file:../?dir=lib" }, diff --git a/tidepool/src/builders/basic.nix b/tidepool/src/builders/basic.nix index ccde394..2ee4109 100644 --- a/tidepool/src/builders/basic.nix +++ b/tidepool/src/builders/basic.nix @@ -26,8 +26,8 @@ in { sorted.result; system = package.platform.build.double; - in - builtins.derivation ( + + built = builtins.derivation ( package.env // { inherit (package) name; @@ -70,6 +70,11 @@ in { ]; } ); + in + built + // { + inherit (package) meta; + }; }; }; } diff --git a/tidepool/src/export.nix b/tidepool/src/export.nix index ba35e4a..c0a8c14 100644 --- a/tidepool/src/export.nix +++ b/tidepool/src/export.nix @@ -6,16 +6,29 @@ }: let lib' = config.lib; in { + freeform = lib.types.any; + config = { exports = { lib = config.lib; modules = import ./modules.nix; packages = { + # foundation-gcc-x86_64 = + # (config.packages.foundation.gcc.versions."13.2.0".extend (args: { + # config = { + # platform = { + # target = lib.modules.overrides.force "x86_64-linux"; + # }; + # }; + # })) + # .config; foundation-gcc = config.packages.foundation.gcc; # example-x = config.packages.example.x; # cross-example-x-x86_64-linux = config.packages.cross.x86_64-linux.example.x; }; }; + + # exported.packages.i686-linux.cross-foundation-gcc-x86_64-linux = config.packages.cross.x86_64-linux.foundation.gcc.package; }; } diff --git a/tidepool/src/lib/systems.nix b/tidepool/src/lib/systems.nix index 7f2ae97..94ea624 100644 --- a/tidepool/src/lib/systems.nix +++ b/tidepool/src/lib/systems.nix @@ -1909,28 +1909,26 @@ in { then {system = args;} else args; - matchers = builtins.mapAttrs (name: match: match resolved.system) lib'.systems.match; - validators = builtins.mapAttrs (name: validate: validate (resolved.gcc.arch or "default")) lib'.systems.match.architecture; - - platformInfo = - { - linux-kernel = settings.linux-kernel or {}; - gcc = settings.gcc or {}; - rustc = settings.rustc or {}; - } - // lib'.systems.platforms.select resolved; - resolved = - matchers - // validators - // platformInfo - // { + { system = lib'.systems.from.string ( if settings ? triple then settings.triple else settings.system ); + inherit + ({ + linux-kernel = settings.linux-kernel or {}; + gcc = settings.gcc or {}; + rustc = settings.rustc or {}; + } + // lib'.systems.platforms.select resolved) + linux-kernel + gcc + rust + ; + double = lib'.systems.into.double resolved.system; triple = lib'.systems.into.triple resolved.system; @@ -2162,6 +2160,8 @@ in { }; }; } + // builtins.mapAttrs (name: match: match resolved.system) lib'.systems.match + // builtins.mapAttrs (name: validate: validate (resolved.gcc.arch or "default")) lib'.systems.validate.architecture // settings; assertions = @@ -2178,7 +2178,7 @@ in { true (resolved.system.abi.assertions or []); in - assert resolved.useAndroidPrebuild -> resolved.isAndroid; + assert resolved.useAndroidPrebuilt -> resolved.isAndroid; assert assertions; # And finally, return the generated system info. resolved; diff --git a/tidepool/src/lib/types.nix b/tidepool/src/lib/types.nix index 414d5df..8945019 100644 --- a/tidepool/src/lib/types.nix +++ b/tidepool/src/lib/types.nix @@ -82,6 +82,13 @@ extend = lib.options.create { description = "Extend the package's submodules with additional configuration."; type = lib.types.function lib.types.raw; + default.value = value: + meta.extend { + modules = + if builtins.isAttrs value + then [{config = value;}] + else lib.lists.from.any value; + }; }; name = lib.options.create { @@ -291,16 +298,6 @@ default.value = config.builder.build config; }; }; - - config = { - extend = value: - meta.extend { - modules = - if builtins.isAttrs value - then [{config = value;}] - else lib.lists.from.any value; - }; - }; }); }; }; diff --git a/tidepool/src/packages/default.nix b/tidepool/src/packages/default.nix index 3ef6b4a..09d4d70 100644 --- a/tidepool/src/packages/default.nix +++ b/tidepool/src/packages/default.nix @@ -15,9 +15,9 @@ in { packages = lib.options.create { description = "The package set."; type = lib.types.submodule { - freeform = lib.modules.overrides.force (lib.types.attrs.of (lib.types.submodule { - freeform = lib.modules.overrides.force lib'.types.alias; - })); + freeform = lib.types.attrs.of (lib.types.submodule { + freeform = lib'.types.alias; + }); options.cross = lib.attrs.generate doubles (system: lib.options.create { @@ -37,7 +37,7 @@ in { }; }; - config.packages.config.cross = lib.attrs.generate doubles ( + config.packages.cross = lib.attrs.generate doubles ( system: builtins.mapAttrs ( @@ -47,7 +47,7 @@ in { setHost = package: if package != {} then - package.extend ({config}: { + (package.extend ({config}: { config = { platform = { host = lib.modules.overrides.force system; @@ -72,7 +72,8 @@ in { }; }; }; - }) + })) + .config else package; updated = diff --git a/tidepool/src/packages/foundation/gcc/default.nix b/tidepool/src/packages/foundation/gcc/default.nix index 5657d52..86af967 100644 --- a/tidepool/src/packages/foundation/gcc/default.nix +++ b/tidepool/src/packages/foundation/gcc/default.nix @@ -15,7 +15,10 @@ in { config.packages.foundation.gcc = { versions = { - "13.2.0" = {config}: { + "13.2.0" = { + config, + meta, + }: { options = { src = lib.options.create { type = lib.types.derivation; @@ -88,23 +91,42 @@ in { builder = builders.basic; env = { - PATH = lib.paths.bin [ - foundation.stage2-gcc - foundation.stage2-binutils - foundation.stage2-gnumake - foundation.stage2-gnused - foundation.stage2-gnugrep - foundation.stage2-gawk - foundation.stage2-diffutils - foundation.stage2-findutils - foundation.stage2-gnutar - foundation.stage2-gzip - foundation.stage2-bzip2 - foundation.stage1-xz - ]; + PATH = let + gcc = + if config.platform.build.triple == config.platform.host.triple + # If we're on the same system then we can use the existing GCC instance. + then foundation.stage2-gcc + # Otherwise we are going to need a cross-compiler. + else + (meta.extend (args: { + config = { + platform = { + target = lib.modules.override.force config.platform.host.triple; + }; + }; + })) + .config + .package; + in + lib.paths.bin [ + foundation.stage2-gcc + foundation.stage2-binutils + foundation.stage2-gnumake + foundation.stage2-gnused + foundation.stage2-gnugrep + foundation.stage2-gawk + foundation.stage2-diffutils + foundation.stage2-findutils + foundation.stage2-gnutar + foundation.stage2-gzip + foundation.stage2-bzip2 + foundation.stage1-xz + ]; }; - phases = { + phases = let + host = lib'.systems.withBuildInfo config.platform.host; + in { unpack = lib.dag.entry.before ["patch"] '' # Unpack tar xf ${config.src} @@ -128,7 +150,7 @@ in { configure = lib.dag.entry.between ["build"] ["patch"] '' # Configure - export CC="gcc -Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" + export CC="gcc -Wl,-dynamic-linker -march=${host.gcc.arch or host.system.cpu.arch} -Wl,${foundation.stage1-musl}/lib/libc.so" export CXX="g++ -Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" export CFLAGS_FOR_TARGET="-Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" export LIBRARY_PATH="${foundation.stage1-musl}/lib" @@ -137,6 +159,7 @@ in { --prefix=$out \ --build=${config.platform.build.triple} \ --host=${config.platform.host.triple} \ + --target=${config.platform.target.triple} \ --with-native-system-header-dir=/include \ --with-sysroot=${foundation.stage1-musl} \ --enable-languages=c,c++ \