From 79b42f0842b879c3263a1c43b03746117c0aa3d8 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 26 Oct 2022 12:34:16 +0200 Subject: [PATCH 1/3] Remove dead code The module system guarantees presence of these attrs. --- modules/apps.nix | 2 +- modules/packages.nix | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/apps.nix b/modules/apps.nix index f081552..7e92fef 100644 --- a/modules/apps.nix +++ b/modules/apps.nix @@ -83,7 +83,7 @@ in config = { flake.apps = mapAttrs - (k: v: v.apps or { }) + (k: v: v.apps) config.allSystems; perInput = system: flake: diff --git a/modules/packages.nix b/modules/packages.nix index 8c08a09..3c06d0b 100644 --- a/modules/packages.nix +++ b/modules/packages.nix @@ -42,7 +42,7 @@ in config = { flake.packages = mapAttrs - (k: v: v.packages or { }) + (k: v: v.packages) config.allSystems; perInput = system: flake: From 62698364244c729a69bcc1ed822b535df71c3f04 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 26 Oct 2022 13:16:04 +0200 Subject: [PATCH 2/3] Generalize transposed attributes --- all-modules.nix | 1 + modules/apps.nix | 11 +------- modules/checks.nix | 11 +------- modules/devShells.nix | 10 +------ modules/legacyPackages.nix | 11 +------- modules/packages.nix | 11 +------- modules/transposition.nix | 56 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 62 insertions(+), 49 deletions(-) create mode 100644 modules/transposition.nix diff --git a/all-modules.nix b/all-modules.nix index 03439a0..42bfdf2 100644 --- a/all-modules.nix +++ b/all-modules.nix @@ -15,6 +15,7 @@ ./modules/overlays.nix ./modules/packages.nix ./modules/perSystem.nix + ./modules/transposition.nix ./modules/withSystem.nix ]; } diff --git a/modules/apps.nix b/modules/apps.nix index 7e92fef..e411fd6 100644 --- a/modules/apps.nix +++ b/modules/apps.nix @@ -81,15 +81,6 @@ in }; config = { - flake.apps = - mapAttrs - (k: v: v.apps) - config.allSystems; - - perInput = system: flake: - optionalAttrs (flake?apps.${system}) { - apps = flake.apps.${system}; - }; - + transposition.apps = { }; }; } diff --git a/modules/checks.nix b/modules/checks.nix index de515e8..4fbb0af 100644 --- a/modules/checks.nix +++ b/modules/checks.nix @@ -39,15 +39,6 @@ in }; config = { - flake.checks = - mapAttrs - (k: v: v.checks) - config.allSystems; - - perInput = system: flake: - optionalAttrs (flake?checks.${system}) { - checks = flake.checks.${system}; - }; - + transposition.checks = { }; }; } diff --git a/modules/devShells.nix b/modules/devShells.nix index 7cbcc12..ecc53b3 100644 --- a/modules/devShells.nix +++ b/modules/devShells.nix @@ -40,14 +40,6 @@ in }); }; config = { - flake.devShells = - mapAttrs - (k: v: v.devShells) - config.allSystems; - - perInput = system: flake: - optionalAttrs (flake?devShells.${system}) { - devShells = flake.devShells.${system}; - }; + transposition.devShells = { }; }; } diff --git a/modules/legacyPackages.nix b/modules/legacyPackages.nix index 69d1baf..eedc075 100644 --- a/modules/legacyPackages.nix +++ b/modules/legacyPackages.nix @@ -38,15 +38,6 @@ in }; config = { - flake.legacyPackages = - mapAttrs - (k: v: v.legacyPackages) - config.allSystems; - - perInput = system: flake: - optionalAttrs (flake?legacyPackages.${system}) { - legacyPackages = flake.legacyPackages.${system}; - }; - + transposition.legacyPackages = { }; }; } diff --git a/modules/packages.nix b/modules/packages.nix index 3c06d0b..05fea46 100644 --- a/modules/packages.nix +++ b/modules/packages.nix @@ -40,15 +40,6 @@ in }); }; config = { - flake.packages = - mapAttrs - (k: v: v.packages) - config.allSystems; - - perInput = system: flake: - optionalAttrs (flake?packages.${system}) { - packages = flake.packages.${system}; - }; - + transposition.packages = { }; }; } diff --git a/modules/transposition.nix b/modules/transposition.nix new file mode 100644 index 0000000..d030857 --- /dev/null +++ b/modules/transposition.nix @@ -0,0 +1,56 @@ +{ config, lib, flake-parts-lib, ... }: + +let + inherit (lib) + filterAttrs + mapAttrs + mkOption + types + ; + inherit (flake-parts-lib) + mkSubmoduleOptions + mkPerSystemOption + ; +in +{ + options = { + transposition = lib.mkOption { + description = '' + A helper that defines transposed attributes in the flake outputs. + + Transposition is the operation that swaps the indices of a data structure. + Here it refers specifically to the transposition between + + + perSystem: .''${system}.''${attribute} + outputs: .''${attribute}.''${system} + + + It also defines the reverse operation in . + ''; + type = + types.lazyAttrsOf + (types.submoduleWith { modules = [ ]; }); + }; + }; + + config = { + flake = + lib.mapAttrs + (attrName: attrConfig: + mapAttrs + (system: v: v.${attrName}) + config.allSystems + ) + config.transposition; + + perInput = + system: flake: + mapAttrs + (attrName: attrConfig: flake.${attrName}.${system}) + (filterAttrs + (attrName: attrConfig: flake?${attrName}.${system}) + config.transposition + ); + }; +} From 0c8eff7513aa1a948fbccabc901124febe6612fa Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 26 Oct 2022 14:07:24 +0200 Subject: [PATCH 3/3] Add transposition..adHoc --- README.md | 10 ++++++++++ modules/transposition.nix | 29 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b9db4b0..e390ea2 100644 --- a/README.md +++ b/README.md @@ -128,3 +128,13 @@ Why so many ways? 1. Flakes counterintuitively handles `system` by enumerating all of them in attribute sets. `flake-parts` does not impose this restriction, but does need to support it. 2. `flake-parts` provides an extensible structure that is richer than the flakes interface alone. + +# How do I define my own flake output attribute? + +Have a look at the [source](https://github.com/hercules-ci/flake-parts/tree/main/modules) for some examples. + +Whether directly or indirectly, you'll be defining an attribute inside [the `flake` option](https://flake.parts/options.html#opt-flake). + +If you want the attribute to be derived from [`perSystem`](https://flake.parts/options.html#opt-perSystem) you can start with [`packages.nix`](https://github.com/hercules-ci/flake-parts/blob/main/modules/packages.nix) as an example, or [`formatter.nix`](https://github.com/hercules-ci/flake-parts/blob/main/modules/formatter.nix) if you need to do some filtering. + +If you really don't care about your attribute, you may temporarily use [`transposition..adHoc = true`](https://flake.parts/options.html#opt-transposition._name_.adHoc) to create and expose a `perSystem` option without merging support, type checking or documentation. diff --git a/modules/transposition.nix b/modules/transposition.nix index d030857..aec1563 100644 --- a/modules/transposition.nix +++ b/modules/transposition.nix @@ -11,6 +11,23 @@ let mkSubmoduleOptions mkPerSystemOption ; + + transpositionModule = { + options = { + adHoc = mkOption { + type = types.bool; + default = false; + description = '' + Whether to provide a stub option declaration for + + The stub option declaration does not support merging and lacks + documentation, so you are recommended to declare the + option yourself and avoid . + ''; + }; + }; + }; + in { options = { @@ -30,7 +47,7 @@ in ''; type = types.lazyAttrsOf - (types.submoduleWith { modules = [ ]; }); + (types.submoduleWith { modules = [ transpositionModule ]; }); }; }; @@ -52,5 +69,15 @@ in (attrName: attrConfig: flake?${attrName}.${system}) config.transposition ); + + perSystem = { ... }: { + options = + mapAttrs + (k: v: lib.mkOption { }) + (filterAttrs + (k: v: v.adHoc) + config.transposition + ); + }; }; }