From 1c67c402131f980996a083d9212ee9dec2f8e2a3 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sat, 15 Jun 2024 03:58:25 -0700 Subject: [PATCH] feat: cross compilation package set --- lib/src/options/default.nix | 2 +- lib/src/types/default.nix | 24 ++++++----- tidepool/flake.lock | 16 +++---- tidepool/src/export.nix | 1 + tidepool/src/packages/default.nix | 69 +++++++++++++++++++++++++++++-- 5 files changed, 89 insertions(+), 23 deletions(-) diff --git a/lib/src/options/default.nix b/lib/src/options/default.nix index c6fdd48..f256195 100644 --- a/lib/src/options/default.nix +++ b/lib/src/options/default.nix @@ -94,7 +94,7 @@ lib: { typeSet = lib.attrs.when ((shared "type") && isTypeMergeable) { type = mergedType; }; - files = builtins.map lib.modules.getFiles result.declarations; + files = result.declarations; serializedFiles = builtins.concatStringsSep " and " files; getSubModules = option.options.type.getSubModules or null; submodules = diff --git a/lib/src/types/default.nix b/lib/src/types/default.nix index b83fad3..c6c990f 100644 --- a/lib/src/types/default.nix +++ b/lib/src/types/default.nix @@ -767,19 +767,21 @@ lib: { definition: if shorthand && builtins.isAttrs definition.value then let - config = - definition.value.config - or ( - builtins.removeAttrs definition.value - (builtins.filter (key: key != "config") lib.modules.VALID_KEYS) - ); + # TODO: Figure out if we can apply additional attributes to the generated module. + # Currently this causes issues to do with redefined options. rest = - if definition.value ? config - then builtins.removeAttrs definition.value ["config"] - else lib.attrs.filter (name: value: builtins.elem name lib.modules.VALID_KEYS) definition.value; + builtins.removeAttrs + (lib.attrs.filter (name: value: builtins.elem name lib.modules.VALID_KEYS) definition.value) + ["freeform"]; in - rest - // { + if definition.value ? config + then { + __file__ = definition.__file__; + config = definition.value.config; + } + else let + config = builtins.removeAttrs definition.value lib.modules.VALID_KEYS; + in { __file__ = definition.__file__; config = config; } diff --git a/tidepool/flake.lock b/tidepool/flake.lock index e3e3dc6..8ec01e0 100644 --- a/tidepool/flake.lock +++ b/tidepool/flake.lock @@ -8,10 +8,10 @@ }, "locked": { "dir": "foundation", - "dirtyRev": "aa1c58f6ee8a3ad6ad569f34477192462533c16b-dirty", - "dirtyShortRev": "aa1c58f-dirty", - "lastModified": 1718411218, - "narHash": "sha256-LOzQGGygC2U08zNwg1YNljjrJKJxnJ8S4RkX2v81yRw=", + "dirtyRev": "2989fdc4fe153f13e7b6b476476805b03c419f0c-dirty", + "dirtyShortRev": "2989fdc-dirty", + "lastModified": 1718444137, + "narHash": "sha256-gArdjry0N4et4B+wQa31JyLJ448JWd9OFA1Roe3EOAc=", "type": "git", "url": "file:../?dir=foundation" }, @@ -24,10 +24,10 @@ "lib": { "locked": { "dir": "lib", - "dirtyRev": "aa1c58f6ee8a3ad6ad569f34477192462533c16b-dirty", - "dirtyShortRev": "aa1c58f-dirty", - "lastModified": 1718411218, - "narHash": "sha256-LOzQGGygC2U08zNwg1YNljjrJKJxnJ8S4RkX2v81yRw=", + "dirtyRev": "2989fdc4fe153f13e7b6b476476805b03c419f0c-dirty", + "dirtyShortRev": "2989fdc-dirty", + "lastModified": 1718444137, + "narHash": "sha256-gArdjry0N4et4B+wQa31JyLJ448JWd9OFA1Roe3EOAc=", "type": "git", "url": "file:../?dir=lib" }, diff --git a/tidepool/src/export.nix b/tidepool/src/export.nix index afb6e36..0b4c052 100644 --- a/tidepool/src/export.nix +++ b/tidepool/src/export.nix @@ -13,6 +13,7 @@ in { packages = { example-x = config.packages.example.x; + cross-example-x-x86_64-linux = config.packages.cross.x86_64-linux.example.x; }; }; }; diff --git a/tidepool/src/packages/default.nix b/tidepool/src/packages/default.nix index df5b01f..49bafa4 100644 --- a/tidepool/src/packages/default.nix +++ b/tidepool/src/packages/default.nix @@ -1,10 +1,11 @@ { lib, + lib', config, }: let - lib' = config.lib; - doubles = lib'.systems.doubles.all; + + packages = builtins.removeAttrs config.packages ["cross"]; in { includes = [ ./aux/foundation.nix @@ -13,7 +14,18 @@ in { options = { packages = lib.options.create { description = "The package set."; - type = lib'.types.packages; + type = lib.types.submodule { + freeform = lib.modules.overrides.force (lib.types.attrs.of (lib.types.submodule { + freeform = lib.modules.overrides.force lib'.types.alias; + })); + + options.cross = lib.attrs.generate doubles (system: + lib.options.create { + description = "The cross-compiled package set for the ${system} target."; + type = lib'.types.packages; + default = {}; + }); + }; }; preferences.packages = { @@ -24,4 +36,55 @@ in { }; }; }; + + config.packages.cross = lib.attrs.generate doubles ( + system: + builtins.mapAttrs + ( + namespace: + builtins.mapAttrs + (name: alias: let + setHost = package: + if package != {} + then + package.extend ({config}: { + config = { + platform = { + host = lib.modules.overrides.force system; + }; + + deps = { + build = { + only = setHost package.deps.build.only; + build = setHost package.deps.build.build; + host = setHost package.deps.build.host; + target = setHost package.deps.build.target; + }; + host = { + only = setHost package.deps.host.only; + host = setHost package.deps.host.host; + target = setHost package.deps.host.target; + }; + target = { + only = setHost package.deps.target.only; + target = setHost package.deps.target.target; + }; + }; + }; + }) + else package; + + updated = + alias + // { + versions = + builtins.mapAttrs + (version: package: setHost package) + alias.versions; + }; + in + updated) + ) + packages + ); }