From d707369f932523bde030d97f7059366a03e0655e Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 06:50:15 -0400 Subject: [PATCH 1/8] dev: Use flake.lock of main flake --- dev/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/default.nix b/dev/default.nix index 6af149b..d6d28a4 100644 --- a/dev/default.nix +++ b/dev/default.nix @@ -1,6 +1,6 @@ let flake = builtins.getFlake (toString ./.); - fmc-lib = import ../lib.nix { inherit (flake.inputs.nixpkgs) lib; }; + fmc-lib = (builtins.getFlake (toString ../.)).lib; self = { inherit (flake) inputs; outPath = ../.; # used by pre-commit module, etc From 8e6bca1b0afcfb4e5c5f9e21f100adf2b2ce2d08 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 06:52:28 -0400 Subject: [PATCH 2/8] Add mkTransposedPerSystemModule for simple per-system exposed attrs --- lib.nix | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib.nix b/lib.nix index 0d728f3..2d38daa 100644 --- a/lib.nix +++ b/lib.nix @@ -98,6 +98,34 @@ let type = flake-parts-lib.mkPerSystemType module; }; + # Helper function for defining a per-system option that + # gets transposed by the usual flake system logic to a + # top-level flake attribute. + mkTransposedPerSystemModule = { name, option, file }: { + _file = file; + + options = { + flake = flake-parts-lib.mkSubmoduleOptions { + ${name} = mkOption { + type = types.lazyAttrsOf option.type; + default = { }; + description = lib.mdDoc '' + See {option}`perSystem.${name}` for description and examples. + ''; + }; + }; + + perSystem = flake-parts-lib.mkPerSystemOption { + _file = file; + + options.${name} = option; + }; + }; + + config = { + transposition.${name} = { }; + }; + }; }; in From 40b344843d32ea7437d1cae1015aaa1bfe44d4e1 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 06:53:32 -0400 Subject: [PATCH 3/8] apps: Use mkTransposedPerSystemModule --- modules/apps.nix | 58 +++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 43 deletions(-) diff --git a/modules/apps.nix b/modules/apps.nix index e411fd6..a3f8219 100644 --- a/modules/apps.nix +++ b/modules/apps.nix @@ -1,15 +1,11 @@ { config, lib, flake-parts-lib, ... }: let inherit (lib) - filterAttrs - mapAttrs mkOption - optionalAttrs types ; inherit (flake-parts-lib) - mkSubmoduleOptions - mkPerSystemOption + mkTransposedPerSystemModule ; programType = lib.types.coercedTo derivationType getExe lib.types.str; @@ -44,43 +40,19 @@ let }; }; in -{ - options = { - flake = mkSubmoduleOptions { - apps = mkOption { - type = types.lazyAttrsOf (types.lazyAttrsOf appType); - default = { }; - description = '' - Programs runnable with nix run .#<name>. - ''; - example = lib.literalExpression or lib.literalExample '' - { - x86_64-linux.default.program = "''${config.packages.hello}/bin/hello"; - } - ''; - }; - }; - - perSystem = mkPerSystemOption - ({ config, system, ... }: { - options = { - apps = mkOption { - type = types.lazyAttrsOf appType; - default = { }; - description = '' - Programs runnable with nix run .#<name>. - ''; - example = lib.literalExpression or lib.literalExample '' - { - default.program = "''${config.packages.hello}/bin/hello"; - } - ''; - }; - }; - }); - - }; - config = { - transposition.apps = { }; +mkTransposedPerSystemModule { + name = "apps"; + option = mkOption { + type = types.lazyAttrsOf appType; + default = { }; + description = '' + Programs runnable with nix run .#<name>. + ''; + example = lib.literalExpression or lib.literalExample '' + { + default.program = "''${config.packages.hello}/bin/hello"; + } + ''; }; + file = ./apps.nix; } From 5bcff37bc172020c3fa26f6ba4bab537c8949133 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 06:58:53 -0400 Subject: [PATCH 4/8] checks: Use mkTransposedPerSystemModule --- modules/checks.nix | 43 ++++++++++--------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/modules/checks.nix b/modules/checks.nix index 4fbb0af..12dde09 100644 --- a/modules/checks.nix +++ b/modules/checks.nix @@ -1,44 +1,21 @@ { config, lib, flake-parts-lib, ... }: let inherit (lib) - filterAttrs - mapAttrs mkOption - optionalAttrs types ; inherit (flake-parts-lib) - mkSubmoduleOptions - mkPerSystemOption + mkTransposedPerSystemModule ; in -{ - options = { - flake = mkSubmoduleOptions { - checks = mkOption { - type = types.lazyAttrsOf (types.lazyAttrsOf types.package); - default = { }; - description = '' - Derivations to be built by nix flake check. - ''; - }; - }; - - perSystem = mkPerSystemOption ({ config, system, ... }: { - _file = ./checks.nix; - options = { - checks = mkOption { - type = types.lazyAttrsOf types.package; - default = { }; - description = '' - Derivations to be built by nix flake check. - ''; - }; - }; - }); - - }; - config = { - transposition.checks = { }; +mkTransposedPerSystemModule { + name = "checks"; + option = mkOption { + type = types.lazyAttrsOf types.package; + default = { }; + description = '' + Derivations to be built by nix flake check. + ''; }; + file = ./checks.nix; } From 5e259eb312e19f1a961fae9e3d971f15567f2c0d Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 07:01:01 -0400 Subject: [PATCH 5/8] devShells: Use mkTransposedPerSystemModule --- modules/devShells.nix | 45 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/modules/devShells.nix b/modules/devShells.nix index ecc53b3..7ebbeb6 100644 --- a/modules/devShells.nix +++ b/modules/devShells.nix @@ -1,45 +1,22 @@ { config, lib, flake-parts-lib, ... }: let inherit (lib) - filterAttrs - mapAttrs mkOption - optionalAttrs types ; inherit (flake-parts-lib) - mkSubmoduleOptions - mkPerSystemOption + mkTransposedPerSystemModule ; in -{ - options = { - flake = mkSubmoduleOptions { - devShells = mkOption { - type = types.lazyAttrsOf (types.lazyAttrsOf types.package); - default = { }; - description = '' - Per system an attribute set of packages used as shells. - nix develop .#<name> will run devShells.<system>.<name>. - ''; - }; - }; - - perSystem = mkPerSystemOption - ({ config, system, ... }: { - options = { - devShells = mkOption { - type = types.lazyAttrsOf types.package; - default = { }; - description = '' - An attribute set of packages to be built by nix develop .#<name>. - nix build .#<name> will run devShells.<name>. - ''; - }; - }; - }); - }; - config = { - transposition.devShells = { }; +mkTransposedPerSystemModule { + name = "devShells"; + option = mkOption { + type = types.lazyAttrsOf types.package; + default = { }; + description = '' + An attribute set of packages to be built by nix develop .#<name>. + nix build .#<name> will run devShells.<name>. + ''; }; + file = ./devShells.nix; } From e7da962abb2fa053a2601211a0e56ded9ac779d1 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 07:03:32 -0400 Subject: [PATCH 6/8] formatter: Use mkTransposedPerSystemModule --- modules/formatter.nix | 54 ++++++++----------------------------------- 1 file changed, 10 insertions(+), 44 deletions(-) diff --git a/modules/formatter.nix b/modules/formatter.nix index a63a9ba..d5b0435 100644 --- a/modules/formatter.nix +++ b/modules/formatter.nix @@ -1,55 +1,21 @@ { config, lib, flake-parts-lib, ... }: let inherit (lib) - filterAttrs - mapAttrs mkOption - optionalAttrs types ; inherit (flake-parts-lib) - mkSubmoduleOptions - mkPerSystemOption + mkTransposedPerSystemModule ; in -{ - options = { - flake = mkSubmoduleOptions { - formatter = mkOption { - type = types.lazyAttrsOf types.package; - default = { }; - description = '' - Per system package used by nix fmt. - ''; - }; - }; - - perSystem = mkPerSystemOption ({ config, ... }: { - _file = ./formatter.nix; - options = { - formatter = mkOption { - type = types.nullOr types.package; - default = null; - description = '' - A package used by nix fmt. - ''; - }; - }; - }); - }; - config = { - flake.formatter = - mapAttrs - (k: v: v.formatter) - (filterAttrs - (k: v: v.formatter != null) - config.allSystems - ); - - perInput = system: flake: - optionalAttrs (flake?formatter.${system}) { - formatter = flake.formatter.${system}; - }; - +mkTransposedPerSystemModule { + name = "formatter"; + option = mkOption { + type = types.nullOr types.package; + default = null; + description = '' + A package used by nix fmt. + ''; }; + file = ./formatter.nix; } From 1e5e56e476cab6cd6663174e032bfe1b1007cf61 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 07:07:59 -0400 Subject: [PATCH 7/8] legacyPackages: Use mkTransposedPerSystemModule --- modules/legacyPackages.nix | 42 +++++++++----------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/modules/legacyPackages.nix b/modules/legacyPackages.nix index eedc075..f07ab16 100644 --- a/modules/legacyPackages.nix +++ b/modules/legacyPackages.nix @@ -1,43 +1,21 @@ { config, lib, flake-parts-lib, ... }: let inherit (lib) - filterAttrs - mapAttrs mkOption - optionalAttrs types ; inherit (flake-parts-lib) - mkSubmoduleOptions - mkPerSystemOption + mkTransposedPerSystemModule ; in -{ - options = { - flake = mkSubmoduleOptions { - legacyPackages = mkOption { - type = types.lazyAttrsOf (types.lazyAttrsOf types.raw); - default = { }; - description = '' - Per system, an attribute set of unmergeable values. This is also used by nix build .#<attrpath>. - ''; - }; - }; - - perSystem = mkPerSystemOption ({ config, ... }: { - options = { - legacyPackages = mkOption { - type = types.lazyAttrsOf types.raw; - default = { }; - description = '' - An attribute set of unmergeable values. This is also used by nix build .#<attrpath>. - ''; - }; - }; - }); - }; - - config = { - transposition.legacyPackages = { }; +mkTransposedPerSystemModule { + name = "legacyPackages"; + option = mkOption { + type = types.lazyAttrsOf types.raw; + default = { }; + description = '' + An attribute set of unmergeable values. This is also used by nix build .#<attrpath>. + ''; }; + file = ./legacyPackages.nix; } From 96c27dbba0d07b584c0145f4c22dcdae7dcf6dda Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sun, 30 Oct 2022 07:08:12 -0400 Subject: [PATCH 8/8] packages: Use mkTransposedPerSystemModule --- modules/packages.nix | 45 +++++++++++--------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/modules/packages.nix b/modules/packages.nix index 05fea46..a6a22c5 100644 --- a/modules/packages.nix +++ b/modules/packages.nix @@ -1,45 +1,22 @@ { config, lib, flake-parts-lib, ... }: let inherit (lib) - filterAttrs - mapAttrs mkOption - optionalAttrs types ; inherit (flake-parts-lib) - mkSubmoduleOptions - mkPerSystemOption + mkTransposedPerSystemModule ; in -{ - options = { - flake = mkSubmoduleOptions { - packages = mkOption { - type = types.lazyAttrsOf (types.lazyAttrsOf types.package); - default = { }; - description = '' - Per system an attribute set of packages. - nix build .#<name> will build packages.<system>.<name>. - ''; - }; - }; - - perSystem = mkPerSystemOption ({ config, ... }: { - _file = ./packages.nix; - options = { - packages = mkOption { - type = types.lazyAttrsOf types.package; - default = { }; - description = '' - An attribute set of packages to be built by nix build .#<name>. - nix build .#<name> will build packages.<name>. - ''; - }; - }; - }); - }; - config = { - transposition.packages = { }; +mkTransposedPerSystemModule { + name = "packages"; + option = mkOption { + type = types.lazyAttrsOf types.package; + default = { }; + description = '' + An attribute set of packages to be built by nix build .#<name>. + nix build .#<name> will build packages.<name>. + ''; }; + file = ./packages.nix; }