From 68445b2146ca8a7c153bfaa3d14d9564e912e755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikodem=20Rabuli=C5=84ski?= Date: Sat, 4 Jan 2025 20:46:16 +0100 Subject: [PATCH] modules/flake/services: init --- README.md | 1 + flake.nix | 1 + hosts/default.nix | 10 +++- modules/flake/default.nix | 1 + modules/flake/services.nix | 95 ++++++++++++++++++++++++++++++++++++++ services/default.nix | 5 ++ 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 modules/flake/services.nix create mode 100644 services/default.nix diff --git a/README.md b/README.md index f7b1f12..c4e3545 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Collection of my personal Nix configurations and opinionated NixOS, nix-darwin, - system - my opinionated nixos/nix-darwin modules - home - my opinionated home-manager modules - flake - flake-parts modules +- services - configs for services I self-host - secrets - agenix secrets - wrappers - nix packages wrapped with my configs (see: [wrapper-manager](https://github.com/viperML/wrapper-manager)) - assets - miscellaneous values reused throughout my config diff --git a/flake.nix b/flake.nix index 8186624..9fc332d 100644 --- a/flake.nix +++ b/flake.nix @@ -16,6 +16,7 @@ ./modules ./wrappers ./pkgs + ./services ]; perSystem = diff --git a/hosts/default.nix b/hosts/default.nix index d2e1d5a..3c334c3 100644 --- a/hosts/default.nix +++ b/hosts/default.nix @@ -62,7 +62,10 @@ nixos = name: module: baseNixos.extendModules { - modules = [ module ]; + modules = [ + module + config.__extraHostConfigs.${name} or { } + ]; specialArgs.configurationName = name; }; @@ -70,7 +73,10 @@ name: module: let eval = baseDarwin._module.args.extendModules { - modules = [ module ]; + modules = [ + module + config.__extraHostConfigs.${name} or { } + ]; specialArgs.configurationName = name; }; in diff --git a/modules/flake/default.nix b/modules/flake/default.nix index 2d83b08..78bb73d 100644 --- a/modules/flake/default.nix +++ b/modules/flake/default.nix @@ -8,5 +8,6 @@ imports = [ (import ./configurations.nix { inherit nixpkgs darwin home-manager; }) + ./services.nix ]; } diff --git a/modules/flake/services.nix b/modules/flake/services.nix new file mode 100644 index 0000000..f2f07d4 --- /dev/null +++ b/modules/flake/services.nix @@ -0,0 +1,95 @@ +# List of features I want this module to eventually have +# TODO: Automatic port allocation +# TODO: Making it possible to conveniently isolate services (running them in NixOS containers) +# TODO: Handling specializations +# TODO: Convenient http handling +# TODO: Automatic backup +{ config, lib, ... }: +let + serviceModule = + { config, ... }: + { + options = { + host = lib.mkOption { + type = lib.types.str; + }; + ports = lib.mkOption { + type = with lib.types; listOf port; + default = [ ]; + }; + hosts = lib.mkOption { + type = with lib.types; listOf str; + default = [ config.host ]; + }; + config = lib.mkOption { + type = lib.types.deferredModule; + default = { }; + }; + hostConfig = lib.mkOption { + type = with lib.types; attrsOf deferredModule; + default = { }; + }; + }; + }; + + moduleToHostConfigs = + cfg: + lib.genAttrs cfg.hosts (host: { + imports = [ + cfg.config + (cfg.hostConfig.${host} or { }) + ]; + }); + + maybeGetPreviousConfigs = acc: host: (acc.${host} or { imports = [ ]; }).imports; +in +{ + _file = ./services.nix; + + options = { + services = lib.mkOption { + type = with lib.types; attrsOf (submodule serviceModule); + default = { }; + }; + + __extraHostConfigs = lib.mkOption { + type = with lib.types; attrsOf deferredModule; + readOnly = true; + }; + }; + + config.__extraHostConfigs = + let + duplicatePorts = lib.pipe config.services [ + lib.attrValues + (map (cfg: cfg.ports)) + lib.flatten + (lib.groupBy' (cnt: _: cnt + 1) 0 toString) + (lib.filterAttrs (_: cnt: cnt > 1)) + lib.attrNames + ]; + assertMsg = + let + plural = lib.length duplicatePorts > 1; + in + "\nBad service config:\nThe following port${if plural then "s" else ""} ${ + if plural then "were" else "was" + } declared multiple times: ${lib.concatStringsSep ", " duplicatePorts}"; + # Here I collect all the services..config into a flat + # __extraHostConfigs..imports = [ + # ... + # ] + # so that I can easily import them in hosts/default.nix + hostConfigs = lib.pipe config.services [ + lib.attrValues + (lib.foldl' ( + acc: cfg: + acc + // lib.mapAttrs (host: c: { + imports = c.imports ++ (maybeGetPreviousConfigs acc host); + }) (moduleToHostConfigs cfg) + ) { }) + ]; + in + if duplicatePorts != [ ] then throw assertMsg else hostConfigs; +} diff --git a/services/default.nix b/services/default.nix new file mode 100644 index 0000000..1f0f2c2 --- /dev/null +++ b/services/default.nix @@ -0,0 +1,5 @@ +{ + imports = [ + + ]; +}