From ed1264c062744f189b234e9aff7486b4287b77b6 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Fri, 26 Jan 2024 21:12:22 +0800 Subject: [PATCH] flake-parts/setups: refactor and add NixVim instance for home-manager configs --- modules/flake-parts/setups/home-manager.nix | 212 ++++++++---------- modules/flake-parts/setups/nixos.nix | 71 +----- modules/flake-parts/setups/nixvim.nix | 26 +-- .../setups/shared/config-options.nix | 22 ++ .../setups/shared/deploy-node-type.nix | 15 ++ .../setups/shared/nixvim-instance-options.nix | 32 +++ 6 files changed, 184 insertions(+), 194 deletions(-) create mode 100644 modules/flake-parts/setups/shared/config-options.nix create mode 100644 modules/flake-parts/setups/shared/deploy-node-type.nix create mode 100644 modules/flake-parts/setups/shared/nixvim-instance-options.nix diff --git a/modules/flake-parts/setups/home-manager.nix b/modules/flake-parts/setups/home-manager.nix index 578dfdca..0080c2a3 100644 --- a/modules/flake-parts/setups/home-manager.nix +++ b/modules/flake-parts/setups/home-manager.nix @@ -4,6 +4,7 @@ let cfg = config.setups.home-manager; homeManagerModules = ../../home-manager; + partsConfig = config; # A thin wrapper around the home-manager configuration function. mkHome = { system, nixpkgsBranch ? "nixpkgs", homeManagerBranch ? "home-manager", extraModules ? [ ] }: @@ -22,19 +23,9 @@ let deploySettingsType = { config, lib, username, ... }: { freeformType = with lib.types; attrsOf anything; + imports = [ ./shared/deploy-node-type.nix ]; options = { - fastConnection = - lib.mkEnableOption "deploy-rs to assume the target machine is considered fast"; - autoRollback = - lib.mkEnableOption "deploy-rs auto-rollback feature" // { - default = true; - }; - magicRollback = - lib.mkEnableOption "deploy-rs magic rollback feature" // { - default = true; - }; - remoteBuild = lib.mkEnableOption "pass the build process to the remote machine"; profiles = lib.mkOption { type = with lib.types; functionTo (attrsOf anything); default = homeenv: { @@ -71,112 +62,100 @@ let }; }; - configType = - let - partsConfig = config; - in - { config, name, lib, ... }: { - options = { - systems = lib.mkOption { - type = with lib.types; listOf str; - default = partsConfig.systems; - defaultText = "config.systems"; - example = [ "x86_64-linux" "aarch64-linux" ]; - description = '' - A list of platforms that the NixOS configuration is supposed to be - deployed on. - ''; - }; - - modules = lib.mkOption { - type = with lib.types; listOf raw; - default = [ ]; - description = '' - A list of NixOS modules specific for that host. - ''; - }; - - overlays = lib.mkOption { - type = with lib.types; listOf (functionTo raw); - default = [ ]; - example = lib.literalExpression '' - [ - inputs.neovim-nightly-overlay.overlays.default - inputs.emacs-overlay.overlays.default - ] - ''; - description = '' - A list of overlays to be applied for that host. - ''; - }; - - nixpkgsBranch = lib.mkOption { - type = lib.types.str; - default = "nixpkgs"; - example = "nixos-unstable-small"; - description = '' - The nixpkgs branch to be used for evaluating the NixOS configuration. - By default, it will use the `nixpkgs` flake input. - - ::: {.note} - This is based from your flake inputs and not somewhere else. If you - want to have support for multiple nixpkgs branch, simply add them as - a flake input. - ::: - ''; - }; - - homeManagerBranch = lib.mkOption { - type = lib.types.str; - default = "home-manager"; - example = "home-manager-stable"; - description = '' - The home-manager branch to be used for the NixOS module. By default, - it will use the `home-manager` flake input. - ''; - }; - - homeDirectory = lib.mkOption { - type = lib.types.path; - default = "/home/${name}"; - example = "/var/home/public-user"; - description = '' - The home directory of the home-manager user. - ''; - }; - - deploy = lib.mkOption { - type = with lib.types; nullOr (submoduleWith { - specialArgs = { - username = name; - }; - modules = [ deploySettingsType ]; - }); - default = null; - description = '' - deploy-rs settings to be passed onto the home-manager configuration - node. - ''; - }; + configType = { config, name, lib, ... }: { + options = { + overlays = lib.mkOption { + type = with lib.types; listOf (functionTo raw); + default = [ ]; + example = lib.literalExpression '' + [ + inputs.neovim-nightly-overlay.overlays.default + inputs.emacs-overlay.overlays.default + ] + ''; + description = '' + A list of overlays to be applied for that host. + ''; }; - config = { - modules = [ - ../../../configs/home-manager/${name} + nixpkgsBranch = lib.mkOption { + type = lib.types.str; + default = "nixpkgs"; + example = "nixos-unstable-small"; + description = '' + The nixpkgs branch to be used for evaluating the NixOS configuration. + By default, it will use the `nixpkgs` flake input. - ( - let - setupConfig = config; - in - { config, lib, ... }: { - nixpkgs.overlays = setupConfig.overlays; - home.username = lib.mkForce name; - home.homeDirectory = lib.mkForce setupConfig.homeDirectory; - } - ) - ]; + ::: {.note} + This is based from your flake inputs and not somewhere else. If you + want to have support for multiple nixpkgs branch, simply add them as + a flake input. + ::: + ''; + }; + + homeManagerBranch = lib.mkOption { + type = lib.types.str; + default = "home-manager"; + example = "home-manager-stable"; + description = '' + The home-manager branch to be used for the NixOS module. By default, + it will use the `home-manager` flake input. + ''; + }; + + homeDirectory = lib.mkOption { + type = lib.types.path; + default = "/home/${name}"; + example = "/var/home/public-user"; + description = '' + The home directory of the home-manager user. + ''; + }; + + deploy = lib.mkOption { + type = with lib.types; nullOr (submoduleWith { + specialArgs = { + username = name; + }; + modules = [ deploySettingsType ]; + }); + default = null; + description = '' + deploy-rs settings to be passed onto the home-manager configuration + node. + ''; }; }; + + config = { + modules = [ + ../../../configs/home-manager/${name} + + ( + let + setupConfig = config; + in + { config, lib, ... }: { + nixpkgs.overlays = setupConfig.overlays; + home.username = lib.mkForce name; + home.homeDirectory = lib.mkForce setupConfig.homeDirectory; + } + ) + + (lib.mkIf (config.nixvim.instance != null) + ({ lib, ... }: { + programs.nixvim = { + enable = true; + imports = + partsConfig.setups.nixvim.configs.${config.nixvim.instance}.modules + ++ partsConfig.setups.nixvim.sharedModules + ++ config.nixvim.additionalModules; + }; + })) + ]; + }; + }; in { options.setups.home-manager = { @@ -209,7 +188,14 @@ in }; configs = lib.mkOption { - type = with lib.types; attrsOf (submodule configType); + type = with lib.types; attrsOf (submoduleWith { + specialArgs = { inherit (config) systems; }; + modules = [ + ./shared/nixvim-instance-options.nix + ./shared/config-options.nix + configType + ]; + }); default = { }; description = '' An attribute set of metadata for the declarative home-manager setups. diff --git a/modules/flake-parts/setups/nixos.nix b/modules/flake-parts/setups/nixos.nix index e295c507..54bc13a2 100644 --- a/modules/flake-parts/setups/nixos.nix +++ b/modules/flake-parts/setups/nixos.nix @@ -54,19 +54,9 @@ let deployNodeType = { config, lib, ... }: { freeformType = with lib.types; attrsOf anything; + imports = [ ./shared/deploy-node-type.nix ]; options = { - fastConnection = - lib.mkEnableOption "deploy-rs to assume the target machine is considered fast"; - autoRollback = - lib.mkEnableOption "deploy-rs auto-rollback feature" // { - default = true; - }; - magicRollback = - lib.mkEnableOption "deploy-rs magic rollback feature" // { - default = true; - }; - remoteBuild = lib.mkEnableOption "pass the build process to the remote machine"; profiles = lib.mkOption { type = with lib.types; functionTo (attrsOf anything); default = os: { @@ -144,17 +134,6 @@ let configType = { config, name, lib, ... }: { options = { - systems = lib.mkOption { - type = with lib.types; listOf str; - default = partsConfig.systems; - defaultText = "config.systems"; - example = [ "x86_64-linux" "aarch64-linux" ]; - description = '' - A list of platforms that the NixOS configuration is supposed to be - deployed on. - ''; - }; - formats = lib.mkOption { type = with lib.types; nullOr (listOf str); default = [ "iso" ]; @@ -166,14 +145,6 @@ let ''; }; - modules = lib.mkOption { - type = with lib.types; listOf raw; - default = [ ]; - description = '' - A list of NixOS modules specific for that host. - ''; - }; - overlays = lib.mkOption { type = with lib.types; listOf (functionTo raw); default = [ ]; @@ -305,37 +276,6 @@ let ''; }; - nixvim = lib.mkOption { - type = lib.types.submodule { - options = { - instance = lib.mkOption { - type = with lib.types; nullOr str; - default = null; - example = "fiesta"; - description = '' - The name of the NixVim configuration from - {option}`setups.nixvim.configs.` to be included as part - of the NixOS system. - ''; - }; - - additionalModules = lib.mkOption { - type = with lib.types; listOf raw; - default = [ ]; - description = '' - A list of additional NixVim modules to be included. - ''; - }; - }; - }; - default = { }; - description = '' - An optional NixVim inclusion for the NixOS system. Take note, this - will override whatever Neovim configuration from your NixOS system so - be sure to only use this if you have none. - ''; - }; - deploy = lib.mkOption { type = with lib.types; nullOr (submodule deployNodeType); default = null; @@ -499,7 +439,14 @@ in }; configs = lib.mkOption { - type = with lib.types; attrsOf (submodule configType); + type = with lib.types; attrsOf (submoduleWith { + specialArgs = { inherit (config) systems; }; + modules = [ + ./shared/config-options.nix + ./shared/nixvim-instance-options.nix + configType + ]; + }); default = { }; description = '' An attribute set of metadata for the declarative NixOS setups. This diff --git a/modules/flake-parts/setups/nixvim.nix b/modules/flake-parts/setups/nixvim.nix index 51a97746..fd8d6227 100644 --- a/modules/flake-parts/setups/nixvim.nix +++ b/modules/flake-parts/setups/nixvim.nix @@ -41,16 +41,6 @@ let configType = { name, lib, config, ... }: { options = { - systems = lib.mkOption { - type = with lib.types; listOf str; - default = partsConfig.systems; - defaultText = "config.systems"; - example = [ "x86_64-linux" "aarch64-linux" ]; - description = '' - A list of systems the NixVim configuration will be built against. - ''; - }; - nixpkgsBranches = lib.mkOption { type = with lib.types; listOf str; description = '' @@ -62,14 +52,6 @@ let "nixos-stable" ]; }; - - modules = lib.mkOption { - type = with lib.types; listOf raw; - default = []; - description = '' - Additional NixVim modules to use. - ''; - }; }; config = { @@ -82,7 +64,13 @@ in { options.setups.nixvim = { configs = lib.mkOption { - type = with lib.types; attrsOf (submodule configType); + type = with lib.types; attrsOf (submoduleWith { + specialArgs = { inherit (config) systems; }; + modules = [ + ./shared/config-options.nix + configType + ]; + }); default = {}; description = '' A set of NixVim configurations to be integrated into the declarative diff --git a/modules/flake-parts/setups/shared/config-options.nix b/modules/flake-parts/setups/shared/config-options.nix new file mode 100644 index 00000000..b3c93aaf --- /dev/null +++ b/modules/flake-parts/setups/shared/config-options.nix @@ -0,0 +1,22 @@ +{ lib, systems, ... }: { + options = { + systems = lib.mkOption { + type = with lib.types; listOf str; + default = systems; + defaultText = "config.systems"; + example = [ "x86_64-linux" "aarch64-linux" ]; + description = '' + A list of platforms that the NixOS configuration is supposed to be + deployed on. + ''; + }; + + modules = lib.mkOption { + type = with lib.types; listOf raw; + default = [ ]; + description = '' + A list of NixOS modules specific for that host. + ''; + }; + }; +} diff --git a/modules/flake-parts/setups/shared/deploy-node-type.nix b/modules/flake-parts/setups/shared/deploy-node-type.nix new file mode 100644 index 00000000..bedd5cac --- /dev/null +++ b/modules/flake-parts/setups/shared/deploy-node-type.nix @@ -0,0 +1,15 @@ +{ lib, ... }: { + options = { + fastConnection = + lib.mkEnableOption "deploy-rs to assume the target machine is considered fast"; + autoRollback = + lib.mkEnableOption "deploy-rs auto-rollback feature" // { + default = true; + }; + magicRollback = + lib.mkEnableOption "deploy-rs magic rollback feature" // { + default = true; + }; + remoteBuild = lib.mkEnableOption "pass the build process to the remote machine"; + }; +} diff --git a/modules/flake-parts/setups/shared/nixvim-instance-options.nix b/modules/flake-parts/setups/shared/nixvim-instance-options.nix new file mode 100644 index 00000000..a96266c3 --- /dev/null +++ b/modules/flake-parts/setups/shared/nixvim-instance-options.nix @@ -0,0 +1,32 @@ +{ lib, ... }: { + options.nixvim = lib.mkOption { + type = lib.types.submodule { + options = { + instance = lib.mkOption { + type = with lib.types; nullOr str; + default = null; + example = "fiesta"; + description = '' + The name of the NixVim configuration from + {option}`setups.nixvim.configs.` to be included as part + of the NixOS system. + ''; + }; + + additionalModules = lib.mkOption { + type = with lib.types; listOf raw; + default = [ ]; + description = '' + A list of additional NixVim modules to be included. + ''; + }; + }; + }; + default = { }; + description = '' + An optional NixVim inclusion for the environment. Take note, this + will override whatever Neovim configuration from your environment so + be sure to only use this if you have none. + ''; + }; +}