From f6c70a278b7424380b6005fa77106bbcb8a9bfd0 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 31 Jul 2024 21:47:48 +0800 Subject: [PATCH] home-manager/state: refactor and add paths and ports sub-option Similarly structured to the NixOS module to make it consistent, yay. --- .../foo-dogsquared/modules/setups/desktop.nix | 2 +- .../modules/setups/development.nix | 2 +- modules/home-manager/_private/default.nix | 2 +- modules/home-manager/_private/state.nix | 37 ----------- .../home-manager/_private/state/default.nix | 28 ++++++++ modules/home-manager/_private/state/paths.nix | 27 ++++++++ modules/home-manager/_private/state/ports.nix | 64 +++++++++++++++++++ modules/home-manager/_private/suites/dev.nix | 4 +- 8 files changed, 124 insertions(+), 42 deletions(-) delete mode 100644 modules/home-manager/_private/state.nix create mode 100644 modules/home-manager/_private/state/default.nix create mode 100644 modules/home-manager/_private/state/paths.nix create mode 100644 modules/home-manager/_private/state/ports.nix diff --git a/configs/home-manager/foo-dogsquared/modules/setups/desktop.nix b/configs/home-manager/foo-dogsquared/modules/setups/desktop.nix index 9d311a69..bad31b54 100644 --- a/configs/home-manager/foo-dogsquared/modules/setups/desktop.nix +++ b/configs/home-manager/foo-dogsquared/modules/setups/desktop.nix @@ -60,7 +60,7 @@ in topdirs = "~/Downloads ~/Documents ~/library"; "skippedNames+" = let - inherit (config.state) ignoreDirectories; + inherit (config.state.paths) ignoreDirectories; in lib.concatStringsSep " " ignoreDirectories; diff --git a/configs/home-manager/foo-dogsquared/modules/setups/development.nix b/configs/home-manager/foo-dogsquared/modules/setups/development.nix index 4d4e1c9d..27781a05 100644 --- a/configs/home-manager/foo-dogsquared/modules/setups/development.nix +++ b/configs/home-manager/foo-dogsquared/modules/setups/development.nix @@ -10,7 +10,7 @@ in config = lib.mkIf cfg.enable (lib.mkMerge [ { - state.ignoreDirectories = [ + state.paths.ignoreDirectories = [ "node_modules" # For Node projects. "result" # For Nix builds. "target" # For Rust builds. diff --git a/modules/home-manager/_private/default.nix b/modules/home-manager/_private/default.nix index ea8ac78f..14769755 100644 --- a/modules/home-manager/_private/default.nix +++ b/modules/home-manager/_private/default.nix @@ -1,7 +1,7 @@ { imports = [ ./extra-arguments.nix - ./state.nix + ./state ./suites/desktop.nix ./suites/dev.nix ./suites/editors.nix diff --git a/modules/home-manager/_private/state.nix b/modules/home-manager/_private/state.nix deleted file mode 100644 index ba5d0a73..00000000 --- a/modules/home-manager/_private/state.nix +++ /dev/null @@ -1,37 +0,0 @@ -{ lib, ... }: - -{ - options.state = lib.mkOption { - type = lib.types.submodule { - freeformType = with lib.types; attrsOf anything; - options = { - ignoreDirectories = lib.mkOption { - type = with lib.types; listOf str; - description = '' - A state variable holding a list of directory names to be excluded - in processes involving walking through directories (e.g., desktop - indexing). - ''; - default = [ ]; - example = [ - "node_modules" - ".direnv" - ]; - }; - }; - }; - description = '' - A set of values to be held in the home-manager configuration. Pretty much - used for anything that requires consistency or deduplicate the source of - truth for module values. - ''; - example = { - sampleValue = 10; - dev.ignoreDirectories = [ - ".git" - "node_modules" - ".direnv" - ]; - }; - }; -} diff --git a/modules/home-manager/_private/state/default.nix b/modules/home-manager/_private/state/default.nix new file mode 100644 index 00000000..263b829b --- /dev/null +++ b/modules/home-manager/_private/state/default.nix @@ -0,0 +1,28 @@ +{ lib, ... }: + +{ + imports = [ + ./ports.nix + ./paths.nix + ]; + + options.state = lib.mkOption { + type = lib.types.submodule { + freeformType = with lib.types; attrsOf anything; + default = { }; + }; + description = '' + A set of values to be held in the home-manager configuration. Pretty much + used for anything that requires consistency or deduplicate the source of + truth for module values. + ''; + example = { + sampleValue = 10; + paths.ignoreDirectories = [ + ".git" + "node_modules" + ".direnv" + ]; + }; + }; +} diff --git a/modules/home-manager/_private/state/paths.nix b/modules/home-manager/_private/state/paths.nix new file mode 100644 index 00000000..46a616f6 --- /dev/null +++ b/modules/home-manager/_private/state/paths.nix @@ -0,0 +1,27 @@ +{ lib, ... }: + +{ + options.state = + let + pathsSubmodule = { lib, ... }: { + options = { + paths = lib.mkOption { + type = with lib.types; attrsOf (listOf path); + default = { }; + description = '' + Set of paths to hold as a single source of truth for path-related + settings throughout the whole home environment. + ''; + example = lib.literalExpression '' + { + ignoreDirectories = [ "''${config.home.homeDirectory}/Nodes" ]; + ignorePaths = [ ".gitignore" "node_modules" "result" ]; + } + ''; + }; + }; + }; + in lib.mkOption { + type = lib.type.submodule pathsSubmodule; + }; +} diff --git a/modules/home-manager/_private/state/ports.nix b/modules/home-manager/_private/state/ports.nix new file mode 100644 index 00000000..f5ea0a0b --- /dev/null +++ b/modules/home-manager/_private/state/ports.nix @@ -0,0 +1,64 @@ +{ lib, ... }: + +let + supportedProtocols = [ "tcp" "udp" ]; +in +{ + options.state = + let + portRangeType = { + options = { + from = lib.mkOption { + type = lib.types.port; + description = '' + The start of the range of TCP/UDP ports to be taken over. + ''; + }; + + to = lib.mkOption { + type = lib.types.port; + description = '' + The end of the range of TCP/UDP ports to be taken over. + ''; + }; + }; + }; + + portValueModule = { lib, ... }: { + options = { + protocols = lib.mkOption { + type = with lib.types; listOf (enum supportedProtocols); + description = '' + Indicates the type of protocol of the service. + ''; + default = [ "tcp" "udp" ]; + example = [ "tcp" ]; + }; + + value = lib.mkOption { + type = with lib.types; either port (submodule portRangeType); + description = '' + The port number itself. + ''; + }; + }; + }; + + portsSubmodule = { lib, ... }: { + options = { + ports = lib.mkOption { + type = with lib.types; attrsOf (submodule portValueModule); + default = { }; + example = lib.literalExpression '' + { + gonic.value = 4629; + mopidy.value = 6034; + } + ''; + }; + }; + }; + in lib.mkOption { + type = lib.types.submodule portsSubmodule; + }; +} diff --git a/modules/home-manager/_private/suites/dev.nix b/modules/home-manager/_private/suites/dev.nix index 5b3400df..a5ddf748 100644 --- a/modules/home-manager/_private/suites/dev.nix +++ b/modules/home-manager/_private/suites/dev.nix @@ -20,7 +20,7 @@ in { ({ # Contains a dev-adjacent list of directory names to be ignored usually # used in walking through directories. - state.ignoreDirectories = [ + state.paths.ignoreDirectories = [ ".git" ".direnv" ]; @@ -171,7 +171,7 @@ in { programs.eza = { enable = true; extraOptions = let - ignoreDirectories = lib.concatStringsSep "|" config.state.ignoreDirectories; + ignoreDirectories = lib.concatStringsSep "|" config.state.paths.ignoreDirectories; in [ "--group-directories-first" "--header"