diff options
| author | seth <[email protected]> | 2024-09-07 11:51:57 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2024-09-07 12:00:19 -0400 |
| commit | dee6c5d9cbc12ff3012a27a5e69dfe331089310b (patch) | |
| tree | 290b5862448b781776f6606534829d4a35c2ac7f /modules/flake | |
| parent | 8539ad118cd34e8f51e736e1c6bec7a19f0e15f1 (diff) | |
tree-wide: use flake module for configurations
Diffstat (limited to 'modules/flake')
| -rw-r--r-- | modules/flake/default.nix | 12 | ||||
| -rw-r--r-- | modules/flake/map-configs.nix | 191 |
2 files changed, 203 insertions, 0 deletions
diff --git a/modules/flake/default.nix b/modules/flake/default.nix new file mode 100644 index 0000000..d06026d --- /dev/null +++ b/modules/flake/default.nix @@ -0,0 +1,12 @@ +let + flakeModules = { + map-configs = ./map-configs.nix; + }; +in +{ + imports = [ flakeModules.map-configs ]; + + flake = { + inherit flakeModules; + }; +} diff --git a/modules/flake/map-configs.nix b/modules/flake/map-configs.nix new file mode 100644 index 0000000..e7733f3 --- /dev/null +++ b/modules/flake/map-configs.nix @@ -0,0 +1,191 @@ +{ + config, + lib, + withSystem, + inputs, + self, + ... +}: +let + nixosSystem = + { + nixpkgs, + modules, + specialArgs, + ... + }@args: + nixpkgs.lib.nixosSystem ( + lib.removeAttrs args [ "nixpkgs" ] + // { + modules = modules ++ builtins.attrValues (self.nixosModules or [ ]); + specialArgs = specialArgs // { + inherit inputs; + }; + } + ); + + darwinSystem = + { + nix-darwin, + modules, + specialArgs, + ... + }@args: + nix-darwin.lib.darwinSystem ( + lib.removeAttrs args [ "nix-darwin" ] + // { + modules = modules ++ builtins.attrValues (self.darwinModules or { }); + specialArgs = specialArgs // { + inherit inputs; + }; + } + ); + + homeManagerConfiguration = + { + modules, + extraSpecialArgs, + ... + }@args: + inputs.home-manager.lib.homeManagerConfiguration ( + args + // { + modules = modules ++ builtins.attrValues (self.homeModules or self.homeManagerModules or { }); + extraSpecialArgs = extraSpecialArgs // { + inherit inputs; + }; + } + ); + + modulesOption = lib.mkOption { + type = lib.types.listOf lib.types.unspecified; + default = [ ]; + description = '' + List of modules to use in the configuration + ''; + }; + + specialArgsOption = lib.mkOption { + type = lib.types.lazyAttrsOf lib.types.raw; + default = { }; + description = '' + Extra arguments to pass to the configuration + ''; + }; + + freeformType = lib.types.attrsOf lib.types.raw; + + nixosConfigurationSubmodule = { + inherit freeformType; + + options = { + nixpkgs = lib.mkOption { + type = lib.types.lazyAttrsOf lib.types.raw; + default = inputs.nixpkgs or (throw "Could not find flake input `nixpkgs`"); + description = '' + Instance of nixpkgs to use `lib.nixosSystem` from + ''; + example = lib.literalExpression '' + inputs.nixpkgs-stable + ''; + }; + + modules = modulesOption; + specialArgs = specialArgsOption; + }; + }; + + homeConfigurationSubmodule = { + inherit freeformType; + + options = { + pkgs = lib.mkOption { + type = lib.types.lazyAttrsOf lib.types.raw; + default = withSystem "x86_64-linux" ({ pkgs, ... }: pkgs); + description = '' + Instance of nixpkgs to use with the configuration + ''; + example = lib.literalExpression '' + inputs.nixpkgs.legacyPackages.aarch64-darwin + ''; + }; + + modules = modulesOption; + extraSpecialArgs = specialArgsOption; + }; + }; + + darwinConfigurationSubmodule = { + inherit freeformType; + + options = { + nix-darwin = lib.mkOption { + type = lib.types.lazyAttrsOf lib.types.raw; + default = + inputs.nix-darwin or inputs.darwin + or (throw "Could not find flake input `nixpkgs` or `nix-darwin`"); + description = '' + Instance of nix-darwin to use `lib.nix-darwin` from + ''; + }; + + modules = modulesOption; + specialArgs = specialArgsOption; + }; + }; +in + +{ + options = { + nixosConfigurations = lib.mkOption { + type = lib.types.lazyAttrsOf (lib.types.submodule nixosConfigurationSubmodule); + default = { }; + apply = lib.mapAttrs (lib.const nixosSystem); + description = '' + Map of configuration names and arguments to `nixosSystem` + ''; + example = lib.literalExpression '' + { + my-machine = { modules = [ ./configuration.nix ]; }; + } + ''; + }; + + homeConfigurations = lib.mkOption { + type = lib.types.lazyAttrsOf (lib.types.submodule homeConfigurationSubmodule); + default = { }; + apply = lib.mapAttrs (lib.const homeManagerConfiguration); + description = '' + Map of configuration names and arguments to `homeManagerConfiguration` + ''; + example = lib.literalExpression '' + { + me = { pkgs = nixpkgs.legacyPackages.x86_64-linux; }; + } + ''; + }; + + darwinConfigurations = lib.mkOption { + type = lib.types.lazyAttrsOf (lib.types.submodule darwinConfigurationSubmodule); + default = { }; + apply = lib.mapAttrs (lib.const darwinSystem); + description = '' + Map of configuration names and arguments to `darwinSystem` + ''; + example = lib.literalExpression '' + { + my-mac = { modules = [ ./darwin-configuration.nix ]; }; + } + ''; + }; + }; + + config.flake = { + inherit (config) nixosConfigurations homeConfigurations darwinConfigurations; + /* + nixosConfigurations = lib.mapAttrs (lib.const nixosSystem) config.nixosConfigurations; + homeConfigurations = lib.mapAttrs (lib.const homeManagerConfiguration) config.homeConfigurations; + darwinConfigurations = lib.mapAttrs (lib.const darwinSystem) config.darwinConfigurations; + */ + }; +} |
