diff options
Diffstat (limited to 'modules/flake')
| -rw-r--r-- | modules/flake/configurations.nix | 267 |
1 files changed, 104 insertions, 163 deletions
diff --git a/modules/flake/configurations.nix b/modules/flake/configurations.nix index c7bff5b..78a5033 100644 --- a/modules/flake/configurations.nix +++ b/modules/flake/configurations.nix @@ -6,107 +6,97 @@ self, ... }: let - namespace = "configurations"; - cfg = config.${namespace}; - inherit (lib) - genAttrs + attrValues literalExpression mapAttrs mdDoc + mkAliasOptionModule mkOption recursiveUpdate types ; builderType = types.functionTo pkgsType; - modulesType = types.listOf types.unspecified; pkgsType = types.lazyAttrsOf types.raw; - kernelFor = type: - { - nixos = "linux"; - darwin = "darwin"; - } - .${type}; - - builderStringFor = type: - { - nixos = "inputs.nixpkgs.lib.nixosSystem"; - darwin = "inputs.nix-darwin.lib.darwinSystem"; - } - .${type}; - - inputsFor = system: withSystem system ({inputs', ...}: inputs'); - - mkSystem = type: name: let - args = cfg.${type}.systems.${name}; - in - args.builder (lib.recursiveUpdate (removeAttrs args ["builder"]) { - modules = - [ - ../../systems/${name} - {networking.hostName = name;} - ] - ++ lib.attrValues self."${type}Modules" - ++ cfg.${type}.modules - ++ args.modules; - - specialArgs = { - inherit inputs; - inputs' = inputsFor args.system; - secretsDir = ../../secrets/${name}; - }; - }); - - mkUser = name: let - args = cfg.home.users.${name}; - in - args.builder (recursiveUpdate (removeAttrs args ["builder"]) { - inherit (args) pkgs; - - modules = - [ - ../../users/${name} - - { - _module.args.osConfig = {}; - programs.home-manager.enable = true; - } - ] - ++ cfg.home.modules - ++ args.modules; - - extraSpecialArgs = { - inherit inputs; - inputs' = inputsFor args.pkgs.stdenv.hostPlatform.system; - }; - }); + defaultBuilderFor = { + nixos = inputs.nixpkgs.lib.nixosSystem; + darwin = (inputs.darwin or inputs.nix-darwin).lib.darwinSystem; + }; - mapSystems = type: mapAttrs (name: _: mkSystem type name) cfg.${type}.systems; - mapUsers = mapAttrs (name: _: mkUser name) cfg.home.users; + builderStringFor = { + nixos = "inputs.nixpkgs.lib.nixosSystem"; + darwin = "inputs.nix-darwin.lib.darwinSystem"; + }; + + kernelFor = { + nixos = "linux"; + darwin = "darwin"; + }; + + applySpecialArgsFor = system: + recursiveUpdate { + inherit inputs; + inputs' = withSystem system ({inputs', ...}: inputs'); + }; + + toSystem = type: name: args: + args.builder ( + recursiveUpdate (builtins.removeAttrs args ["builder"]) { + modules = + [ + ../../systems/${name} + {networking.hostName = name;} + ] + ++ attrValues (self."${type}Modules" or {}) + ++ (args.modules or []); + + specialArgs = applySpecialArgsFor args.system (args.specialArgs or {}); + } + ); + + toUser = name: args: + inputs.home-manager.lib.homeManagerConfiguration ( + recursiveUpdate args { + modules = + [ + ../../users/${name} + + { + _module.args.osConfig = {}; + programs.home-manager.enable = true; + } + ] + ++ attrValues (self.homeModules or {}) + ++ (args.modules or []); + + extraSpecialArgs = let + inherit (args.pkgs.stdenv.hostPlatform) system; + in + applySpecialArgsFor system (args.extraSpecialArgs or {}); + } + ); + + mapSystems = type: mapAttrs (toSystem type); + mapUsers = mapAttrs toUser; + mapNixOS = mapSystems "nixos"; + mapDarwin = mapSystems "darwin"; systemsSubmodule = type: { + freeformType = types.attrsOf types.any; + options = { builder = mkOption { type = builderType; - default = cfg.${type}.builder; + default = defaultBuilderFor.${type}; example = literalExpression (builderStringFor type); description = mdDoc '' Function to build this ${type}Configuration with ''; }; - modules = mkOption { - type = modulesType; - default = []; - example = literalExpression "[ self.${type}Modules.default ]"; - description = mdDoc '' - Extra modules to add to this ${type}Configuration - ''; - }; - system = mkOption { type = types.str; default = "x86_64-${kernelFor type}"; @@ -119,28 +109,11 @@ }; usersSubmodule = { - options = { - builder = mkOption { - type = builderType; - default = cfg.home.builder; - example = literalExpression "inputs.home-manager.lib.homeManagerConfiguration"; - description = mdDoc '' - Function to build this homeConfiguration with - ''; - }; - - modules = mkOption { - type = modulesType; - default = []; - example = literalExpression "[ self.hmModules.default ]"; - description = mdDoc '' - Extra modules to add to this homeConfiguration - ''; - }; + freeformType = types.attrsOf types.any; + options = { pkgs = mkOption { type = pkgsType; - default = cfg.home.pkgs; example = literalExpression "inputs.nixpkgs.legacyPackages.aarch64-linux"; description = mdDoc '' Instance of nixpkgs to use in this homeConfiguration @@ -149,25 +122,8 @@ }; }; - mkSystemOptions = type: { - builder = mkOption { - type = builderType; - example = literalExpression (builderStringFor type); - description = mdDoc '' - Default function to build ${type}Configurations with - ''; - }; - - modules = mkOption { - type = modulesType; - default = []; - example = literalExpression "[ self.${type}Modules.default ]"; - description = mdDoc '' - List of modules to add to all ${type}Configurations - ''; - }; - - systems = mkOption { + mkSystemOptions = type: + mkOption { type = types.attrsOf (types.submodule (systemsSubmodule type)); default = {}; example = literalExpression '' @@ -178,60 +134,45 @@ } ''; description = mdDoc '' - Attribute set of ${type}Configuration definitions + Attribute set of `lib.${type}System` options. The names of + each attribute will be used to import files in the `systems/` + directory ''; }; - }; in { - options.${namespace} = - genAttrs ["nixos" "darwin"] mkSystemOptions - // { - home = { - builder = mkOption { - type = builderType; - example = literalExpression "inputs.home-manager.lib.homeManagerConfiguration"; - description = mdDoc '' - Default function to build homeConfigurations with - ''; - }; - - modules = mkOption { - type = modulesType; - default = []; - example = literalExpression "[ self.homeModules.default ]"; - description = mdDoc '' - List of modules to add to all homeConfigurations - ''; - }; - - pkgs = mkOption { - type = pkgsType; - example = literalExpression "inputs.nixpkgs.legacyPackages.aarch64-linux"; - description = mdDoc '' - Default instance of nixpkgs to use in homeConfigurations - ''; - }; - - users = mkOption { - type = types.attrsOf (types.submodule usersSubmodule); - default = {}; - example = literalExpression '' - { - john = { - pkgs = inputs.nixpkgs.legacyPackages.aarch64-linux; - }; - } - ''; - description = mdDoc '' - Attribute set of homeConfiguration definitions - ''; - }; - }; + # i don't like prefixing so much with `flake` + imports = [ + (mkAliasOptionModule ["deploy"] ["flake" "deploy"]) + (mkAliasOptionModule ["nixosModules"] ["flake" "nixosModules"]) + (mkAliasOptionModule ["darwinModules"] ["flake" "darwinModules"]) + (mkAliasOptionModule ["homeModules"] ["flake" "homeModules"]) + ]; + + options = { + nixosConfigurations = mkSystemOptions "nixos"; + darwinConfigurations = mkSystemOptions "darwin"; + + homeConfigurations = mkOption { + type = types.attrsOf (types.submodule usersSubmodule); + default = {}; + example = literalExpression '' + { + john = { + pkgs = inputs.nixpkgs.legacyPackages.aarch64-linux; + }; + } + ''; + description = mdDoc '' + Attribute set of `lib.homeManagerConfiguration` arguments. The + name of each attribute will be used to import files in the `users/` + directory. + ''; }; + }; config.flake = { - nixosConfigurations = mapSystems "nixos"; - darwinConfigurations = mapSystems "darwin"; - homeConfigurations = mapUsers; + nixosConfigurations = mapNixOS config.nixosConfigurations; + darwinConfigurations = mapDarwin config.darwinConfigurations; + homeConfigurations = mapUsers config.homeConfigurations; }; } |
