From fea8c2cd9ca19c1f78a4722d4828e0c2aa7f4aa5 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Fri, 14 Jun 2024 04:47:54 -0700 Subject: [PATCH] feat: working package export --- README.md | 9 +++--- lib/src/modules/default.nix | 14 +++++++++ tidepool/README.md | 28 +++++++++++++++++ tidepool/default.nix | 2 ++ tidepool/flake.lock | 16 +++++----- tidepool/src/exports/packages.nix | 29 ++++++++++++++++-- tidepool/src/lib/default.nix | 1 + tidepool/src/lib/packages.nix | 37 ++++++++++++++++++++++ tidepool/src/lib/types.nix | 14 ++++----- tidepool/src/packages/aux/foundation.nix | 39 ++++++++++++++++++++++++ tidepool/src/packages/default.nix | 38 +---------------------- 11 files changed, 168 insertions(+), 59 deletions(-) create mode 100644 tidepool/README.md create mode 100644 tidepool/src/lib/packages.nix create mode 100644 tidepool/src/packages/aux/foundation.nix diff --git a/README.md b/README.md index 765c858..915cf3d 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ may collaborate. ## Experiments -| Name | Phase | Description | -| ------------------------------ | --------- | -------------------------------------------------------------------------- | -| [Aux Lib](./lib) | Iteration | A library of common functions used in the Aux ecosystem. | -| [Aux Foundation](./foundation) | Iteration | Foundational packages which allow for bootstrapping a greater package set. | +| Name | Phase | Description | +| ------------------------------ | --------- | ------------------------------------------------------------------------------------ | +| [Aux Lib](./lib) | Iteration | A library of common functions used in the Aux ecosystem. | +| [Aux Foundation](./foundation) | Iteration | Foundational packages which allow for bootstrapping a greater package set. | +| [Aux Tidepool](./tidepool) | Idea | An initial package set built on top of Aux Foundation using Aux Lib's module system. | diff --git a/lib/src/modules/default.nix b/lib/src/modules/default.nix index 2df9967..6aa4bcf 100644 --- a/lib/src/modules/default.nix +++ b/lib/src/modules/default.nix @@ -317,6 +317,20 @@ lib: { vm = lib.modules.override 10; }; + ## Create alias definitions for a given option. + ## + ## @type Option -> Attrs + alias = lib.modules.aliasWith lib.fp.id; + + ## Create alias definitions for a given option. + ## + ## @type (Attrs -> Attrs) -> Option -> Attrs + aliasWith = f: option: let + exists = lib.types.is "option" option && option.isDefined; + value = f (lib.modules.merge option.definitions); + in + lib.modules.when exists value; + ## Combine multiple modules together. ## ## @type List String -> List Module -> { matched :: Attrs, unmatched :: List Definition } diff --git a/tidepool/README.md b/tidepool/README.md new file mode 100644 index 0000000..0949df4 --- /dev/null +++ b/tidepool/README.md @@ -0,0 +1,28 @@ +# Aux Tidepool + +Aux Tidepool is an initial package set built on top of [Aux Foundation](../foundation). Packages +are created and managed using [Aux Lib](../lib)'s module system to allow for highly dynamic and +extensible configuration. + +## Usage + +Packages can be imported both with and without Nix Flakes. To import them using Nix Flakes, +add this repository as an input. + +```nix +inputs.tidepool.url = "https://git.auxolotl.org/auxolotl/labs/archive/main.tar.gz?dir=tidepool"; +``` + +To import this library without using Nix Flakes, you will need to use `fetchTarball` and +import the library entrypoint. + +```nix +let + labs = builtins.fetchTarball { + url = "https://git.auxolotl.org/auxolotl/labs/archive/main.tar.gz"; + sha256 = ""; + }; + tidepool = import "${labs}/tidepool" {}; +in + # ... +``` diff --git a/tidepool/default.nix b/tidepool/default.nix index 8568a91..a4bf9d4 100644 --- a/tidepool/default.nix +++ b/tidepool/default.nix @@ -31,3 +31,5 @@ }; in result.config +# result.config.exported + diff --git a/tidepool/flake.lock b/tidepool/flake.lock index f8cfcef..138d5f2 100644 --- a/tidepool/flake.lock +++ b/tidepool/flake.lock @@ -8,10 +8,10 @@ }, "locked": { "dir": "foundation", - "dirtyRev": "856b88321e5f19019332f8b60b729095c2260340-dirty", - "dirtyShortRev": "856b883-dirty", - "lastModified": 1718299377, - "narHash": "sha256-L6zriSSFi45uJ8u3HVsZ6iDHjNtQBB+aDWQfiReoLkM=", + "dirtyRev": "cdc90a46565dadea3b3c8f673b8b55d4ff75ea92-dirty", + "dirtyShortRev": "cdc90a4-dirty", + "lastModified": 1718356339, + "narHash": "sha256-BSiyT1q5xSlO1DyNJlkRWI82WWxVtQ1Xy8zPOqdqUlU=", "type": "git", "url": "file:../?dir=foundation" }, @@ -24,10 +24,10 @@ "lib": { "locked": { "dir": "lib", - "dirtyRev": "856b88321e5f19019332f8b60b729095c2260340-dirty", - "dirtyShortRev": "856b883-dirty", - "lastModified": 1718299377, - "narHash": "sha256-L6zriSSFi45uJ8u3HVsZ6iDHjNtQBB+aDWQfiReoLkM=", + "dirtyRev": "cdc90a46565dadea3b3c8f673b8b55d4ff75ea92-dirty", + "dirtyShortRev": "cdc90a4-dirty", + "lastModified": 1718356339, + "narHash": "sha256-BSiyT1q5xSlO1DyNJlkRWI82WWxVtQ1Xy8zPOqdqUlU=", "type": "git", "url": "file:../?dir=lib" }, diff --git a/tidepool/src/exports/packages.nix b/tidepool/src/exports/packages.nix index d442ed2..5dfd22e 100644 --- a/tidepool/src/exports/packages.nix +++ b/tidepool/src/exports/packages.nix @@ -3,20 +3,43 @@ lib, }: let lib' = config.lib; + + cfg = config.exports; in { options = { exports.packages = lib.options.create { + type = lib.types.attrs.of (lib.types.function (lib.types.nullish lib.types.derivation)); default.value = {}; }; exported.packages = lib.options.create { + type = lib.types.attrs.of (lib.types.attrs.of lib.types.derivation); default.value = {}; }; }; config = { - exported.packages = { - # i686-linux = config.packages.foundation; - }; + exported.packages = let + all = lib.attrs.generate lib'.systems.doubles.all ( + system: let + packages = + builtins.mapAttrs + (name: resolve: resolve system) + cfg.packages; + + available = + lib.attrs.filter + (name: package: package != null) + packages; + in + available + ); + + available = + lib.attrs.filter + (system: packages: builtins.length (builtins.attrNames packages) != 0) + all; + in + available; }; } diff --git a/tidepool/src/lib/default.nix b/tidepool/src/lib/default.nix index 0ebb58d..c418935 100644 --- a/tidepool/src/lib/default.nix +++ b/tidepool/src/lib/default.nix @@ -6,6 +6,7 @@ in { includes = [ ./options.nix + ./packages.nix ./systems.nix ./types.nix ]; diff --git a/tidepool/src/lib/packages.nix b/tidepool/src/lib/packages.nix new file mode 100644 index 0000000..ab92d10 --- /dev/null +++ b/tidepool/src/lib/packages.nix @@ -0,0 +1,37 @@ +{ + lib, + config, +}: let + lib' = config.lib; +in { + config = { + lib.packages = { + get = path: let + resolved = + if builtins.isList path + then path + else lib.strings.split "." path; + + package = lib.attrs.selectOrThrow resolved config.packages.generic; + in + assert lib.errors.trace (builtins.length resolved > 1) "Packages must have a namespace specified."; + package + // { + namespace = lib.modules.override 99 (builtins.head resolved); + }; + + export = path: system: let + resolved = + if builtins.isList path + then path + else lib.strings.split "." path; + + package = lib'.packages.get resolved; + targeted = lib.attrs.selectOrThrow resolved config.packages.targeted.${system}; + in + if builtins.elem system package.meta.platforms + then targeted.package + else null; + }; + }; +} diff --git a/tidepool/src/lib/types.nix b/tidepool/src/lib/types.nix index 8345669..f068a29 100644 --- a/tidepool/src/lib/types.nix +++ b/tidepool/src/lib/types.nix @@ -53,11 +53,6 @@ in { meta = lib.types.submodule { options = { - name = lib.options.create { - type = lib.types.string; - description = "The name of the package."; - }; - description = lib.options.create { type = lib.types.nullish lib.types.string; default.value = null; @@ -138,7 +133,7 @@ in { lib'.types.package.targeted' { config = { - namespace = namespace; + namespace = lib.modules.override 99 namespace; platform = { build = system; @@ -164,7 +159,7 @@ in { lib'.types.package.targeted' { config = { - namespace = namespace; + namespace = lib.modules.override 99 namespace; platform = { build = system; @@ -194,6 +189,11 @@ in { config, }: { options = { + namespace = lib.options.create { + type = lib.types.nullish lib.types.string; + default.value = null; + }; + pname = lib.options.create { type = lib.types.string; default = { diff --git a/tidepool/src/packages/aux/foundation.nix b/tidepool/src/packages/aux/foundation.nix new file mode 100644 index 0000000..ddab66d --- /dev/null +++ b/tidepool/src/packages/aux/foundation.nix @@ -0,0 +1,39 @@ +{ + lib, + config, + options, +}: let + lib' = config.lib; +in { + config = { + exports.packages.example-x = lib'.packages.export "example.x"; + + packages = { + generic = { + example = { + x = { + meta.platforms = ["i686-linux" "x86_64-linux"]; + version = "1.0.0"; + + builder.build = package: + derivation { + name = package.name; + builder = "/bin/sh"; + system = package.platform.build; + }; + + phases = { + build = package: '' + make --build ${package.platform.build} --host ${package.platform.host} + ''; + + install = lib.dag.entry.after ["build"] '' + make install DESTDIR=$out + ''; + }; + }; + }; + }; + }; + }; +} diff --git a/tidepool/src/packages/default.nix b/tidepool/src/packages/default.nix index 67f72f8..c4352bf 100644 --- a/tidepool/src/packages/default.nix +++ b/tidepool/src/packages/default.nix @@ -17,16 +17,6 @@ packages ); - targeted' = { - i686-linux = - getPackages "i686-linux" generic - // { - cross = { - x86_64-linux = getPackages "x86_64-linux" generic; - }; - }; - }; - targeted = lib.attrs.generate lib'.systems.doubles.all (system: getPackages system generic // { @@ -36,7 +26,7 @@ }); in { includes = [ - # ./aux/foundation.nix + ./aux/foundation.nix ]; options = { @@ -54,32 +44,6 @@ in { config = { packages = { - generic = { - example = { - x = { - meta.platforms = ["i686-linux" "x86_64-linux"]; - version = "1.0.0"; - - builder.build = package: - derivation { - name = package.name; - builder = "/bin/sh"; - system = package.platform.build; - }; - - phases = { - build = package: '' - make --build ${package.platform.build} --host ${package.platform.host} - ''; - - install = lib.dag.entry.after ["build"] '' - make install DESTDIR=$out - ''; - }; - }; - }; - }; - inherit targeted; }; };