diff --git a/README.md b/README.md new file mode 100644 index 0000000..e6c854b --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ + +# Flake Module Core + +_Foundational flake attributes represented using the module system._ + +`flake-modules-core` provides common options for an ecosystem of modules to extend. diff --git a/all-modules.nix b/all-modules.nix new file mode 100644 index 0000000..0e8019f --- /dev/null +++ b/all-modules.nix @@ -0,0 +1,10 @@ +{ lib, ... }: +{ + imports = [ + ./modules/checks.nix + ./modules/devShell.nix + ./modules/legacyPackages.nix + ./modules/packages.nix + ./modules/perSystem.nix + ]; +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..045bfca --- /dev/null +++ b/flake.nix @@ -0,0 +1,15 @@ +{ + description = "Flake basics described using the module system"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs, ... }: { + lib = import ./lib.nix { inherit (nixpkgs) lib; }; + flakeModules = { + core = ./all-modules.nix; + }; + }; + +} diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..1a1a71d --- /dev/null +++ b/lib.nix @@ -0,0 +1,21 @@ +{ lib }: +let + inherit (lib) + mkOption + types + ; + + flake-modules-core-lib = { + evalFlakeModule = + { self + , specialArgs ? { } + }: + module: + + lib.evalModules { + specialArgs = { inherit self flake-modules-core-lib; } // specialArgs; + modules = [ ./all-modules.nix module ]; + }; + }; +in +flake-modules-core-lib diff --git a/modules/checks.nix b/modules/checks.nix new file mode 100644 index 0000000..5ef468a --- /dev/null +++ b/modules/checks.nix @@ -0,0 +1,45 @@ +{ config, lib, flake-modules-core-lib, ... }: +let + inherit (lib) + filterAttrs + genAttrs + mapAttrs + mkOption + optionalAttrs + types + ; +in +{ + options = { + flake = { + checks = mkOption { + type = types.lazyAttrsOf (types.lazyAttrsOf types.package); + default = { }; + }; + }; + }; + config = { + flake.checks = + mapAttrs + (k: v: v.checks) + (filterAttrs + (k: v: v.checks != null) + (genAttrs config.systems config.perSystem) + ); + + perInput = system: flake: + optionalAttrs (flake?checks.${system}) { + checks = flake.checks.${system}; + }; + + perSystem = system: { config, ... }: { + _file = ./checks.nix; + options = { + checks = mkOption { + type = types.lazyAttrsOf types.package; + default = { }; + }; + }; + }; + }; +} diff --git a/modules/devShell.nix b/modules/devShell.nix new file mode 100644 index 0000000..12ca2ac --- /dev/null +++ b/modules/devShell.nix @@ -0,0 +1,44 @@ +{ config, lib, flake-modules-core-lib, ... }: +let + inherit (lib) + filterAttrs + genAttrs + mapAttrs + mkOption + optionalAttrs + types + ; +in +{ + options = { + flake = { + devShell = mkOption { + type = types.lazyAttrsOf types.package; + default = { }; + }; + }; + }; + config = { + flake.devShell = + mapAttrs + (k: v: v.devShell) + (filterAttrs + (k: v: v.devShell != null) + (genAttrs config.systems config.perSystem) + ); + + perInput = system: flake: + optionalAttrs (flake?devShell.${system}) { + devShell = flake.devShell.${system}; + }; + + perSystem = system: { config, ... }: { + _file = ./devShell.nix; + options = { + devShell = mkOption { + type = types.nullOr types.package; + }; + }; + }; + }; +} diff --git a/modules/legacyPackages.nix b/modules/legacyPackages.nix new file mode 100644 index 0000000..7e963c5 --- /dev/null +++ b/modules/legacyPackages.nix @@ -0,0 +1,45 @@ +{ config, lib, flake-modules-core-lib, ... }: +let + inherit (lib) + filterAttrs + genAttrs + mapAttrs + mkOption + optionalAttrs + types + ; +in +{ + options = { + flake = { + legacyPackages = mkOption { + type = types.lazyAttrsOf (types.lazyAttrsOf types.anything); + default = { }; + }; + }; + }; + config = { + flake.legacyPackages = + mapAttrs + (k: v: v.legacyPackages) + (filterAttrs + (k: v: v.legacyPackages != null) + (genAttrs config.systems config.perSystem) + ); + + perInput = system: flake: + optionalAttrs (flake?legacyPackages.${system}) { + legacyPackages = flake.legacyPackages.${system}; + }; + + perSystem = system: { config, ... }: { + _file = ./legacyPackages.nix; + options = { + legacyPackages = mkOption { + type = types.lazyAttrsOf types.anything; + default = { }; + }; + }; + }; + }; +} diff --git a/modules/packages.nix b/modules/packages.nix new file mode 100644 index 0000000..da2c234 --- /dev/null +++ b/modules/packages.nix @@ -0,0 +1,45 @@ +{ config, lib, flake-modules-core-lib, ... }: +let + inherit (lib) + filterAttrs + genAttrs + mapAttrs + mkOption + optionalAttrs + types + ; +in +{ + options = { + flake = { + packages = mkOption { + type = types.lazyAttrsOf (types.lazyAttrsOf types.package); + default = { }; + }; + }; + }; + config = { + flake.packages = + mapAttrs + (k: v: v.packages) + (filterAttrs + (k: v: v.packages != null) + (genAttrs config.systems config.perSystem) + ); + + perInput = system: flake: + optionalAttrs (flake?packages.${system}) { + packages = flake.packages.${system}; + }; + + perSystem = system: { config, ... }: { + _file = ./packages.nix; + options = { + packages = mkOption { + type = types.lazyAttrsOf types.package; + default = { }; + }; + }; + }; + }; +} diff --git a/modules/perSystem.nix b/modules/perSystem.nix new file mode 100644 index 0000000..5ee9193 --- /dev/null +++ b/modules/perSystem.nix @@ -0,0 +1,43 @@ +{ config, lib, self, ... }: +let + inherit (lib) + mapAttrs + mkOption + types + ; + + rootConfig = config; + +in +{ + options = { + systems = mkOption { + description = "All the system types to enumerate in the flake."; + type = types.listOf types.str; + default = [ ]; + }; + + perInput = mkOption { + description = "Function from system to function from flake to system-specific attributes."; + type = types.functionTo (types.functionTo (types.lazyAttrsOf types.unspecified)); + }; + + perSystem = mkOption { + description = "A function from system to flake-like attributes omitting the attribute."; + type = types.functionTo (types.submoduleWith { + modules = [ + ({ config, system, ... }: { + _file = ./perSystem.nix; + config = { + _module.args.inputs' = mapAttrs (k: rootConfig.perInput system) self.inputs; + _module.args.self' = rootConfig.perInput system self; + }; + }) + ]; + shorthandOnlyDefinesConfig = false; + }); + }; + }; + + config.perSystem = system: { _module.args.system = system; }; +} diff --git a/modules/self.nix b/modules/self.nix new file mode 100644 index 0000000..38cf73b --- /dev/null +++ b/modules/self.nix @@ -0,0 +1,6 @@ +{ lib, ... }: { + options.self = lib.mkOption { + description = "The current flake."; + type = type.lazyAttrsOf type.unspecified; + }; +}