Skip to content

Commit

Permalink
Merge pull request #858 from rmachado-studocu/add-support-for-multipl…
Browse files Browse the repository at this point in the history
…e-ips-for-one-hostname

feat: adds support for multiple IP configurations for one hostname in `hostctl.nix`
  • Loading branch information
domenkozar authored Nov 4, 2023
2 parents 86f476f + 853bb50 commit 6fd52e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 10 additions & 1 deletion examples/mkcert/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
{
certificates = [
"example.com"
"another-example.com"
];

hosts = {
"example.com" = "127.0.0.1";
"another-example.com" = [ "127.0.0.1" "::1" ];
};

services.caddy.enable = true;
services.caddy.virtualHosts."example.com" = {
extraConfig = ''
tls ${config.env.DEVENV_STATE}/mkcert/example.com.pem ${config.env.DEVENV_STATE}/mkcert/example.com-key.pem
respond "Hello, world!"
respond "Hello, world from example.com!"
'';
};
services.caddy.virtualHosts."another-example.com" = {
extraConfig = ''
tls ${config.env.DEVENV_STATE}/mkcert/another-example.com.pem ${config.env.DEVENV_STATE}/mkcert/another-example.com-key.pem
respond "Hello, world from another-example.com!"
'';
};
}
8 changes: 6 additions & 2 deletions src/modules/integrations/hostctl.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{ pkgs, lib, config, ... }:

let
reducerFn = (prev: curr: prev ++ (if builtins.typeOf curr.ip == "string" then [ curr ] else builtins.map (ip: { inherit ip; hostname = curr.hostname; }) curr.ip));
reducer = lib.lists.foldl reducerFn [ ];
entries = lib.mapAttrsToList (hostname: ip: { inherit hostname ip; }) config.hosts;
entriesByIp = builtins.groupBy ({ ip, ... }: ip) entries;
separateEntriesWithIps = reducer entries;
entriesByIp = builtins.groupBy ({ ip, ... }: ip) separateEntriesWithIps;
hostnamesByIp = builtins.mapAttrs (hostname: entries: builtins.map ({ hostname, ... }: hostname) entries) entriesByIp;
lines = lib.mapAttrsToList (ip: hostnames: "${ip} ${lib.concatStringsSep " " hostnames}") hostnamesByIp;
hostContent = lib.concatStringsSep "\n" lines;
Expand All @@ -21,11 +24,12 @@ in
};

hosts = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
type = lib.types.attrsOf (lib.types.either lib.types.str (lib.types.listOf lib.types.str));
default = { };
description = "List of hosts entries.";
example = {
"example.com" = "127.0.0.1";
"another-example.com" = [ "::1" "127.0.0.1" ];
};
};
};
Expand Down

0 comments on commit 6fd52e0

Please sign in to comment.