summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorseth <[email protected]>2024-02-10 18:52:50 -0500
committerseth <[email protected]>2024-02-10 18:58:50 -0500
commit72346489e9ea53eb7689f5d632e854411f165da8 (patch)
tree033dc8411281f8308f497c48a851f5a1e2987783
parentde90fa19303213ace45952711bf0fd6b3cd85f3d (diff)
flakeModules/configurations: refactor into freeform module, add aliases
-rw-r--r--modules/flake/configurations.nix267
-rw-r--r--modules/nixos/traits/secrets.nix4
-rw-r--r--systems/default.nix38
-rw-r--r--users/default.nix17
4 files changed, 133 insertions, 193 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;
};
}
diff --git a/modules/nixos/traits/secrets.nix b/modules/nixos/traits/secrets.nix
index 57762cd..862e76f 100644
--- a/modules/nixos/traits/secrets.nix
+++ b/modules/nixos/traits/secrets.nix
@@ -13,6 +13,10 @@ in {
imports = [inputs.agenix.nixosModules.default];
config = lib.mkIf cfg.enable {
+ _module.args = {
+ secretsDir = ../../../secrets/${config.networking.hostName};
+ };
+
age = {
identityPaths = ["/etc/age/key"];
};
diff --git a/systems/default.nix b/systems/default.nix
index 49f0e44..f708430 100644
--- a/systems/default.nix
+++ b/systems/default.nix
@@ -2,33 +2,31 @@
inputs,
self,
...
-}: {
- configurations = {
- nixos = {
- builder = inputs.nixpkgs.lib.nixosSystem;
-
- systems = {
- glados = {};
-
- glados-wsl = {};
+}: let
+ nixos-stable = inputs.nixpkgs-stable.lib.nixosSystem;
+in {
+ nixosConfigurations = {
+ glados = {
+ system = "x86_64-linux";
+ };
- atlas = {
- builder = inputs.nixpkgs-stable.lib.nixosSystem;
- system = "aarch64-linux";
- };
- };
+ glados-wsl = {
+ system = "x86_64-linux";
};
- darwin = {
- builder = inputs.darwin.lib.darwinSystem;
+ atlas = {
+ builder = nixos-stable;
+ system = "aarch64-linux";
+ };
+ };
- systems = {
- caroline = {};
- };
+ darwinConfigurations = {
+ caroline = {
+ system = "x86_64-darwin";
};
};
- flake.deploy = {
+ deploy = {
remoteBuild = true;
fastConnection = false;
nodes = self.lib.deploy.mapNodes [
diff --git a/users/default.nix b/users/default.nix
index 65bcb84..efa88d2 100644
--- a/users/default.nix
+++ b/users/default.nix
@@ -1,16 +1,13 @@
-{inputs, ...}: {
- configurations = {
- home = {
- builder = inputs.home-manager.lib.homeManagerConfiguration;
- pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux;
-
- users = {
- seth = {};
- };
+{inputs, ...}: let
+ unstableFor = inputs.nixpkgs.legacyPackages;
+in {
+ homeConfigurations = {
+ seth = {
+ pkgs = unstableFor.x86_64-linux;
};
};
- flake.homeModules = {
+ homeModules = {
seth = import ./seth/module;
};
}