diff options
Diffstat (limited to 'trivionomicon/flake.nix')
| -rw-r--r-- | trivionomicon/flake.nix | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/trivionomicon/flake.nix b/trivionomicon/flake.nix new file mode 100644 index 0000000..151c3d3 --- /dev/null +++ b/trivionomicon/flake.nix @@ -0,0 +1,229 @@ +{ + inputs = { + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + }: let + mapOverlayOverride = prefix: overlay: final: prev: let + overlayPkgs = overlay final prev; + in + { + "${prefix}" = (prev.${prefix} or {}) // builtins.removeAttrs overlayPkgs ["override"]; + } + // (overlayPkgs.override or {}); + + doctrineNoPkgs = self.lib.mkDoctrine { + lib = nixpkgs.lib; + pkgs = null; + }; + in + flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs {inherit system;}; + in { + formatter = pkgs.alejandra; + + packages = + (import nixpkgs { + inherit system; + overlays = [self.overlays.default]; + }).${ + doctrineNoPkgs.prefix + }; + }) + // { + templates = let + system-flake = { + path = ./templates/system-flake; + description = "Opinionated flake for a NixOS system with Home Manager"; + }; + in { + inherit system-flake; + + default = system-flake; + }; + + overlays = let + overlay = mapOverlayOverride doctrineNoPkgs.prefix (import ./pkgs); + in { + default = overlay; + ${doctrineNoPkgs.prefix} = overlay; + }; + + homeManagerModules.default = ./modules; + nixosModules.default = ./modules; + + lib = { + mkDoctrine = import ./doctrine; + + mkSystemFlake = { + flakes, + system, + doctrinePrefix ? null, + formatter ? "alejandra", + 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} + else null; + + requireFlake = name: + if flakes ? "${name}" + then flakes.${name} + else throw "Required flake input '${name}' is missing"; + + nur = optionalFlake "nur"; + nixpkgs = requireFlake "nixpkgs"; + unstable = optionalFlake "unstable"; + + home-manager = + if hmSourcePath != null + 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; + + overlays = let + conditions = [ + { + overlay = nur.overlays.default; + condition = nur != null; + } + # NB: Preserve the relative order + { + overlay = mapOverlayOverride prefix (import ./pkgs); + condition = true; + } + { + overlay = flakes.self.overlays.default; + condition = true; + } + ]; + in + builtins.map (cond: cond.overlay) (builtins.filter (cond: cond.condition) conditions); + } + // optionalAttrs (paths ? nixpkgsConfig) { + config = import nixpkgsConfigPath {inherit (nixpkgs) lib;}; + }); + + inherit (pkgs) lib; + inherit (nixpkgs.lib) optionalAttrs; # Prevents infinite recursion + inherit (doctrineNoPkgs) prefix; + inherit (doctrineNoPkgs.lib) importAll; + in + { + 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.${prefix}; + + overlays.default = final: prev: let + overlay = final: prev: + if paths ? localOverlay + then import localOverlayPath {inherit final prev flakes;} + else {}; + in + mapOverlayOverride prefix overlay final prev + // optionalAttrs (unstable != null) { + unstable = importPkgs unstable; + }; + } + // optionalAttrs (paths ? nixosSource) { + nixosConfigurations = let + hostConfig = platform: + self.lib.mkSystem { + inherit flakes pkgs; + doctrine = doctrineNoPkgs; + + modules = [ + 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"; + }; + }; + + modules = [ + self.homeManagerModules.default + hmSourcePath + platform + ]; + }; + in + lib.mapAttrs home (importAll {root = hmPlatformsPath;}); + }; + + mkSystem = { + pkgs, + flakes, + doctrine, + modules, + }: + flakes.nixpkgs.lib.makeOverridable flakes.nixpkgs.lib.nixosSystem { + inherit pkgs; + inherit (pkgs.stdenv.hostPlatform) system; + + modules = [self.nixosModules.default] ++ modules; + + specialArgs = { + inherit flakes; + + doctrine = self.lib.mkDoctrine { + inherit pkgs; + inherit (doctrine) prefix; + namespace = "sys"; + }; + }; + }; + }; + }; +} |
