diff --git a/cerulean/default.nix b/cerulean/default.nix index 06492f8..a824f8e 100644 --- a/cerulean/default.nix +++ b/cerulean/default.nix @@ -15,14 +15,19 @@ mix, deploy-rs, ... -} @ inputs: -mix.newMixture inputs (mixture: { +} @ args: +mix.newMixture args (mixture: { includes.public = [ ./nexus ]; version = "0.1.0"; + nixosModules = rec { + default = cerulean; + cerulean = ./nixos-module; + }; + overlays = [ # build deploy-rs as a package not from the flake input, # hence we can rely on a nixpkg binary cache. diff --git a/cerulean/nexus/default.nix b/cerulean/nexus/default.nix index d8e0f7c..65495bf 100644 --- a/cerulean/nexus/default.nix +++ b/cerulean/nexus/default.nix @@ -11,8 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -{mix, ...} @ inputs: -mix.newMixture inputs (mixture: { +{mix, ...} @ args: +mix.newMixture args (mixture: { includes.public = [ ./nodes.nix ./nexus.nix diff --git a/cerulean/nexus/nexus.nix b/cerulean/nexus/nexus.nix index 1171f3e..ae8fe38 100644 --- a/cerulean/nexus/nexus.nix +++ b/cerulean/nexus/nexus.nix @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. { + self, this, nt, lib, @@ -184,15 +185,18 @@ in { nodeName: node: let nixosDecl = lib.nixosSystem { system = node.system; - specialArgs = - nexus.specialArgs - // node.specialArgs - // { - inherit root; - inherit (node) system; - }; + specialArgs = let + specialArgs = + nexus.specialArgs + // node.specialArgs + // { + inherit root specialArgs; + inherit (node) system; + }; + in + specialArgs; modules = - [../nixos-module (findImport (root + "/hosts/${nodeName}"))] + [self.nixosModules.default (findImport (root + "/hosts/${nodeName}"))] ++ (getGroupModules root nodeName node) ++ node.extraModules ++ nexus.extraModules; diff --git a/cerulean/nixos-module/default.nix b/cerulean/nixos-module/default.nix index 3bf3a9c..d657021 100644 --- a/cerulean/nixos-module/default.nix +++ b/cerulean/nixos-module/default.nix @@ -11,9 +11,17 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -{deploy-rs, ...}: { +{ + root, + deploy-rs, + ... +} @ args: { imports = [ - ./nixpkgs.nix + # user configuration + (import (root + "/nixpkgs.nix")) + # options declarations + (import ./nixpkgs.nix (args // {contextName = "hosts";})) + ./home-manager.nix ]; diff --git a/cerulean/nixos-module/home-manager.nix b/cerulean/nixos-module/home-manager.nix index 6833e1d..4c41280 100644 --- a/cerulean/nixos-module/home-manager.nix +++ b/cerulean/nixos-module/home-manager.nix @@ -15,15 +15,27 @@ root, config, lib, + specialArgs, ... -} @ args: { +} @ args: let + inherit + (builtins) + attrNames + filter + pathExists + ; +in { home-manager = { users = config.users.users - |> builtins.attrNames - |> builtins.filter (x: builtins.pathExists (root + "/homes/${x}")) + |> attrNames + |> filter (x: pathExists (root + "/homes/${x}")) |> (x: lib.genAttrs x (y: import (root + "/homes/${y}"))); - extraSpecialArgs = args; + extraSpecialArgs = specialArgs; + sharedModules = [ + (import (root + "/nixpkgs.nix")) + (import ./nixpkgs.nix (args // {contextName = "homes";})) + ]; }; } diff --git a/cerulean/nixos-module/nixpkgs.nix b/cerulean/nixos-module/nixpkgs.nix index ee4ef55..9db5d3d 100644 --- a/cerulean/nixos-module/nixpkgs.nix +++ b/cerulean/nixos-module/nixpkgs.nix @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. { - nt, lib, + system, config, + contextName, ... }: let inherit @@ -22,62 +23,68 @@ mapAttrs ; - inherit - (nt) - flip - ; - - cfg = config.pkgsrc; + cfg = config.nixpkgs.channels; in { - options.pkgsrc = lib.mkOption { - type = lib.types.attrsOf lib.types.attrs; + options.nixpkgs.channels = lib.mkOption { + type = lib.types.attrsOf (lib.types.attrs); default = {}; - description = "Declare and import custom package repositories."; + description = "Declare package repositories per module context (nixos, home-manager, etc)"; example = { - "pkgs" = { - source = "inputs.nixpkgs"; - system = "x86-64-linux"; - config = { - allowUnfree = true; - allowBroken = false; + "homes" = { + "pkgs" = { + source = "inputs.nixpkgs"; + system = "x86-64-linux"; + config = { + allowUnfree = true; + allowBroken = false; + }; + }; + "upkgs" = { + source = "inputs.nixpkgs-unstable"; + system = "x86-64-linux"; + config = { + allowUnfree = true; + allowBroken = false; + }; }; }; }; }; + # or abort '' + # `nixpkgs.channels.${contextName}` does not exist, but neither does `nixpkgs.channels.default`! + # A channel configuration must be declared for module context "${contextName}". + # '' + config = let # TODO: use lib.types.submodule to restrict what options - # TODO: can be given to pkgsrc + # TODO: can be given to `nixpkgs.channels.${moduleName}.${name}` + decl = + cfg.${contextName} or cfg.default; + repos = - cfg + decl |> mapAttrs ( name: args: assert args ? source || abort '' - ${./.} - `pkgsrc.${name} missing required attribute "source"` + ${toString ./.} + `nixpkgs.channels.${contextName}.${name} missing required attribute "source"` ''; - args - |> flip removeAttrs ["source"] + ((removeAttrs args ["source"]) + // {inherit system;}) |> import args.source + |> lib.mkOverride 200 ); in { # NOTE: _module.args is a special option that allows us to # NOTE: set extend specialArgs from inside the modules. # "pkgs" is unique since the nix module system already handles it - _module.args = - removeAttrs repos ["pkgs"]; + _module.args = removeAttrs repos ["pkgs"]; - # nixpkgs = - # lib.mkIf (cfg ? pkgs) - # (let - # pkgs = cfg.pkgs; - # in - # lib.mkForce ( - # (removeAttrs pkgs ["source"]) - # // { - # flake.source = pkgs.source; - # } - # )); + nixpkgs = + if contextName == "hosts" + then {flake.source = lib.mkIf (decl ? pkgs) (lib.mkOverride 200 decl.pkgs.source);} + else {}; }; } diff --git a/flake.lock b/flake.lock index 40c2662..0dd06b1 100644 --- a/flake.lock +++ b/flake.lock @@ -175,11 +175,11 @@ "systems": "systems_2" }, "locked": { - "lastModified": 1770950436, - "narHash": "sha256-+h5jrsIJBea5P+rAk4OqUpScqnHYQTvCRUhgGv/MX34=", + "lastModified": 1770975056, + "narHash": "sha256-ZXTz/P3zUbbM6lNXzt91u8EwfNqhXpYMu8+wvFZqQHE=", "owner": "cry128", "repo": "nt", - "rev": "8725f5079f8f27b3faafeff90e5fc075d55e7d0a", + "rev": "f42dcdd49a7921a7f433512e83d5f93696632412", "type": "github" }, "original": {