trivionomicon/flake: rewrite broken implementation of mkSystemFlake

This commit is contained in:
Alejandro Soto 2025-08-24 18:51:05 -06:00
parent 111d3dabf2
commit bf39923f9c

205
flake.nix
View file

@ -16,15 +16,24 @@
"${namespace}" = builtins.removeAttrs overlayPkgs ["override"]; "${namespace}" = builtins.removeAttrs overlayPkgs ["override"];
} }
// (overlayPkgs.override or {}); // (overlayPkgs.override or {});
doctrineNoPkgs = self.lib.mkDoctrine {
lib = nixpkgs.lib;
pkgs = null;
};
in in
flake-utils.lib.eachDefaultSystem (system: { flake-utils.lib.eachDefaultSystem (system: let
formatter = (import nixpkgs {inherit system;}).alejandra; pkgs = import nixpkgs {inherit system;};
in {
formatter = pkgs.alejandra;
packages = packages =
(import nixpkgs { (import nixpkgs {
inherit system; inherit system;
overlays = [(mapOverlayOverride "local" (import ./pkgs))]; overlays = [(mapOverlayOverride doctrineNoPkgs.prefix (import ./pkgs))];
}).local; }).${
doctrineNoPkgs.prefix
};
}) })
// { // {
templates = let templates = let
@ -39,33 +48,37 @@
}; };
overlays = let overlays = let
overlay = mapOverlayOverride "trivium" (import ./pkgs); overlay = mapOverlayOverride doctrineNoPkgs.prefix (import ./pkgs);
in { in {
default = overlay; default = overlay;
trivium = overlay; ${doctrineNoPkgs.prefix} = overlay;
}; };
homeManagerModules.default = ./modules; homeManagerModules.default = ./modules;
nixosModules.default = ./modules; nixosModules.default = ./modules;
lib = { lib = {
mkDoctrine = import ./doctrine;
mkSystemFlake = { mkSystemFlake = {
flakes, flakes,
system, system,
doctrinePrefix ? null,
formatter ? "alejandra", formatter ? "alejandra",
localOverlayPath ? /. + "${flakes.self}" + /pkgs, paths ? {},
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,
}: let }: let
mkDoctrine = args:
self.lib.mkDoctrine
(args
// optionalAttrs (doctrinePrefix != null) {
prefix = doctrinePrefix;
});
doctrineNoPkgs = mkDoctrine {
lib = nixpkgs.lib;
pkgs = null;
};
optionalFlake = name: optionalFlake = name:
if flakes ? "${name}" if flakes ? "${name}"
then flakes.${name} then flakes.${name}
@ -74,7 +87,7 @@
requireFlake = name: requireFlake = name:
if flakes ? "${name}" if flakes ? "${name}"
then 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"; nur = optionalFlake "nur";
nixpkgs = requireFlake "nixpkgs"; nixpkgs = requireFlake "nixpkgs";
@ -85,13 +98,21 @@
then requireFlake "home-manager" then requireFlake "home-manager"
else null; 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; pkgs = importPkgs nixpkgs;
importPkgs = flake: importPkgs = flake:
import flake ({ import flake ({
inherit system; inherit system;
config = import ./pkgs/config nixpkgs.lib;
overlays = let overlays = let
conditions = [ conditions = [
{ {
@ -100,7 +121,7 @@
} }
# NB: Preserve the relative order # NB: Preserve the relative order
{ {
overlay = self.overlays.trivium; overlay = self.overlays.default;
condition = true; condition = true;
} }
{ {
@ -111,87 +132,87 @@
in in
builtins.map (cond: cond.overlay) (builtins.filter (cond: cond.condition) conditions); builtins.map (cond: cond.overlay) (builtins.filter (cond: cond.condition) conditions);
} }
// ( // optionalAttrs (paths ? nixpkgsConfig) {
if nixpkgsConfigPath != null config = import nixpkgsConfigPath {inherit (nixpkgs) lib;};
then { });
config = import nixpkgsConfigPath {inherit (nixpkgs) lib;};
}
else {}
));
inherit (pkgs.trivium.lib) importAll; inherit (pkgs) lib;
inherit (nixpkgs.lib) optionalAttrs; # Prevents infinite recursion
inherit (doctrineNoPkgs) prefix;
inherit (doctrineNoPkgs.lib) importAll;
in in
with pkgs.lib; {
{ formatter.${system} =
formatter.${system} = if formatter == "alejandra"
if formatter == "alejandra" then pkgs.alejandra
then pkgs.alejandra else if formatter == "nixpkgs-fmt"
else if formatter == "nixpkgs-fmt" then pkgs.nixpkgs-fmt
then pkgs.nixpkgs-fmt else throw "Unknown formatter: '${formatter}'";
else throw "Unknown formatter: '${formatter}'";
packages.${system} = pkgs.local; packages.${system} = pkgs.${prefix};
overlays.default = final: prev: let overlays.default = final: prev: let
overlay = overlay =
if localOverlayPath != null if paths ? localOverlay
then import localOverlayPath then import localOverlayPath
else (final: prev: {}); else (final: prev: {});
in in
mapOverlayOverride "local" overlay final prev mapOverlayOverride prefix overlay final prev
// optionalAttrs (unstable != null) { // optionalAttrs (unstable != null) {
unstable = importPkgs unstable; 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 = { hostConfig = platform:
inherit flakes; 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: modules = [
nixosSystem { self.homeManagerModules.default
modules = [ hmSourcePath
self.nixosModules.default platform
nixosSourcePath ];
platform
];
};
in
mapAttrs (_: hostConfig) (importAll {root = nixosPlatformsPath;});
}
// optionalAttrs (hmSourcePath != null) {
homeConfigurations = let
registry = {...}: {
config.nix.registry =
mapAttrs
(_: value: {flake = value;})
flakes;
}; };
in
home = name: platform: lib.mapAttrs home (importAll {root = hmPlatformsPath;});
home-manager.lib.homeManagerConfiguration { };
inherit pkgs;
extraSpecialArgs = {
inherit flakes;
};
modules = [
self.homeManagerModules.default
hmSourcePath
platform
registry
];
};
in
mapAttrs home (importAll {root = hmPlatformsPath;});
};
}; };
}; };
} }