From 70978650702c37f320a276ec4f0278efb262feae Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Mon, 20 Mar 2023 15:53:40 +0700 Subject: [PATCH 1/9] (drafted (flake.nix outputs.flakeModule)) --- flake.nix | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/flake.nix b/flake.nix index f9442d8ea2d2ca..cfb52b10921094 100644 --- a/flake.nix +++ b/flake.nix @@ -58,5 +58,63 @@ nixosModules = { notDetected = ./nixos/modules/installer/scan/not-detected.nix; }; + + flakeModule = { config, system, ... }: + let + inherit (lib) mkOption types; + nixpkgsOverlaySubmodule = lib.types.mkOptionType { + name = "nixpkgsOverlay"; + description = "A nixpkgs overlay function"; + descriptionClass = "noun"; + # NOTE: This check is not exhaustive, as there is no way + # to check that the function takes two arguments, and + # returns an attrset. + check = lib.isFunction; + merge = _loc: defs: + let + logWarning = + if builtins.length defs > 1 + then builtins.trace "WARNING[nixpkgs.flakeModule]: Multiple overlays are applied in arbitrary order." null + else null; + overlays = + map (x: x.value) + (builtins.seq + logWarning + defs); + in + lib.composeManyExtensions overlays; + }; + nixpkgsSubmodule = with types; submodule { + options = { + overlays = mkOption { + type = listOf nixpkgsOverlaySubmodule; + description = "Nixpkgs overlay"; + default = self: super: { }; + defaultText = lib.literalExpression "self: super: { }"; + }; + }; + }; + + in + { + options = { + nixpkgs = lib.mkOption { + type = nixpkgsSubmodule; + description = '' + Configuration of nixpkgs + ''; + default.overlays = [ ]; + }; + }; + + config = { + perSystem = { lib, ... }: { + config = { + _module.args.pkgs = import ./. + { inherit system; inherit (config.nixpkgs) overlays; }; + }; + }; + }; + }; }; } From 4dc2aea217972b6ac1bc4770eda4e8c345d0dc99 Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Mon, 20 Mar 2023 18:06:50 +0700 Subject: [PATCH 2/9] (movedToFile (flake.nix outputs.flakeModule)) --- flake.nix | 58 +------------------------------------------------ flakeModule.nix | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 57 deletions(-) create mode 100644 flakeModule.nix diff --git a/flake.nix b/flake.nix index cfb52b10921094..e84ab1c067190f 100644 --- a/flake.nix +++ b/flake.nix @@ -59,62 +59,6 @@ notDetected = ./nixos/modules/installer/scan/not-detected.nix; }; - flakeModule = { config, system, ... }: - let - inherit (lib) mkOption types; - nixpkgsOverlaySubmodule = lib.types.mkOptionType { - name = "nixpkgsOverlay"; - description = "A nixpkgs overlay function"; - descriptionClass = "noun"; - # NOTE: This check is not exhaustive, as there is no way - # to check that the function takes two arguments, and - # returns an attrset. - check = lib.isFunction; - merge = _loc: defs: - let - logWarning = - if builtins.length defs > 1 - then builtins.trace "WARNING[nixpkgs.flakeModule]: Multiple overlays are applied in arbitrary order." null - else null; - overlays = - map (x: x.value) - (builtins.seq - logWarning - defs); - in - lib.composeManyExtensions overlays; - }; - nixpkgsSubmodule = with types; submodule { - options = { - overlays = mkOption { - type = listOf nixpkgsOverlaySubmodule; - description = "Nixpkgs overlay"; - default = self: super: { }; - defaultText = lib.literalExpression "self: super: { }"; - }; - }; - }; - - in - { - options = { - nixpkgs = lib.mkOption { - type = nixpkgsSubmodule; - description = '' - Configuration of nixpkgs - ''; - default.overlays = [ ]; - }; - }; - - config = { - perSystem = { lib, ... }: { - config = { - _module.args.pkgs = import ./. - { inherit system; inherit (config.nixpkgs) overlays; }; - }; - }; - }; - }; + flakeModule = import ./flakeModule.nix; }; } diff --git a/flakeModule.nix b/flakeModule.nix new file mode 100644 index 00000000000000..2fafb126004954 --- /dev/null +++ b/flakeModule.nix @@ -0,0 +1,58 @@ +{ lib, config, system, ... }: +let + inherit (lib) mkOption types; + nixpkgsOverlaySubmodule = lib.types.mkOptionType { + name = "nixpkgsOverlay"; + description = "A nixpkgs overlay function"; + descriptionClass = "noun"; + # NOTE: This check is not exhaustive, as there is no way + # to check that the function takes two arguments, and + # returns an attrset. + check = lib.isFunction; + merge = _loc: defs: + let + logWarning = + if builtins.length defs > 1 + then builtins.trace "WARNING[nixpkgs.flakeModule]: Multiple overlays are applied in arbitrary order." null + else null; + overlays = + map (x: x.value) + (builtins.seq + logWarning + defs); + in + lib.composeManyExtensions overlays; + }; + nixpkgsSubmodule = with types; submodule { + options = { + overlays = mkOption { + type = listOf nixpkgsOverlaySubmodule; + description = "Nixpkgs overlay"; + default = self: super: { }; + defaultText = lib.literalExpression "self: super: { }"; + }; + output = mkOption { }; + }; + }; + +in +{ + options = { + nixpkgs = lib.mkOption { + type = nixpkgsSubmodule; + description = '' + Configuration of nixpkgs + ''; + default.overlays = [ ]; + }; + }; + + config = { + perSystem = { lib, ... }: { + config = { + _module.args.pkgs = import ./. + { inherit system; inherit (config.nixpkgs) overlays; }; + }; + }; + }; +} From 75c31d0afbab18f0755296d0de7612a21862cb3c Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Tue, 21 Mar 2023 09:26:58 +0700 Subject: [PATCH 3/9] (developped flakeModule [smallFixes (removed (dependencyOn flake-parts)) (addedOption `nixpkgs.output`)]) --- flakeModule.nix | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/flakeModule.nix b/flakeModule.nix index 2fafb126004954..1f129774d8b4b6 100644 --- a/flakeModule.nix +++ b/flakeModule.nix @@ -1,13 +1,11 @@ { lib, config, system, ... }: let inherit (lib) mkOption types; - nixpkgsOverlaySubmodule = lib.types.mkOptionType { + + nixpkgsOverlaySubmodule = types.mkOptionType { name = "nixpkgsOverlay"; description = "A nixpkgs overlay function"; descriptionClass = "noun"; - # NOTE: This check is not exhaustive, as there is no way - # to check that the function takes two arguments, and - # returns an attrset. check = lib.isFunction; merge = _loc: defs: let @@ -23,15 +21,21 @@ let in lib.composeManyExtensions overlays; }; + nixpkgsSubmodule = with types; submodule { options = { overlays = mkOption { type = listOf nixpkgsOverlaySubmodule; - description = "Nixpkgs overlay"; - default = self: super: { }; - defaultText = lib.literalExpression "self: super: { }"; + description = "Nixpkgs overlays"; + default = [ ]; + }; + + output = mkOption { + type = listOf nixpkgsOverlaySubmodule; + description = "Output pkgs from nixpkgs' flake module"; + default = import ./. + { inherit system; inherit (config.nixpkgs) overlays; }; }; - output = mkOption { }; }; }; @@ -40,19 +44,8 @@ in options = { nixpkgs = lib.mkOption { type = nixpkgsSubmodule; - description = '' - Configuration of nixpkgs - ''; - default.overlays = [ ]; - }; - }; - - config = { - perSystem = { lib, ... }: { - config = { - _module.args.pkgs = import ./. - { inherit system; inherit (config.nixpkgs) overlays; }; - }; + description = "Configuration of nixpkgs"; + default = { }; }; }; } From c2958138383c1de063ad26cf871fbb80df41087b Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Wed, 22 Mar 2023 11:31:39 +0700 Subject: [PATCH 4/9] [(renamed flakeModule flakeModules.default) (added (flakeModule allPkgsPerSystem))] --- flake.nix | 2 +- flakeModule.nix | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index e84ab1c067190f..f369674a072d26 100644 --- a/flake.nix +++ b/flake.nix @@ -59,6 +59,6 @@ notDetected = ./nixos/modules/installer/scan/not-detected.nix; }; - flakeModule = import ./flakeModule.nix; + flakeModules.default = import ./flakeModule.nix; }; } diff --git a/flakeModule.nix b/flakeModule.nix index 1f129774d8b4b6..f26c02c3eb2500 100644 --- a/flakeModule.nix +++ b/flakeModule.nix @@ -1,4 +1,4 @@ -{ lib, config, system, ... }: +{ lib, config, ... }: let inherit (lib) mkOption types; @@ -22,6 +22,13 @@ let lib.composeManyExtensions overlays; }; + mkPkgsFromSystemType = types.mkOptionType { + name = "mkPkgsfromsystem"; + description = ""; + descriptionClass = "noun"; + check = lib.isFunction; + }; + nixpkgsSubmodule = with types; submodule { options = { overlays = mkOption { @@ -30,11 +37,18 @@ let default = [ ]; }; - output = mkOption { - type = listOf nixpkgsOverlaySubmodule; - description = "Output pkgs from nixpkgs' flake module"; - default = import ./. - { inherit system; inherit (config.nixpkgs) overlays; }; + mkPkgsFromSystem = mkOption { + type = mkPkgsFromSystemType; + description = "Returns `pkgs` from `system"; + internal = true; + default = system: import ./. { inherit system; inherit (config.nixpkgs) overlays; }; + }; + + allPkgsPerSystem = mkOption { + type = types.lazyAttrsOf types.unspecified; + description = "Attribute set of `pkgs` named by `system`"; + internal = true; + default = genAttrs lib.systems.parsedPlatform config.nixpkgs.mkPkgsFromSystem; }; }; }; From 54766d1585ce005683862b2109fb6b2668817d6c Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Thu, 23 Mar 2023 08:44:39 +0700 Subject: [PATCH 5/9] (appliedSuggestionsFrom roberth [(removed import) (added documentingComments)]) --- flake.nix | 2 +- flakeModule.nix | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index f369674a072d26..3051d3e4ebdbad 100644 --- a/flake.nix +++ b/flake.nix @@ -59,6 +59,6 @@ notDetected = ./nixos/modules/installer/scan/not-detected.nix; }; - flakeModules.default = import ./flakeModule.nix; + flakeModules.default = ./flakeModule.nix; }; } diff --git a/flakeModule.nix b/flakeModule.nix index f26c02c3eb2500..c057fe9587d6b6 100644 --- a/flakeModule.nix +++ b/flakeModule.nix @@ -10,11 +10,13 @@ let merge = _loc: defs: let logWarning = + # TODO: do not warn when lib.mkOrder/mkBefore/mkAfter are used unambiguously if builtins.length defs > 1 then builtins.trace "WARNING[nixpkgs.flakeModule]: Multiple overlays are applied in arbitrary order." null else null; overlays = map (x: x.value) + # TODO: lib.warnIf to replace the seq + if (builtins.seq logWarning defs); From 048003bba398951799dbdd2ffb8022a86c436a8b Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Thu, 23 Mar 2023 09:37:27 +0700 Subject: [PATCH 6/9] (firstEvaluation (naiveWorkingTest flakePartsModule)) --- flakeModule.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flakeModule.nix b/flakeModule.nix index c057fe9587d6b6..07bf46fe4596fa 100644 --- a/flakeModule.nix +++ b/flakeModule.nix @@ -50,7 +50,7 @@ let type = types.lazyAttrsOf types.unspecified; description = "Attribute set of `pkgs` named by `system`"; internal = true; - default = genAttrs lib.systems.parsedPlatform config.nixpkgs.mkPkgsFromSystem; + default = lib.genAttrs lib.systems.flakeExposed config.nixpkgs.mkPkgsFromSystem; }; }; }; From 7028c3656e8eacf9a5105027156e2111ad4a6e2e Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Thu, 23 Mar 2023 15:07:53 +0700 Subject: [PATCH 7/9] [(renamed nixpkgsOverlayType) (added mkPkgsFromArgs)] --- flakeModule.nix | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/flakeModule.nix b/flakeModule.nix index 07bf46fe4596fa..1d2854bc83a2dc 100644 --- a/flakeModule.nix +++ b/flakeModule.nix @@ -2,7 +2,7 @@ let inherit (lib) mkOption types; - nixpkgsOverlaySubmodule = types.mkOptionType { + nixpkgsOverlayType = types.mkOptionType { name = "nixpkgsOverlay"; description = "A nixpkgs overlay function"; descriptionClass = "noun"; @@ -24,33 +24,50 @@ let lib.composeManyExtensions overlays; }; - mkPkgsFromSystemType = types.mkOptionType { - name = "mkPkgsfromsystem"; - description = ""; + mkPkgsFromArgsType = types.mkOptionType { + name = "mkPkgsFromArgs"; + description = "A function that returns `pkgs` from a set of arguments"; descriptionClass = "noun"; check = lib.isFunction; }; + pkgsType = types.mkOptionType { + name = "nixpkgs"; + description = "An evaluation of Nixpkgs; the top level attribute set of packages"; + check = builtins.isAttrs; + }; + nixpkgsSubmodule = with types; submodule { options = { overlays = mkOption { - type = listOf nixpkgsOverlaySubmodule; + type = listOf nixpkgsOverlayType; description = "Nixpkgs overlays"; default = [ ]; }; - mkPkgsFromSystem = mkOption { - type = mkPkgsFromSystemType; + mkPkgsFromArgs = mkOption { + type = mkPkgsFromArgsType; description = "Returns `pkgs` from `system"; internal = true; - default = system: import ./. { inherit system; inherit (config.nixpkgs) overlays; }; + default = args@{ system, overlays ? config.nixpkgs.overlays, ... }: + import ./. args; }; allPkgsPerSystem = mkOption { type = types.lazyAttrsOf types.unspecified; description = "Attribute set of `pkgs` named by `system`"; internal = true; - default = lib.genAttrs lib.systems.flakeExposed config.nixpkgs.mkPkgsFromSystem; + default = + let + mkPkgsFromSystem = system: mkPkgsFromArgs { inherit system; }; + in + lib.genAttrs lib.systems.flakeExposed config.nixpkgs.mkPkgsFromSystem; + }; + + # TODO: develop or eliminate + pkgs = mkOption { + internal = true; + type = pkgsType; }; }; }; From 2613e69a8342346363a391ad5e4a3fedfb948d7d Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Thu, 23 Mar 2023 17:37:49 +0700 Subject: [PATCH 8/9] (appliedSuggestionsFrom roberth (renamed flakeMOdule.nix)) --- flake.nix | 2 +- flakeModule.nix => pkgs/top-level/flakeModule.nix | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename flakeModule.nix => pkgs/top-level/flakeModule.nix (100%) diff --git a/flake.nix b/flake.nix index 3051d3e4ebdbad..cf22af70d91f68 100644 --- a/flake.nix +++ b/flake.nix @@ -59,6 +59,6 @@ notDetected = ./nixos/modules/installer/scan/not-detected.nix; }; - flakeModules.default = ./flakeModule.nix; + flakeModules.default = ./pkgs/top-level/flakeModule.nix; }; } diff --git a/flakeModule.nix b/pkgs/top-level/flakeModule.nix similarity index 100% rename from flakeModule.nix rename to pkgs/top-level/flakeModule.nix From b4e35771a12955ed92c80982a1204a2013df2a94 Mon Sep 17 00:00:00 2001 From: li <@li:maisiliym.kriom> Date: Sat, 1 Apr 2023 09:35:13 +0700 Subject: [PATCH 9/9] (fixed [byPassOfNixpkgsImpureEval missingArguments]) --- pkgs/top-level/flakeModule.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/top-level/flakeModule.nix b/pkgs/top-level/flakeModule.nix index 1d2854bc83a2dc..2ce8988030516f 100644 --- a/pkgs/top-level/flakeModule.nix +++ b/pkgs/top-level/flakeModule.nix @@ -49,8 +49,10 @@ let type = mkPkgsFromArgsType; description = "Returns `pkgs` from `system"; internal = true; - default = args@{ system, overlays ? config.nixpkgs.overlays, ... }: - import ./. args; + default = args: + import ./. ({ + localSystem = { system = builtins.currentSystem; }; + } // args); }; allPkgsPerSystem = mkOption { @@ -59,7 +61,7 @@ let internal = true; default = let - mkPkgsFromSystem = system: mkPkgsFromArgs { inherit system; }; + mkPkgsFromSystem = system: config.nixpkgs.mkPkgsFromArgs { inherit system; }; in lib.genAttrs lib.systems.flakeExposed config.nixpkgs.mkPkgsFromSystem; };