From 5c8627cde65df0753b0ab4d73ab6b1d5d6e9d865 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sun, 10 Jan 2021 21:52:29 -0500 Subject: [PATCH] extract "exports" & organize code --- nixosFlake.nix | 212 ++++++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 119 deletions(-) diff --git a/nixosFlake.nix b/nixosFlake.nix index 7a5f031..5438afa 100644 --- a/nixosFlake.nix +++ b/nixosFlake.nix @@ -4,7 +4,7 @@ # pass an instance of self self , # pass an instance of the nixpkgs flake used to determine your nixos version - nixpkgs-os + nixpkgsOS , # we assume that the name maps to the project name, and also that the # overlay has an attribute with the `name` prefix that contains all of the # project's packages. @@ -13,7 +13,7 @@ nixpkgsAlt ? null , # list of strings that represent a package path; pass either a list or a path to one backportFromAlt ? [] -, # nixpkgs config for nixpkgs-os & nixpkgs-unstable +, # nixpkgs config for nixpkgsOS & nixpkgs-unstable config ? {} , # pass either an attribute set or a path to one hosts ? null @@ -24,13 +24,13 @@ , # use this to load other flakes modules to supplement your overlays externOverlays ? [] , # main (or only) local nixos configuration module; pass either a nixos module or a path to one - nixosModules ? null + nixosModule ? null , # local nixos configuration modules; pass either an attrset of module or a path to one to each leaf attr nixosModules ? {} , # use this to load other flakes modules to supplement your configuration externNixosModules ? [] , # main (or only) local numtide/devshell configuration module; pass either a devshell module or a path to one - devshellModules ? null + devshellModule ? null , # local numtide/devshell configuration modules; pass either a devshell module or a path to one to each leaf attr devshellModules ? {} , # use this to load other flakes modules to supplement your devshells @@ -51,78 +51,97 @@ let obj ; + exports = { + # let others kick-start from your configuration + templates."${name}" = { + path = self; + description = "template based on ${name}'s configuration"; + }; + defaultTemplate = self.templates.${name}; + } + # share your name-spaced (mainly) custom packages as overlay (convention) + // ( + if overlay != null then { + overlay = maybeImport overlay; + } else {} + ) + # share non-name-spaced custom (mainly non packages) overlays (convention) + // ( + if overlays != {} then { + overlays = maybeImportValues (lib.flattenTree (maybeImport overlays)); + } else {} + ) + # share your primary (or only) nixos configurations as module + // ( + if nixosModule != null then { + nixosModule = maybeImport nixosModule; + } else {} + ) + # share your nixos configurations as modules + // ( + if nixosModules != {} then { + nixosModules = maybeImportValues (lib.flattenTree (maybeImport nixosModules)); + } else {} + ) + # share your primary (or only) devshell configurations as module + // ( + if devshellModule != null then { + devshellModule = maybeImport devshellModule; + } else {} + ) + # share your devshell configurations as modules + // ( + if devshellModules != null then { + devshellModules = maybeImportValues (lib.flattenTree (maybeImport devshellModules)); + } else {} + ); + outputs = lib.eachSystem systems ( system: let - overlays_ = - externOverlays - ++ ( - if self ? "overlay" then - [ self.overlay ] - else - [] - ) - ++ ( - if self ? "overlays" then - builtins.attrValues self.overlays - else - [] - ) - ; - overlays' = let - backportFromAlt' = maybeImport backportFromAlt; - pkgsAlt = import nixpkgsAlt { - inherit - config - system - ; - }; - resolveKey = key: - let - attrs = builtins.filter builtins.isString (builtins.split "\\." key); - op = sum: attr: sum.${attr} or (throw "package \"${key}\" not found"); - in - builtins.foldl' op pkgs attrs - ; - overlay = pkgKey: final: prev: { - pkgKey = resolveKey pkgKey; - }; - in - overlays_ ++ (map overlay backportFromAlt'); - nixosModules' = externNixosModules - ++ ( - if self ? "nixosModule" then - [ self.nixosModules ] - else - [] - ) - ++ ( - if self ? "nixosModules" then - builtins.attrValues self.nixosModules - else - [] - ) + ++ (if self ? "nixosModule" then [ self.nixosModule ] else []) + ++ (if self ? "nixosModules" then builtins.attrValues self.nixosModules else []) ; devshellModules' = externDevshellModules + ++ (if self ? "devshellModule" then [ self.devshellModule ] else []) + ++ (if self ? "devshellModules" then builtins.attrValues self.devshellModules else []) + ; + + overlays' = + externOverlays + ++ (if self ? "overlay" then [ self.overlay ] else []) + ++ (if self ? "overlays" then builtins.attrValues self.overlays else []) ++ ( - if self ? "devshellModule" then - [ self.devshellModules ] - else - [] - ) - ++ ( - if self ? "devshellModules" then - builtins.attrValues self.devshellModules - else - [] + let + resolveKey = pkgs: key: + let + attrs = builtins.filter builtins.isString (builtins.split "\\." key); + op = sum: attr: sum.${attr} or (throw "package \"${key}\" not found"); + in + builtins.foldl' op pkgs attrs + ; + resolveOverlay = pkgs: pkgKey: final: prev: { + pkgKey = resolveKey pkgs pkgKey; + }; + in + let + backportFromAlt' = maybeImport backportFromAlt; + pkgsAlt = import nixpkgsAlt { + inherit + config + system + ; + }; + in + map resolveOverlay pkgsAlt backportFromAlt' ) ; - pkgs = import nixpkgs-os { + pkgs = import nixpkgsOS { overlays = overlays'; inherit config @@ -134,11 +153,12 @@ let in { nixosConfigurations = - import ./hosts ( - recursiveUpdate inputs { - inherit lib pkgs system utils externModules; - } - ); + maybeImportValues (lib.flattenTree (maybeImport hosts)) + ( + recursiveUpdate inputs { + inherit lib pkgs system utils externModules; + } + ); legacyPackages = packages; @@ -146,62 +166,16 @@ let # Filter on broken packages - you dont't want to expose them to the world (_: drv: drv.meta.broken != true) lib.flattenTreeSystem system packages; - } - // ( + } // ( if packages ? defaultPackage then { defaultPackage = packages.defaultPackage; } else {} - ) - // ( + ) // ( if shell != null then { devShell = maybeImport shell { inherit pkgs; }; } else if packages ? devShell then { devShell = packages.devShell; } else {} - ) - # share your name-spaced (mainly) custom packages as overlay (convention) - // ( - if overlay != null then { - overlay = maybeImport overlay; - } else {} - ) - # share non-name-spaced custom (mainly non packages) overlays (convention) - // ( - if overlays != {} then { - overlays = maybeImportValues (lib.flattenTreeSystem system (maybeImport overlays)); - } else {} - ) - # share your primary (or only) nixos configurations as module - // ( - if nixosModule != null then { - nixosModule = maybeImport nixosModule; - } else {} - ) - # share your nixos configurations as modules - // ( - if nixosModules != {} then { - nixosModules = maybeImportValues (lib.flattenTreeSystem system (maybeImport nixosModules)); - } else {} - ) - # share your primary (or only) devshell configurations as module - // ( - if devshellModule != null then { - devshellModule = maybeImport devshellModule; - } else {} - ) - # share your devshell configurations as modules - // ( - if devshellModules != null then { - devshellModules = maybeImportValues (lib.flattenTreeSystem system (maybeImport devshellModules)); - } else {} - ) - # let others kick-start from your configuration - // ( - templates.${name} = { - path = self; - description = "template based on ${name}'s configuration"; - }; - defaultTemplate = self.templates.${name}; - ) - in - outputs + ); + in + outputs // exports