From 7daa3d91b377bbe38265ddb941ef0a1321193714 Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Sun, 24 Aug 2025 18:49:13 -0600 Subject: [PATCH 1/3] trivionomicon/doctrine: add support for overriding the prefix --- doctrine/default.nix | 18 +++++++++++------- doctrine/lib/default.nix | 13 ++++++++----- doctrine/lib/mk-module.nix | 15 +++++++++------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/doctrine/default.nix b/doctrine/default.nix index b4c72b8..0d50d49 100644 --- a/doctrine/default.nix +++ b/doctrine/default.nix @@ -1,12 +1,16 @@ { + lib ? pkgs.lib, pkgs, - namespace, + prefix ? "trivium", + namespace ? null, }: let - doctrine = { - lib = import ./lib {inherit lib pkgs doctrine;}; - prefix = "trivium"; - inherit namespace; - }; - inherit (pkgs) lib; + doctrine = + { + lib = import ./lib {inherit lib pkgs doctrine;}; + inherit namespace prefix; + } + // lib.optionalAttrs (pkgs != null) { + inherit pkgs; + }; in doctrine diff --git a/doctrine/lib/default.nix b/doctrine/lib/default.nix index f189e21..e2d84b8 100644 --- a/doctrine/lib/default.nix +++ b/doctrine/lib/default.nix @@ -13,8 +13,11 @@ closeLib = close {inherit lib;}; closeFull = close {inherit lib pkgs doctrine;}; -in { - inherit close; - importAll = closeLib ./import-all.nix; - mkModule = closeFull ./mk-module.nix; -} +in + { + inherit close; + importAll = closeLib ./import-all.nix; + } + // lib.optionalAttrs (doctrine.namespace != null) { + mkModule = closeFull ./mk-module.nix; + } diff --git a/doctrine/lib/mk-module.nix b/doctrine/lib/mk-module.nix index f76ccfe..ffbe6bc 100644 --- a/doctrine/lib/mk-module.nix +++ b/doctrine/lib/mk-module.nix @@ -11,14 +11,17 @@ requires ? [], prefix ? doctrine.prefix, namespace ? doctrine.namespace, + passthru ? {}, }: let - optionsSet = import options { - inherit config lib pkgs cfg name doctrine; - }; + optionsSet = import options (passthru + // { + inherit config lib pkgs cfg name doctrine; + }); - configSet = import configFiles.${namespace} { - inherit config lib pkgs doctrine cfg; - }; + configSet = import configFiles.${namespace} (passthru + // { + inherit config lib pkgs doctrine cfg; + }); configFiles = lib.filterAttrs (k: v: v != null) { inherit sys hm; From 111d3dabf21e6b413e4cf91136a8fec720a58f28 Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Sun, 24 Aug 2025 18:49:47 -0600 Subject: [PATCH 2/3] trivionomicon/modules/nix-registry: initial commit --- modules/nix-registry/default.nix | 16 ++++++++++++++++ modules/nix-registry/hm.nix | 23 +++++++++++++++++++++++ modules/nix-registry/options.nix | 19 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 modules/nix-registry/default.nix create mode 100644 modules/nix-registry/hm.nix create mode 100644 modules/nix-registry/options.nix diff --git a/modules/nix-registry/default.nix b/modules/nix-registry/default.nix new file mode 100644 index 0000000..8406d88 --- /dev/null +++ b/modules/nix-registry/default.nix @@ -0,0 +1,16 @@ +{ + config, + lib, + pkgs, + doctrine, + flakes, + ... +}: +doctrine.lib.mkModule { + inherit config; + name = "nix-registry"; + hm = ./hm.nix; + options = ./options.nix; + + passthru = {inherit flakes;}; +} diff --git a/modules/nix-registry/hm.nix b/modules/nix-registry/hm.nix new file mode 100644 index 0000000..1c57e95 --- /dev/null +++ b/modules/nix-registry/hm.nix @@ -0,0 +1,23 @@ +{ + pkgs, + lib, + cfg, + flakes, + ... +}: let + registryName = name: + if name == "self" + then cfg.renameSelf + else name; + + registryFilter = { + nixpkgs = true; + unstable = true; + self = cfg.renameSelf != null; + }; +in { + nix.registry = + lib.mapAttrs' + (name: value: lib.nameValuePair (registryName name) {flake = value;}) + (lib.filterAttrs (name: _: registryFilter.${name} or cfg.allInputs) flakes); +} diff --git a/modules/nix-registry/options.nix b/modules/nix-registry/options.nix new file mode 100644 index 0000000..e8898ec --- /dev/null +++ b/modules/nix-registry/options.nix @@ -0,0 +1,19 @@ +{lib, ...}: +with lib.types; { + hm = { + allInputs = mkOption { + type = bool; + default = default; + description = '' + Include all flake inputs. If false, only 'nixpkgs' and 'unstable' + (if available) will be added to the flake registry by default. + ''; + }; + + renameSelf = mkOption { + type = nullOr str; + default = "self"; + description = "Registry name to use for the 'self' input"; + }; + }; +} From bf39923f9ce5cd14c7fcbc40474d01f2f87fb10f Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Sun, 24 Aug 2025 18:51:05 -0600 Subject: [PATCH 3/3] trivionomicon/flake: rewrite broken implementation of mkSystemFlake --- flake.nix | 205 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 113 insertions(+), 92 deletions(-) diff --git a/flake.nix b/flake.nix index 1a20e9e..7ab10c5 100644 --- a/flake.nix +++ b/flake.nix @@ -16,15 +16,24 @@ "${namespace}" = builtins.removeAttrs overlayPkgs ["override"]; } // (overlayPkgs.override or {}); + + doctrineNoPkgs = self.lib.mkDoctrine { + lib = nixpkgs.lib; + pkgs = null; + }; in - flake-utils.lib.eachDefaultSystem (system: { - formatter = (import nixpkgs {inherit system;}).alejandra; + flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs {inherit system;}; + in { + formatter = pkgs.alejandra; packages = (import nixpkgs { inherit system; - overlays = [(mapOverlayOverride "local" (import ./pkgs))]; - }).local; + overlays = [(mapOverlayOverride doctrineNoPkgs.prefix (import ./pkgs))]; + }).${ + doctrineNoPkgs.prefix + }; }) // { templates = let @@ -39,33 +48,37 @@ }; overlays = let - overlay = mapOverlayOverride "trivium" (import ./pkgs); + overlay = mapOverlayOverride doctrineNoPkgs.prefix (import ./pkgs); in { default = overlay; - trivium = overlay; + ${doctrineNoPkgs.prefix} = overlay; }; homeManagerModules.default = ./modules; nixosModules.default = ./modules; lib = { + mkDoctrine = import ./doctrine; + mkSystemFlake = { flakes, system, + doctrinePrefix ? null, formatter ? "alejandra", - localOverlayPath ? /. + "${flakes.self}" + /pkgs, - nixpkgsConfigPath ? localOverlayPath + /config, - nixosSourcePath ? /. + "${flakes.self}" + /sys, - nixosPlatformsPath ? - if nixosSourcePath != null - then nixosSourcePath + /platform - else null, - hmSourcePath ? /. + "${flakes.self}" + /home, - hmPlatformsPath ? - if hmSourcePath != null - then hmSourcePath + /platform - else null, + paths ? {}, }: let + mkDoctrine = args: + self.lib.mkDoctrine + (args + // optionalAttrs (doctrinePrefix != null) { + prefix = doctrinePrefix; + }); + + doctrineNoPkgs = mkDoctrine { + lib = nixpkgs.lib; + pkgs = null; + }; + optionalFlake = name: if flakes ? "${name}" then flakes.${name} @@ -74,7 +87,7 @@ requireFlake = name: if flakes ? "${name}" then flakes.${name} - else throw "Required flake input '${name}' is required but was not provided"; + else throw "Required flake input '${name}' is missing"; nur = optionalFlake "nur"; nixpkgs = requireFlake "nixpkgs"; @@ -85,13 +98,21 @@ then requireFlake "home-manager" else null; + pathFromSelf = path: builtins.toPath "${flakes.self}" + "/${path}"; + + localOverlayPath = pathFromSelf paths.localOverlay; + nixpkgsConfigPath = pathFromSelf paths.nixpkgsConfig; + nixosSourcePath = pathFromSelf paths.nixosSource; + nixosPlatformsPath = pathFromSelf paths.nixosPlatforms; + hmSourcePath = pathFromSelf paths.hmSource; + hmPlatformsPath = pathFromSelf paths.hmPlatforms; + pkgs = importPkgs nixpkgs; importPkgs = flake: import flake ({ inherit system; - config = import ./pkgs/config nixpkgs.lib; overlays = let conditions = [ { @@ -100,7 +121,7 @@ } # NB: Preserve the relative order { - overlay = self.overlays.trivium; + overlay = self.overlays.default; condition = true; } { @@ -111,87 +132,87 @@ in builtins.map (cond: cond.overlay) (builtins.filter (cond: cond.condition) conditions); } - // ( - if nixpkgsConfigPath != null - then { - config = import nixpkgsConfigPath {inherit (nixpkgs) lib;}; - } - else {} - )); + // optionalAttrs (paths ? nixpkgsConfig) { + config = import nixpkgsConfigPath {inherit (nixpkgs) lib;}; + }); - inherit (pkgs.trivium.lib) importAll; + inherit (pkgs) lib; + inherit (nixpkgs.lib) optionalAttrs; # Prevents infinite recursion + inherit (doctrineNoPkgs) prefix; + inherit (doctrineNoPkgs.lib) importAll; in - with pkgs.lib; - { - formatter.${system} = - if formatter == "alejandra" - then pkgs.alejandra - else if formatter == "nixpkgs-fmt" - then pkgs.nixpkgs-fmt - else throw "Unknown formatter: '${formatter}'"; + { + formatter.${system} = + if formatter == "alejandra" + then pkgs.alejandra + else if formatter == "nixpkgs-fmt" + then pkgs.nixpkgs-fmt + else throw "Unknown formatter: '${formatter}'"; - packages.${system} = pkgs.local; + packages.${system} = pkgs.${prefix}; - overlays.default = final: prev: let - overlay = - if localOverlayPath != null - then import localOverlayPath - else (final: prev: {}); - in - mapOverlayOverride "local" overlay final prev - // optionalAttrs (unstable != null) { - unstable = importPkgs unstable; + overlays.default = final: prev: let + overlay = + if paths ? localOverlay + then import localOverlayPath + else (final: prev: {}); + in + mapOverlayOverride prefix overlay final prev + // optionalAttrs (unstable != null) { + unstable = importPkgs unstable; + }; + } + // optionalAttrs (paths ? nixosSource) { + nixosConfigurations = let + nixosSystem = {modules}: + lib.makeOverridable nixpkgs.lib.nixosSystem { + inherit modules pkgs system; + + specialArgs = { + inherit flakes; + + doctrine = mkDoctrine { + inherit pkgs; + namespace = "sys"; + }; + }; }; - } - // optionalAttrs (nixosSourcePath != null) { - nixosConfigurations = let - nixosSystem = {modules}: - makeOverridable nixpkgs.lib.nixosSystem { - inherit modules pkgs system; - specialArgs = { - inherit flakes; + hostConfig = platform: + nixosSystem { + modules = [ + self.nixosModules.default + nixosSourcePath + platform + ]; + }; + in + lib.mapAttrs (_: hostConfig) (importAll {root = nixosPlatformsPath;}); + } + // optionalAttrs (paths ? hmSource) { + homeConfigurations = let + home = name: platform: + home-manager.lib.homeManagerConfiguration { + inherit pkgs; + + extraSpecialArgs = { + inherit flakes; + + doctrine = mkDoctrine { + inherit pkgs; + namespace = "hm"; }; }; - hostConfig = platform: - nixosSystem { - modules = [ - self.nixosModules.default - nixosSourcePath - platform - ]; - }; - in - mapAttrs (_: hostConfig) (importAll {root = nixosPlatformsPath;}); - } - // optionalAttrs (hmSourcePath != null) { - homeConfigurations = let - registry = {...}: { - config.nix.registry = - mapAttrs - (_: value: {flake = value;}) - flakes; + modules = [ + self.homeManagerModules.default + hmSourcePath + platform + ]; }; - - home = name: platform: - home-manager.lib.homeManagerConfiguration { - inherit pkgs; - - extraSpecialArgs = { - inherit flakes; - }; - - modules = [ - self.homeManagerModules.default - hmSourcePath - platform - registry - ]; - }; - in - mapAttrs home (importAll {root = hmPlatformsPath;}); - }; + in + lib.mapAttrs home (importAll {root = hmPlatformsPath;}); + }; }; }; }