all repos — nixfiles @ 5434207aa57186bce175729621e98181a2fce928

System and user configuration, managed by nix and home-manager

dev/web: run caddy as user service with Caddyfile watching

Alin
commit

5434207aa57186bce175729621e98181a2fce928

parent

93288d41342cb9768c35a90d2f7909ebb764ba4a

M system/hosts/marvin.nix → system/hosts/marvin.nix
@@ -3,22 +3,13 @@ config,
pkgs, ... }: -let - tsnet = "fin-marlin.ts.net"; -in { imports = [ ../settings/darwin.nix - ../settings/dev.nix ../settings/programs/shell.nix ../settings/programs/base.nix ]; - local.web = { - enable = true; - projectRoot = "/Users/alin/projects"; - }; - system.primaryUser = "alin"; system.primaryUserHome = "/Users/alin"; users.users."${config.system.primaryUser}".openssh.authorizedKeys.keys = [
@@ -33,24 +24,6 @@
networking = { hostName = "marvin"; localHostName = "Paranoid-Android"; - }; - - services.caddy = { - user = "root"; - group = "wheel"; - virtualHosts = { - "${config.networking.hostName}.${tsnet}" = { - logFormat = "output discard"; - extraConfig = '' - reverse_proxy http://localhost:1234 { - transport http { - dial_timeout 1s - compression off - } - } - ''; - }; - }; }; services.redis = {
M system/hosts/prefect.nix → system/hosts/prefect.nix
@@ -22,7 +22,6 @@ ../settings/user-interface.nix
../settings/programs/kde.nix ../settings/programs/shell.nix ../settings/programs/docker.nix - ../settings/dev.nix ../settings/gaming.nix ../roles/desktop.nix
D system/settings/dev.nix
@@ -1,149 +0,0 @@
-{ - config, - lib, - ... -}: -with lib; - -let - cfg = config.local.web; -in -{ - options.local.web = { - enable = mkEnableOption "Local configuration of caddy"; - - projectRoot = mkOption { - default = "/home/alin/projects"; - type = types.str; - description = "Where projects are stored"; - }; - }; - - config = mkIf cfg.enable { - services.caddy = { - enable = true; - extraConfig = '' - (local_tls) { - tls { - issuer internal { - ca local - } - } - } - ''; - virtualHosts = { - "localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - acme_server { - allow { - domains *.test *.localhost - } - } - ''; - }; - "alin.localhost" = { - logFormat = "output discard"; - serverAliases = [ - "alin.test" - "alanpearce.test" - "alanpearce.localhost" - ]; - extraConfig = '' - import local_tls - reverse_proxy http://localhost:8080 { - transport http { - dial_timeout 1s - compression off - } - } - ''; - }; - "elgit.localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - reverse_proxy http://localhost:5555 { - transport http { - dial_timeout 1s - compression off - } - } - ''; - }; - "searchix.localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - handle_errors { - rewrite * /error.html - templates - file_server - } - handle { - reverse_proxy http://localhost:3000 { - transport http { - dial_timeout 1s - compression off - } - } - } - ''; - }; - "auth.localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - reverse_proxy http://localhost:8082 - ''; - }; - "xhain.localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - root * /Users/alin/projects/github.com/xHain-hackspace/xhain-website/public/ - file_server - ''; - }; - "hugo.localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - reverse_proxy http://localhost:1313 { - transport http { - dial_timeout 1s - compression off - } - } - ''; - }; - "chat.localhost" = { - logFormat = "output discard"; - extraConfig = '' - import local_tls - reverse_proxy http://localhost:9090 { - transport http { - dial_timeout 1s - compression off - } - } - ''; - }; - "llama.localhost" = { - logFormat = "output discard"; - serverAliases = [ "http://llama.localhost" ]; - extraConfig = '' - import local_tls - reverse_proxy http://localhost:11343 { - transport http { - dial_timeout 1s - compression off - } - } - ''; - }; - }; - }; - }; -}
A user/modules/caddy.nix
@@ -0,0 +1,86 @@
+{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.services.caddy; +in +{ + options.services.caddy = { + enable = lib.mkEnableOption "Caddy web server (for local development)"; + + package = lib.mkPackageOption pkgs "caddy" { }; + + configFile = lib.mkOption { + type = lib.types.path; + description = '' + Path to a Caddyfile configuration file. + ''; + }; + + adapter = lib.mkOption { + type = with lib.types; nullOr str; + default = "caddyfile"; + + description = '' + Name of the config adapter to use. + See <https://caddyserver.com/docs/config-adapters> + for the full list. + + If `null` is specified, the `--adapter` argument is omitted when + starting or restarting Caddy. Notably, this allows specification of a + configuration file in Caddy's native JSON format, as long as the + filename does not start with `Caddyfile` (in which case the `caddyfile` + adapter is implicitly enabled). See + <https://caddyserver.com/docs/command-line#caddy-run> for details. + ''; + }; + + watch = lib.mkOption { + type = lib.types.bool; + default = false; + description = '' + Automatically reload the config file, when changed. + ''; + }; + }; + + config = + let + args = [ + "run" + "--config" + "${cfg.configFile}" + ] + ++ lib.optionals (cfg.adapter != null) [ + "--adapter" + cfg.adapter + ] + ++ lib.optional cfg.watch "--watch"; + in + lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + launchd.agents.caddy = { + enable = true; + + config = { + ProgramArguments = [ (lib.getExe cfg.package) ] ++ args; + RunAtLoad = true; + }; + }; + + # untested + systemd.user.services.caddy = { + unitConfig = { + wantedBy = [ "default.target" ]; + }; + serviceConfig = { + ExecStart = "${lib.getExe cfg.package} ${lib.escapeShellArgs args}"; + }; + }; + + }; +}
M user/settings/development/web.nix → user/settings/development/web.nix
@@ -1,8 +1,19 @@
{ + config, pkgs, + lib, ... }: +let + nixfiles = config.home.homeDirectory + "/projects/alin.ovh/nixfiles"; + direct_symlink = + path: config.lib.file.mkOutOfStoreSymlink "${nixfiles}/${lib.removePrefix "/" path}"; +in { + imports = [ + ../../modules/caddy.nix + ]; + home.packages = with pkgs; [ vscode-langservers-extracted stylelint
@@ -34,5 +45,11 @@ cssls.enable = true;
html.enable = true; jsonls.enable = true; }; + }; + + services.caddy = { + enable = true; + configFile = direct_symlink "user/settings/development/web/Caddyfile"; + watch = true; }; }
A user/settings/development/web/Caddyfile
@@ -0,0 +1,44 @@
+{ + local_certs + skip_install_trust +} + +alin.localhost, alin.test, alanpearce.test, alanpearce.localhost { + reverse_proxy http://localhost:8080 +} + +auth.localhost { + reverse_proxy http://localhost:8082 +} + +chat.localhost { + reverse_proxy http://localhost:9090 +} + +elgit.localhost { + reverse_proxy http://localhost:5555 +} + +hugo.localhost { + reverse_proxy http://localhost:1313 +} + +llama.localhost, http://llama.localhost { + reverse_proxy http://localhost:11343 +} + +searchix.localhost { + handle_errors { + rewrite * /error.html + templates + file_server + } + handle { + reverse_proxy http://localhost:3000 + } +} + +xhain.localhost { + root * /Users/alin/projects/github.com/xHain-hackspace/xhain-website/public/ + file_server +}